rid.h 5.3 KB

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