-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoop_ptr.hpp
268 lines (232 loc) · 5.53 KB
/
oop_ptr.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// ___ ___ ___ ___ _____ ____
// | | | | | | | ||_ _|| |
// | | | | | | | | | | | | | | | | _|
// | | | | | | | _| | _| | | | \
// |___| |___| |_| |_| |_| |_|\_\ version 1.0.0
//
// by Paweł Waligóra
//
// https://github.com/pwalig/oop_ptr
//
// Copyright (c) 2025 Paweł Waligóra
// Licensed under MIT license
#pragma once
#define OOP_PTR_PTR_MOVE_CONSTRUCTOR
#define OOP_PTR_OBJECT_COPY_CONSTRUCTOR
#ifndef oop_ptr_get_copy
#define oop_ptr_get_copy get_copy_ptr
#endif // oop_ptr_get_copy
#define oop_ptr_base_declare(b) virtual b* oop_ptr_get_copy() const
#define oop_ptr_child_declare(b) b* oop_ptr_get_copy() const override
#define oop_ptr_define(b, c) b* c::oop_ptr_get_copy() const { return new c(*this); }
// can be typed in template class body
#define oop_ptr_template_base_define(b) inline virtual b* oop_ptr_get_copy() const { return new b(*this); }
// can be typed in template class body
#define oop_ptr_template_child_define(b, c) inline b* oop_ptr_get_copy() const override { return new c(*this); }
template <typename T>
class oop_ptr {
public:
oop_ptr();
#ifdef OOP_PTR_OBJECT_COPY_CONSTRUCTOR
oop_ptr(const T& obj);
#endif
#ifdef OOP_PTR_PTR_COPY_CONSTRUCTOR
oop_ptr(const T* ptr);
#endif
#ifdef OOP_PTR_PTR_MOVE_CONSTRUCTOR
oop_ptr(T* ptr);
#endif
oop_ptr(const oop_ptr<T>& other);
oop_ptr(oop_ptr<T>&& other) noexcept;
#ifdef OOP_PTR_OBJECT_COPY_OPERATOR
oop_ptr<T>& operator= (const T& obj);
#endif
#ifdef OOP_PTR_PTR_COPY_OPERATOR
oop_ptr<T>& operator= (const T* ptr);
#endif
#ifdef OOP_PTR_PTR_MOVE_OPERATOR
oop_ptr<T>& operator= (T* ptr);
#endif
oop_ptr<T>& operator= (const oop_ptr<T>& other);
oop_ptr<T>& operator= (oop_ptr<T>&& other) noexcept;
explicit operator bool() const noexcept;
T* operator-> () const;
T& operator* () const;
T* get() const;
template <typename U>
U* get() const;
template <typename U>
bool gettable() const;
T* release();
template <typename U>
U* release();
~oop_ptr();
static oop_ptr copy(const T& obj);
static oop_ptr copy(const T* ptr);
static oop_ptr move(T* ptr);
const static oop_ptr null;
private:
T* resource;
};
template<typename T>
inline oop_ptr<T>::oop_ptr() : resource(nullptr)
{
}
template<typename T>
const oop_ptr<T> oop_ptr<T>::null = oop_ptr();
#ifdef OOP_PTR_OBJECT_COPY_CONSTRUCTOR
template<typename T>
inline oop_ptr<T>::oop_ptr(const T& obj) : resource(obj.oop_ptr_get_copy())
{
}
#endif
#ifdef OOP_PTR_PTR_COPY_CONSTRUCTOR
template<typename T>
inline oop_ptr<T>::oop_ptr(const T* ptr) : resource(ptr->oop_ptr_get_copy())
{
}
#endif
#ifdef OOP_PTR_PTR_MOVE_CONSTRUCTOR
template<typename T>
inline oop_ptr<T>::oop_ptr(T* ptr) : resource(ptr)
{
}
#endif
template<typename T>
inline oop_ptr<T>::oop_ptr(const oop_ptr<T>& other) : resource(other.resource->oop_ptr_get_copy())
{
}
template<typename T>
inline oop_ptr<T>::oop_ptr(oop_ptr<T>&& other) noexcept : resource(other.resource)
{
other.resource = nullptr;
}
#ifdef OOP_PTR_OBJECT_COPY_OPERATOR
template<typename T>
inline oop_ptr<T>& oop_ptr<T>::operator=(const T& copy)
{
delete (this->resource);
this->resource = copy.oop_ptr_get_copy();
return *this;
}
#endif
#ifdef OOP_PTR_PTR_COPY_OPERATOR
template<typename T>
inline oop_ptr<T>& oop_ptr<T>::operator=(const T* ptr)
{
delete (this->resource);
this->resource = ptr->oop_ptr_get_copy();
return *this;
}
#endif
#ifdef OOP_PTR_PTR_MOVE_OPERATOR
template<typename T>
inline oop_ptr<T>& oop_ptr<T>::operator=(T* ptr)
{
delete (this->resource);
this->resource = ptr;
return *this;
}
#endif
template<typename T>
inline oop_ptr<T>& oop_ptr<T>::operator=(const oop_ptr<T>& other)
{
if (this != &other) {
delete (this->resource);
this->resource = other.resource->oop_ptr_get_copy();
}
return *this;
}
template<typename T>
inline oop_ptr<T>& oop_ptr<T>::operator=(oop_ptr<T>&& other) noexcept
{
if (this != &other) {
delete (this->resource);
this->resource = other.resource;
other.resource = nullptr;
}
return *this;
}
template<typename T>
inline oop_ptr<T>::operator bool() const noexcept
{
return (this->resource != nullptr);
}
template<typename T>
inline T* oop_ptr<T>::operator->() const
{
return this->resource;
}
template<typename T>
inline T& oop_ptr<T>::operator*() const
{
return *(this->resource);
}
template<typename T>
inline T* oop_ptr<T>::get() const
{
return this->resource;
}
template<typename T>
inline T* oop_ptr<T>::release()
{
T* out = this->get();
this->resource = nullptr;
return out;
}
template<typename T>
template<typename U>
inline U* oop_ptr<T>::get() const
{
if (this->resource == nullptr) return nullptr;
if (U* out = dynamic_cast<U*>(this->resource)) return out;
return nullptr;
}
template<typename T>
template<typename U>
inline bool oop_ptr<T>::gettable() const
{
return get<U>() != nullptr;
}
template<typename T>
template<typename U>
inline U* oop_ptr<T>::release()
{
U* out = this->get<U>();
this->resource = nullptr;
return out;
}
template<typename T>
inline oop_ptr<T>::~oop_ptr()
{
if (resource) delete resource;
}
template<typename T>
inline oop_ptr<T> oop_ptr<T>::copy(const T& obj)
{
return copy(&obj);
}
template<typename T>
inline oop_ptr<T> oop_ptr<T>::copy(const T* ptr)
{
#if defined(OOP_PTR_PTR_COPY_CONSTRUCTOR)
return oop_ptr(ptr);
#elif defined (OOP_PTR_PTR_MOVE_CONSTRUCTOR)
return oop_ptr(ptr->oop_ptr_get_copy());
#else
oop_ptr o;
o.resource = obj.oop_ptr_get_copy();
return o;
#endif
}
template<typename T>
inline oop_ptr<T> oop_ptr<T>::move(T* ptr)
{
#ifdef OOP_PTR_PTR_MOVE_CONSTRUCTOR
return oop_ptr(ptr);
#else
oop_ptr o;
o.resource = ptr;
return o;
#endif
}