Samchon Framework for CPP  1.0.0
SmartPointer.hpp
1 #pragma once
2 
3 #include <map>
4 #include <mutex>
5 
6 namespace samchon
7 {
22  template<typename _Ty>
24  {
25  private:
29  static std::map<_Ty*, size_t> useCountMap;
30 
34  static std::mutex mtx;
35 
36  private:
40  _Ty* ptr;
41 
42  public:
43  /* --------------------------------------------------------------
44  CONSTRUCTORS
45  -------------------------------------------------------------- */
51  {
52  ptr = nullptr;
53  };
54 
61  explicit SmartPointer(const _Ty* ptr)
62  : SmartPointer()
63  {
64  reset(ptr);
65  };
66 
74  SmartPointer(const SmartPointer &smartPointer)
75  : SmartPointer(smartPointer.ptr) {};
76 
85  SmartPointer(SmartPointer &&smartPointer)
86  {
87  ptr = smartPointer.ptr;
88  smartPointer.ptr = nullptr;
89  }
90 
107  {
108  std::lock_guard<std::mutex> lockGuard(mtx);
109  destruct(ptr);
110  };
111 
112  public:
113  /* --------------------------------------------------------------
114  SETTERS
115  -------------------------------------------------------------- */
129  void reset(const _Ty* ptr)
130  {
131  std::lock_guard<std::mutex> lockGuard(mtx);
132  if (this->ptr == ptr)
133  return;
134 
135  construct((_Ty*)ptr);
136  destruct(this->ptr);
137 
138  this->ptr = (_Ty*)ptr;
139  };
140 
141  /* --------------------------------------------------------------
142  GETTERS
143  -------------------------------------------------------------- */
155  inline auto get() const -> _Ty*
156  {
157  return ptr;
158  };
159 
171  auto operator->() const -> _Ty*
172  {
173  return get();
174  };
175 
185  auto operator*() const -> _Ty&
186  {
187  return *get();
188  };
189 
190  /* --------------------------------------------------------------
191  STATISTICS
192  -------------------------------------------------------------- */
193  private:
194  static void construct(_Ty *ptr)
195  {
196  if (ptr == nullptr)
197  return;
198 
199  if (useCountMap.find(ptr) != useCountMap.end())
200  useCountMap[ptr]++;
201  else
202  useCountMap[ptr] = 1;
203  }
204  static void destruct(_Ty *ptr)
205  {
206  if (ptr == nullptr)
207  return;
208 
209  if (useCountMap.find(ptr) != useCountMap.end())
210  if (--useCountMap[ptr] == 0)
211  {
212  useCountMap.erase(ptr);
213  delete ptr;
214  }
215  };
216  };
217 
218  template<typename _Ty> std::map<_Ty*, size_t> SmartPointer<_Ty>::useCountMap;
219  template<typename _Ty> std::mutex SmartPointer<_Ty>::mtx;
220 };
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.
static std::map< _Ty *, size_t > useCountMap
TreeMap of use count of each pointer.