in_place_factory.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #define BOOST_PP_ITERATION_LIMITS (0, BOOST_MAX_INPLACE_FACTORY_ARITY)
  19. #define BOOST_PP_FILENAME_1 <boost/utility/in_place_factory.hpp>
  20. #include BOOST_PP_ITERATE()
  21. } // namespace boost
  22. #include <boost/utility/detail/in_place_factory_suffix.hpp>
  23. #define BOOST_UTILITY_INPLACE_FACTORY_04APR2007_HPP
  24. #else
  25. #define N BOOST_PP_ITERATION()
  26. #if N
  27. template< BOOST_PP_ENUM_PARAMS(N, class A) >
  28. #endif
  29. class BOOST_PP_CAT(in_place_factory,N)
  30. :
  31. public in_place_factory_base
  32. {
  33. public:
  34. explicit BOOST_PP_CAT(in_place_factory,N)
  35. ( BOOST_PP_ENUM_BINARY_PARAMS(N,A,const& a) )
  36. #if N > 0
  37. : BOOST_PP_ENUM(N, BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_INIT, _)
  38. #endif
  39. {}
  40. template<class T>
  41. void* apply(void* address) const
  42. {
  43. return new(address) T( BOOST_PP_ENUM_PARAMS(N, m_a) );
  44. }
  45. template<class T>
  46. void* apply(void* address, std::size_t n) const
  47. {
  48. for(char* next = address = this->BOOST_NESTED_TEMPLATE apply<T>(address);
  49. !! --n;)
  50. this->BOOST_NESTED_TEMPLATE apply<T>(next = next+sizeof(T));
  51. return address;
  52. }
  53. BOOST_PP_REPEAT(N, BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_DECL, _)
  54. };
  55. #if N > 0
  56. template< BOOST_PP_ENUM_PARAMS(N, class A) >
  57. inline BOOST_PP_CAT(in_place_factory,N)< BOOST_PP_ENUM_PARAMS(N, A) >
  58. in_place( BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& a) )
  59. {
  60. return BOOST_PP_CAT(in_place_factory,N)< BOOST_PP_ENUM_PARAMS(N, A) >
  61. ( BOOST_PP_ENUM_PARAMS(N, a) );
  62. }
  63. #else
  64. inline in_place_factory0 in_place()
  65. {
  66. return in_place_factory0();
  67. }
  68. #endif
  69. #undef N
  70. #endif
  71. #endif