PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/include/boost/foreach.hpp

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