foreach.hpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // foreach.hpp header file
  3. //
  4. // Copyright 2004 Eric Niebler.
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // See http://www.boost.org/libs/foreach for documentation
  9. //
  10. // Credits:
  11. // Anson Tsao - for the initial inspiration and several good suggestions.
  12. // Thorsten Ottosen - for Boost.Range, and for suggesting a way to detect
  13. // const-qualified rvalues at compile time on VC7.1+
  14. // Russell Hind - For help porting to Borland
  15. // Alisdair Meredith - For help porting to Borland
  16. // Stefan Slapeta - For help porting to Intel
  17. // David Jenkins - For help finding a Microsoft Code Analysis bug
  18. // mimomorin@... - For a patch to use rvalue refs on supporting compilers
  19. #ifndef BOOST_FOREACH
  20. // MS compatible compilers support #pragma once
  21. #if defined(_MSC_VER)
  22. # pragma once
  23. #endif
  24. #include <cstddef>
  25. #include <utility> // for std::pair
  26. #include <boost/config.hpp>
  27. #include <boost/detail/workaround.hpp>
  28. // Define a compiler generic null pointer value
  29. #if defined(BOOST_NO_NULLPTR)
  30. #define BOOST_FOREACH_NULL 0
  31. #else
  32. #define BOOST_FOREACH_NULL nullptr
  33. #endif
  34. // Some compilers let us detect even const-qualified rvalues at compile-time
  35. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) \
  36. || defined(BOOST_MSVC) && !defined(_PREFAST_) \
  37. || (BOOST_WORKAROUND(__GNUC__, == 4) && (__GNUC_MINOR__ <= 5) && !defined(BOOST_INTEL) && \
  38. !defined(BOOST_CLANG)) \
  39. || (BOOST_WORKAROUND(__GNUC__, == 3) && (__GNUC_MINOR__ >= 4) && !defined(BOOST_INTEL) && \
  40. !defined(BOOST_CLANG))
  41. # define BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION
  42. #else
  43. // Some compilers allow temporaries to be bound to non-const references.
  44. // These compilers make it impossible to for BOOST_FOREACH to detect
  45. // temporaries and avoid reevaluation of the collection expression.
  46. # if BOOST_WORKAROUND(__BORLANDC__, < 0x593) \
  47. || (BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION, <= 700) && defined(_MSC_VER)) \
  48. || BOOST_WORKAROUND(__SUNPRO_CC, < 0x5100) \
  49. || BOOST_WORKAROUND(__DECCXX_VER, <= 60590042)
  50. # define BOOST_FOREACH_NO_RVALUE_DETECTION
  51. # endif
  52. // Some compilers do not correctly implement the lvalue/rvalue conversion
  53. // rules of the ternary conditional operator.
  54. # if defined(BOOST_FOREACH_NO_RVALUE_DETECTION) \
  55. || defined(BOOST_NO_SFINAE) \
  56. || BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1400)) \
  57. || BOOST_WORKAROUND(BOOST_INTEL_WIN, BOOST_TESTED_AT(1400)) \
  58. || (BOOST_WORKAROUND(__GNUC__, == 3) && (__GNUC_MINOR__ <= 3) && defined(__APPLE_CC__)) \
  59. || BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600)) \
  60. || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206)) \
  61. || BOOST_WORKAROUND(__SUNPRO_CC, >= 0x5100) \
  62. || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x590))
  63. # define BOOST_FOREACH_NO_CONST_RVALUE_DETECTION
  64. # else
  65. # define BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
  66. # endif
  67. #endif
  68. #include <boost/mpl/if.hpp>
  69. #include <boost/mpl/assert.hpp>
  70. #include <boost/mpl/logical.hpp>
  71. #include <boost/mpl/eval_if.hpp>
  72. #include <boost/noncopyable.hpp>
  73. #include <boost/range/end.hpp>
  74. #include <boost/range/begin.hpp>
  75. #include <boost/range/rend.hpp>
  76. #include <boost/range/rbegin.hpp>
  77. #include <boost/range/iterator.hpp>
  78. #include <boost/range/reverse_iterator.hpp>
  79. #include <boost/type_traits/is_array.hpp>
  80. #include <boost/type_traits/is_const.hpp>
  81. #include <boost/type_traits/is_abstract.hpp>
  82. #include <boost/type_traits/is_base_and_derived.hpp>
  83. #include <boost/type_traits/is_rvalue_reference.hpp>
  84. #include <boost/iterator/iterator_traits.hpp>
  85. #include <boost/utility/addressof.hpp>
  86. #include <boost/foreach_fwd.hpp>
  87. #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
  88. # include <new>
  89. # include <boost/aligned_storage.hpp>
  90. # include <boost/utility/enable_if.hpp>
  91. # include <boost/type_traits/remove_const.hpp>
  92. #endif
  93. namespace boost
  94. {
  95. // forward declarations for iterator_range
  96. template<typename T>
  97. class iterator_range;
  98. // forward declarations for sub_range
  99. template<typename T>
  100. class sub_range;
  101. namespace foreach
  102. {
  103. ///////////////////////////////////////////////////////////////////////////////
  104. // in_range
  105. //
  106. template<typename T>
  107. inline std::pair<T, T> in_range(T begin, T end)
  108. {
  109. return std::make_pair(begin, end);
  110. }
  111. ///////////////////////////////////////////////////////////////////////////////
  112. // boost::foreach::is_lightweight_proxy
  113. // Specialize this for user-defined collection types if they are inexpensive to copy.
  114. // This tells BOOST_FOREACH it can avoid the rvalue/lvalue detection stuff.
  115. template<typename T>
  116. struct is_lightweight_proxy
  117. : boost::mpl::false_
  118. {
  119. };
  120. ///////////////////////////////////////////////////////////////////////////////
  121. // boost::foreach::is_noncopyable
  122. // Specialize this for user-defined collection types if they cannot be copied.
  123. // This also tells BOOST_FOREACH to avoid the rvalue/lvalue detection stuff.
  124. template<typename T>
  125. struct is_noncopyable
  126. #if !defined(BOOST_BROKEN_IS_BASE_AND_DERIVED) && !defined(BOOST_NO_IS_ABSTRACT)
  127. : boost::mpl::or_<
  128. boost::is_abstract<T>
  129. , boost::is_base_and_derived<boost::noncopyable, T>
  130. >
  131. #elif !defined(BOOST_BROKEN_IS_BASE_AND_DERIVED)
  132. : boost::is_base_and_derived<boost::noncopyable, T>
  133. #elif !defined(BOOST_NO_IS_ABSTRACT)
  134. : boost::is_abstract<T>
  135. #else
  136. : boost::mpl::false_
  137. #endif
  138. {
  139. };
  140. } // namespace foreach
  141. } // namespace boost
  142. // vc6/7 needs help ordering the following overloads
  143. #ifdef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  144. # define BOOST_FOREACH_TAG_DEFAULT ...
  145. #else
  146. # define BOOST_FOREACH_TAG_DEFAULT boost::foreach::tag
  147. #endif
  148. ///////////////////////////////////////////////////////////////////////////////
  149. // boost_foreach_is_lightweight_proxy
  150. // Another customization point for the is_lightweight_proxy optimization,
  151. // this one works on legacy compilers. Overload boost_foreach_is_lightweight_proxy
  152. // at the global namespace for your type.
  153. template<typename T>
  154. inline boost::foreach::is_lightweight_proxy<T> *
  155. boost_foreach_is_lightweight_proxy(T *&, BOOST_FOREACH_TAG_DEFAULT) { return 0; }
  156. template<typename T>
  157. inline boost::mpl::true_ *
  158. boost_foreach_is_lightweight_proxy(std::pair<T, T> *&, boost::foreach::tag) { return 0; }
  159. template<typename T>
  160. inline boost::mpl::true_ *
  161. boost_foreach_is_lightweight_proxy(boost::iterator_range<T> *&, boost::foreach::tag) { return 0; }
  162. template<typename T>
  163. inline boost::mpl::true_ *
  164. boost_foreach_is_lightweight_proxy(boost::sub_range<T> *&, boost::foreach::tag) { return 0; }
  165. template<typename T>
  166. inline boost::mpl::true_ *
  167. boost_foreach_is_lightweight_proxy(T **&, boost::foreach::tag) { return 0; }
  168. ///////////////////////////////////////////////////////////////////////////////
  169. // boost_foreach_is_noncopyable
  170. // Another customization point for the is_noncopyable trait,
  171. // this one works on legacy compilers. Overload boost_foreach_is_noncopyable
  172. // at the global namespace for your type.
  173. template<typename T>
  174. inline boost::foreach::is_noncopyable<T> *
  175. boost_foreach_is_noncopyable(T *&, BOOST_FOREACH_TAG_DEFAULT) { return 0; }
  176. namespace boost
  177. {
  178. namespace foreach_detail_
  179. {
  180. ///////////////////////////////////////////////////////////////////////////////
  181. // Define some utilities for assessing the properties of expressions
  182. //
  183. template<typename Bool1, typename Bool2>
  184. inline boost::mpl::and_<Bool1, Bool2> *and_(Bool1 *, Bool2 *) { return 0; }
  185. template<typename Bool1, typename Bool2, typename Bool3>
  186. inline boost::mpl::and_<Bool1, Bool2, Bool3> *and_(Bool1 *, Bool2 *, Bool3 *) { return 0; }
  187. template<typename Bool1, typename Bool2>
  188. inline boost::mpl::or_<Bool1, Bool2> *or_(Bool1 *, Bool2 *) { return 0; }
  189. template<typename Bool1, typename Bool2, typename Bool3>
  190. inline boost::mpl::or_<Bool1, Bool2, Bool3> *or_(Bool1 *, Bool2 *, Bool3 *) { return 0; }
  191. template<typename Bool1>
  192. inline boost::mpl::not_<Bool1> *not_(Bool1 *) { return 0; }
  193. template<typename T>
  194. inline boost::is_array<T> *is_array_(T const &) { return 0; }
  195. template<typename T>
  196. inline boost::is_const<T> *is_const_(T &) { return 0; }
  197. #ifndef BOOST_FOREACH_NO_RVALUE_DETECTION
  198. template<typename T>
  199. inline boost::mpl::true_ *is_const_(T const &) { return 0; }
  200. #endif
  201. #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
  202. template<typename T>
  203. inline boost::mpl::false_ *is_rvalue_(T &, int) { return 0; }
  204. template<typename T>
  205. inline boost::mpl::true_ *is_rvalue_(T const &, ...) { return 0; }
  206. #else
  207. template<typename T>
  208. inline boost::is_rvalue_reference<T &&> *is_rvalue_(T &&, int) { return 0; }
  209. #endif
  210. ///////////////////////////////////////////////////////////////////////////////
  211. // auto_any_t/auto_any
  212. // General utility for putting an object of any type into automatic storage
  213. struct auto_any_base
  214. {
  215. // auto_any_base must evaluate to false in boolean context so that
  216. // they can be declared in if() statements.
  217. operator bool() const
  218. {
  219. return false;
  220. }
  221. };
  222. template<typename T>
  223. struct auto_any : auto_any_base
  224. {
  225. explicit auto_any(T const &t)
  226. : item(t)
  227. {
  228. }
  229. // temporaries of type auto_any will be bound to const auto_any_base
  230. // references, but we still want to be able to mutate the stored
  231. // data, so declare it as mutable.
  232. mutable T item;
  233. };
  234. typedef auto_any_base const &auto_any_t;
  235. template<typename T, typename C>
  236. inline BOOST_DEDUCED_TYPENAME boost::mpl::if_<C, T const, T>::type &auto_any_cast(auto_any_t a)
  237. {
  238. return static_cast<auto_any<T> const &>(a).item;
  239. }
  240. typedef boost::mpl::true_ const_;
  241. ///////////////////////////////////////////////////////////////////////////////
  242. // type2type
  243. //
  244. template<typename T, typename C = boost::mpl::false_>
  245. struct type2type
  246. : boost::mpl::if_<C, T const, T>
  247. {
  248. };
  249. template<typename T>
  250. struct wrap_cstr
  251. {
  252. typedef T type;
  253. };
  254. template<>
  255. struct wrap_cstr<char *>
  256. {
  257. typedef wrap_cstr<char *> type;
  258. typedef char *iterator;
  259. typedef char *const_iterator;
  260. };
  261. template<>
  262. struct wrap_cstr<char const *>
  263. {
  264. typedef wrap_cstr<char const *> type;
  265. typedef char const *iterator;
  266. typedef char const *const_iterator;
  267. };
  268. template<>
  269. struct wrap_cstr<wchar_t *>
  270. {
  271. typedef wrap_cstr<wchar_t *> type;
  272. typedef wchar_t *iterator;
  273. typedef wchar_t *const_iterator;
  274. };
  275. template<>
  276. struct wrap_cstr<wchar_t const *>
  277. {
  278. typedef wrap_cstr<wchar_t const *> type;
  279. typedef wchar_t const *iterator;
  280. typedef wchar_t const *const_iterator;
  281. };
  282. template<typename T>
  283. struct is_char_array
  284. : mpl::and_<
  285. is_array<T>
  286. , mpl::or_<
  287. is_convertible<T, char const *>
  288. , is_convertible<T, wchar_t const *>
  289. >
  290. >
  291. {};
  292. template<typename T, typename C = boost::mpl::false_>
  293. struct foreach_iterator
  294. {
  295. // **** READ THIS IF YOUR COMPILE BREAKS HERE ****
  296. //
  297. // There is an ambiguity about how to iterate over arrays of char and wchar_t.
  298. // Should the last array element be treated as a null terminator to be skipped, or
  299. // is it just like any other element in the array? To fix the problem, you must
  300. // say which behavior you want.
  301. //
  302. // To treat the container as a null-terminated string, merely cast it to a
  303. // char const *, as in BOOST_FOREACH( char ch, (char const *)"hello" ) ...
  304. //
  305. // To treat the container as an array, use boost::as_array() in <boost/range/as_array.hpp>,
  306. // as in BOOST_FOREACH( char ch, boost::as_array("hello") ) ...
  307. BOOST_MPL_ASSERT_MSG( (!is_char_array<T>::value), IS_THIS_AN_ARRAY_OR_A_NULL_TERMINATED_STRING, (T&) );
  308. // If the type is a pointer to a null terminated string (as opposed
  309. // to an array type), there is no ambiguity.
  310. typedef BOOST_DEDUCED_TYPENAME wrap_cstr<T>::type container;
  311. typedef BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<
  312. C
  313. , range_const_iterator<container>
  314. , range_mutable_iterator<container>
  315. >::type type;
  316. };
  317. template<typename T, typename C = boost::mpl::false_>
  318. struct foreach_reverse_iterator
  319. {
  320. // **** READ THIS IF YOUR COMPILE BREAKS HERE ****
  321. //
  322. // There is an ambiguity about how to iterate over arrays of char and wchar_t.
  323. // Should the last array element be treated as a null terminator to be skipped, or
  324. // is it just like any other element in the array? To fix the problem, you must
  325. // say which behavior you want.
  326. //
  327. // To treat the container as a null-terminated string, merely cast it to a
  328. // char const *, as in BOOST_FOREACH( char ch, (char const *)"hello" ) ...
  329. //
  330. // To treat the container as an array, use boost::as_array() in <boost/range/as_array.hpp>,
  331. // as in BOOST_FOREACH( char ch, boost::as_array("hello") ) ...
  332. BOOST_MPL_ASSERT_MSG( (!is_char_array<T>::value), IS_THIS_AN_ARRAY_OR_A_NULL_TERMINATED_STRING, (T&) );
  333. // If the type is a pointer to a null terminated string (as opposed
  334. // to an array type), there is no ambiguity.
  335. typedef BOOST_DEDUCED_TYPENAME wrap_cstr<T>::type container;
  336. typedef BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<
  337. C
  338. , range_reverse_iterator<container const>
  339. , range_reverse_iterator<container>
  340. >::type type;
  341. };
  342. template<typename T, typename C = boost::mpl::false_>
  343. struct foreach_reference
  344. : iterator_reference<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
  345. {
  346. };
  347. ///////////////////////////////////////////////////////////////////////////////
  348. // encode_type
  349. //
  350. template<typename T>
  351. inline type2type<T> *encode_type(T &, boost::false_type*) { return 0; }
  352. template<typename T>
  353. inline type2type<T, const_> *encode_type(T const &, boost::true_type*) { return 0; }
  354. template<typename T>
  355. inline type2type<T> *encode_type(T &, boost::mpl::false_*) { return 0; }
  356. template<typename T>
  357. inline type2type<T, const_> *encode_type(T const &, boost::mpl::true_*) { return 0; }
  358. ///////////////////////////////////////////////////////////////////////////////
  359. // set_false
  360. //
  361. inline bool set_false(bool &b)
  362. {
  363. b = false;
  364. return false;
  365. }
  366. ///////////////////////////////////////////////////////////////////////////////
  367. // to_ptr
  368. //
  369. template<typename T>
  370. inline T *&to_ptr(T const &)
  371. {
  372. static T *t = 0;
  373. return t;
  374. }
  375. // Borland needs a little extra help with arrays
  376. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  377. template<typename T,std::size_t N>
  378. inline T (*&to_ptr(T (&)[N]))[N]
  379. {
  380. static T (*t)[N] = 0;
  381. return t;
  382. }
  383. ///////////////////////////////////////////////////////////////////////////////
  384. // derefof
  385. //
  386. template<typename T>
  387. inline T &derefof(T *t)
  388. {
  389. // This is a work-around for a compiler bug in Borland. If T* is a pointer to array type U(*)[N],
  390. // then dereferencing it results in a U* instead of U(&)[N]. The cast forces the issue.
  391. return reinterpret_cast<T &>(
  392. *const_cast<char *>(
  393. reinterpret_cast<char const volatile *>(t)
  394. )
  395. );
  396. }
  397. # define BOOST_FOREACH_DEREFOF(T) boost::foreach_detail_::derefof(*T)
  398. #else
  399. # define BOOST_FOREACH_DEREFOF(T) (*T)
  400. #endif
  401. #if defined(BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION) \
  402. && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  403. ///////////////////////////////////////////////////////////////////////////////
  404. // Rvalue references makes it drop-dead simple to detect at compile time
  405. // whether an expression is an rvalue.
  406. ///////////////////////////////////////////////////////////////////////////////
  407. # define BOOST_FOREACH_IS_RVALUE(COL) \
  408. boost::foreach_detail_::is_rvalue_((COL), 0)
  409. #elif defined(BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION) \
  410. && defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  411. ///////////////////////////////////////////////////////////////////////////////
  412. // Detect at compile-time whether an expression yields an rvalue or
  413. // an lvalue. This is rather non-standard, but some popular compilers
  414. // accept it.
  415. ///////////////////////////////////////////////////////////////////////////////
  416. ///////////////////////////////////////////////////////////////////////////////
  417. // rvalue_probe
  418. //
  419. template<typename T>
  420. struct rvalue_probe
  421. {
  422. struct private_type_ {};
  423. // can't ever return an array by value
  424. typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
  425. boost::mpl::or_<boost::is_abstract<T>, boost::is_array<T> >, private_type_, T
  426. >::type value_type;
  427. operator value_type() { return *reinterpret_cast<value_type *>(this); } // never called
  428. operator T &() const { return *reinterpret_cast<T *>(const_cast<rvalue_probe *>(this)); } // never called
  429. };
  430. template<typename T>
  431. rvalue_probe<T> const make_probe(T const &)
  432. {
  433. return rvalue_probe<T>();
  434. }
  435. # define BOOST_FOREACH_IS_RVALUE(COL) \
  436. boost::foreach_detail_::and_( \
  437. boost::foreach_detail_::not_(boost::foreach_detail_::is_array_(COL)) \
  438. , (true ? 0 : boost::foreach_detail_::is_rvalue_( \
  439. (true ? boost::foreach_detail_::make_probe(COL) : (COL)), 0)))
  440. #elif defined(BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION)
  441. ///////////////////////////////////////////////////////////////////////////////
  442. // Detect at run-time whether an expression yields an rvalue
  443. // or an lvalue. This is 100% standard C++, but not all compilers
  444. // accept it. Also, it causes FOREACH to break when used with non-
  445. // copyable collection types.
  446. ///////////////////////////////////////////////////////////////////////////////
  447. ///////////////////////////////////////////////////////////////////////////////
  448. // rvalue_probe
  449. //
  450. template<typename T>
  451. struct rvalue_probe
  452. {
  453. rvalue_probe(T &t, bool &b)
  454. : value(t)
  455. , is_rvalue(b)
  456. {
  457. }
  458. struct private_type_ {};
  459. // can't ever return an array or an abstract type by value
  460. #ifdef BOOST_NO_IS_ABSTRACT
  461. typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
  462. boost::is_array<T>, private_type_, T
  463. >::type value_type;
  464. #else
  465. typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
  466. boost::mpl::or_<boost::is_abstract<T>, boost::is_array<T> >, private_type_, T
  467. >::type value_type;
  468. #endif
  469. operator value_type()
  470. {
  471. this->is_rvalue = true;
  472. return this->value;
  473. }
  474. operator T &() const
  475. {
  476. return this->value;
  477. }
  478. private:
  479. T &value;
  480. bool &is_rvalue;
  481. };
  482. template<typename T>
  483. rvalue_probe<T> make_probe(T &t, bool &b) { return rvalue_probe<T>(t, b); }
  484. template<typename T>
  485. rvalue_probe<T const> make_probe(T const &t, bool &b) { return rvalue_probe<T const>(t, b); }
  486. ///////////////////////////////////////////////////////////////////////////////
  487. // simple_variant
  488. // holds either a T or a T const*
  489. template<typename T>
  490. struct simple_variant
  491. {
  492. simple_variant(T const *t)
  493. : is_rvalue(false)
  494. {
  495. *static_cast<T const **>(this->data.address()) = t;
  496. }
  497. simple_variant(T const &t)
  498. : is_rvalue(true)
  499. {
  500. ::new(this->data.address()) T(t);
  501. }
  502. simple_variant(simple_variant const &that)
  503. : is_rvalue(that.is_rvalue)
  504. {
  505. if(this->is_rvalue)
  506. ::new(this->data.address()) T(*that.get());
  507. else
  508. *static_cast<T const **>(this->data.address()) = that.get();
  509. }
  510. ~simple_variant()
  511. {
  512. if(this->is_rvalue)
  513. this->get()->~T();
  514. }
  515. T const *get() const
  516. {
  517. if(this->is_rvalue)
  518. return static_cast<T const *>(this->data.address());
  519. else
  520. return *static_cast<T const * const *>(this->data.address());
  521. }
  522. private:
  523. enum size_type { size = sizeof(T) > sizeof(T*) ? sizeof(T) : sizeof(T*) };
  524. simple_variant &operator =(simple_variant const &);
  525. bool const is_rvalue;
  526. aligned_storage<size> data;
  527. };
  528. // If the collection is an array or is noncopyable, it must be an lvalue.
  529. // If the collection is a lightweight proxy, treat it as an rvalue
  530. // BUGBUG what about a noncopyable proxy?
  531. template<typename LValue, typename IsProxy>
  532. inline BOOST_DEDUCED_TYPENAME boost::enable_if<boost::mpl::or_<LValue, IsProxy>, IsProxy>::type *
  533. should_copy_impl(LValue *, IsProxy *, bool *)
  534. {
  535. return 0;
  536. }
  537. // Otherwise, we must determine at runtime whether it's an lvalue or rvalue
  538. inline bool *
  539. should_copy_impl(boost::mpl::false_ *, boost::mpl::false_ *, bool *is_rvalue)
  540. {
  541. return is_rvalue;
  542. }
  543. #endif
  544. ///////////////////////////////////////////////////////////////////////////////
  545. // contain
  546. //
  547. template<typename T>
  548. inline auto_any<T> contain(T const &t, boost::mpl::true_ *) // rvalue
  549. {
  550. return auto_any<T>(t);
  551. }
  552. template<typename T>
  553. inline auto_any<T *> contain(T &t, boost::mpl::false_ *) // lvalue
  554. {
  555. // Cannot seem to get sunpro to handle addressof() with array types.
  556. #if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x570))
  557. return auto_any<T *>(&t);
  558. #else
  559. return auto_any<T *>(boost::addressof(t));
  560. #endif
  561. }
  562. #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
  563. template<typename T>
  564. inline auto_any<simple_variant<T> >
  565. contain(T const &t, bool *rvalue)
  566. {
  567. return auto_any<simple_variant<T> >(*rvalue ? simple_variant<T>(t) : simple_variant<T>(&t));
  568. }
  569. #endif
  570. /////////////////////////////////////////////////////////////////////////////
  571. // begin
  572. //
  573. template<typename T, typename C>
  574. inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
  575. begin(auto_any_t col, type2type<T, C> *, boost::mpl::true_ *) // rvalue
  576. {
  577. return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>(
  578. boost::begin(auto_any_cast<T, C>(col)));
  579. }
  580. template<typename T, typename C>
  581. inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
  582. begin(auto_any_t col, type2type<T, C> *, boost::mpl::false_ *) // lvalue
  583. {
  584. typedef BOOST_DEDUCED_TYPENAME type2type<T, C>::type type;
  585. typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iterator;
  586. return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>(
  587. iterator(boost::begin(BOOST_FOREACH_DEREFOF((auto_any_cast<type *, boost::mpl::false_>(col))))));
  588. }
  589. #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
  590. template<typename T>
  591. inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, const_>::type>
  592. begin(auto_any_t col, type2type<T, const_> *, bool *)
  593. {
  594. return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, const_>::type>(
  595. boost::begin(*auto_any_cast<simple_variant<T>, boost::mpl::false_>(col).get()));
  596. }
  597. #endif
  598. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  599. template<typename T, typename C>
  600. inline auto_any<T *>
  601. begin(auto_any_t col, type2type<T *, C> *, boost::mpl::true_ *) // null-terminated C-style strings
  602. {
  603. return auto_any<T *>(auto_any_cast<T *, boost::mpl::false_>(col));
  604. }
  605. #endif
  606. ///////////////////////////////////////////////////////////////////////////////
  607. // end
  608. //
  609. template<typename T, typename C>
  610. inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
  611. end(auto_any_t col, type2type<T, C> *, boost::mpl::true_ *) // rvalue
  612. {
  613. return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>(
  614. boost::end(auto_any_cast<T, C>(col)));
  615. }
  616. template<typename T, typename C>
  617. inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
  618. end(auto_any_t col, type2type<T, C> *, boost::mpl::false_ *) // lvalue
  619. {
  620. typedef BOOST_DEDUCED_TYPENAME type2type<T, C>::type type;
  621. typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iterator;
  622. return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>(
  623. iterator(boost::end(BOOST_FOREACH_DEREFOF((auto_any_cast<type *, boost::mpl::false_>(col))))));
  624. }
  625. #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
  626. template<typename T>
  627. inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, const_>::type>
  628. end(auto_any_t col, type2type<T, const_> *, bool *)
  629. {
  630. return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, const_>::type>(
  631. boost::end(*auto_any_cast<simple_variant<T>, boost::mpl::false_>(col).get()));
  632. }
  633. #endif
  634. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  635. template<typename T, typename C>
  636. inline auto_any<int>
  637. end(auto_any_t, type2type<T *, C> *, boost::mpl::true_ *) // null-terminated C-style strings
  638. {
  639. return auto_any<int>(0); // not used
  640. }
  641. #endif
  642. ///////////////////////////////////////////////////////////////////////////////
  643. // done
  644. //
  645. template<typename T, typename C>
  646. inline bool done(auto_any_t cur, auto_any_t end, type2type<T, C> *)
  647. {
  648. typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iter_t;
  649. return auto_any_cast<iter_t, boost::mpl::false_>(cur) == auto_any_cast<iter_t, boost::mpl::false_>(end);
  650. }
  651. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  652. template<typename T, typename C>
  653. inline bool done(auto_any_t cur, auto_any_t, type2type<T *, C> *) // null-terminated C-style strings
  654. {
  655. return ! *auto_any_cast<T *, boost::mpl::false_>(cur);
  656. }
  657. #endif
  658. ///////////////////////////////////////////////////////////////////////////////
  659. // next
  660. //
  661. template<typename T, typename C>
  662. inline void next(auto_any_t cur, type2type<T, C> *)
  663. {
  664. typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iter_t;
  665. ++auto_any_cast<iter_t, boost::mpl::false_>(cur);
  666. }
  667. ///////////////////////////////////////////////////////////////////////////////
  668. // deref
  669. //
  670. template<typename T, typename C>
  671. inline BOOST_DEDUCED_TYPENAME foreach_reference<T, C>::type
  672. deref(auto_any_t cur, type2type<T, C> *)
  673. {
  674. typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iter_t;
  675. return *auto_any_cast<iter_t, boost::mpl::false_>(cur);
  676. }
  677. /////////////////////////////////////////////////////////////////////////////
  678. // rbegin
  679. //
  680. template<typename T, typename C>
  681. inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>
  682. rbegin(auto_any_t col, type2type<T, C> *, boost::mpl::true_ *) // rvalue
  683. {
  684. return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>(
  685. boost::rbegin(auto_any_cast<T, C>(col)));
  686. }
  687. template<typename T, typename C>
  688. inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>
  689. rbegin(auto_any_t col, type2type<T, C> *, boost::mpl::false_ *) // lvalue
  690. {
  691. typedef BOOST_DEDUCED_TYPENAME type2type<T, C>::type type;
  692. typedef BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type iterator;
  693. return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>(
  694. iterator(boost::rbegin(BOOST_FOREACH_DEREFOF((auto_any_cast<type *, boost::mpl::false_>(col))))));
  695. }
  696. #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
  697. template<typename T>
  698. inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, const_>::type>
  699. rbegin(auto_any_t col, type2type<T, const_> *, bool *)
  700. {
  701. return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, const_>::type>(
  702. boost::rbegin(*auto_any_cast<simple_variant<T>, boost::mpl::false_>(col).get()));
  703. }
  704. #endif
  705. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  706. template<typename T, typename C>
  707. inline auto_any<reverse_iterator<T *> >
  708. rbegin(auto_any_t col, type2type<T *, C> *, boost::mpl::true_ *) // null-terminated C-style strings
  709. {
  710. T *p = auto_any_cast<T *, boost::mpl::false_>(col);
  711. while(0 != *p)
  712. ++p;
  713. return auto_any<reverse_iterator<T *> >(reverse_iterator<T *>(p));
  714. }
  715. #endif
  716. ///////////////////////////////////////////////////////////////////////////////
  717. // rend
  718. //
  719. template<typename T, typename C>
  720. inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>
  721. rend(auto_any_t col, type2type<T, C> *, boost::mpl::true_ *) // rvalue
  722. {
  723. return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>(
  724. boost::rend(auto_any_cast<T, C>(col)));
  725. }
  726. template<typename T, typename C>
  727. inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>
  728. rend(auto_any_t col, type2type<T, C> *, boost::mpl::false_ *) // lvalue
  729. {
  730. typedef BOOST_DEDUCED_TYPENAME type2type<T, C>::type type;
  731. typedef BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type iterator;
  732. return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>(
  733. iterator(boost::rend(BOOST_FOREACH_DEREFOF((auto_any_cast<type *, boost::mpl::false_>(col))))));
  734. }
  735. #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
  736. template<typename T>
  737. inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, const_>::type>
  738. rend(auto_any_t col, type2type<T, const_> *, bool *)
  739. {
  740. return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, const_>::type>(
  741. boost::rend(*auto_any_cast<simple_variant<T>, boost::mpl::false_>(col).get()));
  742. }
  743. #endif
  744. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  745. template<typename T, typename C>
  746. inline auto_any<reverse_iterator<T *> >
  747. rend(auto_any_t col, type2type<T *, C> *, boost::mpl::true_ *) // null-terminated C-style strings
  748. {
  749. return auto_any<reverse_iterator<T *> >(
  750. reverse_iterator<T *>(auto_any_cast<T *, boost::mpl::false_>(col)));
  751. }
  752. #endif
  753. ///////////////////////////////////////////////////////////////////////////////
  754. // rdone
  755. //
  756. template<typename T, typename C>
  757. inline bool rdone(auto_any_t cur, auto_any_t end, type2type<T, C> *)
  758. {
  759. typedef BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type iter_t;
  760. return auto_any_cast<iter_t, boost::mpl::false_>(cur) == auto_any_cast<iter_t, boost::mpl::false_>(end);
  761. }
  762. ///////////////////////////////////////////////////////////////////////////////
  763. // rnext
  764. //
  765. template<typename T, typename C>
  766. inline void rnext(auto_any_t cur, type2type<T, C> *)
  767. {
  768. typedef BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type iter_t;
  769. ++auto_any_cast<iter_t, boost::mpl::false_>(cur);
  770. }
  771. ///////////////////////////////////////////////////////////////////////////////
  772. // rderef
  773. //
  774. template<typename T, typename C>
  775. inline BOOST_DEDUCED_TYPENAME foreach_reference<T, C>::type
  776. rderef(auto_any_t cur, type2type<T, C> *)
  777. {
  778. typedef BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type iter_t;
  779. return *auto_any_cast<iter_t, boost::mpl::false_>(cur);
  780. }
  781. } // namespace foreach_detail_
  782. } // namespace boost
  783. // Suppress a bogus code analysis warning on vc8+
  784. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  785. # define BOOST_FOREACH_SUPPRESS_WARNINGS() __pragma(warning(suppress:6001))
  786. #else
  787. # define BOOST_FOREACH_SUPPRESS_WARNINGS()
  788. #endif
  789. ///////////////////////////////////////////////////////////////////////////////
  790. // Define a macro for giving hidden variables a unique name. Not strictly
  791. // needed, but eliminates some warnings on some compilers.
  792. #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500))
  793. // With some versions of MSVC, use of __LINE__ to create unique identifiers
  794. // can fail when the Edit-and-Continue debug flag is used.
  795. # define BOOST_FOREACH_ID(x) x
  796. #else
  797. # define BOOST_FOREACH_ID(x) BOOST_PP_CAT(x, __LINE__)
  798. #endif
  799. // A sneaky way to get the type of the collection without evaluating the expression
  800. #define BOOST_FOREACH_TYPEOF(COL) \
  801. (true ? BOOST_FOREACH_NULL : boost::foreach_detail_::encode_type(COL, boost::foreach_detail_::is_const_(COL)))
  802. // returns true_* if the type is noncopyable
  803. #define BOOST_FOREACH_IS_NONCOPYABLE(COL) \
  804. boost_foreach_is_noncopyable( \
  805. boost::foreach_detail_::to_ptr(COL) \
  806. , boost_foreach_argument_dependent_lookup_hack_value)
  807. // returns true_* if the type is a lightweight proxy (and is not noncopyable)
  808. #define BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL) \
  809. boost::foreach_detail_::and_( \
  810. boost::foreach_detail_::not_(BOOST_FOREACH_IS_NONCOPYABLE(COL)) \
  811. , boost_foreach_is_lightweight_proxy( \
  812. boost::foreach_detail_::to_ptr(COL) \
  813. , boost_foreach_argument_dependent_lookup_hack_value))
  814. #if defined(BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION)
  815. ///////////////////////////////////////////////////////////////////////////////
  816. // R-values and const R-values supported here with zero runtime overhead
  817. ///////////////////////////////////////////////////////////////////////////////
  818. // No variable is needed to track the rvalue-ness of the collection expression
  819. # define BOOST_FOREACH_PREAMBLE() \
  820. BOOST_FOREACH_SUPPRESS_WARNINGS()
  821. // Evaluate the collection expression
  822. # define BOOST_FOREACH_EVALUATE(COL) \
  823. (COL)
  824. # define BOOST_FOREACH_SHOULD_COPY(COL) \
  825. (true ? BOOST_FOREACH_NULL : boost::foreach_detail_::or_( \
  826. BOOST_FOREACH_IS_RVALUE(COL) \
  827. , BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL)))
  828. #elif defined(BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION)
  829. ///////////////////////////////////////////////////////////////////////////////
  830. // R-values and const R-values supported here
  831. ///////////////////////////////////////////////////////////////////////////////
  832. // Declare a variable to track the rvalue-ness of the collection expression
  833. # define BOOST_FOREACH_PREAMBLE() \
  834. BOOST_FOREACH_SUPPRESS_WARNINGS() \
  835. if (bool BOOST_FOREACH_ID(_foreach_is_rvalue) = false) {} else
  836. // Evaluate the collection expression, and detect if it is an lvalue or and rvalue
  837. # define BOOST_FOREACH_EVALUATE(COL) \
  838. (true ? boost::foreach_detail_::make_probe((COL), BOOST_FOREACH_ID(_foreach_is_rvalue)) : (COL))
  839. // The rvalue/lvalue-ness of the collection expression is determined dynamically, unless
  840. // the type is an array or is noncopyable or is non-const, in which case we know it's an lvalue.
  841. // If the type happens to be a lightweight proxy, always make a copy.
  842. # define BOOST_FOREACH_SHOULD_COPY(COL) \
  843. (boost::foreach_detail_::should_copy_impl( \
  844. true ? BOOST_FOREACH_NULL : boost::foreach_detail_::or_( \
  845. boost::foreach_detail_::is_array_(COL) \
  846. , BOOST_FOREACH_IS_NONCOPYABLE(COL) \
  847. , boost::foreach_detail_::not_(boost::foreach_detail_::is_const_(COL))) \
  848. , true ? BOOST_FOREACH_NULL : BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL) \
  849. , &BOOST_FOREACH_ID(_foreach_is_rvalue)))
  850. #elif !defined(BOOST_FOREACH_NO_RVALUE_DETECTION)
  851. ///////////////////////////////////////////////////////////////////////////////
  852. // R-values supported here, const R-values NOT supported here
  853. ///////////////////////////////////////////////////////////////////////////////
  854. // No variable is needed to track the rvalue-ness of the collection expression
  855. # define BOOST_FOREACH_PREAMBLE() \
  856. BOOST_FOREACH_SUPPRESS_WARNINGS()
  857. // Evaluate the collection expression
  858. # define BOOST_FOREACH_EVALUATE(COL) \
  859. (COL)
  860. // Determine whether the collection expression is an lvalue or an rvalue.
  861. // NOTE: this gets the answer wrong for const rvalues.
  862. # define BOOST_FOREACH_SHOULD_COPY(COL) \
  863. (true ? BOOST_FOREACH_NULL : boost::foreach_detail_::or_( \
  864. boost::foreach_detail_::is_rvalue_((COL), 0) \
  865. , BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL)))
  866. #else
  867. ///////////////////////////////////////////////////////////////////////////////
  868. // R-values NOT supported here
  869. ///////////////////////////////////////////////////////////////////////////////
  870. // No variable is needed to track the rvalue-ness of the collection expression
  871. # define BOOST_FOREACH_PREAMBLE() \
  872. BOOST_FOREACH_SUPPRESS_WARNINGS()
  873. // Evaluate the collection expression
  874. # define BOOST_FOREACH_EVALUATE(COL) \
  875. (COL)
  876. // Can't use rvalues with BOOST_FOREACH (unless they are lightweight proxies)
  877. # define BOOST_FOREACH_SHOULD_COPY(COL) \
  878. (true ? BOOST_FOREACH_NULL : BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL))
  879. #endif
  880. #define BOOST_FOREACH_CONTAIN(COL) \
  881. boost::foreach_detail_::contain( \
  882. BOOST_FOREACH_EVALUATE(COL) \
  883. , BOOST_FOREACH_SHOULD_COPY(COL))
  884. #define BOOST_FOREACH_BEGIN(COL) \
  885. boost::foreach_detail_::begin( \
  886. BOOST_FOREACH_ID(_foreach_col) \
  887. , BOOST_FOREACH_TYPEOF(COL) \
  888. , BOOST_FOREACH_SHOULD_COPY(COL))
  889. #define BOOST_FOREACH_END(COL) \
  890. boost::foreach_detail_::end( \
  891. BOOST_FOREACH_ID(_foreach_col) \
  892. , BOOST_FOREACH_TYPEOF(COL) \
  893. , BOOST_FOREACH_SHOULD_COPY(COL))
  894. #define BOOST_FOREACH_DONE(COL) \
  895. boost::foreach_detail_::done( \
  896. BOOST_FOREACH_ID(_foreach_cur) \
  897. , BOOST_FOREACH_ID(_foreach_end) \
  898. , BOOST_FOREACH_TYPEOF(COL))
  899. #define BOOST_FOREACH_NEXT(COL) \
  900. boost::foreach_detail_::next( \
  901. BOOST_FOREACH_ID(_foreach_cur) \
  902. , BOOST_FOREACH_TYPEOF(COL))
  903. #define BOOST_FOREACH_DEREF(COL) \
  904. boost::foreach_detail_::deref( \
  905. BOOST_FOREACH_ID(_foreach_cur) \
  906. , BOOST_FOREACH_TYPEOF(COL))
  907. #define BOOST_FOREACH_RBEGIN(COL) \
  908. boost::foreach_detail_::rbegin( \
  909. BOOST_FOREACH_ID(_foreach_col) \
  910. , BOOST_FOREACH_TYPEOF(COL) \
  911. , BOOST_FOREACH_SHOULD_COPY(COL))
  912. #define BOOST_FOREACH_REND(COL) \
  913. boost::foreach_detail_::rend( \
  914. BOOST_FOREACH_ID(_foreach_col) \
  915. , BOOST_FOREACH_TYPEOF(COL) \
  916. , BOOST_FOREACH_SHOULD_COPY(COL))
  917. #define BOOST_FOREACH_RDONE(COL) \
  918. boost::foreach_detail_::rdone( \
  919. BOOST_FOREACH_ID(_foreach_cur) \
  920. , BOOST_FOREACH_ID(_foreach_end) \
  921. , BOOST_FOREACH_TYPEOF(COL))
  922. #define BOOST_FOREACH_RNEXT(COL) \
  923. boost::foreach_detail_::rnext( \
  924. BOOST_FOREACH_ID(_foreach_cur) \
  925. , BOOST_FOREACH_TYPEOF(COL))
  926. #define BOOST_FOREACH_RDEREF(COL) \
  927. boost::foreach_detail_::rderef( \
  928. BOOST_FOREACH_ID(_foreach_cur) \
  929. , BOOST_FOREACH_TYPEOF(COL))
  930. ///////////////////////////////////////////////////////////////////////////////
  931. // BOOST_FOREACH
  932. //
  933. // For iterating over collections. Collections can be
  934. // arrays, null-terminated strings, or STL containers.
  935. // The loop variable can be a value or reference. For
  936. // example:
  937. //
  938. // std::list<int> int_list(/*stuff*/);
  939. // BOOST_FOREACH(int &i, int_list)
  940. // {
  941. // /*
  942. // * loop body goes here.
  943. // * i is a reference to the int in int_list.
  944. // */
  945. // }
  946. //
  947. // Alternately, you can declare the loop variable first,
  948. // so you can access it after the loop finishes. Obviously,
  949. // if you do it this way, then the loop variable cannot be
  950. // a reference.
  951. //
  952. // int i;
  953. // BOOST_FOREACH(i, int_list)
  954. // { ... }
  955. //
  956. #define BOOST_FOREACH(VAR, COL) \
  957. BOOST_FOREACH_PREAMBLE() \
  958. if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_col) = BOOST_FOREACH_CONTAIN(COL)) {} else \
  959. if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_cur) = BOOST_FOREACH_BEGIN(COL)) {} else \
  960. if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_end) = BOOST_FOREACH_END(COL)) {} else \
  961. for (bool BOOST_FOREACH_ID(_foreach_continue) = true; \
  962. BOOST_FOREACH_ID(_foreach_continue) && !BOOST_FOREACH_DONE(COL); \
  963. BOOST_FOREACH_ID(_foreach_continue) ? BOOST_FOREACH_NEXT(COL) : (void)0) \
  964. if (boost::foreach_detail_::set_false(BOOST_FOREACH_ID(_foreach_continue))) {} else \
  965. for (VAR = BOOST_FOREACH_DEREF(COL); !BOOST_FOREACH_ID(_foreach_continue); BOOST_FOREACH_ID(_foreach_continue) = true)
  966. ///////////////////////////////////////////////////////////////////////////////
  967. // BOOST_REVERSE_FOREACH
  968. //
  969. // For iterating over collections in reverse order. In
  970. // all other respects, BOOST_REVERSE_FOREACH is like
  971. // BOOST_FOREACH.
  972. //
  973. #define BOOST_REVERSE_FOREACH(VAR, COL) \
  974. BOOST_FOREACH_PREAMBLE() \
  975. if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_col) = BOOST_FOREACH_CONTAIN(COL)) {} else \
  976. if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_cur) = BOOST_FOREACH_RBEGIN(COL)) {} else \
  977. if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_end) = BOOST_FOREACH_REND(COL)) {} else \
  978. for (bool BOOST_FOREACH_ID(_foreach_continue) = true; \
  979. BOOST_FOREACH_ID(_foreach_continue) && !BOOST_FOREACH_RDONE(COL); \
  980. BOOST_FOREACH_ID(_foreach_continue) ? BOOST_FOREACH_RNEXT(COL) : (void)0) \
  981. if (boost::foreach_detail_::set_false(BOOST_FOREACH_ID(_foreach_continue))) {} else \
  982. for (VAR = BOOST_FOREACH_RDEREF(COL); !BOOST_FOREACH_ID(_foreach_continue); BOOST_FOREACH_ID(_foreach_continue) = true)
  983. #endif