base_from_member.hpp 5.6 KB

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