Index: boost/type_traits/alignment_traits.hpp =================================================================== RCS file: /cvsroot/boost/boost/boost/type_traits/alignment_traits.hpp,v retrieving revision 1.4 diff -c -r1.4 alignment_traits.hpp *** boost/type_traits/alignment_traits.hpp 8 Oct 2001 11:33:02 -0000 1.4 --- boost/type_traits/alignment_traits.hpp 27 Mar 2002 00:22:53 -0000 *************** *** 16,23 **** --- 16,30 ---- #ifndef BOOST_ICE_TYPE_TRAITS_HPP #include #endif + #include + #include + #include + #include + #include namespace boost{ + template struct alignment_of; + // // get the alignment of some arbitrary type: namespace detail{ *************** *** 36,41 **** --- 43,144 ---- BOOST_STATIC_CONSTANT(std::size_t, value = A < S ? A : S); }; + struct alignment_dummy; + typedef void (*function_ptr)(); + typedef int (alignment_dummy::*member_ptr); + typedef int (alignment_dummy::*member_function_ptr)(); + + /* + * The ct_if implementation is temporary code. It will be replaced with MPL + * in the future... + */ + struct select_then + { + template + struct result + { + typedef Then type; + }; + }; + + struct select_else + { + template + struct result + { + typedef Else type; + }; + }; + + template + struct ct_if_selector + { + typedef select_then type; + }; + + template<> + struct ct_if_selector + { + typedef select_else type; + }; + + template + struct ct_if + { + typedef typename ct_if_selector::type select; + typedef typename select::template result::type type; + }; + + #define BOOST_TT_ALIGNMENT_TYPES BOOST_PP_TUPLE_TO_LIST( \ + 11, ( \ + char, short, int, long, float, double, long double \ + , void*, function_ptr, member_ptr, member_function_ptr)) + + #define BOOST_TT_CHOOSE_LOWER_ALIGNMENT(R,P,I,T) \ + typename ct_if< \ + alignment_of::value <= target, T, char>::type BOOST_PP_CAT(t,I); + + #define BOOST_TT_CHOOSE_LOWER_SIZE(R,P,I,T) \ + typename ct_if< \ + sizeof(T) <= target, T, char>::type BOOST_PP_CAT(t,I); + + #define BOOST_TT_CHOOSE_T(R,P,I,T) T BOOST_PP_CAT(t,I); + + template + union lower_alignment + { + BOOST_PP_LIST_FOR_EACH_I( + BOOST_TT_CHOOSE_LOWER_ALIGNMENT + , ignored, BOOST_TT_ALIGNMENT_TYPES) + }; + + template + union lower_size + { + BOOST_PP_LIST_FOR_EACH_I( + BOOST_TT_CHOOSE_LOWER_SIZE + , ignored, BOOST_TT_ALIGNMENT_TYPES) + }; + + union max_align + { + BOOST_PP_LIST_FOR_EACH_I( + BOOST_TT_CHOOSE_T + , ignored, BOOST_TT_ALIGNMENT_TYPES) + }; + + #undef BOOST_TT_ALIGNMENT_TYPES + #undef BOOST_TT_CHOOSE_LOWER_ALIGNMENT + #undef BOOST_TT_CHOOSE_LOWER_SIZE + #undef BOOST_TT_CHOOSE_T + + template + union aligned_storage + { + Align align; + char bytes[size]; + }; + } // namespace detail template *************** *** 76,84 **** { BOOST_STATIC_CONSTANT(std::size_t, value = 0); }; #endif ! } // namespace boost ! #endif // ALIGNMENT_TYPE_TRAITS_HPP ! --- 179,219 ---- { BOOST_STATIC_CONSTANT(std::size_t, value = 0); }; #endif ! template ! class type_with_alignment ! { ! typedef detail::lower_alignment t1; ! BOOST_STATIC_CONSTANT(bool, t1_aligned = ! (alignment_of::value >= Align) ! & (alignment_of::value % Align == 0)); ! ! typedef detail::lower_size t2; ! ! BOOST_STATIC_CONSTANT(bool, t2_aligned = ! (alignment_of::value >= Align) ! & (alignment_of::value % Align == 0)); ! ! ! typedef typename detail::ct_if< ! t1_aligned ! , t1 ! , typename detail::ct_if< ! t2_aligned ! , t2 ! , detail::max_align ! >::type ! >::type align_t; ! ! BOOST_STATIC_CONSTANT(std::size_t, found = alignment_of::value); ! ! BOOST_STATIC_ASSERT(found >= Align); ! BOOST_STATIC_ASSERT(found % Align == 0); ! ! public: ! typedef align_t type; ! }; ! } // namespace boost + #endif // ALIGNMENT_TYPE_TRAITS_HPP Index: boost/type_traits/type_traits_test.hpp =================================================================== RCS file: /cvsroot/boost/boost/boost/type_traits/type_traits_test.hpp,v retrieving revision 1.21 diff -c -r1.21 type_traits_test.hpp *** boost/type_traits/type_traits_test.hpp 22 Mar 2002 11:25:32 -0000 1.21 --- boost/type_traits/type_traits_test.hpp 27 Mar 2002 00:22:54 -0000 *************** *** 197,202 **** --- 197,222 ---- #define align_test(T) test_align::do_it() + template + struct test_type_with_align + { + static void do_it() + { + enum { align = boost::alignment_of::value }; + typedef typename boost::type_with_alignment::type + align_t; + int new_align = boost::alignment_of::value; + ++test_count; + if (new_align % align) { + ++failures; + std::cout << "checking for an object with same alignment as " + << typeid(T).name() << "...failed" << std::endl; + std::cout << "\tfound: " << typeid(align_t).name() << std::endl; + } + } + }; + + #define type_with_align_test(T) test_type_with_align::do_it() // // the following code allows us to test that a particular // template functions correctly when instanciated inside another template Index: libs/type_traits/tests/alignment_test.cpp =================================================================== RCS file: /cvsroot/boost/boost/libs/type_traits/tests/alignment_test.cpp,v retrieving revision 1.5 diff -c -r1.5 alignment_test.cpp *** libs/type_traits/tests/alignment_test.cpp 24 Feb 2002 02:48:54 -0000 1.5 --- libs/type_traits/tests/alignment_test.cpp 27 Mar 2002 00:22:57 -0000 *************** *** 36,41 **** --- 36,46 ---- align_test(VD); value_test(0, ::boost::alignment_of::value); + type_with_align_test(int); + type_with_align_test(int(*)(int)); + type_with_align_test(VB); + type_with_align_test(VD); + return check_result(argc, argv); }