rid.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**************************************************************************/
  2. /* rid.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef RID_H
  31. #define RID_H
  32. #include "core/list.h"
  33. #include "core/os/memory.h"
  34. #include "core/rid_handle.h"
  35. #include "core/safe_refcount.h"
  36. #include "core/set.h"
  37. #include "core/typedefs.h"
  38. #ifndef RID_HANDLES_ENABLED
  39. class RID_OwnerBase;
  40. class RID_Data {
  41. friend class RID_OwnerBase;
  42. #ifndef DEBUG_ENABLED
  43. RID_OwnerBase *_owner;
  44. #endif
  45. uint32_t _id;
  46. public:
  47. _FORCE_INLINE_ uint32_t get_id() const { return _id; }
  48. virtual ~RID_Data();
  49. };
  50. class RID {
  51. friend class RID_OwnerBase;
  52. mutable RID_Data *_data;
  53. public:
  54. _FORCE_INLINE_ RID_Data *get_data() const { return _data; }
  55. _FORCE_INLINE_ bool operator==(const RID &p_rid) const {
  56. return _data == p_rid._data;
  57. }
  58. _FORCE_INLINE_ bool operator<(const RID &p_rid) const {
  59. return _data < p_rid._data;
  60. }
  61. _FORCE_INLINE_ bool operator<=(const RID &p_rid) const {
  62. return _data <= p_rid._data;
  63. }
  64. _FORCE_INLINE_ bool operator>(const RID &p_rid) const {
  65. return _data > p_rid._data;
  66. }
  67. _FORCE_INLINE_ bool operator!=(const RID &p_rid) const {
  68. return _data != p_rid._data;
  69. }
  70. _FORCE_INLINE_ bool is_valid() const { return _data != nullptr; }
  71. _FORCE_INLINE_ uint32_t get_id() const { return _data ? _data->get_id() : 0; }
  72. _FORCE_INLINE_ RID() {
  73. _data = nullptr;
  74. }
  75. };
  76. class RID_OwnerBase {
  77. protected:
  78. static SafeRefCount refcount;
  79. _FORCE_INLINE_ void _set_data(RID &p_rid, RID_Data *p_data) {
  80. p_rid._data = p_data;
  81. p_data->_id = refcount.refval();
  82. #ifndef DEBUG_ENABLED
  83. p_data->_owner = this;
  84. #endif
  85. }
  86. #ifndef DEBUG_ENABLED
  87. _FORCE_INLINE_ bool _is_owner(const RID &p_rid) const {
  88. return this == p_rid._data->_owner;
  89. }
  90. _FORCE_INLINE_ void _remove_owner(RID &p_rid) {
  91. p_rid._data->_owner = NULL;
  92. }
  93. #endif
  94. public:
  95. virtual void get_owned_list(List<RID> *p_owned) = 0;
  96. static void init_rid();
  97. virtual ~RID_OwnerBase() {}
  98. };
  99. template <class T>
  100. class RID_Owner : public RID_OwnerBase {
  101. public:
  102. #ifdef DEBUG_ENABLED
  103. mutable Set<RID_Data *> id_map;
  104. #endif
  105. public:
  106. _FORCE_INLINE_ RID make_rid(T *p_data) {
  107. RID rid;
  108. _set_data(rid, p_data);
  109. #ifdef DEBUG_ENABLED
  110. id_map.insert(p_data);
  111. #endif
  112. return rid;
  113. }
  114. _FORCE_INLINE_ T *get(const RID &p_rid) {
  115. #ifdef DEBUG_ENABLED
  116. ERR_FAIL_COND_V(!p_rid.is_valid(), nullptr);
  117. ERR_FAIL_COND_V(!id_map.has(p_rid.get_data()), nullptr);
  118. #endif
  119. return static_cast<T *>(p_rid.get_data());
  120. }
  121. _FORCE_INLINE_ T *getornull(const RID &p_rid) {
  122. #ifdef DEBUG_ENABLED
  123. if (p_rid.get_data()) {
  124. ERR_FAIL_COND_V(!id_map.has(p_rid.get_data()), nullptr);
  125. }
  126. #endif
  127. return static_cast<T *>(p_rid.get_data());
  128. }
  129. _FORCE_INLINE_ T *getptr(const RID &p_rid) {
  130. return static_cast<T *>(p_rid.get_data());
  131. }
  132. _FORCE_INLINE_ bool owns(const RID &p_rid) const {
  133. if (p_rid.get_data() == nullptr) {
  134. return false;
  135. }
  136. #ifdef DEBUG_ENABLED
  137. return id_map.has(p_rid.get_data());
  138. #else
  139. return _is_owner(p_rid);
  140. #endif
  141. }
  142. void free(RID p_rid) {
  143. #ifdef DEBUG_ENABLED
  144. id_map.erase(p_rid.get_data());
  145. #else
  146. _remove_owner(p_rid);
  147. #endif
  148. }
  149. void get_owned_list(List<RID> *p_owned) {
  150. #ifdef DEBUG_ENABLED
  151. for (typename Set<RID_Data *>::Element *E = id_map.front(); E; E = E->next()) {
  152. RID r;
  153. _set_data(r, static_cast<T *>(E->get()));
  154. p_owned->push_back(r);
  155. }
  156. #endif
  157. }
  158. };
  159. #endif // not handles
  160. #endif // RID_H