| 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 |
#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 |
SafeNumeric<uint32_t> dereference_count; |
| 41 |
|
| 42 |
protected: |
| 43 |
static void _bind_methods(); |
| 44 |
|
| 45 |
public: |
| 46 |
static constexpr AncestralClass static_ancestral_class = AncestralClass::REF_COUNTED; |
| 47 |
|
| 48 |
_FORCE_INLINE_ bool is_referenced() const { return refcount_init.get() != 1; } |
| 49 |
bool init_ref(); |
| 50 |
bool reference(); |
| 51 |
bool unreference(); |
| 52 |
int get_reference_count() const; |
| 53 |
|
| 54 |
RefCounted(); |
| 55 |
}; |
| 56 |
|
| 57 |
template <typename T> |
| 58 |
class Ref { |
| 59 |
T *reference = nullptr; |
| 60 |
|
| 61 |
_FORCE_INLINE_ void ref(const Ref &p_from) { |
| 62 |
ref_pointer<false>(p_from.reference); |
| 63 |
} |
| 64 |
|
| 65 |
template <bool Init> |
| 66 |
_FORCE_INLINE_ void ref_pointer(T *p_refcounted) { |
| 67 |
if (p_refcounted == reference) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
Ref cleanup_ref; |
| 73 |
cleanup_ref.reference = reference; |
| 74 |
reference = p_refcounted; |
| 75 |
if (reference) { |
| 76 |
if constexpr (Init) { |
| 77 |
if (!reference->init_ref()) { |
| 78 |
reference = nullptr; |
| 79 |
} |
| 80 |
} else { |
| 81 |
if (!reference->reference()) { |
| 82 |
reference = nullptr; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
public: |
| 90 |
static _FORCE_INLINE_ String get_class_static() { |
| 91 |
return T::get_class_static(); |
| 92 |
} |
| 93 |
|
| 94 |
_FORCE_INLINE_ bool operator==(const T *p_ptr) const { |
| 95 |
return reference == p_ptr; |
| 96 |
} |
| 97 |
_FORCE_INLINE_ bool operator!=(const T *p_ptr) const { |
| 98 |
return reference != p_ptr; |
| 99 |
} |
| 100 |
#ifdef STRICT_CHECKS |
| 101 |
|
| 102 |
bool operator==(std::nullptr_t) const = delete; |
| 103 |
bool operator!=(std::nullptr_t) const = delete; |
| 104 |
#endif |
| 105 |
|
| 106 |
_FORCE_INLINE_ bool operator<(const Ref<T> &p_r) const { |
| 107 |
return reference < p_r.reference; |
| 108 |
} |
| 109 |
_FORCE_INLINE_ bool operator==(const Ref<T> &p_r) const { |
| 110 |
return reference == p_r.reference; |
| 111 |
} |
| 112 |
_FORCE_INLINE_ bool operator!=(const Ref<T> &p_r) const { |
| 113 |
return reference != p_r.reference; |
| 114 |
} |
| 115 |
|
| 116 |
_FORCE_INLINE_ T *operator*() const { |
| 117 |
return reference; |
| 118 |
} |
| 119 |
|
| 120 |
_FORCE_INLINE_ T *operator->() const { |
| 121 |
return reference; |
| 122 |
} |
| 123 |
|
| 124 |
_FORCE_INLINE_ T *ptr() const { |
| 125 |
return reference; |
| 126 |
} |
| 127 |
|
| 128 |
operator Variant() const { |
| 129 |
return Variant(reference); |
| 130 |
} |
| 131 |
|
| 132 |
void operator=(const Ref &p_from) { |
| 133 |
ref(p_from); |
| 134 |
} |
| 135 |
|
| 136 |
void operator=(Ref &&p_from) { |
| 137 |
if (reference == p_from.reference) { |
| 138 |
return; |
| 139 |
} |
| 140 |
unref(); |
| 141 |
reference = p_from.reference; |
| 142 |
p_from.reference = nullptr; |
| 143 |
} |
| 144 |
|
| 145 |
template <typename T_Other> |
| 146 |
void operator=(const Ref<T_Other> &p_from) { |
| 147 |
ref_pointer<false>(Object::cast_to<T>(p_from.ptr())); |
| 148 |
} |
| 149 |
|
| 150 |
void operator=(T *p_from) { |
| 151 |
ref_pointer<true>(p_from); |
| 152 |
} |
| 153 |
|
| 154 |
void operator=(const Variant &p_variant) { |
| 155 |
Object *object = p_variant.get_validated_object(); |
| 156 |
|
| 157 |
if (object == reference) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
ref_pointer<false>(Object::cast_to<T>(object)); |
| 162 |
} |
| 163 |
|
| 164 |
template <typename T_Other> |
| 165 |
void reference_ptr(T_Other *p_ptr) { |
| 166 |
if (reference == p_ptr) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
ref_pointer<true>(Object::cast_to<T>(p_ptr)); |
| 171 |
} |
| 172 |
|
| 173 |
Ref(const Ref &p_from) { |
| 174 |
this->operator=(p_from); |
| 175 |
} |
| 176 |
|
| 177 |
Ref(Ref &&p_from) { |
| 178 |
reference = p_from.reference; |
| 179 |
p_from.reference = nullptr; |
| 180 |
} |
| 181 |
|
| 182 |
template <typename T_Other> |
| 183 |
Ref(const Ref<T_Other> &p_from) { |
| 184 |
this->operator=(p_from); |
| 185 |
} |
| 186 |
|
| 187 |
Ref(T *p_from) { |
| 188 |
this->operator=(p_from); |
| 189 |
} |
| 190 |
|
| 191 |
Ref(const Variant &p_from) { |
| 192 |
this->operator=(p_from); |
| 193 |
} |
| 194 |
|
| 195 |
inline bool is_valid() const { return reference != nullptr; } |
| 196 |
inline bool is_null() const { return reference == nullptr; } |
| 197 |
|
| 198 |
void unref() { |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
if (reference) { |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
if (reinterpret_cast<RefCounted *>(reference)->unreference()) { |
| 208 |
memdelete(reinterpret_cast<RefCounted *>(reference)); |
| 209 |
} |
| 210 |
reference = nullptr; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
template <typename... VarArgs> |
| 215 |
void instantiate(VarArgs... p_params) { |
| 216 |
ref(memnew(T(p_params...))); |
| 217 |
} |
| 218 |
|
| 219 |
uint32_t hash() const { return HashMapHasherDefault::hash(reference); } |
| 220 |
|
| 221 |
Ref() = default; |
| 222 |
|
| 223 |
~Ref() { |
| 224 |
unref(); |
| 225 |
} |
| 226 |
}; |
| 227 |
|
| 228 |
class WeakRef : public RefCounted { |
| 229 |
GDCLASS(WeakRef, RefCounted); |
| 230 |
|
| 231 |
ObjectID ref; |
| 232 |
|
| 233 |
protected: |
| 234 |
static void _bind_methods(); |
| 235 |
|
| 236 |
public: |
| 237 |
Variant get_ref() const; |
| 238 |
void set_obj(Object *p_object); |
| 239 |
void set_ref(const Ref<RefCounted> &p_ref); |
| 240 |
}; |
| 241 |
|
| 242 |
template <typename T> |
| 243 |
struct PtrToArg<Ref<T>> { |
| 244 |
_FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) { |
| 245 |
if (p_ptr == nullptr) { |
| 246 |
return Ref<T>(); |
| 247 |
} |
| 248 |
|
| 249 |
return Ref<T>(*reinterpret_cast<T *const *>(p_ptr)); |
| 250 |
} |
| 251 |
|
| 252 |
typedef Ref<T> EncodeT; |
| 253 |
|
| 254 |
_FORCE_INLINE_ static void encode(Ref<T> p_val, const void *p_ptr) { |
| 255 |
|
| 256 |
*(const_cast<Ref<RefCounted> *>(reinterpret_cast<const Ref<RefCounted> *>(p_ptr))) = p_val; |
| 257 |
} |
| 258 |
}; |
| 259 |
|
| 260 |
template <typename T> |
| 261 |
struct GetTypeInfo<Ref<T>> { |
| 262 |
static const Variant::Type VARIANT_TYPE = Variant::OBJECT; |
| 263 |
static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE; |
| 264 |
|
| 265 |
static inline PropertyInfo get_class_info() { |
| 266 |
return PropertyInfo(Variant::OBJECT, String(), PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static()); |
| 267 |
} |
| 268 |
}; |
| 269 |
|
| 270 |
template <typename T> |
| 271 |
struct VariantInternalAccessor<Ref<T>> { |
| 272 |
static _FORCE_INLINE_ Ref<T> get(const Variant *v) { return Ref<T>(*VariantInternal::get_object(v)); } |
| 273 |
static _FORCE_INLINE_ void set(Variant *v, const Ref<T> &p_ref) { VariantInternal::object_assign(v, p_ref); } |
| 274 |
}; |
| 275 |
|
| 276 |
|
| 277 |
template <typename T> |
| 278 |
struct is_zero_constructible<Ref<T>> : std::true_type {}; |
| 279 |
|
| 280 |
template <typename T> |
| 281 |
Ref<T> ObjectDB::get_ref(ObjectID p_instance_id) { |
| 282 |
return Ref<T>(get_instance(p_instance_id)); |
| 283 |
} |