SPH
SharedToken.h
Go to the documentation of this file.
1 #pragma once
2 
4 
9 
11 
12 namespace Detail {
13 
15 public:
16  TokenBlock() = default;
17 
18  INLINE virtual void* getPtr() override {
19  return nullptr;
20  }
21 
22  virtual void deletePtr() override {
23  // no-op
24  }
25 };
26 
27 } // namespace Detail
28 
29 class SharedToken {
30  friend class WeakToken;
31 
32 private:
34 
35 public:
37  : block(alignedNew<Detail::TokenBlock>()) {}
38 
39  SharedToken(std::nullptr_t)
40  : block(nullptr) {}
41 
42  SharedToken(const SharedToken& other)
43  : block(nullptr) {
44  *this = other;
45  }
46 
47  template <typename T>
48  SharedToken(const SharedPtr<T>& ptr) {
49  if (ptr.block) {
50  block = ptr.block;
51  block->increaseUseCnt();
52  block->increaseWeakCnt();
53  } else {
54  block = nullptr;
55  }
56  }
57 
59  this->reset();
60  if (other.block) {
61  block = other.block;
62  block->increaseUseCnt();
63  block->increaseWeakCnt();
64  } else {
65  block = nullptr;
66  }
67  return *this;
68  }
69 
71  this->reset();
72  }
73 
74  void reset() {
75  if (block) {
76  block->decreaseUseCnt();
77  block->decreaseWeakCnt();
78  block = nullptr;
79  }
80  }
81 
82 
83  INLINE explicit operator bool() const {
84  return block != nullptr;
85  }
86 
87  INLINE bool operator!() const {
88  return !this->operator bool();
89  }
90 };
91 
92 
93 class WeakToken {
94 private:
96 
97 public:
99  : block(nullptr) {}
100 
101  WeakToken(std::nullptr_t)
102  : block(nullptr) {}
103 
104  WeakToken(const WeakToken& other)
105  : block(other.block) {
106  if (block) {
107  block->increaseWeakCnt();
108  }
109  }
110 
111  WeakToken(const SharedToken& other)
112  : block(other.block) {
113  if (block) {
114  block->increaseWeakCnt();
115  }
116  }
117 
118  template <typename T>
120  : block(ptr.block) {
121  if (block) {
122  block->increaseWeakCnt();
123  }
124  }
125 
126  WeakToken& operator=(const WeakToken& other) {
127  block = other.block;
128  if (block) {
129  block->increaseWeakCnt();
130  }
131  return *this;
132  }
133 
134  SharedToken lock() const {
135  if (block && block->increaseUseCntIfNonzero()) {
136  SharedToken token;
137  token.block = block;
138  block->increaseWeakCnt();
139  return token;
140  } else {
141  return nullptr;
142  }
143  }
144 };
145 
NAMESPACE_SPH_BEGIN INLINE T * alignedNew(TArgs &&... args)
Creates a new object of type T on heap, using aligned allocation.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
INLINE void decreaseUseCnt()
Definition: SharedPtr.h:50
INLINE int increaseWeakCnt()
Definition: SharedPtr.h:35
INLINE void decreaseWeakCnt()
Definition: SharedPtr.h:58
INLINE int increaseUseCnt()
Definition: SharedPtr.h:25
INLINE bool increaseUseCntIfNonzero()
Definition: SharedPtr.h:41
virtual INLINE void * getPtr() override
Definition: SharedToken.h:18
TokenBlock()=default
virtual void deletePtr() override
Definition: SharedToken.h:22
Detail::ControlBlockHolder * block
Definition: SharedPtr.h:110
SharedToken(const SharedToken &other)
Definition: SharedToken.h:42
void reset()
Definition: SharedToken.h:74
SharedToken(const SharedPtr< T > &ptr)
Definition: SharedToken.h:48
SharedToken(std::nullptr_t)
Definition: SharedToken.h:39
INLINE bool operator!() const
Definition: SharedToken.h:87
SharedToken & operator=(const SharedToken &other)
Definition: SharedToken.h:58
WeakToken(const SharedPtr< T > &ptr)
Definition: SharedToken.h:119
WeakToken(const SharedToken &other)
Definition: SharedToken.h:111
SharedToken lock() const
Definition: SharedToken.h:134
WeakToken(const WeakToken &other)
Definition: SharedToken.h:104
WeakToken & operator=(const WeakToken &other)
Definition: SharedToken.h:126
WeakToken(std::nullptr_t)
Definition: SharedToken.h:101