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
14 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
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 //BOOST_STATIC_ASSERT( function_traits<Nullary_function>::arity == 0 );
207 //BOOST_STATIC_ASSERT( is_convertible< BOOST_DEDUCED_TYPENAME function_traits<
208 // Nullary_function>::result_type >,T>::value );
210 return operator,( r );
214 list_inserter& operator,( const T& r )
220 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205))
222 list_inserter& operator,( const assign_detail::repeater<T> & r )
224 return repeat( r.sz, r.val );
228 list_inserter& operator,( assign_detail::repeater<T> r )
230 return repeat( r.sz, r.val );
234 template< class Nullary_function >
235 list_inserter& operator,( const assign_detail::fun_repeater<Nullary_function>& r )
237 return repeat_fun( r.sz, r.val );
241 list_inserter& repeat( std::size_t sz, T r )
249 template< class Nullary_function >
250 list_inserter& repeat_fun( std::size_t sz, Nullary_function fun )
258 template< class SinglePassIterator >
259 list_inserter& range( SinglePassIterator first,
260 SinglePassIterator last )
262 for( ; first != last; ++first )
267 template< class SinglePassRange >
268 list_inserter& range( const SinglePassRange& r )
270 return range( boost::begin(r), boost::end(r) );
274 list_inserter& operator()( const T& t )
280 #ifndef BOOST_ASSIGN_MAX_PARAMS // use user's value
281 #define BOOST_ASSIGN_MAX_PARAMS 5
283 #define BOOST_ASSIGN_MAX_PARAMETERS (BOOST_ASSIGN_MAX_PARAMS - 1)
284 #define BOOST_ASSIGN_PARAMS1(n) BOOST_PP_ENUM_PARAMS(n, class T)
285 #define BOOST_ASSIGN_PARAMS2(n) BOOST_PP_ENUM_BINARY_PARAMS(n, T, const& t)
286 #define BOOST_ASSIGN_PARAMS3(n) BOOST_PP_ENUM_PARAMS(n, t)
288 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
289 #define BOOST_PP_LOCAL_MACRO(n) \
290 template< class T, BOOST_ASSIGN_PARAMS1(n) > \
291 list_inserter& operator()(T t, BOOST_ASSIGN_PARAMS2(n) ) \
293 BOOST_PP_CAT(insert, BOOST_PP_INC(n))(t, BOOST_ASSIGN_PARAMS3(n), arg_type()); \
298 #include BOOST_PP_LOCAL_ITERATE()
301 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
302 #define BOOST_PP_LOCAL_MACRO(n) \
303 template< class T, BOOST_ASSIGN_PARAMS1(n) > \
304 void BOOST_PP_CAT(insert, BOOST_PP_INC(n))(T const& t, BOOST_ASSIGN_PARAMS2(n), single_arg_type) \
306 insert_( Argument(t, BOOST_ASSIGN_PARAMS3(n) )); \
310 #include BOOST_PP_LOCAL_ITERATE()
312 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
313 #define BOOST_PP_LOCAL_MACRO(n) \
314 template< class T, BOOST_ASSIGN_PARAMS1(n) > \
315 void BOOST_PP_CAT(insert, BOOST_PP_INC(n))(T const& t, BOOST_ASSIGN_PARAMS2(n), n_arg_type) \
317 insert_(t, BOOST_ASSIGN_PARAMS3(n) ); \
321 #include BOOST_PP_LOCAL_ITERATE()
324 Function fun_private() const
331 list_inserter& operator=( const list_inserter& );
335 template< class Function >
336 inline list_inserter< Function >
337 make_list_inserter( Function fun )
339 return list_inserter< Function >( fun );
342 template< class Function, class Argument >
343 inline list_inserter<Function,Argument>
344 make_list_inserter( Function fun, Argument* )
346 return list_inserter<Function,Argument>( fun );
350 inline list_inserter< assign_detail::call_push_back<C>,
351 BOOST_DEDUCED_TYPENAME C::value_type >
354 static BOOST_DEDUCED_TYPENAME C::value_type* p = 0;
355 return make_list_inserter( assign_detail::call_push_back<C>( c ),
360 inline list_inserter< assign_detail::call_push_front<C>,
361 BOOST_DEDUCED_TYPENAME C::value_type >
364 static BOOST_DEDUCED_TYPENAME C::value_type* p = 0;
365 return make_list_inserter( assign_detail::call_push_front<C>( c ),
370 inline list_inserter< assign_detail::call_insert<C>,
371 BOOST_DEDUCED_TYPENAME C::value_type >
374 static BOOST_DEDUCED_TYPENAME C::value_type* p = 0;
375 return make_list_inserter( assign_detail::call_insert<C>( c ),
380 inline list_inserter< assign_detail::call_push<C>,
381 BOOST_DEDUCED_TYPENAME C::value_type >
384 static BOOST_DEDUCED_TYPENAME C::value_type* p = 0;
385 return make_list_inserter( assign_detail::call_push<C>( c ),
390 inline list_inserter< assign_detail::call_add_edge<C> >
393 return make_list_inserter( assign_detail::call_add_edge<C>( c ) );
396 } // namespace 'assign'
397 } // namespace 'boost'
399 #undef BOOST_ASSIGN_PARAMS1
400 #undef BOOST_ASSIGN_PARAMS2
401 #undef BOOST_ASSIGN_PARAMS3
402 #undef BOOST_ASSIGN_MAX_PARAMETERS