ref.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**************************************************************************/
  2. /* ref.hpp */
  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. #ifndef GODOT_REF_HPP
  31. #define GODOT_REF_HPP
  32. #include <godot_cpp/core/defs.hpp>
  33. #include <godot_cpp/classes/object.hpp>
  34. #include <godot_cpp/classes/ref_counted.hpp>
  35. #include <godot_cpp/core/binder_common.hpp>
  36. #include <godot_cpp/core/memory.hpp>
  37. #include <godot_cpp/variant/variant.hpp>
  38. namespace godot {
  39. // Helper class for RefCounted objects, same as Godot one.
  40. class RefCounted;
  41. template <typename T>
  42. class Ref {
  43. T *reference = nullptr;
  44. void ref(const Ref &p_from) {
  45. if (p_from.reference == reference) {
  46. return;
  47. }
  48. unref();
  49. reference = p_from.reference;
  50. if (reference) {
  51. reference->reference();
  52. }
  53. }
  54. void ref_pointer(T *p_ref) {
  55. ERR_FAIL_NULL(p_ref);
  56. if (p_ref->init_ref()) {
  57. reference = p_ref;
  58. }
  59. }
  60. public:
  61. _FORCE_INLINE_ bool operator==(const T *p_ptr) const {
  62. return reference == p_ptr;
  63. }
  64. _FORCE_INLINE_ bool operator!=(const T *p_ptr) const {
  65. return reference != p_ptr;
  66. }
  67. _FORCE_INLINE_ bool operator<(const Ref<T> &p_r) const {
  68. return reference < p_r.reference;
  69. }
  70. _FORCE_INLINE_ bool operator==(const Ref<T> &p_r) const {
  71. return reference == p_r.reference;
  72. }
  73. _FORCE_INLINE_ bool operator!=(const Ref<T> &p_r) const {
  74. return reference != p_r.reference;
  75. }
  76. _FORCE_INLINE_ T *operator*() const {
  77. return reference;
  78. }
  79. _FORCE_INLINE_ T *operator->() const {
  80. return reference;
  81. }
  82. _FORCE_INLINE_ T *ptr() const {
  83. return reference;
  84. }
  85. operator Variant() const {
  86. return Variant(reference);
  87. }
  88. void operator=(const Ref &p_from) {
  89. ref(p_from);
  90. }
  91. template <typename T_Other>
  92. void operator=(const Ref<T_Other> &p_from) {
  93. RefCounted *refb = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_from.ptr()));
  94. if (!refb) {
  95. unref();
  96. return;
  97. }
  98. Ref r;
  99. r.reference = Object::cast_to<T>(refb);
  100. ref(r);
  101. r.reference = nullptr;
  102. }
  103. void operator=(const Variant &p_variant) {
  104. // Needs testing, Variant has a cast to Object * here.
  105. // Object *object = p_variant.get_validated_object();
  106. Object *object = p_variant;
  107. if (object == reference) {
  108. return;
  109. }
  110. unref();
  111. if (!object) {
  112. return;
  113. }
  114. T *r = Object::cast_to<T>(object);
  115. if (r && r->reference()) {
  116. reference = r;
  117. }
  118. }
  119. template <typename T_Other>
  120. void reference_ptr(T_Other *p_ptr) {
  121. if (reference == p_ptr) {
  122. return;
  123. }
  124. unref();
  125. T *r = Object::cast_to<T>(p_ptr);
  126. if (r) {
  127. ref_pointer(r);
  128. }
  129. }
  130. Ref(const Ref &p_from) {
  131. ref(p_from);
  132. }
  133. template <typename T_Other>
  134. Ref(const Ref<T_Other> &p_from) {
  135. RefCounted *refb = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_from.ptr()));
  136. if (!refb) {
  137. unref();
  138. return;
  139. }
  140. Ref r;
  141. r.reference = Object::cast_to<T>(refb);
  142. ref(r);
  143. r.reference = nullptr;
  144. }
  145. Ref(T *p_reference) {
  146. if (p_reference) {
  147. ref_pointer(p_reference);
  148. }
  149. }
  150. Ref(const Variant &p_variant) {
  151. // Needs testing, Variant has a cast to Object * here.
  152. // Object *object = p_variant.get_validated_object();
  153. Object *object = p_variant;
  154. if (!object) {
  155. return;
  156. }
  157. T *r = Object::cast_to<T>(object);
  158. if (r && r->reference()) {
  159. reference = r;
  160. }
  161. }
  162. inline bool is_valid() const { return reference != nullptr; }
  163. inline bool is_null() const { return reference == nullptr; }
  164. void unref() {
  165. if (reference && reference->unreference()) {
  166. memdelete(reference);
  167. }
  168. reference = nullptr;
  169. }
  170. void instantiate() {
  171. ref(memnew(T()));
  172. }
  173. Ref() {}
  174. ~Ref() {
  175. unref();
  176. }
  177. // Used exclusively in the bindings to recreate the Ref Godot encapsulates in return values,
  178. // without adding to the refcount.
  179. inline static Ref<T> _gde_internal_constructor(Object *obj) {
  180. Ref<T> r;
  181. r.reference = (T *)obj;
  182. return r;
  183. }
  184. };
  185. template <typename T>
  186. struct PtrToArg<Ref<T>> {
  187. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  188. GDExtensionRefPtr ref = (GDExtensionRefPtr)p_ptr;
  189. ERR_FAIL_NULL_V(p_ptr, Ref<T>());
  190. return Ref<T>(reinterpret_cast<T *>(godot::internal::get_object_instance_binding(godot::internal::gdextension_interface_ref_get_object(ref))));
  191. }
  192. typedef Ref<T> EncodeT;
  193. _FORCE_INLINE_ static void encode(Ref<T> p_val, void *p_ptr) {
  194. GDExtensionRefPtr ref = (GDExtensionRefPtr)p_ptr;
  195. ERR_FAIL_NULL(ref);
  196. // This code assumes that p_ptr points to an unset Ref<T> variable on the Godot side
  197. // so we only set it if we have an object to set.
  198. if (p_val.is_valid()) {
  199. godot::internal::gdextension_interface_ref_set_object(ref, p_val->_owner);
  200. }
  201. }
  202. };
  203. template <typename T>
  204. struct PtrToArg<const Ref<T> &> {
  205. typedef Ref<T> EncodeT;
  206. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  207. GDExtensionRefPtr ref = const_cast<GDExtensionRefPtr>(p_ptr);
  208. ERR_FAIL_NULL_V(p_ptr, Ref<T>());
  209. return Ref<T>(reinterpret_cast<T *>(godot::internal::get_object_instance_binding(godot::internal::gdextension_interface_ref_get_object(ref))));
  210. }
  211. };
  212. template <typename T>
  213. struct GetTypeInfo<Ref<T>, typename EnableIf<TypeInherits<RefCounted, T>::value>::type> {
  214. static const GDExtensionVariantType VARIANT_TYPE = GDEXTENSION_VARIANT_TYPE_OBJECT;
  215. static const GDExtensionClassMethodArgumentMetadata METADATA = GDEXTENSION_METHOD_ARGUMENT_METADATA_NONE;
  216. static inline PropertyInfo get_class_info() {
  217. return make_property_info(Variant::Type::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
  218. }
  219. };
  220. template <typename T>
  221. struct GetTypeInfo<const Ref<T> &, typename EnableIf<TypeInherits<RefCounted, T>::value>::type> {
  222. static const GDExtensionVariantType VARIANT_TYPE = GDEXTENSION_VARIANT_TYPE_OBJECT;
  223. static const GDExtensionClassMethodArgumentMetadata METADATA = GDEXTENSION_METHOD_ARGUMENT_METADATA_NONE;
  224. static inline PropertyInfo get_class_info() {
  225. return make_property_info(Variant::Type::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, T::get_class_static());
  226. }
  227. };
  228. } // namespace godot
  229. #endif // GODOT_REF_HPP