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/
12 #ifndef BOOST_ASSIGN_LIST_OF_HPP
13 #define BOOST_ASSIGN_LIST_OF_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
19 #include <boost/assign/assignment_exception.hpp>
20 #include <boost/range/iterator_range.hpp>
21 #include <boost/config.hpp>
22 #include <boost/tuple/tuple.hpp>
23 #include <boost/type_traits/remove_const.hpp>
24 #include <boost/type_traits/remove_reference.hpp>
25 #include <boost/type_traits/is_reference.hpp>
26 #include <boost/static_assert.hpp>
27 #include <boost/type_traits/detail/yes_no_type.hpp>
28 #include <boost/type_traits/decay.hpp>
29 #include <boost/type_traits/is_array.hpp>
30 #include <boost/mpl/if.hpp>
35 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
36 #include <boost/preprocessor/repetition/enum_params.hpp>
37 #include <boost/preprocessor/iteration/local.hpp>
39 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
40 // BCB requires full type definition for is_array<> to work correctly.
41 #include <boost/array.hpp>
47 // this here is necessary to avoid compiler error in <boost/array.hpp>
48 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
49 template< class T
, std::size_t sz
>
53 namespace assign_detail
55 /////////////////////////////////////////////////////////////////////////
56 // Part 0: common conversion code
57 /////////////////////////////////////////////////////////////////////////
63 // Add constness to array parameters
64 // to support string literals properly
66 typedef BOOST_DEDUCED_TYPENAME
mpl::eval_if
<
68 ::boost::decay
<const T
>,
69 ::boost::decay
<T
> >::type type
;
72 template< class T
, std::size_t sz
>
73 type_traits::yes_type
assign_is_array( const array
<T
,sz
>* );
74 type_traits::no_type
assign_is_array( ... );
75 template< class T
, class U
>
76 type_traits::yes_type
assign_is_pair( const std::pair
<T
,U
>* );
77 type_traits::no_type
assign_is_pair( ... );
83 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
85 char dummy_
; // BCB would by default use 8 bytes
88 struct adapter_type_tag
90 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
92 char dummy_
; // BCB would by default use 8 bytes
97 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
99 char dummy_
; // BCB would by default use 8 bytes
102 struct default_type_tag
104 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
106 char dummy_
; // BCB would by default use 8 bytes
112 template< class DerivedTAssign
, class Iterator
>
115 public: // Range operations
116 typedef Iterator iterator
;
117 typedef Iterator const_iterator
;
119 iterator
begin() const
121 return static_cast<const DerivedTAssign
*>(this)->begin();
126 return static_cast<const DerivedTAssign
*>(this)->end();
131 template< class Container
>
132 Container
convert_to_container() const
134 static Container
* c
= 0;
135 BOOST_STATIC_CONSTANT( bool, is_array_flag
= sizeof( assign_detail::assign_is_array( c
) )
136 == sizeof( type_traits::yes_type
) );
138 typedef BOOST_DEDUCED_TYPENAME
mpl::if_c
< is_array_flag
,
140 default_type_tag
>::type tag_type
;
142 return convert
<Container
>( c
, tag_type() );
147 template< class Container
>
148 Container
convert( const Container
*, default_type_tag
) const
151 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
152 // old Dinkumware doesn't support iterator type as template
154 iterator it
= begin(),
158 result
.insert( result
.end(), *it
);
163 return Container( begin(), end() );
167 template< class Array
>
168 Array
convert( const Array
*, array_type_tag
) const
170 typedef BOOST_DEDUCED_TYPENAME
Array::value_type value_type
;
172 #if BOOST_WORKAROUND(BOOST_INTEL, <= 910 ) || BOOST_WORKAROUND(__SUNPRO_CC, <= 0x580 )
173 BOOST_DEDUCED_TYPENAME remove_const
<Array
>::type ar
;
177 const std::size_t sz
= ar
.size();
178 if( sz
< static_cast<const DerivedTAssign
*>(this)->size() )
179 throw assign::assignment_exception( "array initialized with too many elements" );
181 iterator i
= begin(),
183 for( ; i
!= e
; ++i
, ++n
)
186 ar
[n
] = value_type();
190 template< class Adapter
>
191 Adapter
convert_to_adapter( const Adapter
* = 0 ) const
194 iterator i
= begin(),
202 struct adapter_converter
;
203 friend struct adapter_converter
;
205 struct adapter_converter
208 adapter_converter( const converter
& this_
) : gl( this_
)
211 adapter_converter( const adapter_converter
& r
)
215 template< class Adapter
>
216 operator Adapter() const
218 return gl
.convert_to_adapter
<Adapter
>();
223 template< class Container
>
224 Container
to_container( Container
& c
) const
226 return convert( &c
, default_type_tag() );
229 adapter_converter
to_adapter() const
231 return adapter_converter( *this );
234 template< class Adapter
>
235 Adapter
to_adapter( Adapter
& a
) const
237 return this->convert_to_adapter( &a
);
240 template< class Array
>
241 Array
to_array( Array
& a
) const
243 return convert( &a
, array_type_tag() );
247 template< class T
, class I
, class Range
>
248 inline bool operator==( const converter
<T
,I
>& l
, const Range
& r
)
250 return ::boost::iterator_range_detail::equal( l
, r
);
253 template< class T
, class I
, class Range
>
254 inline bool operator==( const Range
& l
, const converter
<T
,I
>& r
)
259 template< class T
, class I
, class Range
>
260 inline bool operator!=( const converter
<T
,I
>& l
, const Range
& r
)
265 template< class T
, class I
, class Range
>
266 inline bool operator!=( const Range
& l
, const converter
<T
,I
>& r
)
271 template< class T
, class I
, class Range
>
272 inline bool operator<( const converter
<T
,I
>& l
, const Range
& r
)
274 return ::boost::iterator_range_detail::less_than( l
, r
);
277 template< class T
, class I
, class Range
>
278 inline bool operator<( const Range
& l
, const converter
<T
,I
>& r
)
280 return ::boost::iterator_range_detail::less_than( l
, r
);
283 template< class T
, class I
, class Range
>
284 inline bool operator>( const converter
<T
,I
>& l
, const Range
& r
)
289 template< class T
, class I
, class Range
>
290 inline bool operator>( const Range
& l
, const converter
<T
,I
>& r
)
295 template< class T
, class I
, class Range
>
296 inline bool operator<=( const converter
<T
,I
>& l
, const Range
& r
)
301 template< class T
, class I
, class Range
>
302 inline bool operator<=( const Range
& l
, const converter
<T
,I
>& r
)
307 template< class T
, class I
, class Range
>
308 inline bool operator>=( const converter
<T
,I
>& l
, const Range
& r
)
313 template< class T
, class I
, class Range
>
314 inline bool operator>=( const Range
& l
, const converter
<T
,I
>& r
)
319 template< class T
, class I
, class Elem
, class Traits
>
320 inline std::basic_ostream
<Elem
,Traits
>&
321 operator<<( std::basic_ostream
<Elem
, Traits
>& Os
,
322 const converter
<T
,I
>& r
)
324 return Os
<< ::boost::make_iterator_range( r
.begin(), r
.end() );
327 /////////////////////////////////////////////////////////////////////////
328 // Part 1: flexible, but inefficient interface
329 /////////////////////////////////////////////////////////////////////////
333 public converter
< generic_list
< BOOST_DEDUCED_TYPENAME assign_decay
<T
>::type
>,
334 BOOST_DEDUCED_TYPENAME
std::deque
<BOOST_DEDUCED_TYPENAME
335 assign_decay
<T
>::type
>::iterator
>
337 typedef BOOST_DEDUCED_TYPENAME assign_decay
<T
>::type Ty
;
338 typedef std::deque
<Ty
> impl_type
;
339 mutable impl_type values_
;
342 typedef BOOST_DEDUCED_TYPENAME
impl_type::iterator iterator
;
343 typedef iterator const_iterator
;
344 typedef BOOST_DEDUCED_TYPENAME
impl_type::value_type value_type
;
345 typedef BOOST_DEDUCED_TYPENAME
impl_type::size_type size_type
;
346 typedef BOOST_DEDUCED_TYPENAME
impl_type::difference_type difference_type
;
349 iterator
begin() const { return values_
.begin(); }
350 iterator
end() const { return values_
.end(); }
351 bool empty() const { return values_
.empty(); }
352 size_type
size() const { return values_
.size(); }
355 void push_back( value_type r
) { values_
.push_back( r
); }
358 generic_list
& operator,( const Ty
& u
)
360 this->push_back( u
);
364 generic_list
& operator()()
366 this->push_back( Ty() );
370 generic_list
& operator()( const Ty
& u
)
372 this->push_back( u
);
377 #ifndef BOOST_ASSIGN_MAX_PARAMS // use user's value
378 #define BOOST_ASSIGN_MAX_PARAMS 5
380 #define BOOST_ASSIGN_MAX_PARAMETERS (BOOST_ASSIGN_MAX_PARAMS - 1)
381 #define BOOST_ASSIGN_PARAMS1(n) BOOST_PP_ENUM_PARAMS(n, class U)
382 #define BOOST_ASSIGN_PARAMS2(n) BOOST_PP_ENUM_BINARY_PARAMS(n, U, const& u)
383 #define BOOST_ASSIGN_PARAMS3(n) BOOST_PP_ENUM_PARAMS(n, u)
384 #define BOOST_ASSIGN_PARAMS4(n) BOOST_PP_ENUM_PARAMS(n, U)
385 #define BOOST_ASSIGN_PARAMS2_NO_REF(n) BOOST_PP_ENUM_BINARY_PARAMS(n, U, u)
387 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
388 #define BOOST_PP_LOCAL_MACRO(n) \
389 template< class U, BOOST_ASSIGN_PARAMS1(n) > \
390 generic_list& operator()(U const& u, BOOST_ASSIGN_PARAMS2(n) ) \
392 this->push_back( Ty(u, BOOST_ASSIGN_PARAMS3(n))); \
397 #include BOOST_PP_LOCAL_ITERATE()
401 generic_list
& repeat( std::size_t sz
, U u
)
405 this->push_back( u
);
409 template< class Nullary_function
>
410 generic_list
& repeat_fun( std::size_t sz
, Nullary_function fun
)
414 this->push_back( fun() );
418 template< class SinglePassIterator
>
419 generic_list
& range( SinglePassIterator first
,
420 SinglePassIterator last
)
422 for( ; first
!= last
; ++first
)
423 this->push_back( *first
);
427 template< class SinglePassRange
>
428 generic_list
& range( const SinglePassRange
& r
)
430 return range( boost::begin(r
), boost::end(r
) );
433 template< class Container
>
434 operator Container() const
436 return this-> BOOST_NESTED_TEMPLATE convert_to_container
<Container
>();
440 /////////////////////////////////////////////////////////////////////////
441 // Part 2: efficient, but inconvenient interface
442 /////////////////////////////////////////////////////////////////////////
445 struct assign_reference
448 { /* intentionally empty */ }
450 assign_reference( T
& r
) : ref_(&r
)
453 void operator=( T
& r
)
463 void swap( assign_reference
& r
)
465 std::swap( *ref_
, *r
.ref_
);
479 inline bool operator<( const assign_reference
<T
>& l
,
480 const assign_reference
<T
>& r
)
482 return l
.get_ref() < r
.get_ref();
486 inline bool operator>( const assign_reference
<T
>& l
,
487 const assign_reference
<T
>& r
)
489 return l
.get_ref() > r
.get_ref();
493 inline void swap( assign_reference
<T
>& l
,
494 assign_reference
<T
>& r
)
501 template< class T
, int N
>
502 struct static_generic_list
:
503 public converter
< static_generic_list
<T
,N
>, assign_reference
<T
>* >
506 typedef T internal_value_type
;
509 typedef assign_reference
<internal_value_type
> value_type
;
510 typedef value_type
* iterator
;
511 typedef value_type
* const_iterator
;
512 typedef std::size_t size_type
;
513 typedef std::ptrdiff_t difference_type
;
516 static_generic_list( T
& r
) :
522 static_generic_list
& operator()( T
& r
)
528 iterator
begin() const
535 return &refs_
[current_
];
538 size_type
size() const
540 return static_cast<size_type
>( current_
);
548 template< class ForwardIterator
>
549 static_generic_list
& range( ForwardIterator first
,
550 ForwardIterator last
)
552 for( ; first
!= last
; ++first
)
553 this->insert( *first
);
557 template< class ForwardRange
>
558 static_generic_list
& range( ForwardRange
& r
)
560 return range( boost::begin(r
), boost::end(r
) );
563 template< class ForwardRange
>
564 static_generic_list
& range( const ForwardRange
& r
)
566 return range( boost::begin(r
), boost::end(r
) );
569 template< class Container
>
570 operator Container() const
572 return this-> BOOST_NESTED_TEMPLATE convert_to_container
<Container
>();
582 static_generic_list();
584 mutable assign_reference
<internal_value_type
> refs_
[N
];
588 } // namespace 'assign_detail'
593 inline assign_detail::generic_list
<T
>
596 return assign_detail::generic_list
<T
>()( T() );
600 inline assign_detail::generic_list
<T
>
601 list_of( const T
& t
)
603 return assign_detail::generic_list
<T
>()( t
);
606 template< int N
, class T
>
607 inline assign_detail::static_generic_list
< BOOST_DEDUCED_TYPENAME
assign_detail::assign_decay
<T
>::type
,N
>
610 return assign_detail::static_generic_list
<BOOST_DEDUCED_TYPENAME
assign_detail::assign_decay
<T
>::type
,N
>( t
);
613 template< int N
, class T
>
614 inline assign_detail::static_generic_list
<const BOOST_DEDUCED_TYPENAME
assign_detail::assign_decay
<T
>::type
,N
>
615 cref_list_of( const T
& t
)
617 return assign_detail::static_generic_list
<const BOOST_DEDUCED_TYPENAME
assign_detail::assign_decay
<T
>::type
,N
>( t
);
620 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
621 #define BOOST_PP_LOCAL_MACRO(n) \
622 template< class T, class U, BOOST_ASSIGN_PARAMS1(n) > \
623 inline assign_detail::generic_list<T> \
624 list_of(U const& u, BOOST_ASSIGN_PARAMS2(n) ) \
626 return assign_detail::generic_list<T>()(u, BOOST_ASSIGN_PARAMS3(n)); \
630 #include BOOST_PP_LOCAL_ITERATE()
632 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
633 #define BOOST_PP_LOCAL_MACRO(n) \
634 template< class U, BOOST_ASSIGN_PARAMS1(n) > \
635 inline assign_detail::generic_list< tuple<U, BOOST_ASSIGN_PARAMS4(n)> > \
636 tuple_list_of(U u, BOOST_ASSIGN_PARAMS2_NO_REF(n) ) \
638 return assign_detail::generic_list< tuple<U, BOOST_ASSIGN_PARAMS4(n)> >()( tuple<U,BOOST_ASSIGN_PARAMS4(n)>( u, BOOST_ASSIGN_PARAMS3(n) )); \
642 #include BOOST_PP_LOCAL_ITERATE()
645 template< class Key
, class T
>
646 inline assign_detail::generic_list
< std::pair
648 BOOST_DEDUCED_TYPENAME
assign_detail::assign_decay
<Key
>::type
,
649 BOOST_DEDUCED_TYPENAME
assign_detail::assign_decay
<T
>::type
651 map_list_of( const Key
& k
, const T
& t
)
653 typedef BOOST_DEDUCED_TYPENAME
assign_detail::assign_decay
<Key
>::type k_type
;
654 typedef BOOST_DEDUCED_TYPENAME
assign_detail::assign_decay
<T
>::type t_type
;
655 return assign_detail::generic_list
< std::pair
<k_type
,t_type
> >()( k
, t
);
658 template< class F
, class S
>
659 inline assign_detail::generic_list
< std::pair
661 BOOST_DEDUCED_TYPENAME
assign_detail::assign_decay
<F
>::type
,
662 BOOST_DEDUCED_TYPENAME
assign_detail::assign_decay
<S
>::type
664 pair_list_of( const F
& f
, const S
& s
)
666 return map_list_of( f
, s
);
670 } // namespace 'assign'
671 } // namespace 'boost'
674 #undef BOOST_ASSIGN_PARAMS1
675 #undef BOOST_ASSIGN_PARAMS2
676 #undef BOOST_ASSIGN_PARAMS3
677 #undef BOOST_ASSIGN_PARAMS4
678 #undef BOOST_ASSIGN_PARAMS2_NO_REF
679 #undef BOOST_ASSIGN_MAX_PARAMETERS