"Fossies" - the Fresh Open Source Software Archive

Member "src/Platform/ForEach.h" (10 Oct 2018, 4174 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ForEach.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2  Derived from source code of TrueCrypt 7.1a, which is
    3  Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
    4  by the TrueCrypt License 3.0.
    5 
    6  Modifications and additions to the original source code (contained in this file)
    7  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
    8  and are governed by the Apache License 2.0 the full text of which is
    9  contained in the file License.txt included in VeraCrypt binary and source
   10  code distribution packages.
   11 */
   12 
   13 #ifndef TC_HEADER_Platform_ForEach
   14 #define TC_HEADER_Platform_ForEach
   15 
   16 namespace VeraCrypt
   17 {
   18     class ForEach
   19     {
   20     public:
   21         struct Container
   22         {
   23             Container () : InnerContinue (true), InnerEndCondition (false) { }
   24             virtual ~Container () { }
   25 
   26             void Continue () const { InnerContinue = true; }
   27             bool InnerIsNotEnd () const { return InnerEndCondition = !InnerEndCondition; }
   28             virtual bool IsNotEnd () const = 0;
   29             virtual void Next () const = 0;
   30 
   31             mutable bool InnerContinue;
   32             mutable bool InnerEndCondition;
   33         };
   34 
   35     protected:
   36         template <class T>
   37         struct ContainerForward : Container
   38         {
   39             ContainerForward (const T &container)
   40                 : ContainerCopy (container), EndIterator (ContainerCopy.end()), Iterator (ContainerCopy.begin()) { }
   41 
   42             virtual bool IsNotEnd () const { bool r = InnerContinue && Iterator != EndIterator; InnerContinue = false; return r; }
   43             virtual void Next () const { ++Iterator; }
   44 
   45             const T ContainerCopy;  // Support for temporary objects
   46             typename T::const_iterator EndIterator;
   47             mutable typename T::const_iterator Iterator;
   48 
   49         private:
   50             ContainerForward &operator= (const ContainerForward &);
   51         };
   52 
   53         template <class T>
   54         struct ContainerReverse : Container
   55         {
   56             ContainerReverse (const T &container)
   57                 : ContainerCopy (container), EndIterator (ContainerCopy.rend()), Iterator (ContainerCopy.rbegin()) { }
   58 
   59             virtual bool IsNotEnd () const { bool r = InnerContinue && Iterator != EndIterator; InnerContinue = false; return r; }
   60             virtual void Next () const { ++Iterator; }
   61 
   62             const T ContainerCopy;
   63             typename T::const_reverse_iterator EndIterator;
   64             mutable typename T::const_reverse_iterator Iterator;
   65 
   66         private:
   67             ContainerReverse &operator= (const ContainerReverse &);
   68         };
   69 
   70     public:
   71         template <class T>
   72         static ContainerForward <T> GetContainerForward (const T &container)
   73         {
   74             return ContainerForward <T> (container);
   75         }
   76 
   77         template <class T>
   78         static ContainerReverse <T> GetContainerReverse (const T &container)
   79         {
   80             return ContainerReverse <T> (container);
   81         }
   82 
   83     protected:
   84         template <class T>
   85         struct TypeWrapper { };
   86 
   87     public:
   88         template <class T>
   89         static TypeWrapper <T> ToTypeWrapper (const T &x) { return TypeWrapper <T> (); }
   90 
   91         struct TypeWrapperDummy
   92         {
   93             template <class T>
   94             operator TypeWrapper <T> () const { return TypeWrapper <T> (); }
   95         };
   96 
   97         template <class T>
   98         static const ContainerForward <T> &GetContainerForward (const Container &forEachContainer, const TypeWrapper <T> &)
   99         {
  100             return static_cast <const ContainerForward <T> &> (forEachContainer);
  101         }
  102 
  103         template <class T>
  104         static const ContainerReverse <T> &GetContainerReverse (const Container &forEachContainer, const TypeWrapper <T> &)
  105         {
  106             return static_cast <const ContainerReverse <T> &> (forEachContainer);
  107         }
  108     };
  109 }
  110 
  111 
  112 #define FOREACH_TEMPLATE(dereference,listType,variable,listInstance) \
  113     for (const ForEach::Container &forEachContainer = ForEach::GetContainer##listType (listInstance); forEachContainer.IsNotEnd(); forEachContainer.Next()) \
  114         for (variable = dereference(ForEach::GetContainer##listType (forEachContainer, (true ? ForEach::TypeWrapperDummy() : ForEach::ToTypeWrapper (listInstance))).Iterator); forEachContainer.InnerIsNotEnd(); forEachContainer.Continue())
  115 
  116 #define foreach(variable,listInstance) FOREACH_TEMPLATE(*, Forward, variable, listInstance)
  117 #define foreach_ref(variable,listInstance) FOREACH_TEMPLATE(**, Forward, variable, listInstance)
  118 #define foreach_reverse(variable,listInstance) FOREACH_TEMPLATE(*, Reverse, variable, listInstance)
  119 #define foreach_reverse_ref(variable,listInstance) FOREACH_TEMPLATE(**, Reverse, variable, listInstance)
  120 
  121 
  122 #endif // TC_HEADER_Platform_ForEach