ptr_list_inserter.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Boost.Assign library
  2. //
  3. // Copyright Thorsten Ottosen 2003-2005. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see http://www.boost.org/libs/assign/
  9. //
  10. #ifndef BOOST_ASSIGN_PTR_LIST_INSERTER_HPP
  11. #define BOOST_ASSIGN_PTR_LIST_INSERTER_HPP
  12. #if defined(_MSC_VER)
  13. # pragma once
  14. #endif
  15. #include <boost/assign/list_inserter.hpp>
  16. #include <boost/type_traits/remove_reference.hpp>
  17. #include <boost/type_traits/remove_pointer.hpp>
  18. #include <boost/move/utility.hpp>
  19. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  20. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  21. #include <boost/preprocessor/repetition/enum_params.hpp>
  22. #include <boost/preprocessor/iteration/local.hpp>
  23. #endif
  24. namespace boost
  25. {
  26. namespace assign
  27. {
  28. template< class Function, class Obj >
  29. class ptr_list_inserter
  30. {
  31. typedef BOOST_DEDUCED_TYPENAME
  32. remove_pointer< BOOST_DEDUCED_TYPENAME
  33. remove_reference<Obj>::type >::type
  34. obj_type;
  35. public:
  36. ptr_list_inserter( Function fun ) : insert_( fun )
  37. {}
  38. template< class Function2, class Obj2 >
  39. ptr_list_inserter( const ptr_list_inserter<Function2,Obj2>& r )
  40. : insert_( r.fun_private() )
  41. {}
  42. ptr_list_inserter( const ptr_list_inserter& r ) : insert_( r.insert_ )
  43. {}
  44. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  45. ptr_list_inserter& operator()()
  46. {
  47. insert_( new obj_type() );
  48. return *this;
  49. }
  50. template< class T >
  51. ptr_list_inserter& operator()( const T& t )
  52. {
  53. insert_( new obj_type(t) );
  54. return *this;
  55. }
  56. #ifndef BOOST_ASSIGN_MAX_PARAMS // use user's value
  57. #define BOOST_ASSIGN_MAX_PARAMS 5
  58. #endif
  59. #define BOOST_ASSIGN_MAX_PARAMETERS (BOOST_ASSIGN_MAX_PARAMS - 1)
  60. #define BOOST_ASSIGN_PARAMS1(n) BOOST_PP_ENUM_PARAMS(n, class T)
  61. #define BOOST_ASSIGN_PARAMS2(n) BOOST_PP_ENUM_BINARY_PARAMS(n, T, const& t)
  62. #define BOOST_ASSIGN_PARAMS3(n) BOOST_PP_ENUM_PARAMS(n, t)
  63. #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
  64. #define BOOST_PP_LOCAL_MACRO(n) \
  65. template< class T, BOOST_ASSIGN_PARAMS1(n) > \
  66. ptr_list_inserter& operator()( const T& t, BOOST_ASSIGN_PARAMS2(n) ) \
  67. { \
  68. insert_( new obj_type(t, BOOST_ASSIGN_PARAMS3(n) )); \
  69. return *this; \
  70. } \
  71. /**/
  72. #include BOOST_PP_LOCAL_ITERATE()
  73. #else
  74. template< class... Ts >
  75. ptr_list_inserter& operator()(Ts&&... ts)
  76. {
  77. insert_(new obj_type(boost::forward<Ts>(ts)...));
  78. return *this;
  79. }
  80. #endif
  81. private:
  82. ptr_list_inserter& operator=( const ptr_list_inserter& );
  83. Function insert_;
  84. };
  85. template< class Obj, class Function >
  86. inline ptr_list_inserter< Function, Obj >
  87. make_ptr_list_inserter( Function fun )
  88. {
  89. return ptr_list_inserter< Function, Obj >( fun );
  90. }
  91. template< class C >
  92. inline ptr_list_inserter< assign_detail::call_push_back<C>,
  93. BOOST_DEDUCED_TYPENAME C::reference >
  94. ptr_push_back( C& c )
  95. {
  96. return make_ptr_list_inserter<BOOST_DEDUCED_TYPENAME C::reference>
  97. ( assign_detail::call_push_back<C>( c ) );
  98. }
  99. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  100. template< class T, class C >
  101. inline ptr_list_inserter< assign_detail::call_push_back<C>, T >
  102. ptr_push_back( C& c )
  103. {
  104. return make_ptr_list_inserter<T>(
  105. assign_detail::call_push_back<C>( c ) );
  106. }
  107. #endif
  108. template< class C >
  109. inline ptr_list_inserter< assign_detail::call_push_front<C>,
  110. BOOST_DEDUCED_TYPENAME C::reference >
  111. ptr_push_front( C& c )
  112. {
  113. return make_ptr_list_inserter<BOOST_DEDUCED_TYPENAME C::reference>
  114. ( assign_detail::call_push_front<C>( c ) );
  115. }
  116. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  117. template< class T, class C >
  118. inline ptr_list_inserter< assign_detail::call_push_front<C>, T >
  119. ptr_push_front( C& c )
  120. {
  121. return make_ptr_list_inserter<T>(
  122. assign_detail::call_push_front<C>( c ) );
  123. }
  124. #endif
  125. template< class C >
  126. inline ptr_list_inserter< assign_detail::call_insert<C>,
  127. BOOST_DEDUCED_TYPENAME C::reference>
  128. ptr_insert( C& c )
  129. {
  130. return make_ptr_list_inserter<BOOST_DEDUCED_TYPENAME C::reference>
  131. ( assign_detail::call_insert<C>( c ) );
  132. }
  133. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  134. template< class T, class C >
  135. inline ptr_list_inserter< assign_detail::call_insert<C>, T >
  136. ptr_insert( C& c )
  137. {
  138. return make_ptr_list_inserter<T>( assign_detail::call_insert<C>( c ) );
  139. }
  140. #endif
  141. } // namespace 'assign'
  142. } // namespace 'boost'
  143. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  144. #undef BOOST_ASSIGN_PARAMS1
  145. #undef BOOST_ASSIGN_PARAMS2
  146. #undef BOOST_ASSIGN_PARAMS3
  147. #undef BOOST_ASSIGN_MAX_PARAMETERS
  148. #endif
  149. #endif