foreach.hpp 42 KB

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