Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/object/ref_counted.h
9905 views
1
/**************************************************************************/
2
/* ref_counted.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/object/class_db.h"
34
#include "core/templates/safe_refcount.h"
35
36
class RefCounted : public Object {
37
GDCLASS(RefCounted, Object);
38
SafeRefCount refcount;
39
SafeRefCount refcount_init;
40
41
protected:
42
static void _bind_methods();
43
44
public:
45
_FORCE_INLINE_ bool is_referenced() const { return refcount_init.get() != 1; }
46
bool init_ref();
47
bool reference(); // returns false if refcount is at zero and didn't get increased
48
bool unreference();
49
int get_reference_count() const;
50
51
RefCounted();
52
~RefCounted() {}
53
};
54
55
template <typename T>
56
class Ref {
57
T *reference = nullptr;
58
59
_FORCE_INLINE_ void ref(const Ref &p_from) {
60
ref_pointer<false>(p_from.reference);
61
}
62
63
template <bool Init>
64
_FORCE_INLINE_ void ref_pointer(T *p_refcounted) {
65
if (p_refcounted == reference) {
66
return;
67
}
68
69
// This will go out of scope and get unref'd.
70
Ref cleanup_ref;
71
cleanup_ref.reference = reference;
72
reference = p_refcounted;
73
if (reference) {
74
if constexpr (Init) {
75
if (!reference->init_ref()) {
76
reference = nullptr;
77
}
78
} else {
79
if (!reference->reference()) {
80
reference = nullptr;
81
}
82
}
83
}
84
}
85
86
//virtual RefCounted * get_reference() const { return reference; }
87
public:
88
static _FORCE_INLINE_ String get_class_static() {
89
return T::get_class_static();
90
}
91
92
_FORCE_INLINE_ bool operator==(const T *p_ptr) const {
93
return reference == p_ptr;
94
}
95
_FORCE_INLINE_ bool operator!=(const T *p_ptr) const {
96
return reference != p_ptr;
97
}
98
#ifdef STRICT_CHECKS
99
// Delete these to prevent raw comparisons with `nullptr`.
100
bool operator==(std::nullptr_t) const = delete;
101
bool operator!=(std::nullptr_t) const = delete;
102
#endif // STRICT_CHECKS
103
104
_FORCE_INLINE_ bool operator<(const Ref<T> &p_r) const {
105
return reference < p_r.reference;
106
}
107
_FORCE_INLINE_ bool operator==(const Ref<T> &p_r) const {
108
return reference == p_r.reference;
109
}
110
_FORCE_INLINE_ bool operator!=(const Ref<T> &p_r) const {
111
return reference != p_r.reference;
112
}
113
114
_FORCE_INLINE_ T *operator*() const {
115
return reference;
116
}
117
118
_FORCE_INLINE_ T *operator->() const {
119
return reference;
120
}
121
122
_FORCE_INLINE_ T *ptr() const {
123
return reference;
124
}
125
126
operator Variant() const {
127
return Variant(reference);
128
}
129
130
void operator=(const Ref &p_from) {
131
ref(p_from);
132
}
133
134
void operator=(Ref &&p_from) {
135
if (reference == p_from.reference) {
136
return;
137
}
138
unref();
139
reference = p_from.reference;
140
p_from.reference = nullptr;
141
}
142
143
template <typename T_Other>
144
void operator=(const Ref<T_Other> &p_from) {
145
ref_pointer<false>(Object::cast_to<T>(p_from.ptr()));
146
}
147
148
void operator=(T *p_from) {
149
ref_pointer<true>(p_from);
150
}
151
152
void operator=(const Variant &p_variant) {
153
Object *object = p_variant.get_validated_object();
154
155
if (object == reference) {
156
return;
157
}
158
159
ref_pointer<false>(Object::cast_to<T>(object));
160
}
161
162
template <typename T_Other>
163
void reference_ptr(T_Other *p_ptr) {
164
if (reference == p_ptr) {
165
return;
166
}
167
168
ref_pointer<true>(Object::cast_to<T>(p_ptr));
169
}
170
171
Ref(const Ref &p_from) {
172
this->operator=(p_from);
173
}
174
175
Ref(Ref &&p_from) {
176
reference = p_from.reference;
177
p_from.reference = nullptr;
178
}
179
180
template <typename T_Other>
181
Ref(const Ref<T_Other> &p_from) {
182
this->operator=(p_from);
183
}
184
185
Ref(T *p_from) {
186
this->operator=(p_from);
187
}
188
189
Ref(const Variant &p_from) {
190
this->operator=(p_from);
191
}
192
193
inline bool is_valid() const { return reference != nullptr; }
194
inline bool is_null() const { return reference == nullptr; }
195
196
void unref() {
197
// TODO: this should be moved to mutexes, since this engine does not really
198
// do a lot of referencing on references and stuff
199
// mutexes will avoid more crashes?
200
201
if (reference) {
202
// NOTE: `reinterpret_cast` is "safe" here because we know `T` has simple linear
203
// inheritance to `RefCounted`. This guarantees that `T * == `RefCounted *`, which
204
// allows us to declare `Ref<T>` with forward declared `T` types.
205
if (reinterpret_cast<RefCounted *>(reference)->unreference()) {
206
memdelete(reinterpret_cast<RefCounted *>(reference));
207
}
208
reference = nullptr;
209
}
210
}
211
212
template <typename... VarArgs>
213
void instantiate(VarArgs... p_params) {
214
ref(memnew(T(p_params...)));
215
}
216
217
Ref() = default;
218
219
~Ref() {
220
unref();
221
}
222
};
223
224
class WeakRef : public RefCounted {
225
GDCLASS(WeakRef, RefCounted);
226
227
ObjectID ref;
228
229
protected:
230
static void _bind_methods();
231
232
public:
233
Variant get_ref() const;
234
void set_obj(Object *p_object);
235
void set_ref(const Ref<RefCounted> &p_ref);
236
237
WeakRef() {}
238
};
239
240
template <typename T>
241
struct PtrToArg<Ref<T>> {
242
_FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
243
if (p_ptr == nullptr) {
244
return Ref<T>();
245
}
246
// p_ptr points to a RefCounted object
247
return Ref<T>(*reinterpret_cast<T *const *>(p_ptr));
248
}
249
250
typedef Ref<T> EncodeT;
251
252
_FORCE_INLINE_ static void encode(Ref<T> p_val, const void *p_ptr) {
253
// p_ptr points to an EncodeT object which is a Ref<T> object.
254
*(const_cast<Ref<RefCounted> *>(reinterpret_cast<const Ref<RefCounted> *>(p_ptr))) = p_val;
255
}
256
};
257
258
template <typename T>
259
struct GetTypeInfo<Ref<T>> {
260
static const Variant::Type VARIANT_TYPE = Variant::OBJECT;
261
static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
262
263
static inline PropertyInfo get_class_info() {
264
return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
265
}
266
};
267
268
template <typename T>
269
struct VariantInternalAccessor<Ref<T>> {
270
static _FORCE_INLINE_ Ref<T> get(const Variant *v) { return Ref<T>(*VariantInternal::get_object(v)); }
271
static _FORCE_INLINE_ void set(Variant *v, const Ref<T> &p_ref) { VariantInternal::object_assign(v, p_ref); }
272
};
273
274
// Zero-constructing Ref initializes reference to nullptr (and thus empty).
275
template <typename T>
276
struct is_zero_constructible<Ref<T>> : std::true_type {};
277
278
template <typename T>
279
Ref<T> ObjectDB::get_ref(ObjectID p_instance_id) {
280
return Ref<T>(get_instance(p_instance_id));
281
}
282
283