2 // (C) Copyright John Maddock 2000.
3 // Permission to copy, use, modify, sell and
4 // distribute this software is granted provided this copyright notice appears
5 // in all copies. This software is provided "as is" without express or implied
6 // warranty, and with no claim as to its suitability for any purpose.
9 // defines alignment_of:
11 #ifndef ALIGNMENT_TYPE_TRAITS_HPP
12 #define ALIGNMENT_TYPE_TRAITS_HPP
16 #ifndef BOOST_ICE_TYPE_TRAITS_HPP
17 #include <boost/type_traits/ice.hpp>
19 #include <boost/preprocessor/list/for_each_i.hpp>
20 #include <boost/preprocessor/tuple/to_list.hpp>
21 #include <boost/preprocessor/cat.hpp>
22 #include <boost/type_traits/transform_traits.hpp>
23 #include <boost/static_assert.hpp>
26 # pragma warning(push)
27 # pragma warning(disable: 4121) // alignment is sensitive to packing
31 template <class T> struct alignment_of;
34 // get the alignment of some arbitrary type:
38 struct alignment_of_hack
46 template <unsigned A, unsigned S>
47 struct alignment_logic
49 BOOST_STATIC_CONSTANT(std::size_t, value = A < S ? A : S);
57 BOOST_STATIC_CONSTANT(std::size_t, value =
58 (::boost::detail::alignment_logic<
59 sizeof(detail::alignment_of_hack<T>) - sizeof(T),
65 // references have to be treated specially, assume
66 // that a reference is just a special pointer:
67 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
69 struct alignment_of<T&>
72 BOOST_STATIC_CONSTANT(std::size_t, value = ::boost::alignment_of<T*>::value);
76 // void has to be treated specially:
78 struct alignment_of<void>
79 { BOOST_STATIC_CONSTANT(std::size_t, value = 0); };
80 #ifndef BOOST_NO_CV_VOID_SPECIALIZATIONS
82 struct alignment_of<const void>
83 { BOOST_STATIC_CONSTANT(std::size_t, value = 0); };
85 struct alignment_of<volatile void>
86 { BOOST_STATIC_CONSTANT(std::size_t, value = 0); };
88 struct alignment_of<const volatile void>
89 { BOOST_STATIC_CONSTANT(std::size_t, value = 0); };
93 class alignment_dummy;
94 typedef void (*function_ptr)();
95 typedef int (alignment_dummy::*member_ptr);
96 typedef int (alignment_dummy::*member_function_ptr)();
99 * The ct_if implementation is temporary code. It will be replaced with MPL
104 template<typename Then, typename Else>
113 template<typename Then, typename Else>
120 template<bool Condition>
121 struct ct_if_selector
123 typedef select_then type;
127 struct ct_if_selector<false>
129 typedef select_else type;
132 template<bool Condition, typename Then, typename Else>
135 typedef typename ct_if_selector<Condition>::type select;
136 typedef typename select::template result<Then,Else>::type type;
139 #define BOOST_TT_ALIGNMENT_TYPES BOOST_PP_TUPLE_TO_LIST( \
141 char, short, int, long, float, double, long double \
142 , void*, function_ptr, member_ptr, member_function_ptr))
144 #define BOOST_TT_CHOOSE_LOWER_ALIGNMENT(R,P,I,T) \
146 alignment_of<T>::value <= target, T, char>::type BOOST_PP_CAT(t,I);
148 #define BOOST_TT_CHOOSE_T(R,P,I,T) T BOOST_PP_CAT(t,I);
150 template <std::size_t target>
151 union lower_alignment
153 BOOST_PP_LIST_FOR_EACH_I(
154 BOOST_TT_CHOOSE_LOWER_ALIGNMENT
155 , ignored, BOOST_TT_ALIGNMENT_TYPES)
160 BOOST_PP_LIST_FOR_EACH_I(
162 , ignored, BOOST_TT_ALIGNMENT_TYPES)
165 #undef BOOST_TT_ALIGNMENT_TYPES
166 #undef BOOST_TT_CHOOSE_LOWER_ALIGNMENT
167 #undef BOOST_TT_CHOOSE_T
169 template<int TAlign, int Align>
172 BOOST_STATIC_CONSTANT(bool,
173 value = (TAlign >= Align) & (TAlign % Align == 0));
178 // This alignment method originally due to Brian Parker, implemented by David
179 // Abrahams, and then ported here by Doug Gregor.
181 class type_with_alignment
183 typedef detail::lower_alignment<Align> t1;
185 typedef type_with_alignment<Align> this_type;
187 typedef typename detail::ct_if<
188 (detail::is_aligned<(alignment_of<t1>::value), Align>::value)
193 BOOST_STATIC_CONSTANT(std::size_t, found = alignment_of<align_t>::value);
195 BOOST_STATIC_ASSERT(found >= Align);
196 BOOST_STATIC_ASSERT(found % Align == 0);
199 typedef align_t type;
205 # pragma warning(pop)
208 #endif // ALIGNMENT_TYPE_TRAITS_HPP