]> git.lyx.org Git - lyx.git/blob - boost/boost/type_traits/type_with_alignment.hpp
complie fix
[lyx.git] / boost / boost / type_traits / type_with_alignment.hpp
1
2 // (C) Copyright John Maddock 2000.
3 // Permission to copy, use, modify, sell and distribute this software is 
4 // granted provided this copyright notice appears in all copies. This 
5 // software is provided "as is" without express or implied warranty, 
6 // and with no claim as to its suitability for any purpose.
7
8 #ifndef BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED
9 #define BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED
10
11 #include "boost/mpl/if.hpp"
12 #include "boost/preprocessor/list/for_each_i.hpp"
13 #include "boost/preprocessor/tuple/to_list.hpp"
14 #include "boost/preprocessor/cat.hpp"
15 #include "boost/type_traits/alignment_of.hpp"
16 #include "boost/static_assert.hpp"
17 #include "boost/config.hpp"
18
19 #include <cstddef>
20
21 #ifdef BOOST_MSVC
22 #   pragma warning(push)
23 #   pragma warning(disable: 4121) // alignment is sensitive to packing
24 #endif
25
26 namespace boost {
27
28 namespace detail {
29
30 class alignment_dummy;
31 typedef void (*function_ptr)();
32 typedef int (alignment_dummy::*member_ptr);
33 typedef int (alignment_dummy::*member_function_ptr)();
34
35 #define BOOST_TT_ALIGNMENT_TYPES BOOST_PP_TUPLE_TO_LIST( \
36         11, ( \
37         char, short, int, long, float, double, long double \
38         , void*, function_ptr, member_ptr, member_function_ptr))
39
40 #define BOOST_TT_CHOOSE_MIN_ALIGNMENT(R,P,I,T) \
41         typename mpl::if_c< \
42            alignment_of<T>::value <= target, T, char>::type BOOST_PP_CAT(t,I);
43
44 #define BOOST_TT_CHOOSE_T(R,P,I,T) T BOOST_PP_CAT(t,I);
45            
46 template <std::size_t target>
47 union lower_alignment
48 {
49     BOOST_PP_LIST_FOR_EACH_I(
50           BOOST_TT_CHOOSE_MIN_ALIGNMENT
51         , ignored
52         , BOOST_TT_ALIGNMENT_TYPES
53         )
54 };
55
56 union max_align
57 {
58     BOOST_PP_LIST_FOR_EACH_I(
59           BOOST_TT_CHOOSE_T
60         , ignored
61         , BOOST_TT_ALIGNMENT_TYPES
62         )
63 };
64
65 #undef BOOST_TT_ALIGNMENT_TYPES
66 #undef BOOST_TT_CHOOSE_MIN_ALIGNMENT
67 #undef BOOST_TT_CHOOSE_T
68
69 template<int TAlign, int Align>
70 struct is_aligned
71 {
72     BOOST_STATIC_CONSTANT(bool,
73         value = (TAlign >= Align) & (TAlign % Align == 0)
74         );
75 };
76
77 } // namespace detail
78
79 // This alignment method originally due to Brian Parker, implemented by David
80 // Abrahams, and then ported here by Doug Gregor. 
81 template <int Align>
82 class type_with_alignment
83 {
84     typedef detail::lower_alignment<Align> t1;
85     typedef typename mpl::if_c<
86           ::boost::detail::is_aligned< ::boost::alignment_of<t1>::value,Align >::value
87         , t1
88         , detail::max_align
89         >::type align_t;
90
91     BOOST_STATIC_CONSTANT(std::size_t, found = alignment_of<align_t>::value);
92
93     BOOST_STATIC_ASSERT(found >= Align);
94     BOOST_STATIC_ASSERT(found % Align == 0);
95
96  public:
97     typedef align_t type;
98 };
99
100 } // namespace boost
101
102 #ifdef BOOST_MSVC
103 #   pragma warning(pop)
104 #endif
105
106 #endif // BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED