in_place_factory.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/lib/optional for documentation.
  9. //
  10. // You are welcome to contact the author at:
  11. // fernando_cacciola@hotmail.com
  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
  42. BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) const
  43. {
  44. return new(address) T( BOOST_PP_ENUM_PARAMS(N, m_a) );
  45. }
  46. template<class T>
  47. void* apply(void* address, std::size_t n
  48. BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(T)) const
  49. {
  50. for(char* next = address = this->BOOST_NESTED_TEMPLATE apply<T>(address);
  51. !! --n;)
  52. this->BOOST_NESTED_TEMPLATE apply<T>(next = next+sizeof(T));
  53. return address;
  54. }
  55. BOOST_PP_REPEAT(N, BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_DECL, _)
  56. };
  57. #if N > 0
  58. template< BOOST_PP_ENUM_PARAMS(N, class A) >
  59. inline BOOST_PP_CAT(in_place_factory,N)< BOOST_PP_ENUM_PARAMS(N, A) >
  60. in_place( BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& a) )
  61. {
  62. return BOOST_PP_CAT(in_place_factory,N)< BOOST_PP_ENUM_PARAMS(N, A) >
  63. ( BOOST_PP_ENUM_PARAMS(N, a) );
  64. }
  65. #else
  66. inline in_place_factory0 in_place()
  67. {
  68. return in_place_factory0();
  69. }
  70. #endif
  71. #undef N
  72. #endif
  73. #endif