PageRenderTime 65ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/Lib/include/boost/foreach.hpp

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