-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRefCountingObjectPtr.h
More file actions
289 lines (233 loc) · 11 KB
/
RefCountingObjectPtr.h
File metadata and controls
289 lines (233 loc) · 11 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// RefCountingObject system for AngelScript
// Copyright (c) 2022 Petr Ohlidal
// https://github.com/only-a-ptr/RefCountingObject-AngelScript
// See license (MIT) at the bottom of this file.
// Adopted from "scripthandle" AngelScript addon, distributed with AngelScript SDK.
#pragma once
#include <angelscript.h>
#include <stdio.h> // snprintf
#if !defined(RefCoutingObjectPtr_DEBUGTRACE)
# define RefCoutingObjectPtr_DEBUGTRACE(_Expr)
#endif
#if !defined(RefCoutingObjectPtr_DEBUGTRACE_STATIC)
# define RefCoutingObjectPtr_DEBUGTRACE_STATIC(_Expr)
#endif
#if !defined(RefCountingObjectPtr_ASSERT)
# include <cassert>
# define RefCountingObjectPtr_ASSERT(_Expr_) assert(_Expr_)
#endif
template<class T>
class RefCountingObjectPtr
{
public:
// Constructors
RefCountingObjectPtr(T* ref);
RefCountingObjectPtr();
RefCountingObjectPtr(const RefCountingObjectPtr<T> &other);
~RefCountingObjectPtr();
// Assignments
RefCountingObjectPtr &operator=(const RefCountingObjectPtr<T> &other);
// Intentionally omitting raw-pointer assignment, for simplicity - see raw pointer constructor.
// Compare smart ptr
bool operator==(const RefCountingObjectPtr<T> &o) const { return m_ref == o.m_ref; }
bool operator!=(const RefCountingObjectPtr<T> &o) const { return m_ref != o.m_ref; }
// Compare pointer
bool operator==(const T* o) const { return m_ref == o; }
bool operator!=(const T* o) const { return m_ref != o; }
// Compare nullptr
bool operator==(const nullptr_t) const { return m_ref == nullptr; }
bool operator!=(const nullptr_t) const { return m_ref != nullptr; }
// Get the reference
T *GetRef() { return m_ref; } // To be invoked from C++ only!!
T* operator->() { return m_ref; }
T* operator->() const { return m_ref; }
// Boolean conversion (classic pointer check)
operator bool() const { return (bool)m_ref; }
// GC callback
void EnumReferences(AS_NAMESPACE_QUALIFIER asIScriptEngine *engine);
void ReleaseReferences(AS_NAMESPACE_QUALIFIER asIScriptEngine *engine);
static void RegisterRefCountingObjectPtr(AS_NAMESPACE_QUALIFIER asIScriptEngine* engine, const char* handle_name, const char* obj_name);
protected:
void Set(T* ref);
void ReleaseHandle();
void AddRefHandle();
// Wrapper functions, to be invoked by AngelScript only!
static void ConstructDefault(RefCountingObjectPtr<T> *self) { new(self) RefCountingObjectPtr(); }
static void ConstructCopy(RefCountingObjectPtr<T> *self, const RefCountingObjectPtr &o) { new(self) RefCountingObjectPtr(o); }
static void ConstructRef(RefCountingObjectPtr<T>* self, void** objhandle);
static void Destruct(RefCountingObjectPtr<T> *self) { self->~RefCountingObjectPtr(); }
static T* OpImplCast(RefCountingObjectPtr<T>* self);
static RefCountingObjectPtr & OpAssign(RefCountingObjectPtr<T>* self, void** objhandle);
static bool OpEquals(RefCountingObjectPtr<T>* self, void** objhandle);
static T* DereferenceHandle(void** objhandle);
T *m_ref;
};
template<class T>
void RefCountingObjectPtr<T>::RegisterRefCountingObjectPtr(AS_NAMESPACE_QUALIFIER asIScriptEngine* engine, const char* handle_name, const char* obj_name)
{
int r;
const size_t DECLBUF_MAX = 300;
char decl_buf[DECLBUF_MAX];
#if defined(AS_USE_NAMESPACE)
using namespace AngelScript;
#endif
// With C++11 it is possible to use asGetTypeTraits to automatically determine the flags that represent the C++ class
r = engine->RegisterObjectType(handle_name, sizeof(RefCountingObjectPtr), asOBJ_VALUE | asOBJ_ASHANDLE | asOBJ_GC | asGetTypeTraits<RefCountingObjectPtr>()); RefCountingObjectPtr_ASSERT( r >= 0 );
// construct/destruct
r = engine->RegisterObjectBehaviour(handle_name, asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(RefCountingObjectPtr::ConstructDefault), asCALL_CDECL_OBJFIRST); RefCountingObjectPtr_ASSERT( r >= 0 );
snprintf(decl_buf, DECLBUF_MAX, "void f(%s @&in)", obj_name);
r = engine->RegisterObjectBehaviour(handle_name, asBEHAVE_CONSTRUCT, decl_buf, asFUNCTION(RefCountingObjectPtr::ConstructRef), asCALL_CDECL_OBJFIRST); RefCountingObjectPtr_ASSERT( r >= 0 );
snprintf(decl_buf, DECLBUF_MAX, "void f(const %s &in)", handle_name);
r = engine->RegisterObjectBehaviour(handle_name, asBEHAVE_CONSTRUCT, decl_buf, asFUNCTION(RefCountingObjectPtr::ConstructCopy), asCALL_CDECL_OBJFIRST); RefCountingObjectPtr_ASSERT( r >= 0 );
r = engine->RegisterObjectBehaviour(handle_name, asBEHAVE_DESTRUCT, "void f()", asFUNCTION(RefCountingObjectPtr::Destruct), asCALL_CDECL_OBJFIRST); RefCountingObjectPtr_ASSERT( r >= 0 );
// GC
r = engine->RegisterObjectBehaviour(handle_name, asBEHAVE_ENUMREFS, "void f(int&in)", asMETHOD(RefCountingObjectPtr,EnumReferences), asCALL_THISCALL); RefCountingObjectPtr_ASSERT(r >= 0);
r = engine->RegisterObjectBehaviour(handle_name, asBEHAVE_RELEASEREFS, "void f(int&in)", asMETHOD(RefCountingObjectPtr, ReleaseReferences), asCALL_THISCALL); RefCountingObjectPtr_ASSERT(r >= 0);
// Cast
snprintf(decl_buf, DECLBUF_MAX, "%s @ opImplCast()", obj_name);
r = engine->RegisterObjectMethod(handle_name, decl_buf, asFUNCTION(RefCountingObjectPtr::OpImplCast), asCALL_CDECL_OBJFIRST); RefCountingObjectPtr_ASSERT( r >= 0 );
// GetRef
snprintf(decl_buf, DECLBUF_MAX, "%s @ GetHandle()", obj_name);
r = engine->RegisterObjectMethod(handle_name, decl_buf, asFUNCTION(RefCountingObjectPtr::OpImplCast), asCALL_CDECL_OBJFIRST); RefCountingObjectPtr_ASSERT( r >= 0 );
// Assign
snprintf(decl_buf, DECLBUF_MAX, "%s &opHndlAssign(const %s &in)", handle_name, handle_name);
r = engine->RegisterObjectMethod(handle_name, decl_buf, asMETHOD(RefCountingObjectPtr, operator=), asCALL_THISCALL); RefCountingObjectPtr_ASSERT( r >= 0 );
snprintf(decl_buf, DECLBUF_MAX, "%s &opHndlAssign(const %s @&in)", handle_name, obj_name);
r = engine->RegisterObjectMethod(handle_name, decl_buf, asFUNCTION(RefCountingObjectPtr::OpAssign), asCALL_CDECL_OBJFIRST); RefCountingObjectPtr_ASSERT( r >= 0 );
// Equals
snprintf(decl_buf, DECLBUF_MAX, "bool opEquals(const %s &in) const", handle_name);
r = engine->RegisterObjectMethod(handle_name, decl_buf, asMETHODPR(RefCountingObjectPtr, operator==, (const RefCountingObjectPtr &) const, bool), asCALL_THISCALL); RefCountingObjectPtr_ASSERT( r >= 0 );
snprintf(decl_buf, DECLBUF_MAX, "bool opEquals(const %s @&in) const", obj_name);
r = engine->RegisterObjectMethod(handle_name, decl_buf, asFUNCTION(RefCountingObjectPtr::OpEquals), asCALL_CDECL_OBJFIRST); RefCountingObjectPtr_ASSERT( r >= 0 );
}
// ---------------------------- Internals ------------------------------
template<class T>
T* RefCountingObjectPtr<T>::DereferenceHandle(void** objhandle)
{
// Dereference the handle to get the object itself (see AngelScript SDK, addon 'generic handle', function `Assign()` or `Equals()`).
return static_cast<T*>(*objhandle);
}
template<class T>
inline void RefCountingObjectPtr<T>::ConstructRef(RefCountingObjectPtr<T>* self, void** objhandle)
{
T* ref = DereferenceHandle(objhandle);
new(self)RefCountingObjectPtr(ref);
// Increase refcount manually because constructor is designed for C++ use only.
if (ref)
ref->AddRef();
}
template<class T>
inline T* RefCountingObjectPtr<T>::OpImplCast(RefCountingObjectPtr<T>* self)
{
RefCoutingObjectPtr_DEBUGTRACE_STATIC(self, (T*)nullptr);
T* ref = self->GetRef();
if (ref)
ref->AddRef();
return ref;
}
template<class T>
inline RefCountingObjectPtr<T> & RefCountingObjectPtr<T>::OpAssign(RefCountingObjectPtr<T>* self, void** objhandle)
{
T* ref = DereferenceHandle(objhandle);
self->Set(ref);
return *self;
}
template<class T>
inline bool RefCountingObjectPtr<T>::OpEquals(RefCountingObjectPtr<T>* self, void** objhandle)
{
T* ref = DereferenceHandle(objhandle);
return self->GetRef() == ref;
}
template<class T>
inline RefCountingObjectPtr<T>::RefCountingObjectPtr(T* ref)
: m_ref(nullptr)
{
RefCoutingObjectPtr_DEBUGTRACE((T*)nullptr);
this->Set(ref);
}
template<class T>
inline RefCountingObjectPtr<T>::RefCountingObjectPtr()
: m_ref(nullptr)
{
RefCoutingObjectPtr_DEBUGTRACE((T*)nullptr);
}
template<class T>
inline RefCountingObjectPtr<T>::RefCountingObjectPtr(const RefCountingObjectPtr<T> &other)
{
RefCoutingObjectPtr_DEBUGTRACE(other.m_ref);
m_ref = other.m_ref;
AddRefHandle();
}
template<class T>
inline RefCountingObjectPtr<T>::~RefCountingObjectPtr()
{
RefCoutingObjectPtr_DEBUGTRACE((T*)nullptr);
ReleaseHandle();
}
template<class T>
inline void RefCountingObjectPtr<T>::ReleaseHandle()
{
RefCoutingObjectPtr_DEBUGTRACE((T*)nullptr);
if( m_ref )
{
m_ref->Release();
m_ref = nullptr;
}
}
template<class T>
inline void RefCountingObjectPtr<T>::AddRefHandle()
{
RefCoutingObjectPtr_DEBUGTRACE((T*)nullptr);
if( m_ref )
{
m_ref->AddRef();
}
}
template<class T>
inline RefCountingObjectPtr<T> &RefCountingObjectPtr<T>::operator =(const RefCountingObjectPtr<T> &other)
{
RefCoutingObjectPtr_DEBUGTRACE(other.m_ref);
Set(other.m_ref);
return *this;
}
template<class T>
inline void RefCountingObjectPtr<T>::Set(T* ref)
{
if( m_ref == ref )
return;
ReleaseHandle();
m_ref = ref;
AddRefHandle();
}
template<class T>
inline void RefCountingObjectPtr<T>::EnumReferences(AS_NAMESPACE_QUALIFIER asIScriptEngine *inEngine)
{
// If we're holding a reference, we'll notify the garbage collector of it
if (m_ref)
inEngine->GCEnumCallback(m_ref);
}
template<class T>
inline void RefCountingObjectPtr<T>::ReleaseReferences(AS_NAMESPACE_QUALIFIER asIScriptEngine * /*inEngine*/)
{
// Simply clear the content to release the references
Set(nullptr);
}
/*
MIT License
Copyright (c) 2022 Petr Ohlídal
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/