ref.hpp 8.0 KB

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