ptr_list_of.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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) && (_MSC_VER >= 1020)
  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. typedef std::auto_ptr<impl_type> release_type;
  43. mutable impl_type values_;
  44. public:
  45. typedef BOOST_DEDUCED_TYPENAME impl_type::iterator iterator;
  46. typedef iterator const_iterator;
  47. typedef BOOST_DEDUCED_TYPENAME impl_type::value_type value_type;
  48. typedef BOOST_DEDUCED_TYPENAME impl_type::size_type size_type;
  49. typedef BOOST_DEDUCED_TYPENAME impl_type::difference_type difference_type;
  50. public:
  51. generic_ptr_list() : values_( 32u )
  52. { }
  53. generic_ptr_list( release_type r ) : values_(r)
  54. { }
  55. release_type release()
  56. {
  57. return values_.release();
  58. }
  59. public:
  60. iterator begin() const { return values_.begin(); }
  61. iterator end() const { return values_.end(); }
  62. bool empty() const { return values_.empty(); }
  63. size_type size() const { return values_.size(); }
  64. public:
  65. operator impl_type() const
  66. {
  67. return values_;
  68. }
  69. template< template<class,class,class> class Seq, class U,
  70. class CA, class A >
  71. operator Seq<U,CA,A>() const
  72. {
  73. Seq<U,CA,A> result;
  74. result.transfer( result.end(), values_ );
  75. BOOST_ASSERT( empty() );
  76. return result;
  77. }
  78. template< class PtrContainer >
  79. std::auto_ptr<PtrContainer> convert( const PtrContainer* c ) const
  80. {
  81. std::auto_ptr<PtrContainer> res( new PtrContainer() );
  82. while( !empty() )
  83. res->insert( res->end(),
  84. values_.pop_back().release() );
  85. return res;
  86. }
  87. template< class PtrContainer >
  88. std::auto_ptr<PtrContainer> to_container( const PtrContainer& c ) const
  89. {
  90. return convert( &c );
  91. }
  92. protected:
  93. void push_back( T* r ) { values_.push_back( r ); }
  94. public:
  95. generic_ptr_list& operator()()
  96. {
  97. this->push_back( new T() );
  98. return *this;
  99. }
  100. template< class U >
  101. generic_ptr_list& operator()( const U& u )
  102. {
  103. this->push_back( new T(u) );
  104. return *this;
  105. }
  106. #ifndef BOOST_ASSIGN_MAX_PARAMS // use user's value
  107. #define BOOST_ASSIGN_MAX_PARAMS 5
  108. #endif
  109. #define BOOST_ASSIGN_MAX_PARAMETERS (BOOST_ASSIGN_MAX_PARAMS - 1)
  110. #define BOOST_ASSIGN_PARAMS1(n) BOOST_PP_ENUM_PARAMS(n, class U)
  111. #define BOOST_ASSIGN_PARAMS2(n) BOOST_PP_ENUM_BINARY_PARAMS(n, U, const& u)
  112. #define BOOST_ASSIGN_PARAMS3(n) BOOST_PP_ENUM_PARAMS(n, u)
  113. #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
  114. #define BOOST_PP_LOCAL_MACRO(n) \
  115. template< class U, BOOST_ASSIGN_PARAMS1(n) > \
  116. generic_ptr_list& operator()(U const& u, BOOST_ASSIGN_PARAMS2(n) ) \
  117. { \
  118. this->push_back( new T(u, BOOST_ASSIGN_PARAMS3(n))); \
  119. return *this; \
  120. } \
  121. /**/
  122. #include BOOST_PP_LOCAL_ITERATE()
  123. }; // class 'generic_ptr_list'
  124. } // namespace 'assign_detail'
  125. namespace assign
  126. {
  127. template< class T >
  128. inline assign_detail::generic_ptr_list<T>
  129. ptr_list_of()
  130. {
  131. return assign_detail::generic_ptr_list<T>()();
  132. }
  133. template< class T, class U >
  134. inline assign_detail::generic_ptr_list<T>
  135. ptr_list_of( const U& t )
  136. {
  137. return assign_detail::generic_ptr_list<T>()( t );
  138. }
  139. #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
  140. #define BOOST_PP_LOCAL_MACRO(n) \
  141. template< class T, class U, BOOST_ASSIGN_PARAMS1(n) > \
  142. inline assign_detail::generic_ptr_list<T> \
  143. ptr_list_of(U const& u, BOOST_ASSIGN_PARAMS2(n) ) \
  144. { \
  145. return assign_detail::generic_ptr_list<T>()(u, BOOST_ASSIGN_PARAMS3(n)); \
  146. } \
  147. /**/
  148. #include BOOST_PP_LOCAL_ITERATE()
  149. } // namespace 'assign'
  150. } // namespace 'boost'
  151. #undef BOOST_ASSIGN_PARAMS1
  152. #undef BOOST_ASSIGN_PARAMS2
  153. #undef BOOST_ASSIGN_PARAMS3
  154. #undef BOOST_ASSIGN_MAX_PARAMETERS
  155. #endif