rid.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*************************************************************************/
  2. /* rid.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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/safe_refcount.h"
  35. #include "core/set.h"
  36. #include "core/typedefs.h"
  37. class RID_OwnerBase;
  38. class RID_Data {
  39. friend class RID_OwnerBase;
  40. #ifndef DEBUG_ENABLED
  41. RID_OwnerBase *_owner;
  42. #endif
  43. uint32_t _id;
  44. public:
  45. _FORCE_INLINE_ uint32_t get_id() const { return _id; }
  46. virtual ~RID_Data();
  47. };
  48. class RID {
  49. friend class RID_OwnerBase;
  50. mutable RID_Data *_data;
  51. public:
  52. _FORCE_INLINE_ RID_Data *get_data() const { return _data; }
  53. _FORCE_INLINE_ bool operator==(const RID &p_rid) const {
  54. return _data == p_rid._data;
  55. }
  56. _FORCE_INLINE_ bool operator<(const RID &p_rid) const {
  57. return _data < p_rid._data;
  58. }
  59. _FORCE_INLINE_ bool operator<=(const RID &p_rid) const {
  60. return _data <= p_rid._data;
  61. }
  62. _FORCE_INLINE_ bool operator>(const RID &p_rid) const {
  63. return _data > p_rid._data;
  64. }
  65. _FORCE_INLINE_ bool operator!=(const RID &p_rid) const {
  66. return _data != p_rid._data;
  67. }
  68. _FORCE_INLINE_ bool is_valid() const { return _data != NULL; }
  69. _FORCE_INLINE_ uint32_t get_id() const { return _data ? _data->get_id() : 0; }
  70. _FORCE_INLINE_ RID() {
  71. _data = NULL;
  72. }
  73. };
  74. class RID_OwnerBase {
  75. protected:
  76. static SafeRefCount refcount;
  77. _FORCE_INLINE_ void _set_data(RID &p_rid, RID_Data *p_data) {
  78. p_rid._data = p_data;
  79. refcount.ref();
  80. p_data->_id = refcount.get();
  81. #ifndef DEBUG_ENABLED
  82. p_data->_owner = this;
  83. #endif
  84. }
  85. #ifndef DEBUG_ENABLED
  86. _FORCE_INLINE_ bool _is_owner(const RID &p_rid) const {
  87. return this == p_rid._data->_owner;
  88. }
  89. _FORCE_INLINE_ void _remove_owner(RID &p_rid) {
  90. p_rid._data->_owner = NULL;
  91. }
  92. #endif
  93. public:
  94. virtual void get_owned_list(List<RID> *p_owned) = 0;
  95. static void init_rid();
  96. virtual ~RID_OwnerBase() {}
  97. };
  98. template <class T>
  99. class RID_Owner : public RID_OwnerBase {
  100. public:
  101. #ifdef DEBUG_ENABLED
  102. mutable Set<RID_Data *> id_map;
  103. #endif
  104. public:
  105. _FORCE_INLINE_ RID make_rid(T *p_data) {
  106. RID rid;
  107. _set_data(rid, p_data);
  108. #ifdef DEBUG_ENABLED
  109. id_map.insert(p_data);
  110. #endif
  111. return rid;
  112. }
  113. _FORCE_INLINE_ T *get(const RID &p_rid) {
  114. #ifdef DEBUG_ENABLED
  115. ERR_FAIL_COND_V(!p_rid.is_valid(), NULL);
  116. ERR_FAIL_COND_V(!id_map.has(p_rid.get_data()), NULL);
  117. #endif
  118. return static_cast<T *>(p_rid.get_data());
  119. }
  120. _FORCE_INLINE_ T *getornull(const RID &p_rid) {
  121. #ifdef DEBUG_ENABLED
  122. if (p_rid.get_data()) {
  123. ERR_FAIL_COND_V(!id_map.has(p_rid.get_data()), NULL);
  124. }
  125. #endif
  126. return static_cast<T *>(p_rid.get_data());
  127. }
  128. _FORCE_INLINE_ T *getptr(const RID &p_rid) {
  129. return static_cast<T *>(p_rid.get_data());
  130. }
  131. _FORCE_INLINE_ bool owns(const RID &p_rid) const {
  132. if (p_rid.get_data() == NULL)
  133. return false;
  134. #ifdef DEBUG_ENABLED
  135. return id_map.has(p_rid.get_data());
  136. #else
  137. return _is_owner(p_rid);
  138. #endif
  139. }
  140. void free(RID p_rid) {
  141. #ifdef DEBUG_ENABLED
  142. id_map.erase(p_rid.get_data());
  143. #else
  144. _remove_owner(p_rid);
  145. #endif
  146. }
  147. void get_owned_list(List<RID> *p_owned) {
  148. #ifdef DEBUG_ENABLED
  149. for (typename Set<RID_Data *>::Element *E = id_map.front(); E; E = E->next()) {
  150. RID r;
  151. _set_data(r, static_cast<T *>(E->get()));
  152. p_owned->push_back(r);
  153. }
  154. #endif
  155. }
  156. };
  157. #endif