]> git.lyx.org Git - lyx.git/blob - boost/boost/type_traits/type_with_alignment.hpp
Update in-tree boost to latest from boost 1.34 cvs.
[lyx.git] / boost / boost / type_traits / type_with_alignment.hpp
1 //  (C) Copyright John Maddock 2000.
2 //  Use, modification and distribution are subject to the Boost Software License,
3 //  Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
4 //  http://www.boost.org/LICENSE_1_0.txt).
5 //
6 //  See http://www.boost.org/libs/type_traits for most recent version including documentation.
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/preprocessor/list/transform.hpp>
16 #include <boost/preprocessor/list/append.hpp>
17 #include <boost/type_traits/alignment_of.hpp>
18 #include <boost/type_traits/is_pod.hpp>
19 #include <boost/static_assert.hpp>
20 #include <boost/config.hpp>
21
22 // should be the last #include
23 #include <boost/type_traits/detail/bool_trait_def.hpp>
24
25 #include <cstddef>
26
27 #ifdef BOOST_MSVC
28 #   pragma warning(push)
29 #   pragma warning(disable: 4121) // alignment is sensitive to packing
30 #endif
31
32 namespace boost {
33
34 #ifndef __BORLANDC__
35
36 namespace detail {
37
38 class alignment_dummy;
39 typedef void (*function_ptr)();
40 typedef int (alignment_dummy::*member_ptr);
41 typedef int (alignment_dummy::*member_function_ptr)();
42
43 #ifdef BOOST_HAS_LONG_LONG
44 #define BOOST_TT_ALIGNMENT_BASE_TYPES BOOST_PP_TUPLE_TO_LIST( \
45         12, ( \
46         char, short, int, long,  ::boost::long_long_type, float, double, long double \
47         , void*, function_ptr, member_ptr, member_function_ptr))
48 #else
49 #define BOOST_TT_ALIGNMENT_BASE_TYPES BOOST_PP_TUPLE_TO_LIST( \
50         11, ( \
51         char, short, int, long, float, double, long double \
52         , void*, function_ptr, member_ptr, member_function_ptr))
53 #endif
54
55 #define BOOST_TT_HAS_ONE_T(D,Data,T) boost::detail::has_one_T< T >
56
57 #define BOOST_TT_ALIGNMENT_STRUCT_TYPES                         \
58         BOOST_PP_LIST_TRANSFORM(BOOST_TT_HAS_ONE_T,             \
59                                 X,                              \
60                                 BOOST_TT_ALIGNMENT_BASE_TYPES)
61
62 #define BOOST_TT_ALIGNMENT_TYPES                                \
63         BOOST_PP_LIST_APPEND(BOOST_TT_ALIGNMENT_BASE_TYPES,     \
64                              BOOST_TT_ALIGNMENT_STRUCT_TYPES)
65
66 //
67 // lower_alignment_helper --
68 //
69 // This template gets instantiated a lot, so use partial
70 // specialization when available to reduce the compiler burden.
71 //
72 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
73 template <bool found = true>
74 struct lower_alignment_helper_impl
75 {
76     template <std::size_t, class>
77     struct apply
78     {
79         typedef char type;
80         enum { value = true };
81     };
82 };
83
84 template <>
85 struct lower_alignment_helper_impl<false>
86 {
87     template <std::size_t target, class TestType>
88     struct apply
89       : mpl::if_c<(alignment_of<TestType>::value == target), TestType, char>
90     {
91         enum { value = (alignment_of<TestType>::value == target) };
92     };
93 };
94
95 template <bool found, std::size_t target, class TestType>
96 struct lower_alignment_helper
97   : lower_alignment_helper_impl<found>::template apply<target,TestType>
98 {
99 };
100 #else
101 template <bool found, std::size_t target, class TestType>
102 struct lower_alignment_helper
103 {
104     typedef char type;
105     enum { value = true };
106 };
107
108 template <std::size_t target, class TestType>
109 struct lower_alignment_helper<false,target,TestType>
110 {
111     enum { value = (alignment_of<TestType>::value == target) };
112     typedef typename mpl::if_c<value, TestType, char>::type type;
113 };
114 #endif
115
116 #define BOOST_TT_CHOOSE_MIN_ALIGNMENT(R,P,I,T)                                  \
117         typename lower_alignment_helper<                                        \
118           BOOST_PP_CAT(found,I),target,T                                        \
119         >::type BOOST_PP_CAT(t,I);                                              \
120         enum {                                                                  \
121             BOOST_PP_CAT(found,BOOST_PP_INC(I))                                 \
122               = lower_alignment_helper<BOOST_PP_CAT(found,I),target,T >::value  \
123         };
124
125 #define BOOST_TT_CHOOSE_T(R,P,I,T) T BOOST_PP_CAT(t,I);
126
127 template <typename T>
128 struct has_one_T
129 {
130   T data;
131 };
132
133 template <std::size_t target>
134 union lower_alignment
135 {
136     enum { found0 = false };
137
138     BOOST_PP_LIST_FOR_EACH_I(
139           BOOST_TT_CHOOSE_MIN_ALIGNMENT
140         , ignored
141         , BOOST_TT_ALIGNMENT_TYPES
142         )
143 };
144
145 union max_align
146 {
147     BOOST_PP_LIST_FOR_EACH_I(
148           BOOST_TT_CHOOSE_T
149         , ignored
150         , BOOST_TT_ALIGNMENT_TYPES
151         )
152 };
153
154 #undef BOOST_TT_ALIGNMENT_BASE_TYPES
155 #undef BOOST_TT_HAS_ONE_T
156 #undef BOOST_TT_ALIGNMENT_STRUCT_TYPES
157 #undef BOOST_TT_ALIGNMENT_TYPES
158 #undef BOOST_TT_CHOOSE_MIN_ALIGNMENT
159 #undef BOOST_TT_CHOOSE_T
160
161 template<std::size_t TAlign, std::size_t Align>
162 struct is_aligned
163 {
164     BOOST_STATIC_CONSTANT(bool,
165         value = (TAlign >= Align) & (TAlign % Align == 0)
166         );
167 };
168
169 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
170 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::detail::max_align,true)
171 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::detail::lower_alignment<1> ,true)
172 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::detail::lower_alignment<2> ,true)
173 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::detail::lower_alignment<4> ,true)
174 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::detail::lower_alignment<8> ,true)
175 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::detail::lower_alignment<10> ,true)
176 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::detail::lower_alignment<16> ,true)
177 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::detail::lower_alignment<32> ,true)
178 #endif
179
180 } // namespace detail
181
182 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
183 template<std::size_t Align>
184 struct is_pod< ::boost::detail::lower_alignment<Align> >
185 {
186         BOOST_STATIC_CONSTANT(std::size_t, value = true);
187 };
188 #endif
189
190 // This alignment method originally due to Brian Parker, implemented by David
191 // Abrahams, and then ported here by Doug Gregor.
192 template <std::size_t Align>
193 class type_with_alignment
194 {
195     typedef ::boost::detail::lower_alignment<Align> t1;
196     typedef typename mpl::if_c<
197           ::boost::detail::is_aligned< ::boost::alignment_of<t1>::value,Align >::value
198         , t1
199         , ::boost::detail::max_align
200         >::type align_t;
201
202     BOOST_STATIC_CONSTANT(std::size_t, found = alignment_of<align_t>::value);
203
204     BOOST_STATIC_ASSERT(found >= Align);
205     BOOST_STATIC_ASSERT(found % Align == 0);
206
207  public:
208     typedef align_t type;
209 };
210
211 #if defined(__GNUC__)
212 namespace align {
213 struct __attribute__((__aligned__(2))) a2 {};
214 struct __attribute__((__aligned__(4))) a4 {};
215 struct __attribute__((__aligned__(8))) a8 {};
216 struct __attribute__((__aligned__(16))) a16 {};
217 struct __attribute__((__aligned__(32))) a32 {};
218 }
219
220 template<> class type_with_alignment<1>  { public: typedef char type; };
221 template<> class type_with_alignment<2>  { public: typedef align::a2 type; };
222 template<> class type_with_alignment<4>  { public: typedef align::a4 type; };
223 template<> class type_with_alignment<8>  { public: typedef align::a8 type; };
224 template<> class type_with_alignment<16> { public: typedef align::a16 type; };
225 template<> class type_with_alignment<32> { public: typedef align::a32 type; };
226
227 namespace detail {
228 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::align::a2,true)
229 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::align::a4,true)
230 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::align::a8,true)
231 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::align::a16,true)
232 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::align::a32,true)
233 }
234 #endif
235
236 #else
237
238 //
239 // Borland specific version, we have this for two reasons:
240 // 1) The version above doesn't always compile (with the new test cases for example)
241 // 2) Because of Borlands #pragma option we can create types with alignments that are
242 //    greater that the largest aligned builtin type.
243
244 namespace align{
245 #pragma option push -a16
246 struct a2{ short s; };
247 struct a4{ int s; };
248 struct a8{ double s; };
249 struct a16{ long double s; };
250 #pragma option pop
251 }
252
253 namespace detail {
254
255 typedef ::boost::align::a16 max_align;
256
257 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::align::a2,true)
258 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::align::a4,true)
259 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::align::a8,true)
260 BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::align::a16,true)
261 }
262
263 template <std::size_t N> struct type_with_alignment
264 {
265    // We should never get to here, but if we do use the maximally
266    // aligned type:
267    // BOOST_STATIC_ASSERT(0);
268    typedef align::a16 type;
269 };
270 template <> struct type_with_alignment<1>{ typedef char type; };
271 template <> struct type_with_alignment<2>{ typedef align::a2 type; };
272 template <> struct type_with_alignment<4>{ typedef align::a4 type; };
273 template <> struct type_with_alignment<8>{ typedef align::a8 type; };
274 template <> struct type_with_alignment<16>{ typedef align::a16 type; };
275
276 #endif
277
278 } // namespace boost
279
280 #ifdef BOOST_MSVC
281 #   pragma warning(pop)
282 #endif
283
284 #include <boost/type_traits/detail/bool_trait_undef.hpp>
285
286 #endif // BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED
287
288