Update README.md
[nemesis.git] / Platform / ForEach.h
1 /*
2  Copyright (c) 2008 TrueCrypt Developers Association. All rights reserved.
3
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
6  packages.
7 */
8
9 #ifndef TC_HEADER_Platform_ForEach
10 #define TC_HEADER_Platform_ForEach
11
12 namespace TrueCrypt
13 {
14         class ForEach
15         {
16         public:
17                 struct Container
18                 {
19                         Container () : InnerContinue (true), InnerEndCondition (false) { }
20                         virtual ~Container () { }
21
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;
26
27                         mutable bool InnerContinue;
28                         mutable bool InnerEndCondition;
29                 };
30
31         protected:
32                 template <class T>
33                 struct ContainerForward : Container
34                 {
35                         ContainerForward (const T &container)
36                                 : ContainerCopy (container), EndIterator (ContainerCopy.end()), Iterator (ContainerCopy.begin()) { }
37
38                         virtual bool IsNotEnd () const { bool r = InnerContinue && Iterator != EndIterator; InnerContinue = false; return r; }
39                         virtual void Next () const { ++Iterator; }
40
41                         const T ContainerCopy;  // Support for temporary objects
42                         typename T::const_iterator EndIterator;
43                         mutable typename T::const_iterator Iterator;
44
45                 private:
46                         ContainerForward &operator= (const ContainerForward &);
47                 };
48
49                 template <class T>
50                 struct ContainerReverse : Container
51                 {
52                         ContainerReverse (const T &container)
53                                 : ContainerCopy (container), EndIterator (ContainerCopy.rend()), Iterator (ContainerCopy.rbegin()) { }
54
55                         virtual bool IsNotEnd () const { bool r = InnerContinue && Iterator != EndIterator; InnerContinue = false; return r; }
56                         virtual void Next () const { ++Iterator; }
57
58                         const T ContainerCopy;
59                         typename T::const_reverse_iterator EndIterator;
60                         mutable typename T::const_reverse_iterator Iterator;
61                         
62                 private:
63                         ContainerReverse &operator= (const ContainerReverse &);
64                 };
65
66         public:
67                 template <class T>
68                 static ContainerForward <T> GetContainerForward (const T &container)
69                 {
70                         return ContainerForward <T> (container);
71                 }
72
73                 template <class T>
74                 static ContainerReverse <T> GetContainerReverse (const T &container)
75                 {
76                         return ContainerReverse <T> (container);
77                 }
78
79         protected:
80                 template <class T>
81                 struct TypeWrapper { };
82
83         public:
84                 template <class T>
85                 static TypeWrapper <T> ToTypeWrapper (const T &x) { return TypeWrapper <T> (); }
86
87                 struct TypeWrapperDummy
88                 {
89                         template <class T>
90                         operator TypeWrapper <T> () const { return TypeWrapper <T> (); }
91                 };
92
93                 template <class T>
94                 static const ContainerForward <T> &GetContainerForward (const Container &forEachContainer, const TypeWrapper <T> &)
95                 {
96                         return static_cast <const ContainerForward <T> &> (forEachContainer);
97                 }
98
99                 template <class T>
100                 static const ContainerReverse <T> &GetContainerReverse (const Container &forEachContainer, const TypeWrapper <T> &)
101                 {
102                         return static_cast <const ContainerReverse <T> &> (forEachContainer);
103                 }
104         };
105 }
106
107
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())
111
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)
116
117
118 #endif // TC_HEADER_Platform_ForEach
This page took 0.021041 seconds and 3 git commands to generate. Download a nemesis OSX (sierra+high sierra, tested/working) binary, with fuse-ext3 via e2fsprogs, at this link. application and installer are signed by screwjack, llc. must install fuse with macFUSE layer first.