ptr_list_of.hpp 6.0 KB

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