Ref.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. return Variant((Object *) reference);
  87. }
  88. template<class T_Other>
  89. Ref(const Ref<T_Other> &from)
  90. {
  91. if (from.ptr())
  92. ref_pointer((T *) from.ptr());
  93. else
  94. reference = nullptr;
  95. }
  96. Ref(const Ref &from)
  97. {
  98. reference = nullptr;
  99. ref(from);
  100. }
  101. Ref(T *r)
  102. {
  103. r->reference();
  104. reference = r;
  105. }
  106. template<class T_Other>
  107. Ref(T_Other *r) : Ref((T *) r) {}
  108. Ref(const Variant &variant)
  109. {
  110. reference = nullptr;
  111. T *r = (T *) (Object *) variant;
  112. if (!r) {
  113. unref();
  114. return;
  115. }
  116. Ref re;
  117. re.reference = r;
  118. ref(re);
  119. re.reference = nullptr;
  120. }
  121. template<class T_Other>
  122. static Ref<T> __internal_constructor(T_Other *r)
  123. {
  124. Ref<T> ref;
  125. ref.reference = (T *) r;
  126. return ref;
  127. }
  128. inline bool is_valid() const { return reference != nullptr; }
  129. inline bool is_null() const { return reference == nullptr; }
  130. void unref()
  131. {
  132. if (reference && reference->unreference()) {
  133. godot_object_destroy((godot_object *) reference);
  134. }
  135. reference = nullptr;
  136. }
  137. Ref()
  138. {
  139. reference = nullptr;
  140. }
  141. ~Ref()
  142. {
  143. unref();
  144. }
  145. };
  146. }
  147. #endif