]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/assign/list_of.hpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / boost / assign / list_of.hpp
1 // Boost.Assign library
2 //
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)
7 //
8 // For more information, see http://www.boost.org/libs/assign/
9 //
10
11
12 #ifndef BOOST_ASSIGN_LIST_OF_HPP
13 #define BOOST_ASSIGN_LIST_OF_HPP
14
15 #if defined(_MSC_VER)
16 # pragma once
17 #endif
18
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/throw_exception.hpp>
28 #include <boost/type_traits/detail/yes_no_type.hpp>
29 #include <boost/type_traits/decay.hpp>
30 #include <boost/type_traits/is_array.hpp>
31 #include <boost/mpl/if.hpp>
32 #include <deque>
33 #include <cstddef>
34 #include <utility>
35
36 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
37 #include <boost/preprocessor/repetition/enum_params.hpp>
38 #include <boost/preprocessor/iteration/local.hpp>
39
40 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
41 // BCB requires full type definition for is_array<> to work correctly.
42 #include <boost/array.hpp>
43 #endif
44
45 namespace boost
46 {
47
48 // this here is necessary to avoid compiler error in <boost/array.hpp>
49 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
50 template< class T, std::size_t sz >
51 class array;
52 #endif
53
54 namespace assign_detail
55 {
56 /////////////////////////////////////////////////////////////////////////
57 // Part 0: common conversion code
58 /////////////////////////////////////////////////////////////////////////
59
60 template< class T >
61 struct assign_decay
62 {
63 //
64 // Add constness to array parameters
65 // to support string literals properly
66 //
67 typedef BOOST_DEDUCED_TYPENAME mpl::eval_if<
68 ::boost::is_array<T>,
69 ::boost::decay<const T>,
70 ::boost::decay<T> >::type type;
71 };
72
73 template< class T, std::size_t sz >
74 type_traits::yes_type assign_is_array( const array<T,sz>* );
75 type_traits::no_type assign_is_array( ... );
76 template< class T, class U >
77 type_traits::yes_type assign_is_pair( const std::pair<T,U>* );
78 type_traits::no_type assign_is_pair( ... );
79
80
81
82 struct array_type_tag
83 {
84 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
85 private:
86 char dummy_; // BCB would by default use 8 bytes
87 #endif
88 };
89 struct adapter_type_tag
90 {
91 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
92 private:
93 char dummy_; // BCB would by default use 8 bytes
94 #endif
95 };
96 struct pair_type_tag
97 {
98 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
99 private:
100 char dummy_; // BCB would by default use 8 bytes
101 #endif
102 };
103 struct default_type_tag
104 {
105 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
106 private:
107 char dummy_; // BCB would by default use 8 bytes
108 #endif
109 };
110
111
112
113 template< class DerivedTAssign, class Iterator >
114 class converter
115 {
116 public: // Range operations
117 typedef Iterator iterator;
118 typedef Iterator const_iterator;
119
120 iterator begin() const
121 {
122 return static_cast<const DerivedTAssign*>(this)->begin();
123 }
124
125 iterator end() const
126 {
127 return static_cast<const DerivedTAssign*>(this)->end();
128 }
129
130 public:
131
132 template< class Container >
133 Container convert_to_container() const
134 {
135 static Container* c = 0;
136 BOOST_STATIC_CONSTANT( bool, is_array_flag = sizeof( assign_detail::assign_is_array( c ) )
137 == sizeof( type_traits::yes_type ) );
138
139 typedef BOOST_DEDUCED_TYPENAME mpl::if_c< is_array_flag,
140 array_type_tag,
141 default_type_tag >::type tag_type;
142
143 return convert<Container>( c, tag_type() );
144 }
145
146 private:
147
148 template< class Container >
149 Container convert( const Container*, default_type_tag ) const
150 {
151
152 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
153 // old Dinkumware doesn't support iterator type as template
154 Container result;
155 iterator it = begin(),
156 e = end();
157 while( it != e )
158 {
159 result.insert( result.end(), *it );
160 ++it;
161 }
162 return result;
163 #else
164 return Container( begin(), end() );
165 #endif
166 }
167
168 template< class Array >
169 Array convert( const Array*, array_type_tag ) const
170 {
171 typedef BOOST_DEDUCED_TYPENAME Array::value_type value_type;
172
173 #if BOOST_WORKAROUND(BOOST_INTEL, <= 910 ) || BOOST_WORKAROUND(__SUNPRO_CC, <= 0x580 )
174 BOOST_DEDUCED_TYPENAME remove_const<Array>::type ar;
175 #else
176 Array ar;
177 #endif
178 const std::size_t sz = ar.size();
179 if( sz < static_cast<const DerivedTAssign*>(this)->size() )
180 BOOST_THROW_EXCEPTION( assign::assignment_exception( "array initialized with too many elements" ) );
181 std::size_t n = 0;
182 iterator i = begin(),
183 e = end();
184 for( ; i != e; ++i, ++n )
185 ar[n] = *i;
186 for( ; n < sz; ++n )
187 ar[n] = value_type();
188 return ar;
189 }
190
191 template< class Adapter >
192 Adapter convert_to_adapter( const Adapter* = 0 ) const
193 {
194 Adapter a;
195 iterator i = begin(),
196 e = end();
197 for( ; i != e; ++i )
198 a.push( *i );
199 return a;
200 }
201
202 private:
203 struct adapter_converter;
204 friend struct adapter_converter;
205
206 struct adapter_converter
207 {
208 const converter& gl;
209 adapter_converter( const converter& this_ ) : gl( this_ )
210 {}
211
212 adapter_converter( const adapter_converter& r )
213 : gl( r.gl )
214 { }
215
216 template< class Adapter >
217 operator Adapter() const
218 {
219 return gl.convert_to_adapter<Adapter>();
220 }
221 };
222
223 public:
224 template< class Container >
225 Container to_container( Container& c ) const
226 {
227 return convert( &c, default_type_tag() );
228 }
229
230 adapter_converter to_adapter() const
231 {
232 return adapter_converter( *this );
233 }
234
235 template< class Adapter >
236 Adapter to_adapter( Adapter& a ) const
237 {
238 return this->convert_to_adapter( &a );
239 }
240
241 template< class Array >
242 Array to_array( Array& a ) const
243 {
244 return convert( &a, array_type_tag() );
245 }
246 };
247
248 template< class T, class I, class Range >
249 inline bool operator==( const converter<T,I>& l, const Range& r )
250 {
251 return ::boost::iterator_range_detail::equal( l, r );
252 }
253
254 template< class T, class I, class Range >
255 inline bool operator==( const Range& l, const converter<T,I>& r )
256 {
257 return r == l;
258 }
259
260 template< class T, class I, class Range >
261 inline bool operator!=( const converter<T,I>& l, const Range& r )
262 {
263 return !( l == r );
264 }
265
266 template< class T, class I, class Range >
267 inline bool operator!=( const Range& l, const converter<T,I>& r )
268 {
269 return !( l == r );
270 }
271
272 template< class T, class I, class Range >
273 inline bool operator<( const converter<T,I>& l, const Range& r )
274 {
275 return ::boost::iterator_range_detail::less_than( l, r );
276 }
277
278 template< class T, class I, class Range >
279 inline bool operator<( const Range& l, const converter<T,I>& r )
280 {
281 return ::boost::iterator_range_detail::less_than( l, r );
282 }
283
284 template< class T, class I, class Range >
285 inline bool operator>( const converter<T,I>& l, const Range& r )
286 {
287 return r < l;
288 }
289
290 template< class T, class I, class Range >
291 inline bool operator>( const Range& l, const converter<T,I>& r )
292 {
293 return r < l;
294 }
295
296 template< class T, class I, class Range >
297 inline bool operator<=( const converter<T,I>& l, const Range& r )
298 {
299 return !( l > r );
300 }
301
302 template< class T, class I, class Range >
303 inline bool operator<=( const Range& l, const converter<T,I>& r )
304 {
305 return !( l > r );
306 }
307
308 template< class T, class I, class Range >
309 inline bool operator>=( const converter<T,I>& l, const Range& r )
310 {
311 return !( l < r );
312 }
313
314 template< class T, class I, class Range >
315 inline bool operator>=( const Range& l, const converter<T,I>& r )
316 {
317 return !( l < r );
318 }
319
320 template< class T, class I, class Elem, class Traits >
321 inline std::basic_ostream<Elem,Traits>&
322 operator<<( std::basic_ostream<Elem, Traits>& Os,
323 const converter<T,I>& r )
324 {
325 return Os << ::boost::make_iterator_range( r.begin(), r.end() );
326 }
327
328 /////////////////////////////////////////////////////////////////////////
329 // Part 1: flexible, but inefficient interface
330 /////////////////////////////////////////////////////////////////////////
331
332 template< class T >
333 class generic_list :
334 public converter< generic_list< BOOST_DEDUCED_TYPENAME assign_decay<T>::type >,
335 BOOST_DEDUCED_TYPENAME std::deque<BOOST_DEDUCED_TYPENAME
336 assign_decay<T>::type>::iterator >
337 {
338 typedef BOOST_DEDUCED_TYPENAME assign_decay<T>::type Ty;
339 typedef std::deque<Ty> impl_type;
340 mutable impl_type values_;
341
342 public:
343 typedef BOOST_DEDUCED_TYPENAME impl_type::iterator iterator;
344 typedef iterator const_iterator;
345 typedef BOOST_DEDUCED_TYPENAME impl_type::value_type value_type;
346 typedef BOOST_DEDUCED_TYPENAME impl_type::size_type size_type;
347 typedef BOOST_DEDUCED_TYPENAME impl_type::difference_type difference_type;
348
349 public:
350 iterator begin() const { return values_.begin(); }
351 iterator end() const { return values_.end(); }
352 bool empty() const { return values_.empty(); }
353 size_type size() const { return values_.size(); }
354
355 private:
356 void push_back( value_type r ) { values_.push_back( r ); }
357
358 public:
359 generic_list& operator,( const Ty& u )
360 {
361 this->push_back( u );
362 return *this;
363 }
364
365 generic_list& operator()()
366 {
367 this->push_back( Ty() );
368 return *this;
369 }
370
371 generic_list& operator()( const Ty& u )
372 {
373 this->push_back( u );
374 return *this;
375 }
376
377
378 #ifndef BOOST_ASSIGN_MAX_PARAMS // use user's value
379 #define BOOST_ASSIGN_MAX_PARAMS 5
380 #endif
381 #define BOOST_ASSIGN_MAX_PARAMETERS (BOOST_ASSIGN_MAX_PARAMS - 1)
382 #define BOOST_ASSIGN_PARAMS1(n) BOOST_PP_ENUM_PARAMS(n, class U)
383 #define BOOST_ASSIGN_PARAMS2(n) BOOST_PP_ENUM_BINARY_PARAMS(n, U, const& u)
384 #define BOOST_ASSIGN_PARAMS3(n) BOOST_PP_ENUM_PARAMS(n, u)
385 #define BOOST_ASSIGN_PARAMS4(n) BOOST_PP_ENUM_PARAMS(n, U)
386 #define BOOST_ASSIGN_PARAMS2_NO_REF(n) BOOST_PP_ENUM_BINARY_PARAMS(n, U, u)
387
388 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
389 #define BOOST_PP_LOCAL_MACRO(n) \
390 template< class U, BOOST_ASSIGN_PARAMS1(n) > \
391 generic_list& operator()(U const& u, BOOST_ASSIGN_PARAMS2(n) ) \
392 { \
393 this->push_back( Ty(u, BOOST_ASSIGN_PARAMS3(n))); \
394 return *this; \
395 } \
396 /**/
397
398 #include BOOST_PP_LOCAL_ITERATE()
399
400
401 template< class U >
402 generic_list& repeat( std::size_t sz, U u )
403 {
404 std::size_t i = 0;
405 while( i++ != sz )
406 this->push_back( u );
407 return *this;
408 }
409
410 template< class Nullary_function >
411 generic_list& repeat_fun( std::size_t sz, Nullary_function fun )
412 {
413 std::size_t i = 0;
414 while( i++ != sz )
415 this->push_back( fun() );
416 return *this;
417 }
418
419 template< class SinglePassIterator >
420 generic_list& range( SinglePassIterator first,
421 SinglePassIterator last )
422 {
423 for( ; first != last; ++first )
424 this->push_back( *first );
425 return *this;
426 }
427
428 template< class SinglePassRange >
429 generic_list& range( const SinglePassRange& r )
430 {
431 return range( boost::begin(r), boost::end(r) );
432 }
433
434 template< class Container >
435 operator Container() const
436 {
437 return this-> BOOST_NESTED_TEMPLATE convert_to_container<Container>();
438 }
439 };
440
441 /////////////////////////////////////////////////////////////////////////
442 // Part 2: efficient, but inconvenient interface
443 /////////////////////////////////////////////////////////////////////////
444
445 template< class T >
446 struct assign_reference
447 {
448 assign_reference()
449 { /* intentionally empty */ }
450
451 assign_reference( T& r ) : ref_(&r)
452 { }
453
454 void operator=( T& r )
455 {
456 ref_ = &r;
457 }
458
459 operator T&() const
460 {
461 return *ref_;
462 }
463
464 void swap( assign_reference& r )
465 {
466 std::swap( *ref_, *r.ref_ );
467 }
468
469 T& get_ref() const
470 {
471 return *ref_;
472 }
473
474 private:
475 T* ref_;
476
477 };
478
479 template< class T >
480 inline bool operator<( const assign_reference<T>& l,
481 const assign_reference<T>& r )
482 {
483 return l.get_ref() < r.get_ref();
484 }
485
486 template< class T >
487 inline bool operator>( const assign_reference<T>& l,
488 const assign_reference<T>& r )
489 {
490 return l.get_ref() > r.get_ref();
491 }
492
493 template< class T >
494 inline void swap( assign_reference<T>& l,
495 assign_reference<T>& r )
496 {
497 l.swap( r );
498 }
499
500
501
502 template< class T, int N >
503 struct static_generic_list :
504 public converter< static_generic_list<T,N>, assign_reference<T>* >
505 {
506 private:
507 typedef T internal_value_type;
508
509 public:
510 typedef assign_reference<internal_value_type> value_type;
511 typedef value_type* iterator;
512 typedef value_type* const_iterator;
513 typedef std::size_t size_type;
514 typedef std::ptrdiff_t difference_type;
515
516
517 static_generic_list( T& r ) :
518 current_(1)
519 {
520 refs_[0] = r;
521 }
522
523 static_generic_list& operator()( T& r )
524 {
525 insert( r );
526 return *this;
527 }
528
529 iterator begin() const
530 {
531 return &refs_[0];
532 }
533
534 iterator end() const
535 {
536 return &refs_[current_];
537 }
538
539 size_type size() const
540 {
541 return static_cast<size_type>( current_ );
542 }
543
544 bool empty() const
545 {
546 return false;
547 }
548
549 template< class ForwardIterator >
550 static_generic_list& range( ForwardIterator first,
551 ForwardIterator last )
552 {
553 for( ; first != last; ++first )
554 this->insert( *first );
555 return *this;
556 }
557
558 template< class ForwardRange >
559 static_generic_list& range( ForwardRange& r )
560 {
561 return range( boost::begin(r), boost::end(r) );
562 }
563
564 template< class ForwardRange >
565 static_generic_list& range( const ForwardRange& r )
566 {
567 return range( boost::begin(r), boost::end(r) );
568 }
569
570 template< class Container >
571 operator Container() const
572 {
573 return this-> BOOST_NESTED_TEMPLATE convert_to_container<Container>();
574 }
575
576 private:
577 void insert( T& r )
578 {
579 refs_[current_] = r;
580 ++current_;
581 }
582
583 static_generic_list();
584
585 mutable assign_reference<internal_value_type> refs_[N];
586 int current_;
587 };
588
589 } // namespace 'assign_detail'
590
591 namespace assign
592 {
593 template< class T >
594 inline assign_detail::generic_list<T>
595 list_of()
596 {
597 return assign_detail::generic_list<T>()( T() );
598 }
599
600 template< class T >
601 inline assign_detail::generic_list<T>
602 list_of( const T& t )
603 {
604 return assign_detail::generic_list<T>()( t );
605 }
606
607 template< int N, class T >
608 inline assign_detail::static_generic_list< BOOST_DEDUCED_TYPENAME assign_detail::assign_decay<T>::type,N>
609 ref_list_of( T& t )
610 {
611 return assign_detail::static_generic_list<BOOST_DEDUCED_TYPENAME assign_detail::assign_decay<T>::type,N>( t );
612 }
613
614 template< int N, class T >
615 inline assign_detail::static_generic_list<const BOOST_DEDUCED_TYPENAME assign_detail::assign_decay<T>::type,N>
616 cref_list_of( const T& t )
617 {
618 return assign_detail::static_generic_list<const BOOST_DEDUCED_TYPENAME assign_detail::assign_decay<T>::type,N>( t );
619 }
620
621 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
622 #define BOOST_PP_LOCAL_MACRO(n) \
623 template< class T, class U, BOOST_ASSIGN_PARAMS1(n) > \
624 inline assign_detail::generic_list<T> \
625 list_of(U const& u, BOOST_ASSIGN_PARAMS2(n) ) \
626 { \
627 return assign_detail::generic_list<T>()(u, BOOST_ASSIGN_PARAMS3(n)); \
628 } \
629 /**/
630
631 #include BOOST_PP_LOCAL_ITERATE()
632
633 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_ASSIGN_MAX_PARAMETERS)
634 #define BOOST_PP_LOCAL_MACRO(n) \
635 template< class U, BOOST_ASSIGN_PARAMS1(n) > \
636 inline assign_detail::generic_list< tuple<U, BOOST_ASSIGN_PARAMS4(n)> > \
637 tuple_list_of(U u, BOOST_ASSIGN_PARAMS2_NO_REF(n) ) \
638 { \
639 return assign_detail::generic_list< tuple<U, BOOST_ASSIGN_PARAMS4(n)> >()( tuple<U,BOOST_ASSIGN_PARAMS4(n)>( u, BOOST_ASSIGN_PARAMS3(n) )); \
640 } \
641 /**/
642
643 #include BOOST_PP_LOCAL_ITERATE()
644
645
646 template< class Key, class T >
647 inline assign_detail::generic_list< std::pair
648 <
649 BOOST_DEDUCED_TYPENAME assign_detail::assign_decay<Key>::type,
650 BOOST_DEDUCED_TYPENAME assign_detail::assign_decay<T>::type
651 > >
652 map_list_of( const Key& k, const T& t )
653 {
654 typedef BOOST_DEDUCED_TYPENAME assign_detail::assign_decay<Key>::type k_type;
655 typedef BOOST_DEDUCED_TYPENAME assign_detail::assign_decay<T>::type t_type;
656 return assign_detail::generic_list< std::pair<k_type,t_type> >()( k, t );
657 }
658
659 template< class F, class S >
660 inline assign_detail::generic_list< std::pair
661 <
662 BOOST_DEDUCED_TYPENAME assign_detail::assign_decay<F>::type,
663 BOOST_DEDUCED_TYPENAME assign_detail::assign_decay<S>::type
664 > >
665 pair_list_of( const F& f, const S& s )
666 {
667 return map_list_of( f, s );
668 }
669
670
671 } // namespace 'assign'
672 } // namespace 'boost'
673
674
675 #undef BOOST_ASSIGN_PARAMS1
676 #undef BOOST_ASSIGN_PARAMS2
677 #undef BOOST_ASSIGN_PARAMS3
678 #undef BOOST_ASSIGN_PARAMS4
679 #undef BOOST_ASSIGN_PARAMS2_NO_REF
680 #undef BOOST_ASSIGN_MAX_PARAMETERS
681
682 #endif