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
15
16namespace original {
29 template <typename TYPE, typename DELETER = deleter<TYPE>>
30 class ownerPtr final : public autoPtr<TYPE, ownerPtr<TYPE, DELETER>, DELETER>{
31 public:
37 explicit ownerPtr(TYPE* p = std::nullptr_t{});
38
39 ownerPtr(const ownerPtr& other) = delete;
40 ownerPtr& operator=(const ownerPtr& other) = delete;
41
47 ownerPtr(ownerPtr&& other) noexcept;
48
55 ownerPtr& operator=(ownerPtr&& other) noexcept;
56
63 TYPE* unlock();
64
69 [[nodiscard]] std::string className() const override;
70
74 ~ownerPtr() override;
75
76 // Factory friend functions
77 template <typename T, typename DEL, typename... Args>
78 friend ownerPtr<T, DEL> makeOwnerPtr(Args&&... args);
79
80 template <typename T, typename DEL, typename... Args>
81 friend ownerPtr<T, DEL> makeOwnerPtrArray(u_integer size, Args&&... args);
82 };
83
99 template <typename T, typename DEL = deleter<T>, typename... Args>
100 ownerPtr<T, DEL> makeOwnerPtr(Args&&... args);
101
118 template <typename T, typename DEL = deleter<T[]>, typename... Args>
119 ownerPtr<T, DEL> makeOwnerPtrArray(u_integer size, Args&&... args);
120
121
122 // ----------------- Definitions of ownerPtr.h -----------------
123
124
125 template<typename TYPE, typename DELETER>
127 : autoPtr<TYPE, ownerPtr, DELETER>(p) {
128 this->addStrongRef();
129 }
130
131 template<typename TYPE, typename DELETER>
133 this->operator=(std::move(other));
134 }
135
136 template<typename TYPE, typename DELETER>
138 if (this == &other)
139 return *this;
140
141 this->removeStrongRef();
142 this->clean();
143 this->ref_count = other.ref_count;
145 other.addStrongRef();
146 return *this;
147 }
148
149 template<typename TYPE, typename DELETER>
151 TYPE* p = this->get();
152 this->setPtr(nullptr);
153 return p;
154 }
155
156 template<typename TYPE, typename DELETER>
158 return "ownerPtr";
159 }
160
161 template<typename TYPE, typename DELETER>
165
166 template<typename T, typename DEL, typename ... Args>
168 return ownerPtr<T, DEL>(new T{std::forward<Args>(args)...});
169 }
170
171 template<typename T, typename DEL, typename ... Args>
172 ownerPtr<T, DEL> makeOwnerPtrArray(const u_integer size, Args&& ...args) {
173 auto owner_ptr = ownerPtr<T, DEL>(new T[size]);
174 for (u_integer i = 0; i < size; i++)
175 {
176 owner_ptr[i] = T(std::forward<Args>(args)...);
177 }
178 return owner_ptr;
179 }
180}
181
182#endif //OWNERPTR_H
Base class for reference-counted smart pointers.
static refCount< TYPE, DELETER > * newRefCount(TYPE *p=nullptr)
Create new reference counter.
Definition autoPtr.h:377
void removeStrongRef()
Decrement strong reference count.
Definition autoPtr.h:351
void addStrongRef()
Increment strong reference count.
Definition autoPtr.h:341
void setPtr(TYPE *p)
Replace managed pointer.
Definition autoPtr.h:333
void clean() noexcept
Cleanup resources when expired.
Definition autoPtr.h:367
autoPtr(TYPE *p)
Construct from raw pointer.
Definition autoPtr.h:328
refCount< TYPE, DELETER > * ref_count
Reference counter object.
Definition autoPtr.h:38
const TYPE * get() const
Get managed pointer const version.
Definition autoPtr.h:408
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:30
friend ownerPtr< T, DEL > makeOwnerPtrArray(u_integer size, Args &&... args)
Creates a new ownerPtr managing a dynamically allocated array.
Definition ownerPtr.h:172
~ownerPtr() override
Destructor releases owned resources.
Definition ownerPtr.h:162
friend ownerPtr< T, DEL > makeOwnerPtr(Args &&... args)
Creates a new ownerPtr managing a dynamically allocated object.
Definition ownerPtr.h:167
ownerPtr(TYPE *p=std::nullptr_t{})
Construct from raw pointer.
Definition ownerPtr.h:126
TYPE * unlock()
Release ownership of managed object.
Definition ownerPtr.h:150
ownerPtr(const ownerPtr &other)=delete
Copy construction prohibited.
ownerPtr & operator=(const ownerPtr &other)=delete
Copy assignment prohibited.
std::string className() const override
Get class name identifier.
Definition ownerPtr.h:157
Default deleters for resource management.
Main namespace for the project Original.
Definition algorithms.h:21
std::uint32_t u_integer
32-bit unsigned integer type for sizes/indexes
Definition config.h:17
ownerPtr< T, DEL > makeOwnerPtr(Args &&... args)
Creates a new ownerPtr managing a dynamically allocated object.
Definition ownerPtr.h:167
ownerPtr< T, DEL > makeOwnerPtrArray(u_integer size, Args &&... args)
Creates a new ownerPtr managing a dynamically allocated array.
Definition ownerPtr.h:172