2017-06-19 00:03:59 +00:00
|
|
|
#ifndef REF_H
|
|
|
|
#define REF_H
|
|
|
|
|
|
|
|
#include "Variant.hpp"
|
2017-10-20 23:42:10 +00:00
|
|
|
#include "GodotGlobal.hpp"
|
2017-06-19 00:03:59 +00:00
|
|
|
|
|
|
|
namespace godot {
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class Ref {
|
|
|
|
T *reference;
|
|
|
|
|
|
|
|
void ref(const Ref &from)
|
|
|
|
{
|
|
|
|
if (from.reference == reference) return;
|
|
|
|
|
|
|
|
unref();
|
|
|
|
|
|
|
|
reference = from.reference;
|
|
|
|
if (reference) reference->reference();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ref_pointer(T *r)
|
|
|
|
{
|
|
|
|
if (!r) return;
|
|
|
|
|
|
|
|
if (r->init_ref()) reference = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
inline bool operator==(const Ref<T> &r) const
|
|
|
|
{
|
|
|
|
return reference == r.reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(const Ref<T> &r) const
|
|
|
|
{
|
|
|
|
return reference != r.reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline T *operator->()
|
|
|
|
{
|
|
|
|
return reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline T *operator*()
|
|
|
|
{
|
|
|
|
return reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline T *ptr()
|
|
|
|
{
|
|
|
|
return reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const T *operator->() const
|
|
|
|
{
|
|
|
|
return reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const T *operator*() const
|
|
|
|
{
|
|
|
|
return reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const T *ptr() const
|
|
|
|
{
|
|
|
|
return reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void operator=(const Ref &from)
|
|
|
|
{
|
|
|
|
ref(from);
|
|
|
|
}
|
2017-06-20 22:42:29 +00:00
|
|
|
|
|
|
|
template<class T_Other>
|
|
|
|
void operator=(const Ref<T_Other> &from)
|
|
|
|
{
|
|
|
|
Ref<T> n((T *) from.ptr());
|
|
|
|
ref(n);
|
|
|
|
}
|
2017-06-19 00:03:59 +00:00
|
|
|
|
|
|
|
void operator=(const Variant &variant)
|
|
|
|
{
|
2017-06-20 22:42:29 +00:00
|
|
|
T *r = (T *) (Object *) variant;
|
2017-06-19 00:03:59 +00:00
|
|
|
if (!r) {
|
|
|
|
unref();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref re;
|
|
|
|
re.reference = r;
|
|
|
|
ref(re);
|
|
|
|
re.reference = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator Variant() const
|
|
|
|
{
|
2017-06-20 22:42:29 +00:00
|
|
|
return Variant((Object *) reference);
|
2017-06-19 00:03:59 +00:00
|
|
|
}
|
|
|
|
|
2017-06-20 22:42:29 +00:00
|
|
|
template<class T_Other>
|
|
|
|
Ref(const Ref<T_Other> &from)
|
|
|
|
{
|
|
|
|
if (from.ptr())
|
|
|
|
ref_pointer((T *) from.ptr());
|
|
|
|
else
|
|
|
|
reference = nullptr;
|
|
|
|
}
|
2017-06-19 00:03:59 +00:00
|
|
|
|
|
|
|
Ref(const Ref &from)
|
|
|
|
{
|
|
|
|
reference = nullptr;
|
|
|
|
ref(from);
|
|
|
|
}
|
2017-06-20 22:42:29 +00:00
|
|
|
|
|
|
|
|
2017-06-19 00:03:59 +00:00
|
|
|
Ref(T *r)
|
|
|
|
{
|
2017-06-21 00:14:54 +00:00
|
|
|
r->reference();
|
|
|
|
reference = r;
|
2017-06-19 00:03:59 +00:00
|
|
|
}
|
|
|
|
|
2017-06-20 22:42:29 +00:00
|
|
|
template<class T_Other>
|
|
|
|
Ref(T_Other *r) : Ref((T *) r) {}
|
|
|
|
|
2017-06-19 00:03:59 +00:00
|
|
|
Ref(const Variant &variant)
|
|
|
|
{
|
|
|
|
reference = nullptr;
|
2017-06-20 22:42:29 +00:00
|
|
|
T *r = (T *) (Object *) variant;
|
2017-06-19 00:03:59 +00:00
|
|
|
if (!r) {
|
|
|
|
unref();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref re;
|
|
|
|
re.reference = r;
|
|
|
|
ref(re);
|
|
|
|
re.reference = nullptr;
|
|
|
|
}
|
2017-06-21 00:14:54 +00:00
|
|
|
|
|
|
|
template<class T_Other>
|
|
|
|
static Ref<T> __internal_constructor(T_Other *r)
|
|
|
|
{
|
|
|
|
Ref<T> ref;
|
|
|
|
ref.reference = (T *) r;
|
|
|
|
return ref;
|
|
|
|
}
|
2017-06-19 00:03:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
inline bool is_valid() const { return reference != nullptr; }
|
|
|
|
inline bool is_null() const { return reference == nullptr; }
|
|
|
|
|
|
|
|
void unref()
|
|
|
|
{
|
|
|
|
if (reference && reference->unreference()) {
|
2017-10-20 23:42:10 +00:00
|
|
|
godot::api->godot_object_destroy((godot_object *) reference);
|
2017-06-19 00:03:59 +00:00
|
|
|
}
|
|
|
|
reference = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-01-17 00:54:02 +00:00
|
|
|
void instance()
|
|
|
|
{
|
|
|
|
ref(new T);
|
|
|
|
}
|
|
|
|
|
2017-06-19 00:03:59 +00:00
|
|
|
Ref()
|
|
|
|
{
|
|
|
|
reference = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Ref()
|
|
|
|
{
|
|
|
|
unref();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|