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

source code of boost/libs/assign/include/boost/assign/ptr_list_of.hpp