libUTL++
AutoPtr.h
1 #pragma once
2 
4 
5 UTL_NS_BEGIN;
6 
8 
29 
31 template <typename T = Object>
32 class AutoPtr
33 {
34 public:
37  {
38  init();
39  }
40 
46  AutoPtr(T* ptr, bool owner = true)
47  {
48  init();
49  set(ptr, owner);
50  }
51 
53  AutoPtr(AutoPtr<T>&& rhs) noexcept
54  {
55  init();
56  steal(rhs);
57  }
58 
60  template <typename NonT>
61  AutoPtr(AutoPtr<NonT>&& rhs) noexcept
62  {
63  init();
64  stealNonT(rhs);
65  }
66 
69  {
70  deInit();
71  }
72 
74 
75 
76  T*
77  get() const
78  {
79  return _ptr;
80  }
81 
83  bool
84  isNull() const
85  {
86  return (_ptr == nullptr);
87  }
88 
90  bool
91  isOwner() const
92  {
93  return _owner;
94  }
96 
98 
99 
103  void
105  {
106  if (_owner) delete _ptr;
107  _ptr = nullptr;
108  _owner = false;
109  }
110 
115  T*
117  {
118  _owner = false;
119  return _ptr;
120  }
121 
123  void
124  set(T* ptr, bool owner = true)
125  {
126  if (ptr != _ptr)
127  {
128  if (_owner) delete _ptr;
129  _ptr = ptr;
130  }
131  _owner = owner;
132  }
133 
135  template <typename NonT>
136  void
137  setNonT(NonT* ptr, bool owner = true)
138  {
139  set(dynamic_cast<T*>(ptr), owner);
140  ASSERTD(_ptr != nullptr);
141  }
143 
145 
146 
147  void
149  {
150  set(rhs.get(), rhs.isOwner());
151  rhs.release();
152  }
153 
155  template <typename NonT>
156  void
158  {
159  setNonT(rhs.get(), rhs.isOwner());
160  rhs.release();
161  }
162 
164  AutoPtr<T>& operator=(AutoPtr<T>&& rhs) noexcept
165  {
166  steal(rhs);
167  return self;
168  }
169 
171  template <typename NonT>
172  AutoPtr<T>& operator=(const AutoPtr<NonT>&& rhs) noexcept
173  {
174  steal(rhs);
175  return self;
176  }
177 
180  {
181  set(ptr);
182  return self;
183  }
184 
186  template <typename NonT>
187  AutoPtr<T>& operator=(NonT* ptr)
188  {
189  setNonT(ptr);
190  return self;
191  }
193 
195 
196 
197  T& operator*() const
198  {
199  return *ptr();
200  }
201 
203  T* operator->() const
204  {
205  return ptr();
206  }
207 
209  operator T*() const
210  {
211  return _ptr;
212  }
214 protected:
215  T*
216  ptr() const
217  {
218  ASSERTD(_ptr != nullptr);
219  return _ptr;
220  }
221 
222 protected:
223  T* _ptr;
224  bool _owner;
225 
226 private:
227  void
228  init()
229  {
230  _ptr = nullptr;
231  _owner = false;
232  }
233  void
234  deInit()
235  {
236  if (_owner) delete _ptr;
237  }
238 };
239 
241 
242 UTL_NS_END;
243 
245 
246 template <typename X, typename Y>
247 bool operator==(const utl::AutoPtr<X>& lhs, const utl::AutoPtr<Y>& rhs)
248 {
249  return (lhs.get() == rhs.get());
250 }
251 
252 template <typename T>
253 bool operator==(const utl::AutoPtr<T>& lhs, std::nullptr_t rhs)
254 {
255  return (lhs.get() == nullptr);
256 }
257 
258 template <typename T>
259 bool operator==(std::nullptr_t lhs, const utl::AutoPtr<T>& rhs)
260 {
261  return (rhs.get() == nullptr);
262 }
263 
264 template <typename X, typename Y>
265 bool operator!=(const utl::AutoPtr<X>& lhs, const utl::AutoPtr<Y>& rhs)
266 {
267  return (lhs.get() != rhs.get());
268 }
269 
270 template <typename T>
271 bool operator!=(const utl::AutoPtr<T>& lhs, std::nullptr_t rhs)
272 {
273  return (lhs.get() != nullptr);
274 }
275 
276 template <typename T>
277 bool operator!=(std::nullptr_t lhs, const utl::AutoPtr<T>& rhs)
278 {
279  return (rhs.get() != nullptr);
280 }
void stealNonT(AutoPtr< NonT > &rhs)
Steal from AutoPtr<NonT>.
Definition: AutoPtr.h:157
void deInit()
De-initialize UTL++.
void setNonT(NonT *ptr, bool owner=true)
Set new pointer and ownership flag.
Definition: AutoPtr.h:137
void clear()
Delete the pointee if the owner flag is true.
Definition: AutoPtr.h:104
T & operator*() const
Pointer dereference operator.
Definition: AutoPtr.h:197
~AutoPtr()
Destructor.
Definition: AutoPtr.h:68
AutoPtr(AutoPtr< T > &&rhs) noexcept
Move constructor (moving AutoPtr<T>).
Definition: AutoPtr.h:53
T * operator->() const
Member access operator.
Definition: AutoPtr.h:203
AutoPtr< T > & operator=(T *ptr)
Assignment operator from T*.
Definition: AutoPtr.h:179
void steal(AutoPtr< T > &rhs)
Steal from AutoPtr<T>.
Definition: AutoPtr.h:148
AutoPtr(T *ptr, bool owner=true)
Constructor.
Definition: AutoPtr.h:46
AutoPtr< T > & operator=(NonT *ptr)
Assignment operator from NonT*.
Definition: AutoPtr.h:187
T * get() const
Get the pointer.
Definition: AutoPtr.h:77
AutoPtr< T > & operator=(const AutoPtr< NonT > &&rhs) noexcept
Move assignment from AutoPtr<NonT>.
Definition: AutoPtr.h:172
AutoPtr(AutoPtr< NonT > &&rhs) noexcept
Move constructor (moving AutoPtr<NonT>).
Definition: AutoPtr.h:61
bool isNull() const
Determine whether the pointer is nullptr.
Definition: AutoPtr.h:84
bool isOwner() const
Get owner flag.
Definition: AutoPtr.h:91
AutoPtr< T > & operator=(AutoPtr< T > &&rhs) noexcept
Move assignment from AutoPtr<T>.
Definition: AutoPtr.h:164
T * release()
Release ownership (while keeping the pointer).
Definition: AutoPtr.h:116
A "smart" pointer that can also be dumb.
Definition: AutoPtr.h:32
void init()
Initialize UTL++.
AutoPtr()
Constructor.
Definition: AutoPtr.h:36
#define ASSERTD
Do an assertion in DEBUG mode only.