source: NonGTP/Boost/boost/python/detail/referent_storage.hpp @ 857

Revision 857, 2.2 KB checked in by igarcia, 18 years ago (diff)
Line 
1// Copyright David Abrahams 2002.
2// Distributed under the Boost Software License, Version 1.0. (See
3// accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5#ifndef REFERENT_STORAGE_DWA200278_HPP
6# define REFERENT_STORAGE_DWA200278_HPP
7# include <boost/mpl/if.hpp>
8# include <cstddef>
9
10namespace boost { namespace python { namespace detail {
11
12struct alignment_dummy;
13typedef void (*function_ptr)();
14typedef int (alignment_dummy::*member_ptr);
15typedef int (alignment_dummy::*member_function_ptr)();
16
17# define BOOST_PYTHON_ALIGNER(T, n)                     \
18        typename mpl::if_c<                             \
19           sizeof(T) <= size, T, char>::type t##n
20
21// Storage for size bytes, aligned to all fundamental types no larger than size
22template <std::size_t size>
23union aligned_storage
24{
25    BOOST_PYTHON_ALIGNER(char, 0);
26    BOOST_PYTHON_ALIGNER(short, 1);
27    BOOST_PYTHON_ALIGNER(int, 2);
28    BOOST_PYTHON_ALIGNER(long, 3);
29    BOOST_PYTHON_ALIGNER(float, 4);
30    BOOST_PYTHON_ALIGNER(double, 5);
31    BOOST_PYTHON_ALIGNER(long double, 6);
32    BOOST_PYTHON_ALIGNER(void*, 7);
33    BOOST_PYTHON_ALIGNER(function_ptr, 8);
34    BOOST_PYTHON_ALIGNER(member_ptr, 9);
35    BOOST_PYTHON_ALIGNER(member_function_ptr, 10);
36    char bytes[size];
37};
38
39# undef BOOST_PYTHON_ALIGNER
40
41  // Compute the size of T's referent. We wouldn't need this at all,
42  // but sizeof() is broken in CodeWarriors <= 8.0
43  template <class T> struct referent_size;
44 
45# ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
46 
47  template <class T>
48  struct referent_size<T&>
49  {
50      BOOST_STATIC_CONSTANT(
51          std::size_t, value = sizeof(T));
52  };
53
54# else
55
56  template <class T> struct referent_size
57  {
58      static T f();
59      BOOST_STATIC_CONSTANT(std::size_t, value = sizeof(f()));
60  };
61 
62# endif
63
64// A metafunction returning a POD type which can store U, where T ==
65// U&. If T is not a reference type, returns a POD which can store T.
66template <class T>
67struct referent_storage
68{
69    typedef aligned_storage<
70        ::boost::python::detail::referent_size<T>::value
71    > type;
72};
73
74}}} // namespace boost::python::detail
75
76#endif // REFERENT_STORAGE_DWA200278_HPP
Note: See TracBrowser for help on using the repository browser.