base_from_member.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // boost utility/base_from_member.hpp header file --------------------------//
  2. // Copyright 2001, 2003, 2004 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/preprocessor/arithmetic/inc.hpp>
  10. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  11. #include <boost/preprocessor/repetition/enum_params.hpp>
  12. #include <boost/preprocessor/repetition/repeat_from_to.hpp>
  13. // Base-from-member arity configuration macro ------------------------------//
  14. // The following macro determines how many arguments will be in the largest
  15. // constructor template of base_from_member. Constructor templates will be
  16. // generated from one argument to this maximum. Code from other files can read
  17. // this number if they need to always match the exact maximum base_from_member
  18. // uses. The maximum constructor length can be changed by overriding the
  19. // #defined constant. Make sure to apply the override, if any, for all source
  20. // files during project compiling for consistency.
  21. // Contributed by Jonathan Turkanis
  22. #ifndef BOOST_BASE_FROM_MEMBER_MAX_ARITY
  23. #define BOOST_BASE_FROM_MEMBER_MAX_ARITY 10
  24. #endif
  25. // An iteration of a constructor template for base_from_member -------------//
  26. // A macro that should expand to:
  27. // template < typename T1, ..., typename Tn >
  28. // base_from_member( T1 x1, ..., Tn xn )
  29. // : member( x1, ..., xn )
  30. // {}
  31. // This macro should only persist within this file.
  32. #define BOOST_PRIVATE_CTR_DEF( z, n, data ) \
  33. template < BOOST_PP_ENUM_PARAMS(n, typename T) > \
  34. explicit base_from_member( BOOST_PP_ENUM_BINARY_PARAMS(n, T, x) ) \
  35. : member( BOOST_PP_ENUM_PARAMS(n, x) ) \
  36. {} \
  37. /**/
  38. namespace boost
  39. {
  40. // Base-from-member class template -----------------------------------------//
  41. // Helper to initialize a base object so a derived class can use this
  42. // object in the initialization of another base class. Used by
  43. // Dietmar Kuehl from ideas by Ron Klatcho to solve the problem of a
  44. // base class needing to be initialized by a member.
  45. // Contributed by Daryle Walker
  46. template < typename MemberType, int UniqueID = 0 >
  47. class base_from_member
  48. {
  49. protected:
  50. MemberType member;
  51. base_from_member()
  52. : member()
  53. {}
  54. BOOST_PP_REPEAT_FROM_TO( 1, BOOST_PP_INC(BOOST_BASE_FROM_MEMBER_MAX_ARITY),
  55. BOOST_PRIVATE_CTR_DEF, _ )
  56. }; // boost::base_from_member
  57. } // namespace boost
  58. // Undo any private macros
  59. #undef BOOST_PRIVATE_CTR_DEF
  60. #endif // BOOST_UTILITY_BASE_FROM_MEMBER_HPP