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