fix doc example typo
[boost.git] / boost / assign / ptr_list_of.hpp
blob3055ce791b5cc7f20e56f6603e6af27d8602633a
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 //
12 #ifndef BOOST_ASSIGN_PTR_LIST_OF_HPP
13 #define BOOST_ASSIGN_PTR_LIST_OF_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
16 # pragma once
17 #endif
19 #include <boost/assign/list_of.hpp>
20 #include <boost/type_traits/remove_const.hpp>
21 #include <boost/type_traits/remove_reference.hpp>
22 #include <boost/type_traits/is_reference.hpp>
23 #include <boost/static_assert.hpp>
24 #include <boost/type_traits/detail/yes_no_type.hpp>
25 #include <boost/type_traits/decay.hpp>
26 #include <boost/type_traits/is_array.hpp>
27 #include <boost/mpl/if.hpp>
28 #include <boost/ptr_container/ptr_vector.hpp>
30 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
31 #include <boost/preprocessor/repetition/enum_params.hpp>
32 #include <boost/preprocessor/iteration/local.hpp>
34 namespace boost
37 namespace assign_detail
39 /////////////////////////////////////////////////////////////////////////
40 // Part 1: flexible and efficient interface
41 /////////////////////////////////////////////////////////////////////////
43 template< class T >
44 class generic_ptr_list :
45 public converter< generic_ptr_list<T>,
46 BOOST_DEDUCED_TYPENAME boost::ptr_vector<T>::iterator >
48 protected:
49 typedef boost::ptr_vector<T> impl_type;
50 typedef std::auto_ptr<impl_type> release_type;
51 mutable impl_type values_;
53 public:
54 typedef BOOST_DEDUCED_TYPENAME impl_type::iterator iterator;
55 typedef iterator const_iterator;
56 typedef BOOST_DEDUCED_TYPENAME impl_type::value_type value_type;
57 typedef BOOST_DEDUCED_TYPENAME impl_type::size_type size_type;
58 typedef BOOST_DEDUCED_TYPENAME impl_type::difference_type difference_type;
59 public:
60 generic_ptr_list() : values_( 32u )
61 { }
63 generic_ptr_list( release_type r ) : values_(r)
64 { }
66 release_type release()
68 return values_.release();
71 public:
72 iterator begin() const { return values_.begin(); }
73 iterator end() const { return values_.end(); }
74 bool empty() const { return values_.empty(); }
75 size_type size() const { return values_.size(); }
77 public:
79 operator impl_type() const
81 return values_;
84 template< template<class,class,class> class Seq, class U,
85 class CA, class A >
86 operator Seq<U,CA,A>() const
88 Seq<U,CA,A> result;
89 result.transfer( result.end(), values_ );
90 BOOST_ASSERT( empty() );
91 return result;
94 template< class PtrContainer >
95 std::auto_ptr<PtrContainer> convert( const PtrContainer* c ) const
97 std::auto_ptr<PtrContainer> res( new PtrContainer() );
98 while( !empty() )
99 res->insert( res->end(),
100 values_.pop_back().release() );
101 return res;
104 template< class PtrContainer >
105 std::auto_ptr<PtrContainer> to_container( const PtrContainer& c ) const
107 return convert( &c );
110 protected:
111 void push_back( T* r ) { values_.push_back( r ); }
113 public:
114 generic_ptr_list& operator()()
116 this->push_back( new T() );
117 return *this;
120 template< class U >
121 generic_ptr_list& operator()( const U& u )
123 this->push_back( new T(u) );
124 return *this;
128 #ifndef BOOST_ASSIGN_MAX_PARAMS // use user's value
129 #define BOOST_ASSIGN_MAX_PARAMS 5
130 #endif
131 #define BOOST_ASSIGN_MAX_PARAMETERS (BOOST_ASSIGN_MAX_PARAMS - 1)
132 #define BOOST_ASSIGN_PARAMS1(n) BOOST_PP_ENUM_PARAMS(n, class U)
133 #define BOOST_ASSIGN_PARAMS2(n) BOOST_PP_ENUM_BINARY_PARAMS(n, U, const& u)
134 #define BOOST_ASSIGN_PARAMS3(n) BOOST_PP_ENUM_PARAMS(n, u)
136 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
137 #define BOOST_PP_LOCAL_MACRO(n) \
138 template< class U, BOOST_ASSIGN_PARAMS1(n) > \
139 generic_ptr_list& operator()(U const& u, BOOST_ASSIGN_PARAMS2(n) ) \
141 this->push_back( new T(u, BOOST_ASSIGN_PARAMS3(n))); \
142 return *this; \
144 /**/
146 #include BOOST_PP_LOCAL_ITERATE()
148 }; // class 'generic_ptr_list'
150 } // namespace 'assign_detail'
152 namespace assign
154 template< class T >
155 inline assign_detail::generic_ptr_list<T>
156 ptr_list_of()
158 return assign_detail::generic_ptr_list<T>()();
161 template< class T, class U >
162 inline assign_detail::generic_ptr_list<T>
163 ptr_list_of( const U& t )
165 return assign_detail::generic_ptr_list<T>()( t );
169 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
170 #define BOOST_PP_LOCAL_MACRO(n) \
171 template< class T, class U, BOOST_ASSIGN_PARAMS1(n) > \
172 inline assign_detail::generic_ptr_list<T> \
173 ptr_list_of(U const& u, BOOST_ASSIGN_PARAMS2(n) ) \
175 return assign_detail::generic_ptr_list<T>()(u, BOOST_ASSIGN_PARAMS3(n)); \
177 /**/
179 #include BOOST_PP_LOCAL_ITERATE()
182 } // namespace 'assign'
183 } // namespace 'boost'
186 #undef BOOST_ASSIGN_PARAMS1
187 #undef BOOST_ASSIGN_PARAMS2
188 #undef BOOST_ASSIGN_PARAMS3
189 #undef BOOST_ASSIGN_MAX_PARAMETERS
191 #endif