ref.hpp 7.9 KB

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