foreach.hpp 45 KB

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