base_from_member.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #define BOOST_PRIVATE_CTR_DEF( z, n, data ) \
  38. template < BOOST_PP_ENUM_PARAMS(n, typename T) > \
  39. base_from_member( BOOST_PP_ENUM_BINARY_PARAMS(n, T, x) ) \
  40. : member( BOOST_PP_ENUM_PARAMS(n, x) ) \
  41. {} \
  42. /**/
  43. namespace boost
  44. {
  45. namespace detail
  46. {
  47. // Type-unmarking class template -------------------------------------------//
  48. // Type-trait to get the raw type, i.e. the type without top-level reference nor
  49. // cv-qualification, from a type expression. Mainly for function arguments, any
  50. // reference part is stripped first.
  51. // Contributed by Daryle Walker
  52. template < typename T >
  53. struct remove_cv_ref
  54. {
  55. typedef typename ::boost::remove_cv<typename
  56. ::boost::remove_reference<T>::type>::type type;
  57. }; // boost::detail::remove_cv_ref
  58. // Unmarked-type comparison class template ---------------------------------//
  59. // Type-trait to check if two type expressions have the same raw type.
  60. // Contributed by Daryle Walker, based on a work-around by Luc Danton
  61. template < typename T, typename U >
  62. struct is_related
  63. : public ::boost::is_same<
  64. typename ::boost::detail::remove_cv_ref<T>::type,
  65. typename ::boost::detail::remove_cv_ref<U>::type >
  66. {};
  67. // Enable-if-on-unidentical-unmarked-type class template -------------------//
  68. // Enable-if on the first two type expressions NOT having the same raw type.
  69. // Contributed by Daryle Walker, based on a work-around by Luc Danton
  70. #ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  71. template<typename ...T>
  72. struct enable_if_unrelated
  73. : public ::boost::enable_if_c<true>
  74. {};
  75. template<typename T, typename U, typename ...U2>
  76. struct enable_if_unrelated<T, U, U2...>
  77. : public ::boost::disable_if< ::boost::detail::is_related<T, U> >
  78. {};
  79. #endif
  80. } // namespace boost::detail
  81. // Base-from-member class template -----------------------------------------//
  82. // Helper to initialize a base object so a derived class can use this
  83. // object in the initialization of another base class. Used by
  84. // Dietmar Kuehl from ideas by Ron Klatcho to solve the problem of a
  85. // base class needing to be initialized by a member.
  86. // Contributed by Daryle Walker
  87. template < typename MemberType, int UniqueID = 0 >
  88. class base_from_member
  89. {
  90. protected:
  91. MemberType member;
  92. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
  93. !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
  94. !defined(BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS) && \
  95. !(defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 4))
  96. template <typename ...T, typename EnableIf = typename
  97. ::boost::detail::enable_if_unrelated<base_from_member, T...>::type>
  98. explicit BOOST_CONSTEXPR base_from_member( T&& ...x )
  99. BOOST_NOEXCEPT_IF( BOOST_NOEXCEPT_EXPR(::new ((void*) 0) MemberType(
  100. static_cast<T&&>(x)... )) ) // no std::is_nothrow_constructible...
  101. : member( static_cast<T&&>(x)... ) // ...nor std::forward needed
  102. {}
  103. #else
  104. base_from_member()
  105. : member()
  106. {}
  107. template < typename T0 > explicit base_from_member( T0 x0 ) : member( x0 ) {}
  108. BOOST_PP_REPEAT_FROM_TO( 2, BOOST_PP_INC(BOOST_BASE_FROM_MEMBER_MAX_ARITY),
  109. BOOST_PRIVATE_CTR_DEF, _ )
  110. #endif
  111. }; // boost::base_from_member
  112. template < typename MemberType, int UniqueID >
  113. class base_from_member<MemberType&, UniqueID>
  114. {
  115. protected:
  116. MemberType& member;
  117. explicit BOOST_CONSTEXPR base_from_member( MemberType& x )
  118. BOOST_NOEXCEPT
  119. : member( x )
  120. {}
  121. }; // boost::base_from_member
  122. } // namespace boost
  123. // Undo any private macros
  124. #undef BOOST_PRIVATE_CTR_DEF
  125. #endif // BOOST_UTILITY_BASE_FROM_MEMBER_HPP