|
|
1.1 root 1: /* 1.1.1.6 ! root 2: Copyright (c) 2008 TrueCrypt Developers Association. All rights reserved. 1.1 root 3: 1.1.1.6 ! root 4: Governed by the TrueCrypt License 2.8 the full text of which is contained in ! 5: the file License.txt included in TrueCrypt binary and source code distribution ! 6: packages. 1.1 root 7: */ 8: 9: #ifndef TC_HEADER_Platform_SharedPtr 10: #define TC_HEADER_Platform_SharedPtr 11: 12: #include <stdexcept> 13: #include "SharedVal.h" 14: 15: namespace TrueCrypt 16: { 17: template <class T> 18: class SharedPtr 19: { 20: public: 21: explicit SharedPtr () 22: : Pointer (nullptr), UseCount (nullptr) { } 23: 24: explicit SharedPtr (T *pointer) 25: : Pointer (pointer), UseCount (new SharedVal <uint64> (1)) { } 26: 27: SharedPtr (const SharedPtr &source) 28: { 29: CopyFrom (source); 30: } 31: 32: ~SharedPtr () 33: { 34: Release(); 35: } 36: 37: SharedPtr &operator= (const SharedPtr &source) 38: { 39: if (&source == this) 40: return *this; 41: 42: Release(); 43: CopyFrom (source); 44: return *this; 45: } 46: 47: bool operator == (const SharedPtr &other) 48: { 49: return get() == other.get(); 50: } 51: 52: bool operator != (const SharedPtr &other) 53: { 54: return get() != other.get(); 55: } 56: 57: T &operator* () const 58: { 59: #ifdef DEBUG 60: if (Pointer == nullptr) 61: throw std::runtime_error (SRC_POS); 62: #endif 63: return *Pointer; 64: } 65: 66: T *operator-> () const 67: { 68: #ifdef DEBUG 69: if (Pointer == nullptr) 70: throw std::runtime_error (SRC_POS); 71: #endif 72: return Pointer; 73: } 74: 75: operator bool () const 76: { 77: return Pointer != nullptr; 78: } 79: 80: T *get () const 81: { 82: return Pointer; 83: } 84: 85: void reset () 86: { 87: Release(); 88: } 89: 90: void reset (T *pointer) 91: { 92: *this = SharedPtr (pointer); 93: } 94: 95: uint64 use_count () const 96: { 97: if (!UseCount) 98: return 0; 99: 100: return *UseCount; 101: } 102: 103: protected: 104: void CopyFrom (const SharedPtr &source) 105: { 106: Pointer = source.Pointer; 107: UseCount = source.UseCount; 108: 109: if (UseCount) 110: UseCount->Increment(); 111: } 112: 113: void Release () 114: { 115: if (UseCount != nullptr) 116: { 117: if (UseCount->Decrement() == 0) 118: { 119: if (Pointer != nullptr) 120: delete Pointer; 121: delete UseCount; 122: } 123: 124: Pointer = nullptr; 125: UseCount = nullptr; 126: } 127: } 128: 129: T *Pointer; 130: SharedVal <uint64> *UseCount; 131: }; 132: 133: #ifdef shared_ptr 134: #undef shared_ptr 135: #endif 136: #define shared_ptr TrueCrypt::SharedPtr 137: 138: #define make_shared_auto(typeName,instanceName) shared_ptr <typeName> instanceName (new typeName ()) 139: 140: template <class T> shared_ptr <T> make_shared () 141: { 142: return shared_ptr <T> (new T ()); 143: } 144: 145: template <class T, class A> shared_ptr <T> make_shared (const A &arg) 146: { 147: return shared_ptr <T> (new T (arg)); 148: } 149: } 150: 151: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.