ref.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*************************************************************************/
  2. /* ref.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 <class 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_COND(!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->() {
  77. return reference;
  78. }
  79. _FORCE_INLINE_ T *operator*() {
  80. return reference;
  81. }
  82. _FORCE_INLINE_ const T *operator->() const {
  83. return reference;
  84. }
  85. _FORCE_INLINE_ const T *ptr() const {
  86. return reference;
  87. }
  88. _FORCE_INLINE_ T *ptr() {
  89. return reference;
  90. }
  91. _FORCE_INLINE_ const T *operator*() const {
  92. return reference;
  93. }
  94. operator Variant() const {
  95. return Variant(reference);
  96. }
  97. void operator=(const Ref &p_from) {
  98. ref(p_from);
  99. }
  100. template <class T_Other>
  101. void operator=(const Ref<T_Other> &p_from) {
  102. RefCounted *refb = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_from.ptr()));
  103. if (!refb) {
  104. unref();
  105. return;
  106. }
  107. Ref r;
  108. r.reference = Object::cast_to<T>(refb);
  109. ref(r);
  110. r.reference = nullptr;
  111. }
  112. void operator=(const Variant &p_variant) {
  113. // Needs testing, Variant has a cast to Object * here.
  114. // Object *object = p_variant.get_validated_object();
  115. Object *object = p_variant;
  116. if (object == reference) {
  117. return;
  118. }
  119. unref();
  120. if (!object) {
  121. return;
  122. }
  123. T *r = Object::cast_to<T>(object);
  124. if (r && r->reference()) {
  125. reference = r;
  126. }
  127. }
  128. template <class T_Other>
  129. void reference_ptr(T_Other *p_ptr) {
  130. if (reference == p_ptr) {
  131. return;
  132. }
  133. unref();
  134. T *r = Object::cast_to<T>(p_ptr);
  135. if (r) {
  136. ref_pointer(r);
  137. }
  138. }
  139. Ref(const Ref &p_from) {
  140. ref(p_from);
  141. }
  142. template <class T_Other>
  143. Ref(const Ref<T_Other> &p_from) {
  144. RefCounted *refb = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_from.ptr()));
  145. if (!refb) {
  146. unref();
  147. return;
  148. }
  149. Ref r;
  150. r.reference = Object::cast_to<T>(refb);
  151. ref(r);
  152. r.reference = nullptr;
  153. }
  154. Ref(T *p_reference) {
  155. if (p_reference) {
  156. ref_pointer(p_reference);
  157. }
  158. }
  159. Ref(const Variant &p_variant) {
  160. // Needs testing, Variant has a cast to Object * here.
  161. // Object *object = p_variant.get_validated_object();
  162. Object *object = p_variant;
  163. if (!object) {
  164. return;
  165. }
  166. T *r = Object::cast_to<T>(object);
  167. if (r && r->reference()) {
  168. reference = r;
  169. }
  170. }
  171. inline bool is_valid() const { return reference != nullptr; }
  172. inline bool is_null() const { return reference == nullptr; }
  173. void unref() {
  174. if (reference && reference->unreference()) {
  175. memdelete(reference);
  176. }
  177. reference = nullptr;
  178. }
  179. void instantiate() {
  180. ref(memnew(T()));
  181. }
  182. Ref() {}
  183. ~Ref() {
  184. unref();
  185. }
  186. // Used exclusively in the bindings to recreate the Ref Godot encapsulates in return values,
  187. // without adding to the refcount.
  188. inline static Ref<T> ___internal_constructor(Object *obj) {
  189. Ref<T> r;
  190. r.reference = (T *)obj;
  191. return r;
  192. }
  193. };
  194. template <class T>
  195. struct PtrToArg<Ref<T>> {
  196. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  197. return Ref<T>(reinterpret_cast<T *>(godot::internal::gde_interface->object_get_instance_binding(*reinterpret_cast<GDExtensionObjectPtr *>(const_cast<void *>(p_ptr)), godot::internal::token, &T::___binding_callbacks)));
  198. }
  199. typedef Ref<T> EncodeT;
  200. _FORCE_INLINE_ static void encode(Ref<T> p_val, void *p_ptr) {
  201. *reinterpret_cast<const GodotObject **>(p_ptr) = p_val->_owner;
  202. }
  203. };
  204. template <class T>
  205. struct PtrToArg<const Ref<T> &> {
  206. typedef Ref<T> EncodeT;
  207. _FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
  208. return Ref<T>(reinterpret_cast<T *>(godot::internal::gde_interface->object_get_instance_binding(*reinterpret_cast<GDExtensionObjectPtr *>(const_cast<void *>(p_ptr)), godot::internal::token, &T::___binding_callbacks)));
  209. }
  210. };
  211. template <class T>
  212. struct GetTypeInfo<Ref<T>, typename EnableIf<TypeInherits<RefCounted, T>::value>::type> {
  213. static const GDExtensionVariantType VARIANT_TYPE = GDEXTENSION_VARIANT_TYPE_OBJECT;
  214. static const GDExtensionClassMethodArgumentMetadata METADATA = GDEXTENSION_METHOD_ARGUMENT_METADATA_NONE;
  215. static inline PropertyInfo get_class_info() {
  216. return make_property_info(Variant::Type::OBJECT, T::get_class_static());
  217. }
  218. };
  219. template <class T>
  220. struct GetTypeInfo<const Ref<T> &, typename EnableIf<TypeInherits<RefCounted, T>::value>::type> {
  221. static const GDExtensionVariantType VARIANT_TYPE = GDEXTENSION_VARIANT_TYPE_OBJECT;
  222. static const GDExtensionClassMethodArgumentMetadata METADATA = GDEXTENSION_METHOD_ARGUMENT_METADATA_NONE;
  223. static inline PropertyInfo get_class_info() {
  224. return make_property_info(Variant::Type::OBJECT, T::get_class_static());
  225. }
  226. };
  227. } // namespace godot
  228. #endif // GODOT_REF_HPP