4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Matthias Ettrich
9 * Full author contact details are available in file CREDITS.
16 // Code stolen from Q_FOREACH, augmented to use a reference to the
17 // original container instead of a copy. Copies are cheap (if not
18 // mutated) for Qt's containers due to copy-on-write. The are less
19 // cheap for Standard containers, that's why the modification.
20 // Drawback is that we can't use temporary containers as they
21 // will be destroyed before the loop is finished. So always write
23 // Container const & container = functionReturningTemporaryOrReference()
24 // foreach (ContainerItem const & item, container) {
28 // to extend the lifetime of the reference.
30 #if defined(Q_CC_GNU) && !defined(Q_CC_INTEL)
32 /* make use of typeof-extension */
34 class ForeachContainer {
36 inline ForeachContainer(const T & t) : c(t), brk(0), i(c.begin()), e(c.end()) { }
39 typename T::const_iterator i, e;
42 #define foreach(variable, container) \
43 for (ForeachContainer<__typeof__(container)> _container_(container); \
44 !_container_.brk && _container_.i != _container_.e; \
45 __extension__ ({ ++_container_.brk; ++_container_.i; })) \
46 for (variable = *_container_.i;; __extension__ ({--_container_.brk; break;}))
50 struct ForeachContainerBase {};
53 class ForeachContainer : public ForeachContainerBase {
55 inline ForeachContainer(const T& t): c(t), brk(0), i(c.begin()), e(c.end()){};
58 mutable typename T::const_iterator i, e;
59 inline bool condition() const { return (!brk++ && i != e); }
62 template <typename T> inline T *foreachPointer(const T &) { return 0; }
64 template <typename T> inline ForeachContainer<T> foreachContainerNew(const T& t)
65 { return ForeachContainer<T>(t); }
68 inline const ForeachContainer<T> *foreachContainer(const ForeachContainerBase *base, const T *)
69 { return static_cast<const ForeachContainer<T> *>(base); }
71 #define foreach(variable, container) \
72 for (const ForeachContainerBase &_container_ = foreachContainerNew(container); \
73 foreachContainer(&_container_, true ? 0 : foreachPointer(container))->condition(); \
74 ++foreachContainer(&_container_, true ? 0 : foreachPointer(container))->i) \
75 for (variable = *foreachContainer(&_container_, true ? 0 : foreachPointer(container))->i; \
76 foreachContainer(&_container_, true ? 0 : foreachPointer(container))->brk; \
77 --foreachContainer(&_container_, true ? 0 : foreachPointer(container))->brk)