godot-cpp/include/core/Ref.hpp

216 lines
3.8 KiB
C++
Raw Normal View History

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"
Nativescript 1.1 implemented instance binding data usage This commit changes the way C++ wrapper classes work. Previously, wrapper classes were merely wrapper *interfaces*. They used the `this` pointer to store the actual foreign Godot Object. With the NativeScript 1.1 extension it is now possible to have low-overhead language binding data attached to Objects. The C++ bindings use that feature to implement *proper* wrappers and enable regular C++ inheritance usage that way. Some things might still be buggy and untested, but the C++ SimpleDemo works with those changes. new and free change, custom free will crash engine, be wary fix exporting of non-object types fix free() crash with custom resources added type tags and safe object casting fix global type registration order fix cast_to changed build system to be more self contained updated .gitignore use typeid() for type tags now fix indentation in bindings generator remove accidentally added files fix gitignore Fixed up registering tool and updated godot_headers Fix crash when calling String::split/split_floats Was casting to the wrong object type. Also adds parse_ints function to String with the same logic Better warning/error macros Change gitignore so we get our gen folders New documentation based on nativescript 1.1 Fixed GODOT_SUBCLASS macro Preventing crash when function returned null ptr Adds needed include <typeinfo> Solves this issue #168 due to not having the include of typeinfo Fix compile error of 'WARN_PRINT' and 'ERR_PRINT'. cannot pass non-trivial object of type 'godot::String' to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs] update vector3::distance_to Remove godot_api.json as its now in the godot_headers submodule (api.json)
2018-02-11 14:50:01 +00:00
#include "Reference.hpp"
2017-06-19 00:03:59 +00:00
namespace godot {
// Replicates Godot's Ref<T> behavior
// Rewritten from f5234e70be7dec4930c2d5a0e829ff480d044b1d.
template <class T>
2017-06-19 00:03:59 +00:00
class Ref {
2018-02-23 13:08:36 +00:00
T *reference = nullptr;
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) {
ERR_FAIL_COND(!p_ref);
if (p_ref->init_ref())
reference = p_ref;
}
2017-06-19 00:03:59 +00:00
public:
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;
}
inline T *operator*() {
2017-06-19 00:03:59 +00:00
return reference;
}
inline const T *operator->() const {
2017-06-19 00:03:59 +00:00
return reference;
}
inline const T *ptr() const {
2017-06-19 00:03:59 +00:00
return reference;
}
inline T *ptr() {
2017-06-19 00:03:59 +00:00
return reference;
}
inline const T *operator*() const {
2017-06-19 00:03:59 +00:00
return reference;
}
2018-01-19 10:40:50 +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
return Variant((Object*)reference);
2017-06-19 00:03:59 +00:00
}
void operator=(const Ref &p_from) {
2018-01-19 10:40:50 +00:00
ref(p_from);
}
2018-01-19 10:40:50 +00:00
template <class T_Other>
void operator=(const Ref<T_Other> &p_from) {
// TODO We need a safe cast
Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
if (!refb) {
unref();
return;
}
Ref r;
//r.reference = Object::cast_to<T>(refb);
r.reference = (T*)refb;
ref(r);
2018-02-23 13:08:36 +00:00
r.reference = nullptr;
2017-06-19 00:03:59 +00:00
}
void operator=(const Variant &p_variant) {
// TODO We need a safe cast
Nativescript 1.1 implemented instance binding data usage This commit changes the way C++ wrapper classes work. Previously, wrapper classes were merely wrapper *interfaces*. They used the `this` pointer to store the actual foreign Godot Object. With the NativeScript 1.1 extension it is now possible to have low-overhead language binding data attached to Objects. The C++ bindings use that feature to implement *proper* wrappers and enable regular C++ inheritance usage that way. Some things might still be buggy and untested, but the C++ SimpleDemo works with those changes. new and free change, custom free will crash engine, be wary fix exporting of non-object types fix free() crash with custom resources added type tags and safe object casting fix global type registration order fix cast_to changed build system to be more self contained updated .gitignore use typeid() for type tags now fix indentation in bindings generator remove accidentally added files fix gitignore Fixed up registering tool and updated godot_headers Fix crash when calling String::split/split_floats Was casting to the wrong object type. Also adds parse_ints function to String with the same logic Better warning/error macros Change gitignore so we get our gen folders New documentation based on nativescript 1.1 Fixed GODOT_SUBCLASS macro Preventing crash when function returned null ptr Adds needed include <typeinfo> Solves this issue #168 due to not having the include of typeinfo Fix compile error of 'WARN_PRINT' and 'ERR_PRINT'. cannot pass non-trivial object of type 'godot::String' to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs] update vector3::distance_to Remove godot_api.json as its now in the godot_headers submodule (api.json)
2018-02-11 14:50:01 +00:00
Reference *refb = (Reference *) T::___get_from_variant(p_variant);
if (!refb) {
unref();
return;
2018-01-19 10:40:50 +00:00
}
Ref r;
// TODO We need a safe cast
//r.reference = Object::cast_to<T>(refb);
r.reference = (T *)refb;
ref(r);
2018-02-23 13:08:36 +00:00
r.reference = nullptr;
}
Ref(const Ref &p_from) {
2018-02-23 13:08:36 +00:00
reference = nullptr;
ref(p_from);
}
template <class T_Other>
Ref(const Ref<T_Other> &p_from) {
2018-02-23 13:08:36 +00:00
reference = nullptr;
// TODO We need a safe cast
Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
if (!refb) {
unref();
return;
2018-01-19 10:40:50 +00:00
}
Ref r;
// TODO We need a safe cast
//r.reference = Object::cast_to<T>(refb);
r.reference = (T *)refb;
ref(r);
2018-02-23 13:08:36 +00:00
r.reference = nullptr;
2017-06-19 00:03:59 +00:00
}
Ref(T *p_reference) {
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
}
Ref(const Variant &p_variant) {
2018-02-23 13:08:36 +00:00
reference = nullptr;
// TODO We need a safe cast
Nativescript 1.1 implemented instance binding data usage This commit changes the way C++ wrapper classes work. Previously, wrapper classes were merely wrapper *interfaces*. They used the `this` pointer to store the actual foreign Godot Object. With the NativeScript 1.1 extension it is now possible to have low-overhead language binding data attached to Objects. The C++ bindings use that feature to implement *proper* wrappers and enable regular C++ inheritance usage that way. Some things might still be buggy and untested, but the C++ SimpleDemo works with those changes. new and free change, custom free will crash engine, be wary fix exporting of non-object types fix free() crash with custom resources added type tags and safe object casting fix global type registration order fix cast_to changed build system to be more self contained updated .gitignore use typeid() for type tags now fix indentation in bindings generator remove accidentally added files fix gitignore Fixed up registering tool and updated godot_headers Fix crash when calling String::split/split_floats Was casting to the wrong object type. Also adds parse_ints function to String with the same logic Better warning/error macros Change gitignore so we get our gen folders New documentation based on nativescript 1.1 Fixed GODOT_SUBCLASS macro Preventing crash when function returned null ptr Adds needed include <typeinfo> Solves this issue #168 due to not having the include of typeinfo Fix compile error of 'WARN_PRINT' and 'ERR_PRINT'. cannot pass non-trivial object of type 'godot::String' to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs] update vector3::distance_to Remove godot_api.json as its now in the godot_headers submodule (api.json)
2018-02-11 14:50:01 +00:00
Reference *refb = (Reference *) T::___get_from_variant(p_variant);
if (!refb) {
unref();
return;
}
Ref r;
// TODO We need a safe cast
//r.reference = Object::cast_to<T>(refb);
r.reference = (T *)refb;
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; }
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()) {
//memdelete(reference);
Nativescript 1.1 implemented instance binding data usage This commit changes the way C++ wrapper classes work. Previously, wrapper classes were merely wrapper *interfaces*. They used the `this` pointer to store the actual foreign Godot Object. With the NativeScript 1.1 extension it is now possible to have low-overhead language binding data attached to Objects. The C++ bindings use that feature to implement *proper* wrappers and enable regular C++ inheritance usage that way. Some things might still be buggy and untested, but the C++ SimpleDemo works with those changes. new and free change, custom free will crash engine, be wary fix exporting of non-object types fix free() crash with custom resources added type tags and safe object casting fix global type registration order fix cast_to changed build system to be more self contained updated .gitignore use typeid() for type tags now fix indentation in bindings generator remove accidentally added files fix gitignore Fixed up registering tool and updated godot_headers Fix crash when calling String::split/split_floats Was casting to the wrong object type. Also adds parse_ints function to String with the same logic Better warning/error macros Change gitignore so we get our gen folders New documentation based on nativescript 1.1 Fixed GODOT_SUBCLASS macro Preventing crash when function returned null ptr Adds needed include <typeinfo> Solves this issue #168 due to not having the include of typeinfo Fix compile error of 'WARN_PRINT' and 'ERR_PRINT'. cannot pass non-trivial object of type 'godot::String' to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs] update vector3::distance_to Remove godot_api.json as its now in the godot_headers submodule (api.json)
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
void instance() {
//ref(memnew(T));
Nativescript 1.1 implemented instance binding data usage This commit changes the way C++ wrapper classes work. Previously, wrapper classes were merely wrapper *interfaces*. They used the `this` pointer to store the actual foreign Godot Object. With the NativeScript 1.1 extension it is now possible to have low-overhead language binding data attached to Objects. The C++ bindings use that feature to implement *proper* wrappers and enable regular C++ inheritance usage that way. Some things might still be buggy and untested, but the C++ SimpleDemo works with those changes. new and free change, custom free will crash engine, be wary fix exporting of non-object types fix free() crash with custom resources added type tags and safe object casting fix global type registration order fix cast_to changed build system to be more self contained updated .gitignore use typeid() for type tags now fix indentation in bindings generator remove accidentally added files fix gitignore Fixed up registering tool and updated godot_headers Fix crash when calling String::split/split_floats Was casting to the wrong object type. Also adds parse_ints function to String with the same logic Better warning/error macros Change gitignore so we get our gen folders New documentation based on nativescript 1.1 Fixed GODOT_SUBCLASS macro Preventing crash when function returned null ptr Adds needed include <typeinfo> Solves this issue #168 due to not having the include of typeinfo Fix compile error of 'WARN_PRINT' and 'ERR_PRINT'. cannot pass non-trivial object of type 'godot::String' to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs] update vector3::distance_to Remove godot_api.json as its now in the godot_headers submodule (api.json)
2018-02-11 14:50:01 +00:00
ref(T::_new());
2018-01-17 00:54:02 +00:00
}
Ref() {
2018-02-23 13:08:36 +00:00
reference = nullptr;
2017-06-19 00:03:59 +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.
inline static Ref<T> __internal_constructor(Object *obj)
{
Ref<T> r;
r.reference = (T*)obj;
return r;
}
2017-06-19 00:03:59 +00:00
};
}
#endif