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 | template <bool found, std::size_t target, class TestType> |
73 | struct lower_alignment_helper |
74 | { |
75 | typedef char type; |
76 | enum { value = true }; |
77 | }; |
78 | |
79 | template <std::size_t target, class TestType> |
80 | struct lower_alignment_helper<false,target,TestType> |
81 | { |
82 | enum { value = (alignment_of<TestType>::value == target) }; |
83 | typedef typename mpl::if_c<value, TestType, char>::type type; |
84 | }; |
85 | |
86 | #define BOOST_TT_CHOOSE_MIN_ALIGNMENT(R,P,I,T) \ |
87 | typename lower_alignment_helper< \ |
88 | BOOST_PP_CAT(found,I),target,T \ |
89 | >::type BOOST_PP_CAT(t,I); \ |
90 | enum { \ |
91 | BOOST_PP_CAT(found,BOOST_PP_INC(I)) \ |
92 | = lower_alignment_helper<BOOST_PP_CAT(found,I),target,T >::value \ |
93 | }; |
94 | |
95 | #define BOOST_TT_CHOOSE_T(R,P,I,T) T BOOST_PP_CAT(t,I); |
96 | |
97 | template <typename T> |
98 | struct has_one_T |
99 | { |
100 | T data; |
101 | }; |
102 | |
103 | template <std::size_t target> |
104 | union lower_alignment |
105 | { |
106 | enum { found0 = false }; |
107 | |
108 | BOOST_PP_LIST_FOR_EACH_I( |
109 | BOOST_TT_CHOOSE_MIN_ALIGNMENT |
110 | , ignored |
111 | , BOOST_TT_ALIGNMENT_TYPES |
112 | ) |
113 | }; |
114 | |
115 | union max_align |
116 | { |
117 | BOOST_PP_LIST_FOR_EACH_I( |
118 | BOOST_TT_CHOOSE_T |
119 | , ignored |
120 | , BOOST_TT_ALIGNMENT_TYPES |
121 | ) |
122 | }; |
123 | |
124 | #undef BOOST_TT_ALIGNMENT_BASE_TYPES |
125 | #undef BOOST_TT_HAS_ONE_T |
126 | #undef BOOST_TT_ALIGNMENT_STRUCT_TYPES |
127 | #undef BOOST_TT_ALIGNMENT_TYPES |
128 | #undef BOOST_TT_CHOOSE_MIN_ALIGNMENT |
129 | #undef BOOST_TT_CHOOSE_T |
130 | |
131 | template<std::size_t TAlign, std::size_t Align> |
132 | struct is_aligned |
133 | { |
134 | BOOST_STATIC_CONSTANT(bool, |
135 | value = (TAlign >= Align) & (TAlign % Align == 0) |
136 | ); |
137 | }; |
138 | |
139 | |
140 | } // namespace detail |
141 | |
142 | template<std::size_t Align> |
143 | struct is_pod< ::boost::detail::lower_alignment<Align> > |
144 | { |
145 | BOOST_STATIC_CONSTANT(std::size_t, value = true); |
146 | }; |
147 | |
148 | // This alignment method originally due to Brian Parker, implemented by David |
149 | // Abrahams, and then ported here by Doug Gregor. |
150 | namespace detail{ |
151 | |
152 | template <std::size_t Align> |
153 | class type_with_alignment_imp |
154 | { |
155 | typedef ::boost::detail::lower_alignment<Align> t1; |
156 | typedef typename mpl::if_c< |
157 | ::boost::detail::is_aligned< ::boost::alignment_of<t1>::value,Align >::value |
158 | , t1 |
159 | , ::boost::detail::max_align |
160 | >::type align_t; |
161 | |
162 | BOOST_STATIC_CONSTANT(std::size_t, found = alignment_of<align_t>::value); |
163 | |
164 | BOOST_STATIC_ASSERT(found >= Align); |
165 | BOOST_STATIC_ASSERT(found % Align == 0); |
166 | |
167 | public: |
168 | typedef align_t type; |
169 | }; |
170 | |
171 | } |
172 | |
173 | template <std::size_t Align> |
174 | class type_with_alignment |
175 | : public ::boost::detail::type_with_alignment_imp<Align> |
176 | { |
177 | }; |
178 | |
179 | #if defined(__GNUC__) || (defined (__SUNPRO_CC) && (__SUNPRO_CC >= 0x5130)) |
180 | namespace tt_align_ns { |
181 | struct __attribute__((__aligned__(2))) a2 {}; |
182 | struct __attribute__((__aligned__(4))) a4 {}; |
183 | struct __attribute__((__aligned__(8))) a8 {}; |
184 | struct __attribute__((__aligned__(16))) a16 {}; |
185 | struct __attribute__((__aligned__(32))) a32 {}; |
186 | struct __attribute__((__aligned__(64))) a64 {}; |
187 | struct __attribute__((__aligned__(128))) a128 {}; |
188 | } |
189 | |
190 | template<> class type_with_alignment<1> { public: typedef char type; }; |
191 | template<> class type_with_alignment<2> { public: typedef tt_align_ns::a2 type; }; |
192 | template<> class type_with_alignment<4> { public: typedef tt_align_ns::a4 type; }; |
193 | template<> class type_with_alignment<8> { public: typedef tt_align_ns::a8 type; }; |
194 | template<> class type_with_alignment<16> { public: typedef tt_align_ns::a16 type; }; |
195 | template<> class type_with_alignment<32> { public: typedef tt_align_ns::a32 type; }; |
196 | template<> class type_with_alignment<64> { public: typedef tt_align_ns::a64 type; }; |
197 | template<> class type_with_alignment<128> { public: typedef tt_align_ns::a128 type; }; |
198 | |
199 | namespace detail { |
200 | BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::tt_align_ns::a2,true) |
201 | BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::tt_align_ns::a4,true) |
202 | BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::tt_align_ns::a8,true) |
203 | BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::tt_align_ns::a16,true) |
204 | BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::tt_align_ns::a32,true) |
205 | BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::tt_align_ns::a64,true) |
206 | BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::tt_align_ns::a128,true) |
207 | } |
208 | #endif |
209 | #if defined(BOOST_MSVC) || (defined(BOOST_INTEL) && defined(_MSC_VER)) |
210 | // |
211 | // MSVC supports types which have alignments greater than the normal |
212 | // maximum: these are used for example in the types __m64 and __m128 |
213 | // to provide types with alignment requirements which match the SSE |
214 | // registers. Therefore we extend type_with_alignment<> to support |
215 | // such types, however, we have to be careful to use a builtin type |
216 | // whenever possible otherwise we break previously working code: |
217 | // see http://article.gmane.org/gmane.comp.lib.boost.devel/173011 |
218 | // for an example and test case. Thus types like a8 below will |
219 | // be used *only* if the existing implementation can't provide a type |
220 | // with suitable alignment. This does mean however, that type_with_alignment<> |
221 | // may return a type which cannot be passed through a function call |
222 | // by value (and neither can any type containing such a type like |
223 | // Boost.Optional). However, this only happens when we have no choice |
224 | // in the matter because no other "ordinary" type is available. |
225 | // |
226 | namespace tt_align_ns { |
227 | struct __declspec(align(8)) a8 { |
228 | char m[8]; |
229 | typedef a8 type; |
230 | }; |
231 | struct __declspec(align(16)) a16 { |
232 | char m[16]; |
233 | typedef a16 type; |
234 | }; |
235 | struct __declspec(align(32)) a32 { |
236 | char m[32]; |
237 | typedef a32 type; |
238 | }; |
239 | struct __declspec(align(64)) a64 |
240 | { |
241 | char m[64]; |
242 | typedef a64 type; |
243 | }; |
244 | struct __declspec(align(128)) a128 { |
245 | char m[128]; |
246 | typedef a128 type; |
247 | }; |
248 | } |
249 | |
250 | template<> class type_with_alignment<8> |
251 | { |
252 | typedef mpl::if_c< |
253 | ::boost::alignment_of<boost::detail::max_align>::value < 8, |
254 | tt_align_ns::a8, |
255 | boost::detail::type_with_alignment_imp<8> >::type t1; |
256 | public: |
257 | typedef t1::type type; |
258 | }; |
259 | template<> class type_with_alignment<16> |
260 | { |
261 | typedef mpl::if_c< |
262 | ::boost::alignment_of<boost::detail::max_align>::value < 16, |
263 | tt_align_ns::a16, |
264 | boost::detail::type_with_alignment_imp<16> >::type t1; |
265 | public: |
266 | typedef t1::type type; |
267 | }; |
268 | template<> class type_with_alignment<32> |
269 | { |
270 | typedef mpl::if_c< |
271 | ::boost::alignment_of<boost::detail::max_align>::value < 32, |
272 | tt_align_ns::a32, |
273 | boost::detail::type_with_alignment_imp<32> >::type t1; |
274 | public: |
275 | typedef t1::type type; |
276 | }; |
277 | template<> class type_with_alignment<64> { |
278 | typedef mpl::if_c< |
279 | ::boost::alignment_of<boost::detail::max_align>::value < 64, |
280 | tt_align_ns::a64, |
281 | boost::detail::type_with_alignment_imp<64> >::type t1; |
282 | public: |
283 | typedef t1::type type; |
284 | }; |
285 | template<> class type_with_alignment<128> { |
286 | typedef mpl::if_c< |
287 | ::boost::alignment_of<boost::detail::max_align>::value < 128, |
288 | tt_align_ns::a128, |
289 | boost::detail::type_with_alignment_imp<128> >::type t1; |
290 | public: |
291 | typedef t1::type type; |
292 | }; |
293 | |
294 | namespace detail { |
295 | BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::tt_align_ns::a8,true) |
296 | BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::tt_align_ns::a16,true) |
297 | BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::tt_align_ns::a32,true) |
298 | BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::tt_align_ns::a64,true) |
299 | BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::tt_align_ns::a128,true) |
300 | } |
301 | #endif |
302 | |
303 | #else |
304 | |
305 | // |
306 | // Borland specific version, we have this for two reasons: |
307 | // 1) The version above doesn't always compile (with the new test cases for example) |
308 | // 2) Because of Borlands #pragma option we can create types with alignments that are |
309 | // greater that the largest aligned builtin type. |
310 | |
311 | namespace tt_align_ns{ |
312 | #pragma option push -a16 |
313 | struct a2{ short s; }; |
314 | struct a4{ int s; }; |
315 | struct a8{ double s; }; |
316 | struct a16{ long double s; }; |
317 | #pragma option pop |
318 | } |
319 | |
320 | namespace detail { |
321 | |
322 | typedef ::boost::tt_align_ns::a16 max_align; |
323 | |
324 | //#if ! BOOST_WORKAROUND(__CODEGEARC__, BOOST_TESTED_AT(0x610)) |
325 | BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::tt_align_ns::a2,true) |
326 | BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::tt_align_ns::a4,true) |
327 | BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::tt_align_ns::a8,true) |
328 | BOOST_TT_AUX_BOOL_TRAIT_IMPL_SPEC1(is_pod,::boost::tt_align_ns::a16,true) |
329 | //#endif |
330 | } |
331 | |
332 | template <std::size_t N> struct type_with_alignment |
333 | { |
334 | // We should never get to here, but if we do use the maximally |
335 | // aligned type: |
336 | // BOOST_STATIC_ASSERT(0); |
337 | typedef tt_align_ns::a16 type; |
338 | }; |
339 | template <> struct type_with_alignment<1>{ typedef char type; }; |
340 | template <> struct type_with_alignment<2>{ typedef tt_align_ns::a2 type; }; |
341 | template <> struct type_with_alignment<4>{ typedef tt_align_ns::a4 type; }; |
342 | template <> struct type_with_alignment<8>{ typedef tt_align_ns::a8 type; }; |
343 | template <> struct type_with_alignment<16>{ typedef tt_align_ns::a16 type; }; |
344 | |
345 | #endif |
346 | |
347 | } // namespace boost |
348 | |
349 | #ifdef BOOST_MSVC |
350 | # pragma warning(pop) |
351 | #endif |
352 | |
353 | #include <boost/type_traits/detail/bool_trait_undef.hpp> |
354 | |
355 | #endif // BOOST_TT_TYPE_WITH_ALIGNMENT_INCLUDED |
356 | |
357 | |
358 | |