Update for doxygen 1.5.5, graph generation, and match current code.
[tagua/yd.git] / tests / options / foreach.hpp
blobe0ff483b2c7b940b964a377386231bbf4657b0b8
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 //
9 // Credits:
10 // Anson Tsao - for the initial inspiration and several good suggestions.
11 // Thorsten Ottosen - for Boost.Range, and for suggesting a way to detect
12 // const-qualified rvalues at compile time on VC7.1+
13 // Russell Hind - For help porting to Borland
14 // Alisdair Meredith - For help porting to Borland
15 // Stefan Slapeta - For help porting to Intel
17 #ifndef BOOST_FOREACH
19 // MS compatible compilers support #pragma once
20 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
21 # pragma once
22 #endif
24 #include <cstddef>
25 #include <utility> // for std::pair
27 #include <boost/config.hpp>
28 #include <boost/detail/workaround.hpp>
30 #if 0
31 // Some compilers let us detect even const-qualified rvalues at compile-time
32 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1310) \
33 || BOOST_WORKAROUND(__GNUC__, >= 4) \
34 || (BOOST_WORKAROUND(__GNUC__, == 3) && (__GNUC_MINOR__ >= 4))
35 # define BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION
36 #else
37 // Some compilers allow temporaries to be bound to non-const references.
38 // These compilers make it impossible to for BOOST_FOREACH to detect
39 // temporaries and avoid reevaluation of the collection expression.
40 # if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) \
41 || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) \
42 || (BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION, <= 700) && defined(_MSC_VER)) \
43 || BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x570)) \
44 || BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
45 # define BOOST_FOREACH_NO_RVALUE_DETECTION
46 # endif
47 // Some compilers do not correctly implement the lvalue/rvalue conversion
48 // rules of the ternary conditional operator.
49 # if defined(BOOST_FOREACH_NO_RVALUE_DETECTION) \
50 || defined(BOOST_NO_SFINAE) \
51 || BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1400)) \
52 || BOOST_WORKAROUND(BOOST_INTEL_WIN, <= 810) \
53 || BOOST_WORKAROUND(__GNUC__, < 3) \
54 || (BOOST_WORKAROUND(__GNUC__, == 3) && (__GNUC_MINOR__ <= 2)) \
55 || (BOOST_WORKAROUND(__GNUC__, == 3) && (__GNUC_MINOR__ <= 3) && defined(__APPLE_CC__)) \
56 || BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600)) \
57 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
58 # define BOOST_FOREACH_NO_CONST_RVALUE_DETECTION
59 # else
60 # define BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
61 # endif
62 #endif
63 #endif
65 #include <boost/mpl/if.hpp>
66 #include <boost/mpl/logical.hpp>
67 #include <boost/mpl/eval_if.hpp>
68 #include <boost/noncopyable.hpp>
69 #include <boost/range/end.hpp>
70 #include <boost/range/begin.hpp>
71 #include <boost/range/result_iterator.hpp>
72 #include <boost/type_traits/is_array.hpp>
73 #include <boost/type_traits/is_const.hpp>
74 #include <boost/type_traits/is_abstract.hpp>
75 #include <boost/type_traits/is_base_and_derived.hpp>
76 #include <boost/iterator/iterator_traits.hpp>
77 #include <boost/utility/addressof.hpp>
79 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
80 # include <new>
81 # include <boost/aligned_storage.hpp>
82 # include <boost/utility/enable_if.hpp>
83 # include <boost/type_traits/remove_const.hpp>
84 #endif
86 // This must be at global scope, hence the uglified name
87 enum boost_foreach_argument_dependent_lookup_hack
89 boost_foreach_argument_dependent_lookup_hack_value
92 namespace boost
95 // forward declarations for iterator_range
96 template<typename T>
97 class iterator_range;
99 // forward declarations for sub_range
100 template<typename T>
101 class sub_range;
103 namespace foreach
105 ///////////////////////////////////////////////////////////////////////////////
106 // in_range
108 template<typename T>
109 inline std::pair<T, T> in_range(T begin, T end)
111 return std::make_pair(begin, end);
114 ///////////////////////////////////////////////////////////////////////////////
115 // boost::foreach::tag
117 typedef boost_foreach_argument_dependent_lookup_hack tag;
119 ///////////////////////////////////////////////////////////////////////////////
120 // boost::foreach::is_lightweight_proxy
121 // Specialize this for user-defined collection types if they are inexpensive to copy.
122 // This tells BOOST_FOREACH it can avoid the rvalue/lvalue detection stuff.
123 template<typename T>
124 struct is_lightweight_proxy
125 : boost::mpl::false_
129 ///////////////////////////////////////////////////////////////////////////////
130 // boost::foreach::is_noncopyable
131 // Specialize this for user-defined collection types if they cannot be copied.
132 // This also tells BOOST_FOREACH to avoid the rvalue/lvalue detection stuff.
133 template<typename T>
134 struct is_noncopyable
135 #if !defined(BOOST_BROKEN_IS_BASE_AND_DERIVED) && !defined(BOOST_NO_IS_ABSTRACT)
136 : boost::mpl::or_<
137 boost::is_abstract<T>
138 , boost::is_base_and_derived<boost::noncopyable, T>
140 #elif !defined(BOOST_BROKEN_IS_BASE_AND_DERIVED)
141 : boost::is_base_and_derived<boost::noncopyable, T>
142 #elif !defined(BOOST_NO_IS_ABSTRACT)
143 : boost::is_abstract<T>
144 #else
145 : boost::mpl::false_
146 #endif
150 } // namespace foreach
152 } // namespace boost
154 // vc6/7 needs help ordering the following overloads
155 #ifdef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
156 # define BOOST_FOREACH_TAG_DEFAULT ...
157 #else
158 # define BOOST_FOREACH_TAG_DEFAULT boost::foreach::tag
159 #endif
161 ///////////////////////////////////////////////////////////////////////////////
162 // boost_foreach_is_lightweight_proxy
163 // Another customization point for the is_lightweight_proxy optimization,
164 // this one works on legacy compilers. Overload boost_foreach_is_lightweight_proxy
165 // at the global namespace for your type.
166 template<typename T>
167 inline boost::foreach::is_lightweight_proxy<T> *
168 boost_foreach_is_lightweight_proxy(T *&, BOOST_FOREACH_TAG_DEFAULT) { return 0; }
170 template<typename T>
171 inline boost::mpl::true_ *
172 boost_foreach_is_lightweight_proxy(std::pair<T, T> *&, boost::foreach::tag) { return 0; }
174 template<typename T>
175 inline boost::mpl::true_ *
176 boost_foreach_is_lightweight_proxy(boost::iterator_range<T> *&, boost::foreach::tag) { return 0; }
178 template<typename T>
179 inline boost::mpl::true_ *
180 boost_foreach_is_lightweight_proxy(boost::sub_range<T> *&, boost::foreach::tag) { return 0; }
182 template<typename T>
183 inline boost::mpl::true_ *
184 boost_foreach_is_lightweight_proxy(T **&, boost::foreach::tag) { return 0; }
186 ///////////////////////////////////////////////////////////////////////////////
187 // boost_foreach_is_noncopyable
188 // Another customization point for the is_noncopyable trait,
189 // this one works on legacy compilers. Overload boost_foreach_is_noncopyable
190 // at the global namespace for your type.
191 template<typename T>
192 inline boost::foreach::is_noncopyable<T> *
193 boost_foreach_is_noncopyable(T *&, BOOST_FOREACH_TAG_DEFAULT) { return 0; }
195 namespace boost
198 namespace foreach_detail_
201 ///////////////////////////////////////////////////////////////////////////////
202 // Define some utilities for assessing the properties of expressions
204 typedef char yes_type;
205 typedef char (&no_type)[2];
206 yes_type is_true(boost::mpl::true_ *);
207 no_type is_true(boost::mpl::false_ *);
209 // Extracts the desired property from the expression without evaluating it
210 #define BOOST_FOREACH_PROTECT(expr) \
211 (static_cast<boost::mpl::bool_<1 == sizeof(boost::foreach_detail_::is_true(expr))> *>(0))
213 template<typename Bool1, typename Bool2>
214 inline boost::mpl::and_<Bool1, Bool2> *and_(Bool1 *, Bool2 *) { return 0; }
216 template<typename Bool1, typename Bool2, typename Bool3>
217 inline boost::mpl::and_<Bool1, Bool2, Bool3> *and_(Bool1 *, Bool2 *, Bool3 *) { return 0; }
219 template<typename Bool1, typename Bool2>
220 inline boost::mpl::or_<Bool1, Bool2> *or_(Bool1 *, Bool2 *) { return 0; }
222 template<typename Bool1, typename Bool2, typename Bool3>
223 inline boost::mpl::or_<Bool1, Bool2, Bool3> *or_(Bool1 *, Bool2 *, Bool3 *) { return 0; }
225 template<typename Bool>
226 inline boost::mpl::not_<Bool> *not_(Bool *) { return 0; }
228 template<typename T>
229 inline boost::mpl::false_ *is_rvalue_(T &, int) { return 0; }
231 template<typename T>
232 inline boost::mpl::true_ *is_rvalue_(T const &, ...) { return 0; }
234 template<typename T>
235 inline boost::is_array<T> *is_array_(T const &) { return 0; }
237 template<typename T>
238 inline boost::is_const<T> *is_const_(T &) { return 0; }
240 #ifndef BOOST_FOREACH_NO_RVALUE_DETECTION
241 template<typename T>
242 inline boost::mpl::true_ *is_const_(T const &) { return 0; }
243 #endif
245 ///////////////////////////////////////////////////////////////////////////////
246 // auto_any_t/auto_any
247 // General utility for putting an object of any type into automatic storage
248 struct auto_any_base
250 // auto_any_base must evaluate to false in boolean context so that
251 // they can be declared in if() statements.
252 operator bool() const
254 return false;
258 template<typename T>
259 struct auto_any : auto_any_base
261 auto_any(T const &t)
262 : item(t)
266 // temporaries of type auto_any will be bound to const auto_any_base
267 // references, but we still want to be able to mutate the stored
268 // data, so declare it as mutable.
269 mutable T item;
272 typedef auto_any_base const &auto_any_t;
274 template<typename T, typename C>
275 inline BOOST_DEDUCED_TYPENAME boost::mpl::if_<C, T const, T>::type &auto_any_cast(auto_any_t a)
277 return static_cast<auto_any<T> const &>(a).item;
280 typedef boost::mpl::true_ const_;
282 ///////////////////////////////////////////////////////////////////////////////
283 // type2type
285 template<typename T, typename C = boost::mpl::false_>
286 struct type2type
287 : boost::mpl::if_<C, T const, T>
291 template<typename T, typename C = boost::mpl::false_>
292 struct foreach_iterator
294 typedef BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<
296 , range_const_iterator<T>
297 , range_iterator<T>
298 >::type type;
301 template<typename T, typename C = boost::mpl::false_>
302 struct foreach_reference
303 : iterator_reference<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
307 ///////////////////////////////////////////////////////////////////////////////
308 // encode_type
310 template<typename T>
311 inline type2type<T> *encode_type(T &, boost::mpl::false_ *) { return 0; }
313 template<typename T>
314 inline type2type<T, const_> *encode_type(T const &, boost::mpl::true_ *) { return 0; }
316 ///////////////////////////////////////////////////////////////////////////////
317 // set_false
319 inline bool set_false(bool &b) { return b = false; }
321 ///////////////////////////////////////////////////////////////////////////////
322 // to_ptr
324 template<typename T>
325 inline T *&to_ptr(T const &)
327 static T *t = 0;
328 return t;
331 // Borland needs a little extra help with arrays
332 #if 0 // BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
333 template<typename T,std::size_t N>
334 inline T (*&to_ptr(T (&)[N]))[N]
336 static T (*t)[N] = 0;
337 return t;
339 #endif
341 ///////////////////////////////////////////////////////////////////////////////
342 // derefof
344 template<typename T>
345 inline T &derefof(T *t)
347 // This is a work-around for a compiler bug in Borland. If T* is a pointer to array type U(*)[N],
348 // then dereferencing it results in a U* instead of U(&)[N]. The cast forces the issue.
349 return reinterpret_cast<T &>(
350 *const_cast<char *>(
351 reinterpret_cast<char const volatile *>(t)
356 #ifdef BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION
357 ///////////////////////////////////////////////////////////////////////////////
358 // Detect at compile-time whether an expression yields an rvalue or
359 // an lvalue. This is rather non-standard, but some popular compilers
360 // accept it.
361 ///////////////////////////////////////////////////////////////////////////////
363 ///////////////////////////////////////////////////////////////////////////////
364 // rvalue_probe
366 template<typename T>
367 struct rvalue_probe
369 struct private_type_ {};
370 // can't ever return an array by value
371 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
372 boost::mpl::or_<boost::is_abstract<T>, boost::is_array<T> >, private_type_, T
373 >::type value_type;
374 operator value_type();
375 operator T &() const;
378 template<typename T>
379 rvalue_probe<T> const make_probe(T const &t);
381 # define BOOST_FOREACH_IS_RVALUE(COL) \
382 boost::foreach_detail_::and_( \
383 boost::foreach_detail_::not_(boost::foreach_detail_::is_array_(COL)) \
384 , BOOST_FOREACH_PROTECT(boost::foreach_detail_::is_rvalue_( \
385 (true ? boost::foreach_detail_::make_probe(COL) : (COL)), 0)))
387 #elif defined(BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION)
388 ///////////////////////////////////////////////////////////////////////////////
389 // Detect at run-time whether an expression yields an rvalue
390 // or an lvalue. This is 100% standard C++, but not all compilers
391 // accept it. Also, it causes FOREACH to break when used with non-
392 // copyable collection types.
393 ///////////////////////////////////////////////////////////////////////////////
395 ///////////////////////////////////////////////////////////////////////////////
396 // rvalue_probe
398 template<typename T>
399 struct rvalue_probe
401 rvalue_probe(T &t, bool &b)
402 : value(t)
403 , is_rvalue(b)
407 struct private_type_ {};
408 // can't ever return an array or an abstract type by value
409 #ifdef BOOST_NO_IS_ABSTRACT
410 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
411 boost::is_array<T>, private_type_, T
412 >::type value_type;
413 #else
414 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
415 boost::mpl::or_<boost::is_abstract<T>, boost::is_array<T> >, private_type_, T
416 >::type value_type;
417 #endif
419 operator value_type()
421 this->is_rvalue = true;
422 return this->value;
425 operator T &() const
427 return this->value;
430 private:
431 T &value;
432 bool &is_rvalue;
435 template<typename T>
436 rvalue_probe<T> make_probe(T &t, bool &b) { return rvalue_probe<T>(t, b); }
438 template<typename T>
439 rvalue_probe<T const> make_probe(T const &t, bool &b) { return rvalue_probe<T const>(t, b); }
441 ///////////////////////////////////////////////////////////////////////////////
442 // simple_variant
443 // holds either a T or a T const*
444 template<typename T>
445 struct simple_variant
447 simple_variant(T const *t)
448 : is_rvalue(false)
450 *static_cast<T const **>(this->data.address()) = t;
453 simple_variant(T const &t)
454 : is_rvalue(true)
456 ::new(this->data.address()) T(t);
459 simple_variant(simple_variant const &that)
460 : is_rvalue(that.is_rvalue)
462 if(this->is_rvalue)
463 ::new(this->data.address()) T(*that.get());
464 else
465 *static_cast<T const **>(this->data.address()) = that.get();
468 ~simple_variant()
470 if(this->is_rvalue)
471 this->get()->~T();
474 T const *get() const
476 if(this->is_rvalue)
477 return static_cast<T const *>(this->data.address());
478 else
479 return *static_cast<T const * const *>(this->data.address());
482 private:
483 enum size_type { size = sizeof(T) > sizeof(T*) ? sizeof(T) : sizeof(T*) };
484 simple_variant &operator =(simple_variant const &);
485 bool const is_rvalue;
486 aligned_storage<size> data;
489 // If the collection is an array or is noncopyable, it must be an lvalue.
490 // If the collection is a lightweight proxy, treat it as an rvalue
491 // BUGBUG what about a noncopyable proxy?
492 template<typename LValue, typename IsProxy>
493 inline BOOST_DEDUCED_TYPENAME boost::enable_if<boost::mpl::or_<LValue, IsProxy>, IsProxy>::type *
494 should_copy_impl(LValue *, IsProxy *, bool *)
496 return 0;
499 // Otherwise, we must determine at runtime whether it's an lvalue or rvalue
500 inline bool *
501 should_copy_impl(boost::mpl::false_ *, boost::mpl::false_ *, bool *is_rvalue)
503 return is_rvalue;
506 #endif
508 ///////////////////////////////////////////////////////////////////////////////
509 // contain
511 template<typename T>
512 inline auto_any<T> contain(T const &t, boost::mpl::true_ *) // rvalue
514 return t;
517 template<typename T>
518 inline auto_any<T *> contain(T &t, boost::mpl::false_ *) // lvalue
520 // Cannot seem to get sunpro to handle addressof() with array types.
521 #if 0 // BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x570))
522 return &t;
523 #else
524 return boost::addressof(t);
525 #endif
528 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
529 template<typename T>
530 auto_any<simple_variant<T> >
531 contain(T const &t, bool *rvalue)
533 return *rvalue ? simple_variant<T>(t) : simple_variant<T>(&t);
535 #endif
537 /////////////////////////////////////////////////////////////////////////////
538 // begin
540 template<typename T, typename C>
541 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
542 begin(auto_any_t col, type2type<T, C> *, boost::mpl::true_ *) // rvalue
544 return boost::begin(auto_any_cast<T, C>(col));
547 template<typename T, typename C>
548 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
549 begin(auto_any_t col, type2type<T, C> *, boost::mpl::false_ *) // lvalue
551 typedef BOOST_DEDUCED_TYPENAME type2type<T, C>::type type;
552 typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iterator;
553 return iterator(boost::begin(derefof(auto_any_cast<type *, boost::mpl::false_>(col))));
556 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
557 template<typename T>
558 auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, const_>::type>
559 begin(auto_any_t col, type2type<T, const_> *, bool *)
561 return boost::begin(*auto_any_cast<simple_variant<T>, boost::mpl::false_>(col).get());
563 #endif
565 ///////////////////////////////////////////////////////////////////////////////
566 // end
568 template<typename T, typename C>
569 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
570 end(auto_any_t col, type2type<T, C> *, boost::mpl::true_ *) // rvalue
572 return boost::end(auto_any_cast<T, C>(col));
575 template<typename T, typename C>
576 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
577 end(auto_any_t col, type2type<T, C> *, boost::mpl::false_ *) // lvalue
579 typedef BOOST_DEDUCED_TYPENAME type2type<T, C>::type type;
580 typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iterator;
581 return iterator(boost::end(derefof(auto_any_cast<type *, boost::mpl::false_>(col))));
584 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
585 template<typename T>
586 auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, const_>::type>
587 end(auto_any_t col, type2type<T, const_> *, bool *)
589 return boost::end(*auto_any_cast<simple_variant<T>, boost::mpl::false_>(col).get());
591 #endif
593 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
594 template<typename T, typename C>
595 inline auto_any<int>
596 end(auto_any_t col, type2type<T *, C> *, boost::mpl::true_ *) // null-terminated C-style strings
598 return 0; // not used
600 #endif
602 ///////////////////////////////////////////////////////////////////////////////
603 // done
605 template<typename T, typename C>
606 inline bool done(auto_any_t cur, auto_any_t end, type2type<T, C> *)
608 typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iter_t;
609 return auto_any_cast<iter_t, boost::mpl::false_>(cur) == auto_any_cast<iter_t, boost::mpl::false_>(end);
612 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
613 template<typename T, typename C>
614 inline bool done(auto_any_t cur, auto_any_t, type2type<T *, C> *) // null-terminated C-style strings
616 return ! *auto_any_cast<T *, boost::mpl::false_>(cur);
618 #endif
620 ///////////////////////////////////////////////////////////////////////////////
621 // next
623 template<typename T, typename C>
624 inline void next(auto_any_t cur, type2type<T, C> *)
626 typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iter_t;
627 ++auto_any_cast<iter_t, boost::mpl::false_>(cur);
630 ///////////////////////////////////////////////////////////////////////////////
631 // deref
633 template<typename T, typename C>
634 inline BOOST_DEDUCED_TYPENAME foreach_reference<T, C>::type
635 deref(auto_any_t cur, type2type<T, C> *)
637 typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iter_t;
638 return *auto_any_cast<iter_t, boost::mpl::false_>(cur);
641 } // namespace foreach_detail_
642 } // namespace boost
644 // A sneaky way to get the type of the collection without evaluating the expression
645 #define BOOST_FOREACH_TYPEOF(COL) \
646 (true ? 0 : boost::foreach_detail_::encode_type(COL, boost::foreach_detail_::is_const_(COL)))
648 // returns true_* if the type is noncopyable
649 #define BOOST_FOREACH_IS_NONCOPYABLE(COL) \
650 boost_foreach_is_noncopyable( \
651 boost::foreach_detail_::to_ptr(COL) \
652 , boost_foreach_argument_dependent_lookup_hack_value)
654 // returns true_* if the type is a lightweight proxy (and is not noncopyable)
655 #define BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL) \
656 boost::foreach_detail_::and_( \
657 boost::foreach_detail_::not_(BOOST_FOREACH_IS_NONCOPYABLE(COL)) \
658 , boost_foreach_is_lightweight_proxy( \
659 boost::foreach_detail_::to_ptr(COL) \
660 , boost_foreach_argument_dependent_lookup_hack_value))
662 #ifdef BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION
663 ///////////////////////////////////////////////////////////////////////////////
664 // R-values and const R-values supported here with zero runtime overhead
665 ///////////////////////////////////////////////////////////////////////////////
667 // No variable is needed to track the rvalue-ness of the collection expression
668 # define BOOST_FOREACH_PREAMBLE() \
669 /**/
671 // Evaluate the collection expression
672 # define BOOST_FOREACH_EVALUATE(COL) \
673 (COL)
675 # define BOOST_FOREACH_SHOULD_COPY(COL) \
676 (true ? 0 : boost::foreach_detail_::or_( \
677 BOOST_FOREACH_IS_RVALUE(COL) \
678 , BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL)))
680 #elif defined(BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION)
681 ///////////////////////////////////////////////////////////////////////////////
682 // R-values and const R-values supported here
683 ///////////////////////////////////////////////////////////////////////////////
685 // Declare a variable to track the rvalue-ness of the collection expression
686 # define BOOST_FOREACH_PREAMBLE() \
687 if (bool _foreach_is_rvalue = false) {} else
689 // Evaluate the collection expression, and detect if it is an lvalue or and rvalue
690 # define BOOST_FOREACH_EVALUATE(COL) \
691 (true ? boost::foreach_detail_::make_probe((COL), _foreach_is_rvalue) : (COL))
693 // The rvalue/lvalue-ness of the collection expression is determined dynamically, unless
694 // type type is an array or is noncopyable or is non-const, in which case we know it's an lvalue.
695 // If the type happens to be a lightweight proxy, always make a copy.
696 # define BOOST_FOREACH_SHOULD_COPY(COL) \
697 (boost::foreach_detail_::should_copy_impl( \
698 true ? 0 : boost::foreach_detail_::or_( \
699 boost::foreach_detail_::is_array_(COL) \
700 , BOOST_FOREACH_IS_NONCOPYABLE(COL) \
701 , boost::foreach_detail_::not_(boost::foreach_detail_::is_const_(COL))) \
702 , true ? 0 : BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL) \
703 , &_foreach_is_rvalue))
705 #elif !defined(BOOST_FOREACH_NO_RVALUE_DETECTION)
706 ///////////////////////////////////////////////////////////////////////////////
707 // R-values supported here, const R-values NOT supported here
708 ///////////////////////////////////////////////////////////////////////////////
710 // No variable is needed to track the rvalue-ness of the collection expression
711 # define BOOST_FOREACH_PREAMBLE() \
712 /**/
714 // Evaluate the collection expression
715 # define BOOST_FOREACH_EVALUATE(COL) \
716 (COL)
718 // Determine whether the collection expression is an lvalue or an rvalue.
719 // NOTE: this gets the answer wrong for const rvalues.
720 # define BOOST_FOREACH_SHOULD_COPY(COL) \
721 (true ? 0 : boost::foreach_detail_::or_( \
722 boost::foreach_detail_::is_rvalue_((COL), 0) \
723 , BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL)))
725 #else
726 ///////////////////////////////////////////////////////////////////////////////
727 // R-values NOT supported here
728 ///////////////////////////////////////////////////////////////////////////////
730 // No variable is needed to track the rvalue-ness of the collection expression
731 # define BOOST_FOREACH_PREAMBLE() \
732 /**/
734 // Evaluate the collection expression
735 # define BOOST_FOREACH_EVALUATE(COL) \
736 (COL)
738 // Can't use rvalues with BOOST_FOREACH (unless they are lightweight proxies)
739 # define BOOST_FOREACH_SHOULD_COPY(COL) \
740 (true ? 0 : BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL))
742 #endif
744 #define BOOST_FOREACH_CONTAIN(COL) \
745 boost::foreach_detail_::contain( \
746 BOOST_FOREACH_EVALUATE(COL) \
747 , BOOST_FOREACH_SHOULD_COPY(COL))
749 #define BOOST_FOREACH_BEGIN(COL) \
750 boost::foreach_detail_::begin( \
751 _foreach_col \
752 , BOOST_FOREACH_TYPEOF(COL) \
753 , BOOST_FOREACH_SHOULD_COPY(COL))
755 #define BOOST_FOREACH_END(COL) \
756 boost::foreach_detail_::end( \
757 _foreach_col \
758 , BOOST_FOREACH_TYPEOF(COL) \
759 , BOOST_FOREACH_SHOULD_COPY(COL))
761 #define BOOST_FOREACH_DONE(COL) \
762 boost::foreach_detail_::done( \
763 _foreach_cur \
764 , _foreach_end \
765 , BOOST_FOREACH_TYPEOF(COL))
767 #define BOOST_FOREACH_NEXT(COL) \
768 boost::foreach_detail_::next( \
769 _foreach_cur \
770 , BOOST_FOREACH_TYPEOF(COL))
772 #define BOOST_FOREACH_DEREF(COL) \
773 boost::foreach_detail_::deref( \
774 _foreach_cur \
775 , BOOST_FOREACH_TYPEOF(COL))
777 ///////////////////////////////////////////////////////////////////////////////
778 // BOOST_FOREACH
780 // For iterating over collections. Collections can be
781 // arrays, null-terminated strings, or STL containers.
782 // The loop variable can be a value or reference. For
783 // example:
785 // std::list<int> int_list(/*stuff*/);
786 // BOOST_FOREACH(int &i, int_list)
787 // {
788 // /*
789 // * loop body goes here.
790 // * i is a reference to the int in int_list.
791 // */
792 // }
794 // Alternately, you can declare the loop variable first,
795 // so you can access it after the loop finishes. Obviously,
796 // if you do it this way, then the loop variable cannot be
797 // a reference.
799 // int i;
800 // BOOST_FOREACH(i, int_list)
801 // { ... }
803 #define BOOST_FOREACH(VAR, COL) \
804 BOOST_FOREACH_PREAMBLE() \
805 if (boost::foreach_detail_::auto_any_t _foreach_col = BOOST_FOREACH_CONTAIN(COL)) {} else \
806 if (boost::foreach_detail_::auto_any_t _foreach_cur = BOOST_FOREACH_BEGIN(COL)) {} else \
807 if (boost::foreach_detail_::auto_any_t _foreach_end = BOOST_FOREACH_END(COL)) {} else \
808 for (bool _foreach_continue = true; \
809 _foreach_continue && !BOOST_FOREACH_DONE(COL); \
810 _foreach_continue ? BOOST_FOREACH_NEXT(COL) : (void)0) \
811 if (boost::foreach_detail_::set_false(_foreach_continue)) {} else \
812 for (VAR = BOOST_FOREACH_DEREF(COL); !_foreach_continue; _foreach_continue = true)
814 #endif