Ref.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. public:
  10. inline bool operator==(const Ref<T> &r) const
  11. {
  12. return reference == r.reference;
  13. }
  14. inline bool operator!=(const Ref<T> &r) const
  15. {
  16. return reference != r.reference;
  17. }
  18. inline T *operator->()
  19. {
  20. return reference;
  21. }
  22. inline T *operator*()
  23. {
  24. return reference;
  25. }
  26. inline T *ptr()
  27. {
  28. return reference;
  29. }
  30. inline const T *operator->() const
  31. {
  32. return reference;
  33. }
  34. inline const T *operator*() const
  35. {
  36. return reference;
  37. }
  38. inline const T *ptr() const
  39. {
  40. return reference;
  41. }
  42. void operator=(const Ref &from)
  43. {
  44. if (reference)
  45. unref();
  46. if (from.reference) {
  47. reference = from.reference;
  48. reference->reference();
  49. } else {
  50. reference = nullptr;
  51. }
  52. }
  53. template<class T_Other>
  54. void operator=(const Ref<T_Other> &from)
  55. {
  56. if (reference)
  57. unref();
  58. if (from.reference) {
  59. reference = (T *) from.reference;
  60. reference->reference();
  61. } else {
  62. reference = nullptr;
  63. }
  64. }
  65. void operator=(const Variant &variant)
  66. {
  67. if (reference)
  68. unref();
  69. reference = (T *) (Object *) variant;
  70. }
  71. operator Variant() const
  72. {
  73. return Variant((Object *) reference);
  74. }
  75. template<class T_Other>
  76. Ref(const Ref<T_Other> &from)
  77. {
  78. if (from.reference) {
  79. reference = (T *) from.reference;
  80. reference->reference();
  81. } else {
  82. reference = nullptr;
  83. }
  84. }
  85. Ref(const Ref &from)
  86. {
  87. if (from.reference) {
  88. reference = from.reference;
  89. reference->reference();
  90. } else {
  91. reference = nullptr;
  92. }
  93. }
  94. Ref(T *r)
  95. {
  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 = (T *) (Object *) variant;
  103. }
  104. inline bool is_valid() const { return reference != nullptr; }
  105. inline bool is_null() const { return reference == nullptr; }
  106. void unref()
  107. {
  108. if (reference && reference->unreference()) {
  109. godot::api->godot_object_destroy((godot_object *) reference);
  110. }
  111. reference = nullptr;
  112. }
  113. void instance()
  114. {
  115. unref();
  116. reference = new T;
  117. }
  118. Ref()
  119. {
  120. reference = nullptr;
  121. }
  122. ~Ref()
  123. {
  124. unref();
  125. }
  126. };
  127. }
  128. #endif