foreach.hpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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