Line data Source code
1 : // boost utility/base_from_member.hpp header file --------------------------// 2 : 3 : // Copyright 2001, 2003, 2004, 2012 Daryle Walker. Use, modification, and 4 : // distribution are subject to the Boost Software License, Version 1.0. (See 5 : // accompanying file LICENSE_1_0.txt or a copy at 6 : // <http://www.boost.org/LICENSE_1_0.txt>.) 7 : 8 : // See <http://www.boost.org/libs/utility/> for the library's home page. 9 : 10 : #ifndef BOOST_UTILITY_BASE_FROM_MEMBER_HPP 11 : #define BOOST_UTILITY_BASE_FROM_MEMBER_HPP 12 : 13 : #include <boost/config.hpp> 14 : #include <boost/preprocessor/arithmetic/inc.hpp> 15 : #include <boost/preprocessor/repetition/enum_binary_params.hpp> 16 : #include <boost/preprocessor/repetition/enum_params.hpp> 17 : #include <boost/preprocessor/repetition/repeat_from_to.hpp> 18 : #include <boost/type_traits/is_same.hpp> 19 : #include <boost/type_traits/remove_cv.hpp> 20 : #include <boost/type_traits/remove_reference.hpp> 21 : #include <boost/utility/enable_if.hpp> 22 : 23 : 24 : // Base-from-member arity configuration macro ------------------------------// 25 : 26 : // The following macro determines how many arguments will be in the largest 27 : // constructor template of base_from_member. Constructor templates will be 28 : // generated from one argument to this maximum. Code from other files can read 29 : // this number if they need to always match the exact maximum base_from_member 30 : // uses. The maximum constructor length can be changed by overriding the 31 : // #defined constant. Make sure to apply the override, if any, for all source 32 : // files during project compiling for consistency. 33 : 34 : // Contributed by Jonathan Turkanis 35 : 36 : #ifndef BOOST_BASE_FROM_MEMBER_MAX_ARITY 37 : #define BOOST_BASE_FROM_MEMBER_MAX_ARITY 10 38 : #endif 39 : 40 : 41 : // An iteration of a constructor template for base_from_member -------------// 42 : 43 : // A macro that should expand to: 44 : // template < typename T1, ..., typename Tn > 45 : // base_from_member( T1 x1, ..., Tn xn ) 46 : // : member( x1, ..., xn ) 47 : // {} 48 : // This macro should only persist within this file. 49 : 50 : #ifndef BOOST_UTILITY_DOCS 51 : #define BOOST_PRIVATE_CTR_DEF( z, n, data ) \ 52 : template < BOOST_PP_ENUM_PARAMS(n, typename T) > \ 53 : base_from_member( BOOST_PP_ENUM_BINARY_PARAMS(n, T, x) ) \ 54 : : member( BOOST_PP_ENUM_PARAMS(n, x) ) \ 55 : {} \ 56 : /**/ 57 : #endif // BOOST_UTILITY_DOCS 58 : 59 : namespace boost 60 : { 61 : 62 : namespace detail 63 : { 64 : 65 : // Type-unmarking class template -------------------------------------------// 66 : 67 : // Type-trait to get the raw type, i.e. the type without top-level reference nor 68 : // cv-qualification, from a type expression. Mainly for function arguments, any 69 : // reference part is stripped first. 70 : 71 : // Contributed by Daryle Walker 72 : 73 : template < typename T > 74 : struct remove_cv_ref 75 : { 76 : typedef typename ::boost::remove_cv<typename 77 : ::boost::remove_reference<T>::type>::type type; 78 : 79 : }; // boost::detail::remove_cv_ref 80 : 81 : // Unmarked-type comparison class template ---------------------------------// 82 : 83 : // Type-trait to check if two type expressions have the same raw type. 84 : 85 : // Contributed by Daryle Walker, based on a work-around by Luc Danton 86 : 87 : template < typename T, typename U > 88 : struct is_related 89 : : public ::boost::is_same< 90 : typename ::boost::detail::remove_cv_ref<T>::type, 91 : typename ::boost::detail::remove_cv_ref<U>::type > 92 : {}; 93 : 94 : // Enable-if-on-unidentical-unmarked-type class template -------------------// 95 : 96 : // Enable-if on the first two type expressions NOT having the same raw type. 97 : 98 : // Contributed by Daryle Walker, based on a work-around by Luc Danton 99 : 100 : #ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES 101 : template<typename ...T> 102 : struct enable_if_unrelated 103 : : public ::boost::enable_if_c<true> 104 : {}; 105 : 106 : template<typename T, typename U, typename ...U2> 107 : struct enable_if_unrelated<T, U, U2...> 108 : : public ::boost::disable_if< ::boost::detail::is_related<T, U> > 109 : {}; 110 : #endif 111 : 112 : } // namespace boost::detail 113 : 114 : 115 : // Base-from-member class template -----------------------------------------// 116 : 117 : // Helper to initialize a base object so a derived class can use this 118 : // object in the initialization of another base class. Used by 119 : // Dietmar Kuehl from ideas by Ron Klatcho to solve the problem of a 120 : // base class needing to be initialized by a member. 121 : 122 : // Contributed by Daryle Walker 123 : 124 : template < typename MemberType, int UniqueID = 0 > 125 : class base_from_member 126 : { 127 : protected: 128 : MemberType member; 129 : 130 : #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \ 131 : !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \ 132 : !defined(BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS) && \ 133 : !(defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 4)) 134 : template <typename ...T, typename EnableIf = typename 135 : ::boost::detail::enable_if_unrelated<base_from_member, T...>::type> 136 : explicit BOOST_CONSTEXPR base_from_member( T&& ...x ) 137 : BOOST_NOEXCEPT_IF( BOOST_NOEXCEPT_EXPR(::new ((void*) 0) MemberType( 138 : static_cast<T&&>(x)... )) ) // no std::is_nothrow_constructible... 139 : : member( static_cast<T&&>(x)... ) // ...nor std::forward needed 140 : {} 141 : #else 142 : base_from_member() 143 : : member() 144 : {} 145 : 146 72607 : template < typename T0 > explicit base_from_member( T0 x0 ) : member( x0 ) {} 147 : BOOST_PP_REPEAT_FROM_TO( 2, BOOST_PP_INC(BOOST_BASE_FROM_MEMBER_MAX_ARITY), 148 : BOOST_PRIVATE_CTR_DEF, _ ) 149 : #endif 150 : 151 : }; // boost::base_from_member 152 : 153 : template < typename MemberType, int UniqueID > 154 : class base_from_member<MemberType&, UniqueID> 155 : { 156 : protected: 157 : MemberType& member; 158 : 159 : explicit BOOST_CONSTEXPR base_from_member( MemberType& x ) 160 : BOOST_NOEXCEPT 161 : : member( x ) 162 : {} 163 : 164 : }; // boost::base_from_member 165 : 166 : } // namespace boost 167 : 168 : 169 : // Undo any private macros 170 : #undef BOOST_PRIVATE_CTR_DEF 171 : 172 : 173 : #endif // BOOST_UTILITY_BASE_FROM_MEMBER_HPP