Ref.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. void operator=(const Variant &variant)
  67. {
  68. T *r = variant;
  69. if (!r) {
  70. unref();
  71. return;
  72. }
  73. Ref re;
  74. re.reference = r;
  75. ref(re);
  76. re.reference = nullptr;
  77. }
  78. operator Variant() const
  79. {
  80. ref();
  81. return Variant((Object *) this);
  82. }
  83. Ref(const Ref &from)
  84. {
  85. reference = nullptr;
  86. ref(from);
  87. }
  88. Ref(T *r)
  89. {
  90. if (r)
  91. ref_pointer(r);
  92. else
  93. reference = nullptr;
  94. }
  95. Ref(const Variant &variant)
  96. {
  97. reference = nullptr;
  98. T *r = variant;
  99. if (!r) {
  100. unref();
  101. return;
  102. }
  103. Ref re;
  104. re.reference = r;
  105. ref(re);
  106. re.reference = nullptr;
  107. }
  108. inline bool is_valid() const { return reference != nullptr; }
  109. inline bool is_null() const { return reference == nullptr; }
  110. void unref()
  111. {
  112. if (reference && reference->unreference()) {
  113. godot_object_destroy((godot_object *) reference);
  114. }
  115. reference = nullptr;
  116. }
  117. Ref()
  118. {
  119. reference = nullptr;
  120. }
  121. ~Ref()
  122. {
  123. unref();
  124. }
  125. };
  126. }
  127. #endif