Ref.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 REF_H
  31. #define REF_H
  32. #include "GodotGlobal.hpp"
  33. #include "Reference.hpp"
  34. #include "Variant.hpp"
  35. namespace godot {
  36. // Replicates Godot's Ref<T> behavior
  37. // Rewritten from f5234e70be7dec4930c2d5a0e829ff480d044b1d.
  38. template <class T>
  39. class Ref {
  40. // TODO For this nice check to work, each class must actually #include Reference classes mentionned in its methods,
  41. // which might be annoying for coders who prefer to forward-declare to reduce compile times
  42. // static_assert(std::is_base_of<Reference, T>::value,
  43. // "Ref<T> can only be used with classes deriving from Reference");
  44. T *reference = nullptr;
  45. void ref(const Ref &p_from) {
  46. if (p_from.reference == reference)
  47. return;
  48. unref();
  49. reference = p_from.reference;
  50. if (reference)
  51. reference->reference();
  52. }
  53. void ref_pointer(T *p_ref) {
  54. ERR_FAIL_COND(p_ref == nullptr);
  55. if (p_ref->init_ref())
  56. reference = p_ref;
  57. }
  58. public:
  59. inline bool operator<(const Ref<T> &p_r) const {
  60. return reference < p_r.reference;
  61. }
  62. inline bool operator==(const Ref<T> &p_r) const {
  63. return reference == p_r.reference;
  64. }
  65. inline bool operator!=(const Ref<T> &p_r) const {
  66. return reference != p_r.reference;
  67. }
  68. inline T *operator->() {
  69. return reference;
  70. }
  71. inline T *operator*() {
  72. return reference;
  73. }
  74. inline const T *operator->() const {
  75. return reference;
  76. }
  77. inline const T *ptr() const {
  78. return reference;
  79. }
  80. inline T *ptr() {
  81. return reference;
  82. }
  83. inline const T *operator*() const {
  84. return reference;
  85. }
  86. operator Variant() const {
  87. // Note: the C API handles the cases where the object is a Reference,
  88. // so the Variant will be correctly constructed with a RefPtr engine-side
  89. return Variant((Object *)reference);
  90. }
  91. void operator=(const Ref &p_from) {
  92. ref(p_from);
  93. }
  94. template <class T_Other>
  95. void operator=(const Ref<T_Other> &p_from) {
  96. Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
  97. if (refb == nullptr) {
  98. unref();
  99. return;
  100. }
  101. Ref r;
  102. r.reference = Object::cast_to<T>(refb);
  103. ref(r);
  104. r.reference = nullptr;
  105. }
  106. void operator=(const Variant &p_variant) {
  107. Object *refb = T::___get_from_variant(p_variant);
  108. if (refb == nullptr) {
  109. unref();
  110. return;
  111. }
  112. Ref r;
  113. r.reference = Object::cast_to<T>(refb);
  114. ref(r);
  115. r.reference = nullptr;
  116. }
  117. Ref(const Ref &p_from) {
  118. reference = nullptr;
  119. ref(p_from);
  120. }
  121. template <class T_Other>
  122. Ref(const Ref<T_Other> &p_from) {
  123. reference = nullptr;
  124. Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
  125. if (refb == nullptr) {
  126. unref();
  127. return;
  128. }
  129. Ref r;
  130. r.reference = Object::cast_to<T>(refb);
  131. ref(r);
  132. r.reference = nullptr;
  133. }
  134. Ref(T *p_reference) {
  135. if (p_reference)
  136. ref_pointer(p_reference);
  137. else
  138. reference = nullptr;
  139. }
  140. Ref(const Variant &p_variant) {
  141. reference = nullptr;
  142. Object *refb = T::___get_from_variant(p_variant);
  143. if (refb == nullptr) {
  144. unref();
  145. return;
  146. }
  147. Ref r;
  148. r.reference = Object::cast_to<T>(refb);
  149. ref(r);
  150. r.reference = nullptr;
  151. }
  152. inline bool is_valid() const { return reference != nullptr; }
  153. inline bool is_null() const { return reference == nullptr; }
  154. void unref() {
  155. //TODO this should be moved to mutexes, since this engine does not really
  156. // do a lot of referencing on references and stuff
  157. // mutexes will avoid more crashes?
  158. if (reference && reference->unreference()) {
  159. //memdelete(reference);
  160. reference->free();
  161. }
  162. reference = nullptr;
  163. }
  164. void instance() {
  165. //ref(memnew(T));
  166. ref(T::_new());
  167. }
  168. Ref() {
  169. reference = nullptr;
  170. }
  171. ~Ref() {
  172. unref();
  173. }
  174. // Used exclusively in the bindings to recreate the Ref Godot encapsulates in return values,
  175. // without adding to the refcount.
  176. inline static Ref<T> __internal_constructor(Object *obj) {
  177. Ref<T> r;
  178. r.reference = (T *)obj;
  179. return r;
  180. }
  181. };
  182. } // namespace godot
  183. #endif