ptr_list_of.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_OF_HPP
  11. #define BOOST_ASSIGN_PTR_LIST_OF_HPP
  12. #if defined(_MSC_VER)
  13. # pragma once
  14. #endif
  15. #include <boost/assign/list_of.hpp>
  16. #include <boost/type_traits/remove_const.hpp>
  17. #include <boost/type_traits/remove_reference.hpp>
  18. #include <boost/type_traits/is_reference.hpp>
  19. #include <boost/static_assert.hpp>
  20. #include <boost/type_traits/detail/yes_no_type.hpp>
  21. #include <boost/type_traits/decay.hpp>
  22. #include <boost/type_traits/is_array.hpp>
  23. #include <boost/ptr_container/ptr_vector.hpp>
  24. #include <boost/move/utility.hpp>
  25. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  26. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  27. #include <boost/preprocessor/repetition/enum_params.hpp>
  28. #include <boost/preprocessor/iteration/local.hpp>
  29. #endif
  30. namespace boost
  31. {
  32. namespace assign_detail
  33. {
  34. /////////////////////////////////////////////////////////////////////////
  35. // Part 1: flexible and efficient interface
  36. /////////////////////////////////////////////////////////////////////////
  37. template< class T >
  38. class generic_ptr_list :
  39. public converter< generic_ptr_list<T>,
  40. BOOST_DEDUCED_TYPENAME boost::ptr_vector<T>::iterator >
  41. {
  42. protected:
  43. typedef boost::ptr_vector<T> impl_type;
  44. #if defined(BOOST_NO_AUTO_PTR)
  45. typedef std::unique_ptr<impl_type> release_type;
  46. #else
  47. typedef std::auto_ptr<impl_type> release_type;
  48. #endif
  49. mutable impl_type values_;
  50. public:
  51. typedef BOOST_DEDUCED_TYPENAME impl_type::iterator iterator;
  52. typedef iterator const_iterator;
  53. typedef BOOST_DEDUCED_TYPENAME impl_type::value_type value_type;
  54. typedef BOOST_DEDUCED_TYPENAME impl_type::size_type size_type;
  55. typedef BOOST_DEDUCED_TYPENAME impl_type::difference_type difference_type;
  56. public:
  57. generic_ptr_list() : values_( 32u )
  58. { }
  59. generic_ptr_list( release_type r ) : values_(r)
  60. { }
  61. release_type release()
  62. {
  63. return values_.release();
  64. }
  65. public:
  66. iterator begin() const { return values_.begin(); }
  67. iterator end() const { return values_.end(); }
  68. bool empty() const { return values_.empty(); }
  69. size_type size() const { return values_.size(); }
  70. public:
  71. operator impl_type() const
  72. {
  73. return values_;
  74. }
  75. template< template<class,class,class> class Seq, class U,
  76. class CA, class A >
  77. operator Seq<U,CA,A>() const
  78. {
  79. Seq<U,CA,A> result;
  80. result.transfer( result.end(), values_ );
  81. BOOST_ASSERT( empty() );
  82. return result;
  83. }
  84. template< class PtrContainer >
  85. #if defined(BOOST_NO_AUTO_PTR)
  86. std::unique_ptr<PtrContainer>
  87. #else
  88. std::auto_ptr<PtrContainer>
  89. #endif
  90. convert( const PtrContainer* c ) const
  91. {
  92. #if defined(BOOST_NO_AUTO_PTR)
  93. std::unique_ptr<PtrContainer> res( new PtrContainer() );
  94. #else
  95. std::auto_ptr<PtrContainer> res( new PtrContainer() );
  96. #endif
  97. while( !empty() )
  98. res->insert( res->end(),
  99. values_.pop_back().release() );
  100. return res;
  101. }
  102. template< class PtrContainer >
  103. #if defined(BOOST_NO_AUTO_PTR)
  104. std::unique_ptr<PtrContainer>
  105. #else
  106. std::auto_ptr<PtrContainer>
  107. #endif
  108. to_container( const PtrContainer& c ) const
  109. {
  110. return convert( &c );
  111. }
  112. protected:
  113. void push_back( T* r ) { values_.push_back( r ); }
  114. public:
  115. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  116. generic_ptr_list& operator()()
  117. {
  118. this->push_back( new T() );
  119. return *this;
  120. }
  121. template< class U >
  122. generic_ptr_list& operator()( const U& u )
  123. {
  124. this->push_back( new T(u) );
  125. return *this;
  126. }
  127. #ifndef BOOST_ASSIGN_MAX_PARAMS // use user's value
  128. #define BOOST_ASSIGN_MAX_PARAMS 5
  129. #endif
  130. #define BOOST_ASSIGN_MAX_PARAMETERS (BOOST_ASSIGN_MAX_PARAMS - 1)
  131. #define BOOST_ASSIGN_PARAMS1(n) BOOST_PP_ENUM_PARAMS(n, class U)
  132. #define BOOST_ASSIGN_PARAMS2(n) BOOST_PP_ENUM_BINARY_PARAMS(n, U, const& u)
  133. #define BOOST_ASSIGN_PARAMS3(n) BOOST_PP_ENUM_PARAMS(n, u)
  134. #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
  135. #define BOOST_PP_LOCAL_MACRO(n) \
  136. template< class U, BOOST_ASSIGN_PARAMS1(n) > \
  137. generic_ptr_list& operator()(U const& u, BOOST_ASSIGN_PARAMS2(n) ) \
  138. { \
  139. this->push_back( new T(u, BOOST_ASSIGN_PARAMS3(n))); \
  140. return *this; \
  141. } \
  142. /**/
  143. #include BOOST_PP_LOCAL_ITERATE()
  144. #else
  145. template< class... Us >
  146. generic_ptr_list& operator()(Us&&... us)
  147. {
  148. this->push_back(new T(boost::forward<Us>(us)...));
  149. return *this;
  150. }
  151. #endif
  152. }; // class 'generic_ptr_list'
  153. } // namespace 'assign_detail'
  154. namespace assign
  155. {
  156. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  157. template< class T >
  158. inline assign_detail::generic_ptr_list<T>
  159. ptr_list_of()
  160. {
  161. assign_detail::generic_ptr_list<T> gpl;
  162. gpl();
  163. return gpl;
  164. }
  165. template< class T, class U >
  166. inline assign_detail::generic_ptr_list<T>
  167. ptr_list_of( const U& t )
  168. {
  169. assign_detail::generic_ptr_list<T> gpl;
  170. gpl( t );
  171. return gpl;
  172. }
  173. #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
  174. #define BOOST_PP_LOCAL_MACRO(n) \
  175. template< class T, class U, BOOST_ASSIGN_PARAMS1(n) > \
  176. inline assign_detail::generic_ptr_list<T> \
  177. ptr_list_of(U const& u, BOOST_ASSIGN_PARAMS2(n) ) \
  178. { \
  179. return assign_detail::generic_ptr_list<T>()(u, BOOST_ASSIGN_PARAMS3(n)); \
  180. } \
  181. /**/
  182. #include BOOST_PP_LOCAL_ITERATE()
  183. #else
  184. template< class T, class... Us >
  185. inline assign_detail::generic_ptr_list<T>
  186. ptr_list_of(Us&&... us)
  187. {
  188. assign_detail::generic_ptr_list<T> gpl;
  189. gpl(boost::forward<Us>(us)...);
  190. return gpl;
  191. }
  192. #endif
  193. } // namespace 'assign'
  194. } // namespace 'boost'
  195. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  196. #undef BOOST_ASSIGN_PARAMS1
  197. #undef BOOST_ASSIGN_PARAMS2
  198. #undef BOOST_ASSIGN_PARAMS3
  199. #undef BOOST_ASSIGN_MAX_PARAMETERS
  200. #endif
  201. #endif