2 Copyright (c) 2008 TrueCrypt Developers Association. All rights reserved.
4 Governed by the TrueCrypt License 3.0 the full text of which is contained in
5 the file License.txt included in TrueCrypt binary and source code distribution
9 #ifndef TC_HEADER_Platform_ForEach
10 #define TC_HEADER_Platform_ForEach
19 Container () : InnerContinue (true), InnerEndCondition (false) { }
20 virtual ~Container () { }
22 void Continue () const { InnerContinue = true; }
23 bool InnerIsNotEnd () const { return InnerEndCondition = !InnerEndCondition; }
24 virtual bool IsNotEnd () const = 0;
25 virtual void Next () const = 0;
27 mutable bool InnerContinue;
28 mutable bool InnerEndCondition;
33 struct ContainerForward : Container
35 ContainerForward (const T &container)
36 : ContainerCopy (container), EndIterator (ContainerCopy.end()), Iterator (ContainerCopy.begin()) { }
38 virtual bool IsNotEnd () const { bool r = InnerContinue && Iterator != EndIterator; InnerContinue = false; return r; }
39 virtual void Next () const { ++Iterator; }
41 const T ContainerCopy; // Support for temporary objects
42 typename T::const_iterator EndIterator;
43 mutable typename T::const_iterator Iterator;
46 ContainerForward &operator= (const ContainerForward &);
50 struct ContainerReverse : Container
52 ContainerReverse (const T &container)
53 : ContainerCopy (container), EndIterator (ContainerCopy.rend()), Iterator (ContainerCopy.rbegin()) { }
55 virtual bool IsNotEnd () const { bool r = InnerContinue && Iterator != EndIterator; InnerContinue = false; return r; }
56 virtual void Next () const { ++Iterator; }
58 const T ContainerCopy;
59 typename T::const_reverse_iterator EndIterator;
60 mutable typename T::const_reverse_iterator Iterator;
63 ContainerReverse &operator= (const ContainerReverse &);
68 static ContainerForward <T> GetContainerForward (const T &container)
70 return ContainerForward <T> (container);
74 static ContainerReverse <T> GetContainerReverse (const T &container)
76 return ContainerReverse <T> (container);
81 struct TypeWrapper { };
85 static TypeWrapper <T> ToTypeWrapper (const T &x) { return TypeWrapper <T> (); }
87 struct TypeWrapperDummy
90 operator TypeWrapper <T> () const { return TypeWrapper <T> (); }
94 static const ContainerForward <T> &GetContainerForward (const Container &forEachContainer, const TypeWrapper <T> &)
96 return static_cast <const ContainerForward <T> &> (forEachContainer);
100 static const ContainerReverse <T> &GetContainerReverse (const Container &forEachContainer, const TypeWrapper <T> &)
102 return static_cast <const ContainerReverse <T> &> (forEachContainer);
108 #define FOREACH_TEMPLATE(dereference,listType,variable,listInstance) \
109 for (const ForEach::Container &forEachContainer = ForEach::GetContainer##listType (listInstance); forEachContainer.IsNotEnd(); forEachContainer.Next()) \
110 for (variable = dereference(ForEach::GetContainer##listType (forEachContainer, (true ? ForEach::TypeWrapperDummy() : ForEach::ToTypeWrapper (listInstance))).Iterator); forEachContainer.InnerIsNotEnd(); forEachContainer.Continue())
112 #define foreach(variable,listInstance) FOREACH_TEMPLATE(*, Forward, variable, listInstance)
113 #define foreach_ref(variable,listInstance) FOREACH_TEMPLATE(**, Forward, variable, listInstance)
114 #define foreach_reverse(variable,listInstance) FOREACH_TEMPLATE(*, Reverse, variable, listInstance)
115 #define foreach_reverse_ref(variable,listInstance) FOREACH_TEMPLATE(**, Reverse, variable, listInstance)
118 #endif // TC_HEADER_Platform_ForEach