PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/boost/foreach.hpp

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