Ref.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #ifndef REF_H
  2. #define REF_H
  3. #include "Variant.hpp"
  4. #include "GodotGlobal.hpp"
  5. namespace godot {
  6. template<class T>
  7. class Ref {
  8. T *reference;
  9. void ref(const Ref &from)
  10. {
  11. if (from.reference == reference) return;
  12. unref();
  13. reference = from.reference;
  14. if (reference) reference->reference();
  15. }
  16. void ref_pointer(T *r)
  17. {
  18. if (!r) return;
  19. if (r->init_ref()) reference = r;
  20. }
  21. public:
  22. inline bool operator==(const Ref<T> &r) const
  23. {
  24. return reference == r.reference;
  25. }
  26. inline bool operator!=(const Ref<T> &r) const
  27. {
  28. return reference != r.reference;
  29. }
  30. inline T *operator->()
  31. {
  32. return reference;
  33. }
  34. inline T *operator*()
  35. {
  36. return reference;
  37. }
  38. inline T *ptr()
  39. {
  40. return reference;
  41. }
  42. inline const T *operator->() const
  43. {
  44. return reference;
  45. }
  46. inline const T *operator*() const
  47. {
  48. return reference;
  49. }
  50. inline const T *ptr() const
  51. {
  52. return reference;
  53. }
  54. void operator=(const Ref &from)
  55. {
  56. ref(from);
  57. }
  58. template<class T_Other>
  59. void operator=(const Ref<T_Other> &from)
  60. {
  61. Ref<T> n((T *) from.ptr());
  62. ref(n);
  63. }
  64. void operator=(const Variant &variant)
  65. {
  66. T *r = (T *) (Object *) variant;
  67. if (!r) {
  68. unref();
  69. return;
  70. }
  71. Ref re;
  72. re.reference = r;
  73. ref(re);
  74. re.reference = nullptr;
  75. }
  76. operator Variant() const
  77. {
  78. return Variant((Object *) reference);
  79. }
  80. template<class T_Other>
  81. Ref(const Ref<T_Other> &from)
  82. {
  83. if (from.ptr())
  84. ref_pointer((T *) from.ptr());
  85. else
  86. reference = nullptr;
  87. }
  88. Ref(const Ref &from)
  89. {
  90. reference = nullptr;
  91. ref(from);
  92. }
  93. Ref(T *r)
  94. {
  95. r->reference();
  96. reference = r;
  97. }
  98. template<class T_Other>
  99. Ref(T_Other *r) : Ref((T *) r) {}
  100. Ref(const Variant &variant)
  101. {
  102. reference = nullptr;
  103. T *r = (T *) (Object *) variant;
  104. if (!r) {
  105. unref();
  106. return;
  107. }
  108. Ref re;
  109. re.reference = r;
  110. ref(re);
  111. re.reference = nullptr;
  112. }
  113. template<class T_Other>
  114. static Ref<T> __internal_constructor(T_Other *r)
  115. {
  116. Ref<T> ref;
  117. ref.reference = (T *) r;
  118. return ref;
  119. }
  120. inline bool is_valid() const { return reference != nullptr; }
  121. inline bool is_null() const { return reference == nullptr; }
  122. void unref()
  123. {
  124. if (reference && reference->unreference()) {
  125. godot::api->godot_object_destroy((godot_object *) reference);
  126. }
  127. reference = nullptr;
  128. }
  129. Ref()
  130. {
  131. reference = nullptr;
  132. }
  133. ~Ref()
  134. {
  135. unref();
  136. }
  137. };
  138. }
  139. #endif