rcolyer.net
RC Lib  Version 202403231100
PtrSharedCommon.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////
2 //
3 // RC Library, (c) 2011-2014, Ryan A. Colyer
4 //
5 /// \file PtrSharedCommon.h
6 /// Common components for the shared *Ptr.h files. Do not include directly.
7 /////////////////////////////////////////////////////////////////////
8 
9  public:
10 
11  /// Returns a direct reference to the enclosed pointer.
12  /** The reference is read/write, so it can be used as a function
13  * parameter which updates the value of this pointer.
14  * @return A reference to the enclosed pointer.
15  */
16  inline T* Raw() const { return helper->t_ptr; }
17 
18  protected:
19  /// @cond PROTECTED
20 
21  /// This class gets tossed along with assignments, and maintains
22  /// a reference count.
23  class PtrHelper {
24  public:
25 
26  inline PtrHelper(T *t_ptr, bool auto_delete)
27  : t_ptr (t_ptr),
28  cnt (1),
29  auto_delete(auto_delete) {
30  }
31 
32 
33  inline void Add() {
34  ++cnt;
35  }
36 
37  inline void Del() {
38  u64 cur_cnt = --cnt;
39  if (cur_cnt == 0) {
40  if (auto_delete) {
41  Delete();
42  }
43  delete(this);
44  }
45  }
46 
47  inline void Delete() {
48  T* loc_t_ptr;
49 #ifdef CPP11
50  loc_t_ptr = t_ptr.exchange(NULL);
51 #else
52  loc_t_ptr = t_ptr;
53  t_ptr = NULL;
54 #endif
55  if (loc_t_ptr != NULL) {
56  delete(loc_t_ptr);
57  }
58  }
59 
60  inline void Revoke() {
61  t_ptr = NULL;
62  }
63 
64 #ifdef CPP11
65  std::atomic<T*> t_ptr;
66  std::atomic<u64> cnt;
67 #else
68  T *t_ptr;
69  u64 cnt;
70 #endif
71  bool auto_delete;
72  };
73  /// @endcond
74 
T * Raw() const
Returns a direct reference to the enclosed pointer.
Definition: PtrSharedCommon.h:16
uint64_t u64
64-bit unsigned integer.
Definition: Types.h:31
email address
— (c) 2015