ref.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. // FIXME
  113. // Object *object = p_variant.get_validated_object();
  114. // if (object == reference) {
  115. // return;
  116. // }
  117. // unref();
  118. // if (!object) {
  119. // return;
  120. // }
  121. // T *r = Object::cast_to<T>(object);
  122. // if (r && r->reference()) {
  123. // reference = r;
  124. // }
  125. }
  126. template <class T_Other>
  127. void reference_ptr(T_Other *p_ptr) {
  128. if (reference == p_ptr) {
  129. return;
  130. }
  131. unref();
  132. T *r = Object::cast_to<T>(p_ptr);
  133. if (r) {
  134. ref_pointer(r);
  135. }
  136. }
  137. Ref(const Ref &p_from) {
  138. ref(p_from);
  139. }
  140. Ref(T *p_reference) {
  141. if (p_reference) {
  142. ref_pointer(p_reference);
  143. }
  144. }
  145. Ref(const Variant &p_variant) {
  146. // FIXME
  147. // Object *object = p_variant.get_validated_object();
  148. // if (!object) {
  149. // return;
  150. // }
  151. // T *r = Object::cast_to<T>(object);
  152. // if (r && r->reference()) {
  153. // reference = r;
  154. // }
  155. }
  156. inline bool is_valid() const { return reference != nullptr; }
  157. inline bool is_null() const { return reference == nullptr; }
  158. void unref() {
  159. if (reference && reference->unreference()) {
  160. memdelete(reference);
  161. }
  162. reference = nullptr;
  163. }
  164. void instantiate() {
  165. ref(memnew(T));
  166. }
  167. Ref() {}
  168. ~Ref() {
  169. unref();
  170. }
  171. // Used exclusively in the bindings to recreate the Ref Godot encapsulates in return values,
  172. // without adding to the refcount.
  173. inline static Ref<T> ___internal_constructor(Object *obj) {
  174. Ref<T> r;
  175. r.reference = (T *)obj;
  176. return r;
  177. }
  178. };
  179. } // namespace godot
  180. #endif // ! GODOT_CPP_REF_HPP