in_place_factory.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (C) 2003, Fernando Luis Cacciola Carballal.
  2. // Copyright (C) 2007, Tobias Schwinger.
  3. //
  4. // Use, modification, and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/optional for documentation.
  9. //
  10. // You are welcome to contact the author at:
  11. // [email protected]
  12. //
  13. #ifndef BOOST_UTILITY_INPLACE_FACTORY_04APR2007_HPP
  14. #ifndef BOOST_PP_IS_ITERATING
  15. #include <boost/utility/detail/in_place_factory_prefix.hpp>
  16. namespace boost {
  17. class in_place_factory_base {} ;
  18. #ifndef BOOST_UTILITY_DOCS
  19. #define BOOST_PP_ITERATION_LIMITS (0, BOOST_MAX_INPLACE_FACTORY_ARITY)
  20. #define BOOST_PP_FILENAME_1 <boost/utility/in_place_factory.hpp>
  21. #endif // BOOST_UTILITY_DOCS
  22. #include BOOST_PP_ITERATE()
  23. } // namespace boost
  24. #include <boost/utility/detail/in_place_factory_suffix.hpp>
  25. #ifndef BOOST_UTILITY_DOCS
  26. #define BOOST_UTILITY_INPLACE_FACTORY_04APR2007_HPP
  27. #endif
  28. #else
  29. #define N BOOST_PP_ITERATION()
  30. #if N
  31. template< BOOST_PP_ENUM_PARAMS(N, class A) >
  32. #endif
  33. class BOOST_PP_CAT(in_place_factory,N)
  34. :
  35. public in_place_factory_base
  36. {
  37. public:
  38. explicit BOOST_PP_CAT(in_place_factory,N)
  39. ( BOOST_PP_ENUM_BINARY_PARAMS(N,A,const& a) )
  40. #if N > 0
  41. : BOOST_PP_ENUM(N, BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_INIT, _)
  42. #endif
  43. {}
  44. template<class T>
  45. void* apply(void* address) const
  46. {
  47. return new(address) T( BOOST_PP_ENUM_PARAMS(N, m_a) );
  48. }
  49. template<class T>
  50. void* apply(void* address, std::size_t n) const
  51. {
  52. for(char* next = address = this->BOOST_NESTED_TEMPLATE apply<T>(address);
  53. !! --n;)
  54. this->BOOST_NESTED_TEMPLATE apply<T>(next = next+sizeof(T));
  55. return address;
  56. }
  57. BOOST_PP_REPEAT(N, BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_DECL, _)
  58. };
  59. #if N > 0
  60. template< BOOST_PP_ENUM_PARAMS(N, class A) >
  61. inline BOOST_PP_CAT(in_place_factory,N)< BOOST_PP_ENUM_PARAMS(N, A) >
  62. in_place( BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& a) )
  63. {
  64. return BOOST_PP_CAT(in_place_factory,N)< BOOST_PP_ENUM_PARAMS(N, A) >
  65. ( BOOST_PP_ENUM_PARAMS(N, a) );
  66. }
  67. #else
  68. inline in_place_factory0 in_place()
  69. {
  70. return in_place_factory0();
  71. }
  72. #endif
  73. #undef N
  74. #endif
  75. #endif