ref.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*************************************************************************/
  2. /* ref.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_CPP_REF_HPP
  31. #define GODOT_CPP_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/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 <class 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_COND(!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->() {
  76. return reference;
  77. }
  78. _FORCE_INLINE_ T *operator*() {
  79. return reference;
  80. }
  81. _FORCE_INLINE_ const T *operator->() const {
  82. return reference;
  83. }
  84. _FORCE_INLINE_ const T *ptr() const {
  85. return reference;
  86. }
  87. _FORCE_INLINE_ T *ptr() {
  88. return reference;
  89. }
  90. _FORCE_INLINE_ const T *operator*() const {
  91. return reference;
  92. }
  93. operator Variant() const {
  94. return Variant(reference);
  95. }
  96. void operator=(const Ref &p_from) {
  97. ref(p_from);
  98. }
  99. template <class T_Other>
  100. void operator=(const Ref<T_Other> &p_from) {
  101. RefCounted *refb = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_from.ptr()));
  102. if (!refb) {
  103. unref();
  104. return;
  105. }
  106. Ref r;
  107. r.reference = Object::cast_to<T>(refb);
  108. ref(r);
  109. r.reference = nullptr;
  110. }
  111. void operator=(const Variant &p_variant) {
  112. // Needs testing, Variant has a cast to Object * here.
  113. // Object *object = p_variant.get_validated_object();
  114. Object *object = p_variant;
  115. if (object == reference) {
  116. return;
  117. }
  118. unref();
  119. if (!object) {
  120. return;
  121. }
  122. T *r = Object::cast_to<T>(object);
  123. if (r && r->reference()) {
  124. reference = r;
  125. }
  126. }
  127. template <class T_Other>
  128. void reference_ptr(T_Other *p_ptr) {
  129. if (reference == p_ptr) {
  130. return;
  131. }
  132. unref();
  133. T *r = Object::cast_to<T>(p_ptr);
  134. if (r) {
  135. ref_pointer(r);
  136. }
  137. }
  138. Ref(const Ref &p_from) {
  139. ref(p_from);
  140. }
  141. template <class T_Other>
  142. Ref(const Ref<T_Other> &p_from) {
  143. RefCounted *refb = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_from.ptr()));
  144. if (!refb) {
  145. unref();
  146. return;
  147. }
  148. Ref r;
  149. r.reference = Object::cast_to<T>(refb);
  150. ref(r);
  151. r.reference = nullptr;
  152. }
  153. Ref(T *p_reference) {
  154. if (p_reference) {
  155. ref_pointer(p_reference);
  156. }
  157. }
  158. Ref(const Variant &p_variant) {
  159. // Needs testing, Variant has a cast to Object * here.
  160. // Object *object = p_variant.get_validated_object();
  161. Object *object = p_variant;
  162. if (!object) {
  163. return;
  164. }
  165. T *r = Object::cast_to<T>(object);
  166. if (r && r->reference()) {
  167. reference = r;
  168. }
  169. }
  170. inline bool is_valid() const { return reference != nullptr; }
  171. inline bool is_null() const { return reference == nullptr; }
  172. void unref() {
  173. if (reference && reference->unreference()) {
  174. memdelete(reference);
  175. }
  176. reference = nullptr;
  177. }
  178. void instantiate() {
  179. ref(memnew(T()));
  180. }
  181. Ref() {}
  182. ~Ref() {
  183. unref();
  184. }
  185. // Used exclusively in the bindings to recreate the Ref Godot encapsulates in return values,
  186. // without adding to the refcount.
  187. inline static Ref<T> ___internal_constructor(Object *obj) {
  188. Ref<T> r;
  189. r.reference = (T *)obj;
  190. return r;
  191. }
  192. };
  193. } // namespace godot
  194. #endif // ! GODOT_CPP_REF_HPP