list_of.hpp 19 KB

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