"Fossies" - the Fresh Open Source Software Archive

Member "src/Platform/SharedPtr.h" (10 Oct 2018, 2899 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 "SharedPtr.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_SharedPtr
   14 #define TC_HEADER_Platform_SharedPtr
   15 
   16 #include <stdexcept>
   17 #include "SharedVal.h"
   18 
   19 #ifdef nullptr
   20 
   21 namespace VeraCrypt
   22 {
   23     template <class T>
   24     class SharedPtr
   25     {
   26     public:
   27         explicit SharedPtr ()
   28             : Pointer (nullptr), UseCount (nullptr) { }
   29 
   30         explicit SharedPtr (T *pointer)
   31             : Pointer (pointer), UseCount (new SharedVal <uint64> (1)) { }
   32 
   33         SharedPtr (const SharedPtr &source)
   34         {
   35             CopyFrom (source);
   36         }
   37 
   38         ~SharedPtr ()
   39         {
   40             Release();
   41         }
   42 
   43         SharedPtr &operator= (const SharedPtr &source)
   44         {
   45             if (&source == this)
   46                 return *this;
   47 
   48             Release();
   49             CopyFrom (source);
   50             return *this;
   51         }
   52 
   53         bool operator == (const SharedPtr &other)
   54         {
   55             return get() == other.get();
   56         }
   57 
   58         bool operator != (const SharedPtr &other)
   59         {
   60             return get() != other.get();
   61         }
   62 
   63         T &operator* () const
   64         {
   65 #ifdef DEBUG
   66             if (Pointer == nullptr)
   67                 throw std::runtime_error (SRC_POS);
   68 #endif
   69             return *Pointer;
   70         }
   71 
   72         T *operator-> () const
   73         {
   74 #ifdef DEBUG
   75             if (Pointer == nullptr)
   76                 throw std::runtime_error (SRC_POS);
   77 #endif
   78             return Pointer;
   79         }
   80 
   81         operator bool () const
   82         {
   83             return Pointer != nullptr;
   84         }
   85 
   86         T *get () const
   87         {
   88             return Pointer;
   89         }
   90 
   91         void reset ()
   92         {
   93             Release();
   94         }
   95 
   96         void reset (T *pointer)
   97         {
   98             *this = SharedPtr (pointer);
   99         }
  100 
  101         uint64 use_count () const
  102         {
  103             if (!UseCount)
  104                 return 0;
  105 
  106             return *UseCount;
  107         }
  108 
  109     protected:
  110         void CopyFrom (const SharedPtr &source)
  111         {
  112             Pointer = source.Pointer;
  113             UseCount = source.UseCount;
  114 
  115             if (UseCount)
  116                 UseCount->Increment();
  117         }
  118 
  119         void Release ()
  120         {
  121             if (UseCount != nullptr)
  122             {
  123                 if (UseCount->Decrement() == 0)
  124                 {
  125                     if (Pointer != nullptr)
  126                         delete Pointer;
  127                     delete UseCount;
  128                 }
  129 
  130                 Pointer = nullptr;
  131                 UseCount = nullptr;
  132             }
  133         }
  134 
  135         T *Pointer;
  136         SharedVal <uint64> *UseCount;
  137     };
  138 
  139 #ifdef shared_ptr
  140 #undef shared_ptr
  141 #endif
  142 #define shared_ptr VeraCrypt::SharedPtr
  143 
  144 #ifdef make_shared
  145 #undef make_shared
  146 #endif
  147 
  148     template <class T> shared_ptr <T> make_shared ()
  149     {
  150         return shared_ptr <T> (new T ());
  151     }
  152 
  153     template <class T, class A> shared_ptr <T> make_shared (const A &arg)
  154     {
  155         return shared_ptr <T> (new T (arg));
  156     }
  157 
  158 #define make_shared VeraCrypt::make_shared
  159 
  160 }
  161 
  162 #endif // nullptr
  163 
  164 #define make_shared_auto(typeName,instanceName) shared_ptr <typeName> instanceName (new typeName ())
  165 
  166 #endif // TC_HEADER_Platform_SharedPtr