Samchon Framework for CPP  1.0.0
SmartPointer.hpp
1 #pragma once
2 #include <map>
3 #include <mutex>
4 
5 namespace samchon
6 {
21  template<typename _Ty>
23  {
24  private:
28  static std::map<_Ty*, size_t> useCountMap;
29 
33  static std::mutex mtx;
34 
35  private:
39  _Ty* ptr;
40 
41  public:
42  /* --------------------------------------------------------------
43  CONSTRUCTORS
44  -------------------------------------------------------------- */
50  {
51  ptr = nullptr;
52  };
53 
60  explicit SmartPointer(const _Ty* ptr)
61  : SmartPointer()
62  {
63  reset(ptr);
64  };
65 
73  SmartPointer(const SmartPointer &smartPointer)
74  : SmartPointer(smartPointer.ptr) {};
75 
84  SmartPointer(SmartPointer &&smartPointer)
85  {
86  ptr = smartPointer.ptr;
87  smartPointer.ptr = nullptr;
88  }
89 
106  {
107  std::lock_guard<std::mutex> lockGuard(mtx);
108  destruct(ptr);
109  };
110 
111  public:
112  /* --------------------------------------------------------------
113  SETTERS
114  -------------------------------------------------------------- */
128  void reset(const _Ty* ptr)
129  {
130  std::lock_guard<std::mutex> lockGuard(mtx);
131  if (this->ptr == ptr)
132  return;
133 
134  construct((_Ty*)ptr);
135  destruct(this->ptr);
136 
137  this->ptr = (_Ty*)ptr;
138  };
139 
140  /* --------------------------------------------------------------
141  GETTERS
142  -------------------------------------------------------------- */
154  inline auto get() const -> _Ty*
155  {
156  return ptr;
157  };
158 
170  auto operator->() const -> _Ty*
171  {
172  return get();
173  };
174 
184  auto operator*() const -> _Ty&
185  {
186  return *get();
187  };
188 
189  /* --------------------------------------------------------------
190  STATISTICS
191  -------------------------------------------------------------- */
192  private:
193  static void construct(_Ty *ptr)
194  {
195  if (ptr == nullptr)
196  return;
197 
198  if (useCountMap.find(ptr) != useCountMap.end())
199  useCountMap[ptr]++;
200  else
201  useCountMap[ptr] = 1;
202  }
203  static void destruct(_Ty *ptr)
204  {
205  if (ptr == nullptr)
206  return;
207 
208  if (useCountMap.find(ptr) != useCountMap.end())
209  if (--useCountMap[ptr] == 0)
210  {
211  useCountMap.erase(ptr);
212  delete ptr;
213  }
214  };
215  };
216 
217  template<typename _Ty> std::map<_Ty*, size_t> SmartPointer<_Ty>::useCountMap;
218  template<typename _Ty> std::mutex SmartPointer<_Ty>::mtx;
219 };
SmartPointer(const _Ty *ptr)
Constrct from pointer.
auto operator->() const -> _Ty *
Dereference object membr.
SmartPointer(SmartPointer &&smartPointer)
Move constructor.
Global shared pointer .
void reset(const _Ty *ptr)
Reset pointer.
static std::mutex mtx
Mutex assigned to useCountMap.
~SmartPointer()
Destroy SmartPointer.
SmartPointer(const SmartPointer &smartPointer)
Copy Constructor.
SmartPointer()
Default Constructor.
_Ty * ptr
A pointer managed by SmartPointer.
auto operator*() const -> _Ty &
Dereference object.
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
static std::map< _Ty *, size_t > useCountMap
Map of use count of each pointer.