Ref.hpp 2.4 KB

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