Chris@16: // boost utility/base_from_member.hpp header file --------------------------// Chris@16: Chris@16: // Copyright 2001, 2003, 2004, 2012 Daryle Walker. Use, modification, and Chris@16: // distribution are subject to the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or a copy at Chris@16: // .) Chris@16: Chris@16: // See for the library's home page. Chris@16: Chris@16: #ifndef BOOST_UTILITY_BASE_FROM_MEMBER_HPP Chris@16: #define BOOST_UTILITY_BASE_FROM_MEMBER_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: Chris@16: // Base-from-member arity configuration macro ------------------------------// Chris@16: Chris@16: // The following macro determines how many arguments will be in the largest Chris@16: // constructor template of base_from_member. Constructor templates will be Chris@16: // generated from one argument to this maximum. Code from other files can read Chris@16: // this number if they need to always match the exact maximum base_from_member Chris@16: // uses. The maximum constructor length can be changed by overriding the Chris@16: // #defined constant. Make sure to apply the override, if any, for all source Chris@16: // files during project compiling for consistency. Chris@16: Chris@16: // Contributed by Jonathan Turkanis Chris@16: Chris@16: #ifndef BOOST_BASE_FROM_MEMBER_MAX_ARITY Chris@16: #define BOOST_BASE_FROM_MEMBER_MAX_ARITY 10 Chris@16: #endif Chris@16: Chris@16: Chris@16: // An iteration of a constructor template for base_from_member -------------// Chris@16: Chris@16: // A macro that should expand to: Chris@16: // template < typename T1, ..., typename Tn > Chris@16: // base_from_member( T1 x1, ..., Tn xn ) Chris@16: // : member( x1, ..., xn ) Chris@16: // {} Chris@16: // This macro should only persist within this file. Chris@16: Chris@16: #define BOOST_PRIVATE_CTR_DEF( z, n, data ) \ Chris@16: template < BOOST_PP_ENUM_PARAMS(n, typename T) > \ Chris@16: explicit base_from_member( BOOST_PP_ENUM_BINARY_PARAMS(n, T, x) ) \ Chris@16: : member( BOOST_PP_ENUM_PARAMS(n, x) ) \ Chris@16: {} \ Chris@16: /**/ Chris@16: Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: Chris@16: // Type-unmarking class template -------------------------------------------// Chris@16: Chris@16: // Type-trait to get the raw type, i.e. the type without top-level reference nor Chris@16: // cv-qualification, from a type expression. Mainly for function arguments, any Chris@16: // reference part is stripped first. Chris@16: Chris@16: // Contributed by Daryle Walker Chris@16: Chris@16: template < typename T > Chris@16: struct remove_cv_ref Chris@16: { Chris@16: typedef typename ::boost::remove_cv::type>::type type; Chris@16: Chris@16: }; // boost::detail::remove_cv_ref Chris@16: Chris@16: // Unmarked-type comparison class template ---------------------------------// Chris@16: Chris@16: // Type-trait to check if two type expressions have the same raw type. Chris@16: Chris@16: // Contributed by Daryle Walker, based on a work-around by Luc Danton Chris@16: Chris@16: template < typename T, typename U > Chris@16: struct is_related Chris@16: : public ::boost::is_same< Chris@16: typename ::boost::detail::remove_cv_ref::type, Chris@16: typename ::boost::detail::remove_cv_ref::type > Chris@16: {}; Chris@16: Chris@16: // Enable-if-on-unidentical-unmarked-type class template -------------------// Chris@16: Chris@16: // Enable-if on the first two type expressions NOT having the same raw type. Chris@16: Chris@16: // Contributed by Daryle Walker, based on a work-around by Luc Danton Chris@16: Chris@16: #ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES Chris@16: template Chris@16: struct enable_if_unrelated Chris@16: : public ::boost::enable_if_c Chris@16: {}; Chris@16: Chris@16: template Chris@16: struct enable_if_unrelated Chris@16: : public ::boost::disable_if< ::boost::detail::is_related > Chris@16: {}; Chris@16: #endif Chris@16: Chris@16: } // namespace boost::detail Chris@16: Chris@16: Chris@16: // Base-from-member class template -----------------------------------------// Chris@16: Chris@16: // Helper to initialize a base object so a derived class can use this Chris@16: // object in the initialization of another base class. Used by Chris@16: // Dietmar Kuehl from ideas by Ron Klatcho to solve the problem of a Chris@16: // base class needing to be initialized by a member. Chris@16: Chris@16: // Contributed by Daryle Walker Chris@16: Chris@16: template < typename MemberType, int UniqueID = 0 > Chris@16: class base_from_member Chris@16: { Chris@16: protected: Chris@16: MemberType member; Chris@16: Chris@16: #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \ Chris@16: !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \ Chris@16: !defined(BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS) && \ Chris@16: !(defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 4)) Chris@16: template ::type> Chris@16: explicit BOOST_CONSTEXPR base_from_member( T&& ...x ) Chris@16: BOOST_NOEXCEPT_IF( BOOST_NOEXCEPT_EXPR(::new ((void*) 0) MemberType( Chris@16: static_cast(x)... )) ) // no std::is_nothrow_constructible... Chris@16: : member( static_cast(x)... ) // ...nor std::forward needed Chris@16: {} Chris@16: #else Chris@16: base_from_member() Chris@16: : member() Chris@16: {} Chris@16: Chris@16: BOOST_PP_REPEAT_FROM_TO( 1, BOOST_PP_INC(BOOST_BASE_FROM_MEMBER_MAX_ARITY), Chris@16: BOOST_PRIVATE_CTR_DEF, _ ) Chris@16: #endif Chris@16: Chris@16: }; // boost::base_from_member Chris@16: Chris@101: template < typename MemberType, int UniqueID > Chris@101: class base_from_member Chris@101: { Chris@101: protected: Chris@101: MemberType& member; Chris@101: Chris@101: explicit BOOST_CONSTEXPR base_from_member( MemberType& x ) Chris@101: BOOST_NOEXCEPT Chris@101: : member( x ) Chris@101: {} Chris@101: Chris@101: }; // boost::base_from_member Chris@101: Chris@16: } // namespace boost Chris@16: Chris@16: Chris@16: // Undo any private macros Chris@16: #undef BOOST_PRIVATE_CTR_DEF Chris@16: Chris@16: Chris@16: #endif // BOOST_UTILITY_BASE_FROM_MEMBER_HPP