Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/object/ref_counted.h
20888 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
static constexpr AncestralClass static_ancestral_class = AncestralClass::REF_COUNTED;
46
47
_FORCE_INLINE_ bool is_referenced() const { return refcount_init.get() != 1; }
48
bool init_ref();
49
bool reference(); // returns false if refcount is at zero and didn't get increased
50
bool unreference();
51
int get_reference_count() const;
52
53
RefCounted();
54
};
55
56
template <typename T>
57
class Ref {
58
T *reference = nullptr;
59
60
_FORCE_INLINE_ void ref(const Ref &p_from) {
61
ref_pointer<false>(p_from.reference);
62
}
63
64
template <bool Init>
65
_FORCE_INLINE_ void ref_pointer(T *p_refcounted) {
66
if (p_refcounted == reference) {
67
return;
68
}
69
70
// This will go out of scope and get unref'd.
71
Ref cleanup_ref;
72
cleanup_ref.reference = reference;
73
reference = p_refcounted;
74
if (reference) {
75
if constexpr (Init) {
76
if (!reference->init_ref()) {
77
reference = nullptr;
78
}
79
} else {
80
if (!reference->reference()) {
81
reference = nullptr;
82
}
83
}
84
}
85
}
86
87
//virtual RefCounted * get_reference() const { return reference; }
88
public:
89
static _FORCE_INLINE_ String get_class_static() {
90
return T::get_class_static();
91
}
92
93
_FORCE_INLINE_ bool operator==(const T *p_ptr) const {
94
return reference == p_ptr;
95
}
96
_FORCE_INLINE_ bool operator!=(const T *p_ptr) const {
97
return reference != p_ptr;
98
}
99
#ifdef STRICT_CHECKS
100
// Delete these to prevent raw comparisons with `nullptr`.
101
bool operator==(std::nullptr_t) const = delete;
102
bool operator!=(std::nullptr_t) const = delete;
103
#endif // STRICT_CHECKS
104
105
_FORCE_INLINE_ bool operator<(const Ref<T> &p_r) const {
106
return reference < p_r.reference;
107
}
108
_FORCE_INLINE_ bool operator==(const Ref<T> &p_r) const {
109
return reference == p_r.reference;
110
}
111
_FORCE_INLINE_ bool operator!=(const Ref<T> &p_r) const {
112
return reference != p_r.reference;
113
}
114
115
_FORCE_INLINE_ T *operator*() const {
116
return reference;
117
}
118
119
_FORCE_INLINE_ T *operator->() const {
120
return reference;
121
}
122
123
_FORCE_INLINE_ T *ptr() const {
124
return reference;
125
}
126
127
operator Variant() const {
128
return Variant(reference);
129
}
130
131
void operator=(const Ref &p_from) {
132
ref(p_from);
133
}
134
135
void operator=(Ref &&p_from) {
136
if (reference == p_from.reference) {
137
return;
138
}
139
unref();
140
reference = p_from.reference;
141
p_from.reference = nullptr;
142
}
143
144
template <typename T_Other>
145
void operator=(const Ref<T_Other> &p_from) {
146
ref_pointer<false>(Object::cast_to<T>(p_from.ptr()));
147
}
148
149
void operator=(T *p_from) {
150
ref_pointer<true>(p_from);
151
}
152
153
void operator=(const Variant &p_variant) {
154
Object *object = p_variant.get_validated_object();
155
156
if (object == reference) {
157
return;
158
}
159
160
ref_pointer<false>(Object::cast_to<T>(object));
161
}
162
163
template <typename T_Other>
164
void reference_ptr(T_Other *p_ptr) {
165
if (reference == p_ptr) {
166
return;
167
}
168
169
ref_pointer<true>(Object::cast_to<T>(p_ptr));
170
}
171
172
Ref(const Ref &p_from) {
173
this->operator=(p_from);
174
}
175
176
Ref(Ref &&p_from) {
177
reference = p_from.reference;
178
p_from.reference = nullptr;
179
}
180
181
template <typename T_Other>
182
Ref(const Ref<T_Other> &p_from) {
183
this->operator=(p_from);
184
}
185
186
Ref(T *p_from) {
187
this->operator=(p_from);
188
}
189
190
Ref(const Variant &p_from) {
191
this->operator=(p_from);
192
}
193
194
inline bool is_valid() const { return reference != nullptr; }
195
inline bool is_null() const { return reference == nullptr; }
196
197
void unref() {
198
// TODO: this should be moved to mutexes, since this engine does not really
199
// do a lot of referencing on references and stuff
200
// mutexes will avoid more crashes?
201
202
if (reference) {
203
// NOTE: `reinterpret_cast` is "safe" here because we know `T` has simple linear
204
// inheritance to `RefCounted`. This guarantees that `T * == `RefCounted *`, which
205
// allows us to declare `Ref<T>` with forward declared `T` types.
206
if (reinterpret_cast<RefCounted *>(reference)->unreference()) {
207
memdelete(reinterpret_cast<RefCounted *>(reference));
208
}
209
reference = nullptr;
210
}
211
}
212
213
template <typename... VarArgs>
214
void instantiate(VarArgs... p_params) {
215
ref(memnew(T(p_params...)));
216
}
217
218
uint32_t hash() const { return HashMapHasherDefault::hash(reference); }
219
220
Ref() = default;
221
222
~Ref() {
223
unref();
224
}
225
};
226
227
class WeakRef : public RefCounted {
228
GDCLASS(WeakRef, RefCounted);
229
230
ObjectID ref;
231
232
protected:
233
static void _bind_methods();
234
235
public:
236
Variant get_ref() const;
237
void set_obj(Object *p_object);
238
void set_ref(const Ref<RefCounted> &p_ref);
239
};
240
241
template <typename T>
242
struct PtrToArg<Ref<T>> {
243
_FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
244
if (p_ptr == nullptr) {
245
return Ref<T>();
246
}
247
// p_ptr points to a RefCounted object
248
return Ref<T>(*reinterpret_cast<T *const *>(p_ptr));
249
}
250
251
typedef Ref<T> EncodeT;
252
253
_FORCE_INLINE_ static void encode(Ref<T> p_val, const void *p_ptr) {
254
// p_ptr points to an EncodeT object which is a Ref<T> object.
255
*(const_cast<Ref<RefCounted> *>(reinterpret_cast<const Ref<RefCounted> *>(p_ptr))) = p_val;
256
}
257
};
258
259
template <typename T>
260
struct GetTypeInfo<Ref<T>> {
261
static const Variant::Type VARIANT_TYPE = Variant::OBJECT;
262
static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
263
264
static inline PropertyInfo get_class_info() {
265
return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
266
}
267
};
268
269
template <typename T>
270
struct VariantInternalAccessor<Ref<T>> {
271
static _FORCE_INLINE_ Ref<T> get(const Variant *v) { return Ref<T>(*VariantInternal::get_object(v)); }
272
static _FORCE_INLINE_ void set(Variant *v, const Ref<T> &p_ref) { VariantInternal::object_assign(v, p_ref); }
273
};
274
275
// Zero-constructing Ref initializes reference to nullptr (and thus empty).
276
template <typename T>
277
struct is_zero_constructible<Ref<T>> : std::true_type {};
278
279
template <typename T>
280
Ref<T> ObjectDB::get_ref(ObjectID p_instance_id) {
281
return Ref<T>(get_instance(p_instance_id));
282
}
283
284