typed_in_place_factory.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // fernando_cacciola@hotmail.com
  12. //
  13. #ifndef BOOST_UTILITY_TYPED_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 typed_in_place_factory_base {} ;
  18. #define BOOST_PP_ITERATION_LIMITS (0, BOOST_MAX_INPLACE_FACTORY_ARITY)
  19. #define BOOST_PP_FILENAME_1 <boost/utility/typed_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_TYPED_INPLACE_FACTORY_04APR2007_HPP
  24. #else
  25. #define N BOOST_PP_ITERATION()
  26. template< class T BOOST_PP_ENUM_TRAILING_PARAMS(N,class A) >
  27. class BOOST_PP_CAT(typed_in_place_factory,N)
  28. :
  29. public typed_in_place_factory_base
  30. {
  31. public:
  32. typedef T value_type;
  33. explicit BOOST_PP_CAT(typed_in_place_factory,N)
  34. ( BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& a) )
  35. #if N > 0
  36. : BOOST_PP_ENUM(N, BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_INIT, _)
  37. #endif
  38. {}
  39. void* apply (void* address) const
  40. {
  41. return new(address) T( BOOST_PP_ENUM_PARAMS(N, m_a) );
  42. }
  43. void* apply (void* address, std::size_t n) const
  44. {
  45. for(void* next = address = this->apply(address); !! --n;)
  46. this->apply(next = static_cast<char *>(next) + sizeof(T));
  47. return address;
  48. }
  49. BOOST_PP_REPEAT(N, BOOST_DEFINE_INPLACE_FACTORY_CLASS_MEMBER_DECL, _)
  50. };
  51. template< class T BOOST_PP_ENUM_TRAILING_PARAMS(N, class A) >
  52. inline BOOST_PP_CAT(typed_in_place_factory,N)<
  53. T BOOST_PP_ENUM_TRAILING_PARAMS(N, A) >
  54. in_place( BOOST_PP_ENUM_BINARY_PARAMS(N, A, const& a) )
  55. {
  56. return BOOST_PP_CAT(typed_in_place_factory,N)<
  57. T BOOST_PP_ENUM_TRAILING_PARAMS(N, A) >( BOOST_PP_ENUM_PARAMS(N, a) );
  58. }
  59. #undef N
  60. #endif
  61. #endif