1 // Boost.Assign library
3 // Copyright Thorsten Ottosen 2003-2004. 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)
8 // For more information, see http://www.boost.org/libs/assign/
11 #ifndef BOOST_ASSIGN_LIST_INSERTER_HPP
12 #define BOOST_ASSIGN_LIST_INSERTER_HPP
18 #include <boost/detail/workaround.hpp>
20 #include <boost/mpl/if.hpp>
21 #include <boost/type_traits/is_same.hpp>
22 #include <boost/range/begin.hpp>
23 #include <boost/range/end.hpp>
24 #include <boost/config.hpp>
27 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
28 #include <boost/preprocessor/repetition/enum_params.hpp>
29 #include <boost/preprocessor/cat.hpp>
30 #include <boost/preprocessor/iteration/local.hpp>
31 #include <boost/preprocessor/arithmetic/inc.hpp>
35 namespace assign_detail
43 repeater( std::size_t sz_, T r ) : sz( sz_ ), val( r )
53 fun_repeater( std::size_t sz_, Fun r ) : sz( sz_ ), val( r )
62 call_push_back( C& c ) : c_( c )
66 void operator()( T r )
77 call_push_front( C& c ) : c_( c )
81 void operator()( T r )
92 call_push( C& c ) : c_( c )
96 void operator()( T r )
107 call_insert( C& c ) : c_( c )
111 void operator()( T r )
122 call_add_edge( C& c ) : c_(c)
126 void operator()( T l, T r )
128 add_edge( l, r, c_ );
131 template< class T, class EP >
132 void operator()( T l, T r, const EP& ep )
134 add_edge( l, r, ep, c_ );
139 struct forward_n_arguments {};
141 } // namespace 'assign_detail'
147 inline assign_detail::repeater<T>
148 repeat( std::size_t sz, T r )
150 return assign_detail::repeater<T>( sz, r );
153 template< class Function >
154 inline assign_detail::fun_repeater<Function>
155 repeat_fun( std::size_t sz, Function r )
157 return assign_detail::fun_repeater<Function>( sz, r );
161 template< class Function, class Argument = assign_detail::forward_n_arguments >
164 struct single_arg_type {};
165 struct n_arg_type {};
167 typedef BOOST_DEDUCED_TYPENAME mpl::if_c< is_same<Argument,assign_detail::forward_n_arguments>::value,
169 single_arg_type >::type arg_type;
173 list_inserter( Function fun ) : insert_( fun )
176 template< class Function2, class Arg >
177 list_inserter( const list_inserter<Function2,Arg>& r )
178 : insert_( r.fun_private() )
181 list_inserter( const list_inserter& r ) : insert_( r.insert_ )
184 list_inserter& operator()()
186 insert_( Argument() );
191 list_inserter& operator=( const T& r )
198 list_inserter& operator=( assign_detail::repeater<T> r )
200 return operator,( r );
203 template< class Nullary_function >
204 list_inserter& operator=( const assign_detail::fun_repeater<Nullary_function>& r )
206 return operator,( r );
210 list_inserter& operator,( const T& r )
216 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205))
218 list_inserter& operator,( const assign_detail::repeater<T> & r )
220 return repeat( r.sz, r.val );
224 list_inserter& operator,( assign_detail::repeater<T> r )
226 return repeat( r.sz, r.val );
230 template< class Nullary_function >
231 list_inserter& operator,( const assign_detail::fun_repeater<Nullary_function>& r )
233 return repeat_fun( r.sz, r.val );
237 list_inserter& repeat( std::size_t sz, T r )
245 template< class Nullary_function >
246 list_inserter& repeat_fun( std::size_t sz, Nullary_function fun )
254 template< class SinglePassIterator >
255 list_inserter& range( SinglePassIterator first,
256 SinglePassIterator last )
258 for( ; first != last; ++first )
263 template< class SinglePassRange >
264 list_inserter& range( const SinglePassRange& r )
266 return range( boost::begin(r), boost::end(r) );
270 list_inserter& operator()( const T& t )
276 #ifndef BOOST_ASSIGN_MAX_PARAMS // use user's value
277 #define BOOST_ASSIGN_MAX_PARAMS 5
279 #define BOOST_ASSIGN_MAX_PARAMETERS (BOOST_ASSIGN_MAX_PARAMS - 1)
280 #define BOOST_ASSIGN_PARAMS1(n) BOOST_PP_ENUM_PARAMS(n, class T)
281 #define BOOST_ASSIGN_PARAMS2(n) BOOST_PP_ENUM_BINARY_PARAMS(n, T, const& t)
282 #define BOOST_ASSIGN_PARAMS3(n) BOOST_PP_ENUM_PARAMS(n, t)
284 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
285 #define BOOST_PP_LOCAL_MACRO(n) \
286 template< class T, BOOST_ASSIGN_PARAMS1(n) > \
287 list_inserter& operator()(T t, BOOST_ASSIGN_PARAMS2(n) ) \
289 BOOST_PP_CAT(insert, BOOST_PP_INC(n))(t, BOOST_ASSIGN_PARAMS3(n), arg_type()); \
294 #include BOOST_PP_LOCAL_ITERATE()
297 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
298 #define BOOST_PP_LOCAL_MACRO(n) \
299 template< class T, BOOST_ASSIGN_PARAMS1(n) > \
300 void BOOST_PP_CAT(insert, BOOST_PP_INC(n))(T const& t, BOOST_ASSIGN_PARAMS2(n), single_arg_type) \
302 insert_( Argument(t, BOOST_ASSIGN_PARAMS3(n) )); \
306 #include BOOST_PP_LOCAL_ITERATE()
308 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
309 #define BOOST_PP_LOCAL_MACRO(n) \
310 template< class T, BOOST_ASSIGN_PARAMS1(n) > \
311 void BOOST_PP_CAT(insert, BOOST_PP_INC(n))(T const& t, BOOST_ASSIGN_PARAMS2(n), n_arg_type) \
313 insert_(t, BOOST_ASSIGN_PARAMS3(n) ); \
317 #include BOOST_PP_LOCAL_ITERATE()
320 Function fun_private() const
327 list_inserter& operator=( const list_inserter& );
331 template< class Function >
332 inline list_inserter< Function >
333 make_list_inserter( Function fun )
335 return list_inserter< Function >( fun );
338 template< class Function, class Argument >
339 inline list_inserter<Function,Argument>
340 make_list_inserter( Function fun, Argument* )
342 return list_inserter<Function,Argument>( fun );
346 inline list_inserter< assign_detail::call_push_back<C>,
347 BOOST_DEDUCED_TYPENAME C::value_type >
350 static BOOST_DEDUCED_TYPENAME C::value_type* p = 0;
351 return make_list_inserter( assign_detail::call_push_back<C>( c ),
356 inline list_inserter< assign_detail::call_push_front<C>,
357 BOOST_DEDUCED_TYPENAME C::value_type >
360 static BOOST_DEDUCED_TYPENAME C::value_type* p = 0;
361 return make_list_inserter( assign_detail::call_push_front<C>( c ),
366 inline list_inserter< assign_detail::call_insert<C>,
367 BOOST_DEDUCED_TYPENAME C::value_type >
370 static BOOST_DEDUCED_TYPENAME C::value_type* p = 0;
371 return make_list_inserter( assign_detail::call_insert<C>( c ),
376 inline list_inserter< assign_detail::call_push<C>,
377 BOOST_DEDUCED_TYPENAME C::value_type >
380 static BOOST_DEDUCED_TYPENAME C::value_type* p = 0;
381 return make_list_inserter( assign_detail::call_push<C>( c ),
386 inline list_inserter< assign_detail::call_add_edge<C> >
389 return make_list_inserter( assign_detail::call_add_edge<C>( c ) );
392 } // namespace 'assign'
393 } // namespace 'boost'
395 #undef BOOST_ASSIGN_PARAMS1
396 #undef BOOST_ASSIGN_PARAMS2
397 #undef BOOST_ASSIGN_PARAMS3
398 #undef BOOST_ASSIGN_MAX_PARAMETERS