// struct_traits.hpp: type trsits for struct // Copyright Takeshi Mouri 2006, 2007. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // See http://hamigaki.sourceforge.jp/libs/binary #ifndef HAMIGAKI_BINARY_STRUCT_TRAITS_HPP #define HAMIGAKI_BINARY_STRUCT_TRAITS_HPP #include #include #include #include #include #include #include #include namespace hamigaki { template struct struct_traits; template struct member_base { typedef Struct struct_type; typedef Type member_type; Type& operator()(Struct& x) const { return x.*PtrToMember; } const Type& operator()(const Struct& x) const { return x.*PtrToMember; } }; template< class Struct, class Type, Type Struct::* PtrToMember, endianness E=native > struct member : member_base { static const endianness endian = E; }; template struct padding { typedef void struct_type; typedef void member_type; static const endianness endian = native; template void operator()(const Struct& x) const { } }; template struct struct_size; namespace detail { template struct sizeof_impl : struct_size::type {}; template struct sizeof_impl : boost::mpl::size_t::value*Size> {}; template<> struct sizeof_impl : boost::mpl::size_t<1> {}; template<> struct sizeof_impl : boost::mpl::size_t<1> {}; template<> struct sizeof_impl : boost::mpl::size_t<1> {}; template<> struct sizeof_impl : boost::mpl::size_t { }; template<> struct sizeof_impl : boost::mpl::size_t { }; template<> struct sizeof_impl : boost::mpl::size_t { }; template<> struct sizeof_impl : boost::mpl::size_t { }; template<> struct sizeof_impl : boost::mpl::size_t { }; template<> struct sizeof_impl : boost::mpl::size_t { }; #if defined(BOOST_HAS_LONG_LONG) template<> struct sizeof_impl : boost::mpl::size_t { }; template<> struct sizeof_impl : boost::mpl::size_t { }; #endif // defined(BOOST_HAS_LONG_LONG) } // namespace detail template struct member_size; template struct member_size > : detail::sizeof_impl { }; template struct member_size > : boost::mpl::size_t {}; template struct struct_size : boost::mpl::size_t< boost::mpl::accumulate< typename struct_traits::members, boost::mpl::size_t<0>, boost::mpl::plus< boost::mpl::_1, member_size > >::type::value > { }; template struct binary_size : detail::sizeof_impl {}; template struct member_offset : boost::mpl::size_t< boost::mpl::accumulate< boost::mpl::iterator_range< typename boost::mpl::begin< typename struct_traits::members >::type, typename boost::mpl::find_if< typename struct_traits::members, boost::is_convertible >::type >, boost::mpl::size_t<0>, boost::mpl::plus< boost::mpl::_1, member_size > >::type::value > { }; template struct binary_offset : member_offset > { }; } // End namespace hamigaki. #endif // HAMIGAKI_BINARY_STRUCT_TRAITS_HPP