ORIGINAL
Loading...
Searching...
No Matches
ownerPtr.h
Go to the documentation of this file.
1#ifndef OWNERPTR_H
2#define OWNERPTR_H
3
4#include <utility>
5#include "autoPtr.h"
6#include "deleter.h"
7
23namespace original {
36 template <typename TYPE, typename DELETER = deleter<TYPE>>
37 class ownerPtr final : public autoPtr<TYPE, ownerPtr<TYPE, DELETER>, DELETER>{
38 template<typename, typename> friend class ownerPtr;
39 public:
45 explicit ownerPtr(TYPE* p = std::nullptr_t{});
46
47 ownerPtr(const ownerPtr& other) = delete;
48 ownerPtr& operator=(const ownerPtr& other) = delete;
49
55 ownerPtr(ownerPtr&& other) noexcept;
56
63 ownerPtr& operator=(ownerPtr&& other) noexcept;
64
72 template<typename U, typename DEL = DELETER::template rebound_deleter<U>>
74
82 template<typename U, typename DEL = DELETER::template rebound_deleter<U>>
84
92 template<typename U, typename DEL = DELETER::template rebound_deleter<U>>
94
101 TYPE* unlock();
102
107 [[nodiscard]] std::string className() const override;
108
112 ~ownerPtr() override;
113
114 // Factory friend functions
115 template <typename T, typename DEL, typename... Args>
117
118 template <typename T, typename DEL, typename... Args>
120 };
121
137 template <typename T, typename DEL = deleter<T>, typename... Args>
139
156 template <typename T, typename DEL = deleter<T[]>, typename... Args>
158
159
160 // ----------------- Definitions of ownerPtr.h -----------------
161
162
163 template<typename TYPE, typename DELETER>
168
169 template<typename TYPE, typename DELETER>
171 this->operator=(std::move(other));
172 }
173
174 template<typename TYPE, typename DELETER>
176 if (this == &other)
177 return *this;
178
179 this->removeStrongRef();
180 this->clean();
181 this->ref_count = *other.ref_count;
184 return *this;
185 }
186
187 template <typename TYPE, typename DELETER>
188 template<typename U, typename DEL>
191 return ownerPtr<U, DEL>{
192 static_cast<U*>(this->unlock())
193 };
194 }
195
196 template <typename TYPE, typename DELETER>
197 template<typename U, typename DEL>
200 auto p = dynamic_cast<U*>(this->get());
201 if (p == nullptr) {
202 return ownerPtr<U, DEL>{};
203 }
204 this->unlock();
205 return ownerPtr<U, DEL>{p};
206 }
207
208 template <typename TYPE, typename DELETER>
209 template<typename U, typename DEL>
212 return ownerPtr<U, DEL>{
213 const_cast<U*>(this->unlock())
214 };
215 }
216
217 template<typename TYPE, typename DELETER>
219 return this->releasePtr();
220 }
221
222 template<typename TYPE, typename DELETER>
224 return "ownerPtr";
225 }
226
227 template<typename TYPE, typename DELETER>
231
232 template<typename T, typename DEL, typename ... Args>
234 return ownerPtr<T, DEL>(new T{std::forward<Args>(args)...});
235 }
236
237 template<typename T, typename DEL, typename ... Args>
239 auto owner_ptr = ownerPtr<T, DEL>(new T[size]);
240 for (u_integer i = 0; i < size; i++)
241 {
242 owner_ptr[i] = T(std::forward<Args>(args)...);
243 }
244 return owner_ptr;
245 }
246}
247
248#endif //OWNERPTR_H
Base class for reference-counted smart pointers.
Base smart pointer with reference counting.
Definition autoPtr.h:68
static refCount< TYPE, DELETER > * newRefCount(TYPE *p=nullptr)
Create new reference counter.
Definition autoPtr.h:581
void addStrongRef() const
Increment strong reference count.
Definition autoPtr.h:513
TYPE * releasePtr() noexcept
Release ownership of the managed pointer.
Definition autoPtr.h:545
atomic< refCountBase * > ref_count
Reference counter object.
Definition autoPtr.h:71
void removeStrongRef() const
Decrement strong reference count.
Definition autoPtr.h:529
const TYPE * get() const
Get managed pointer const version.
Definition autoPtr.h:629
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:37
friend ownerPtr< T, DEL > makeOwnerPtrArray(u_integer size, Args &&... args)
Creates a new ownerPtr managing a dynamically allocated array.
Definition ownerPtr.h:238
~ownerPtr() override
Destructor releases owned resources.
Definition ownerPtr.h:228
friend ownerPtr< T, DEL > makeOwnerPtr(Args &&... args)
Creates a new ownerPtr managing a dynamically allocated object.
Definition ownerPtr.h:233
ownerPtr< U, DEL > staticCastMoveTo()
Performs static cast and transfers ownership.
Definition ownerPtr.h:190
TYPE * unlock()
Release ownership of managed object.
Definition ownerPtr.h:218
ownerPtr(const ownerPtr &other)=delete
Copy construction prohibited.
ownerPtr & operator=(const ownerPtr &other)=delete
Copy assignment prohibited.
ownerPtr< U, DEL > dynamicCastMoveTo()
Performs dynamic cast and transfers ownership.
Definition ownerPtr.h:199
ownerPtr< U, DEL > constCastMoveTo()
Performs const cast and transfers ownership.
Definition ownerPtr.h:211
std::string className() const override
Get class name identifier.
Definition ownerPtr.h:223
Default deleters for resource management.
std::uint32_t u_integer
32-bit unsigned integer type for sizes and indexes
Definition config.h:263
Main namespace for the project Original.
Definition algorithms.h:21
ownerPtr< T, DEL > makeOwnerPtr(Args &&... args)
Creates a new ownerPtr managing a dynamically allocated object.
Definition ownerPtr.h:233
ownerPtr< T, DEL > makeOwnerPtrArray(u_integer size, Args &&... args)
Creates a new ownerPtr managing a dynamically allocated array.
Definition ownerPtr.h:238