2017-06-19 00:03:59 +00:00
|
|
|
#ifndef REF_H
|
|
|
|
#define REF_H
|
|
|
|
|
2017-10-20 23:42:10 +00:00
|
|
|
#include "GodotGlobal.hpp"
|
2018-02-11 14:50:01 +00:00
|
|
|
#include "Reference.hpp"
|
2018-11-23 22:09:41 +00:00
|
|
|
#include "Variant.hpp"
|
2017-06-19 00:03:59 +00:00
|
|
|
|
|
|
|
namespace godot {
|
|
|
|
|
2018-01-22 21:27:10 +00:00
|
|
|
// Replicates Godot's Ref<T> behavior
|
|
|
|
// Rewritten from f5234e70be7dec4930c2d5a0e829ff480d044b1d.
|
|
|
|
template <class T>
|
2017-06-19 00:03:59 +00:00
|
|
|
class Ref {
|
2020-12-08 22:10:59 +00:00
|
|
|
// TODO For this nice check to work, each class must actually #include Reference classes mentionned in its methods,
|
|
|
|
// which might be annoying for coders who prefer to forward-declare to reduce compile times
|
|
|
|
// static_assert(std::is_base_of<Reference, T>::value,
|
|
|
|
// "Ref<T> can only be used with classes deriving from Reference");
|
2018-01-22 21:27:10 +00:00
|
|
|
|
2018-02-23 13:08:36 +00:00
|
|
|
T *reference = nullptr;
|
2018-01-22 21:27:10 +00:00
|
|
|
|
|
|
|
void ref(const Ref &p_from) {
|
|
|
|
|
|
|
|
if (p_from.reference == reference)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unref();
|
|
|
|
|
|
|
|
reference = p_from.reference;
|
|
|
|
if (reference)
|
|
|
|
reference->reference();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ref_pointer(T *p_ref) {
|
|
|
|
|
2020-12-08 22:10:59 +00:00
|
|
|
ERR_FAIL_COND(p_ref == nullptr);
|
2018-01-22 21:27:10 +00:00
|
|
|
|
|
|
|
if (p_ref->init_ref())
|
|
|
|
reference = p_ref;
|
|
|
|
}
|
2017-06-19 00:03:59 +00:00
|
|
|
|
|
|
|
public:
|
2018-01-22 21:27:10 +00:00
|
|
|
inline bool operator<(const Ref<T> &p_r) const {
|
|
|
|
|
|
|
|
return reference < p_r.reference;
|
|
|
|
}
|
|
|
|
inline bool operator==(const Ref<T> &p_r) const {
|
|
|
|
|
|
|
|
return reference == p_r.reference;
|
|
|
|
}
|
|
|
|
inline bool operator!=(const Ref<T> &p_r) const {
|
|
|
|
|
|
|
|
return reference != p_r.reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline T *operator->() {
|
|
|
|
|
2017-06-19 00:03:59 +00:00
|
|
|
return reference;
|
|
|
|
}
|
2018-01-22 21:27:10 +00:00
|
|
|
|
|
|
|
inline T *operator*() {
|
|
|
|
|
2017-06-19 00:03:59 +00:00
|
|
|
return reference;
|
|
|
|
}
|
2018-01-22 21:27:10 +00:00
|
|
|
|
|
|
|
inline const T *operator->() const {
|
|
|
|
|
2017-06-19 00:03:59 +00:00
|
|
|
return reference;
|
|
|
|
}
|
2018-01-22 21:27:10 +00:00
|
|
|
|
|
|
|
inline const T *ptr() const {
|
|
|
|
|
2017-06-19 00:03:59 +00:00
|
|
|
return reference;
|
|
|
|
}
|
2018-01-22 21:27:10 +00:00
|
|
|
inline T *ptr() {
|
|
|
|
|
2017-06-19 00:03:59 +00:00
|
|
|
return reference;
|
|
|
|
}
|
2018-01-22 21:27:10 +00:00
|
|
|
|
|
|
|
inline const T *operator*() const {
|
|
|
|
|
2017-06-19 00:03:59 +00:00
|
|
|
return reference;
|
|
|
|
}
|
2018-01-19 10:40:50 +00:00
|
|
|
|
2018-01-22 21:27:10 +00:00
|
|
|
operator Variant() const {
|
|
|
|
// Note: the C API handles the cases where the object is a Reference,
|
|
|
|
// so the Variant will be correctly constructed with a RefPtr engine-side
|
2018-11-23 22:09:41 +00:00
|
|
|
return Variant((Object *)reference);
|
2017-06-19 00:03:59 +00:00
|
|
|
}
|
2017-06-20 22:42:29 +00:00
|
|
|
|
2018-01-22 21:27:10 +00:00
|
|
|
void operator=(const Ref &p_from) {
|
2018-01-19 10:40:50 +00:00
|
|
|
|
2018-01-22 21:27:10 +00:00
|
|
|
ref(p_from);
|
2017-06-20 22:42:29 +00:00
|
|
|
}
|
2018-01-19 10:40:50 +00:00
|
|
|
|
2018-01-22 21:27:10 +00:00
|
|
|
template <class T_Other>
|
|
|
|
void operator=(const Ref<T_Other> &p_from) {
|
|
|
|
Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
|
2020-12-08 22:10:59 +00:00
|
|
|
if (refb == nullptr) {
|
2018-01-22 21:27:10 +00:00
|
|
|
unref();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Ref r;
|
2020-12-08 22:10:59 +00:00
|
|
|
r.reference = Object::cast_to<T>(refb);
|
2018-01-22 21:27:10 +00:00
|
|
|
ref(r);
|
2018-02-23 13:08:36 +00:00
|
|
|
r.reference = nullptr;
|
2017-06-19 00:03:59 +00:00
|
|
|
}
|
2018-01-22 21:27:10 +00:00
|
|
|
|
|
|
|
void operator=(const Variant &p_variant) {
|
2020-12-08 22:10:59 +00:00
|
|
|
Object *refb = T::___get_from_variant(p_variant);
|
|
|
|
if (refb == nullptr) {
|
2018-01-22 21:27:10 +00:00
|
|
|
unref();
|
|
|
|
return;
|
2018-01-19 10:40:50 +00:00
|
|
|
}
|
2018-01-22 21:27:10 +00:00
|
|
|
Ref r;
|
2020-12-08 22:10:59 +00:00
|
|
|
r.reference = Object::cast_to<T>(refb);
|
2018-01-22 21:27:10 +00:00
|
|
|
ref(r);
|
2018-02-23 13:08:36 +00:00
|
|
|
r.reference = nullptr;
|
2017-06-20 22:42:29 +00:00
|
|
|
}
|
2018-01-22 21:27:10 +00:00
|
|
|
|
|
|
|
Ref(const Ref &p_from) {
|
|
|
|
|
2018-02-23 13:08:36 +00:00
|
|
|
reference = nullptr;
|
2018-01-22 21:27:10 +00:00
|
|
|
ref(p_from);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T_Other>
|
|
|
|
Ref(const Ref<T_Other> &p_from) {
|
2018-02-23 13:08:36 +00:00
|
|
|
reference = nullptr;
|
2018-01-22 21:27:10 +00:00
|
|
|
Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
|
2020-12-08 22:10:59 +00:00
|
|
|
if (refb == nullptr) {
|
2018-01-22 21:27:10 +00:00
|
|
|
unref();
|
|
|
|
return;
|
2018-01-19 10:40:50 +00:00
|
|
|
}
|
2018-01-22 21:27:10 +00:00
|
|
|
Ref r;
|
2020-12-08 22:10:59 +00:00
|
|
|
r.reference = Object::cast_to<T>(refb);
|
2018-01-22 21:27:10 +00:00
|
|
|
ref(r);
|
2018-02-23 13:08:36 +00:00
|
|
|
r.reference = nullptr;
|
2017-06-19 00:03:59 +00:00
|
|
|
}
|
2017-06-20 22:42:29 +00:00
|
|
|
|
2018-01-22 21:27:10 +00:00
|
|
|
Ref(T *p_reference) {
|
2017-06-20 22:42:29 +00:00
|
|
|
|
2018-01-22 21:27:10 +00:00
|
|
|
if (p_reference)
|
|
|
|
ref_pointer(p_reference);
|
|
|
|
else
|
2018-02-23 13:08:36 +00:00
|
|
|
reference = nullptr;
|
2017-06-19 00:03:59 +00:00
|
|
|
}
|
2017-06-20 22:42:29 +00:00
|
|
|
|
2018-01-22 21:27:10 +00:00
|
|
|
Ref(const Variant &p_variant) {
|
|
|
|
|
2018-02-23 13:08:36 +00:00
|
|
|
reference = nullptr;
|
2020-12-08 22:10:59 +00:00
|
|
|
Object *refb = T::___get_from_variant(p_variant);
|
|
|
|
if (refb == nullptr) {
|
2018-01-22 21:27:10 +00:00
|
|
|
unref();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Ref r;
|
2020-12-08 22:10:59 +00:00
|
|
|
r.reference = Object::cast_to<T>(refb);
|
2018-01-22 21:27:10 +00:00
|
|
|
ref(r);
|
2018-02-23 13:08:36 +00:00
|
|
|
r.reference = nullptr;
|
2017-06-19 00:03:59 +00:00
|
|
|
}
|
2017-06-21 00:14:54 +00:00
|
|
|
|
2018-02-23 13:08:36 +00:00
|
|
|
inline bool is_valid() const { return reference != nullptr; }
|
|
|
|
inline bool is_null() const { return reference == nullptr; }
|
2018-01-22 21:27:10 +00:00
|
|
|
|
|
|
|
void unref() {
|
|
|
|
//TODO this should be moved to mutexes, since this engine does not really
|
|
|
|
// do a lot of referencing on references and stuff
|
|
|
|
// mutexes will avoid more crashes?
|
|
|
|
|
2017-06-19 00:03:59 +00:00
|
|
|
if (reference && reference->unreference()) {
|
2018-01-22 21:27:10 +00:00
|
|
|
|
|
|
|
//memdelete(reference);
|
2018-02-11 14:50:01 +00:00
|
|
|
reference->free();
|
2017-06-19 00:03:59 +00:00
|
|
|
}
|
2018-02-23 13:08:36 +00:00
|
|
|
reference = nullptr;
|
2017-06-19 00:03:59 +00:00
|
|
|
}
|
2018-01-19 10:40:50 +00:00
|
|
|
|
2018-01-22 21:27:10 +00:00
|
|
|
void instance() {
|
|
|
|
//ref(memnew(T));
|
2018-02-11 14:50:01 +00:00
|
|
|
ref(T::_new());
|
2018-01-17 00:54:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 21:27:10 +00:00
|
|
|
Ref() {
|
|
|
|
|
2018-02-23 13:08:36 +00:00
|
|
|
reference = nullptr;
|
2017-06-19 00:03:59 +00:00
|
|
|
}
|
2018-01-22 21:27:10 +00:00
|
|
|
|
|
|
|
~Ref() {
|
|
|
|
|
2017-06-19 00:03:59 +00:00
|
|
|
unref();
|
|
|
|
}
|
2018-01-24 22:16:00 +00:00
|
|
|
|
|
|
|
// Used exclusively in the bindings to recreate the Ref Godot encapsulates in return values,
|
|
|
|
// without adding to the refcount.
|
2018-11-23 22:09:41 +00:00
|
|
|
inline static Ref<T> __internal_constructor(Object *obj) {
|
2018-01-24 22:16:00 +00:00
|
|
|
Ref<T> r;
|
2018-11-23 22:09:41 +00:00
|
|
|
r.reference = (T *)obj;
|
2018-01-24 22:16:00 +00:00
|
|
|
return r;
|
|
|
|
}
|
2017-06-19 00:03:59 +00:00
|
|
|
};
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
} // namespace godot
|
2017-06-19 00:03:59 +00:00
|
|
|
|
|
|
|
#endif
|