rid.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*************************************************************************/
  2. /* rid.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef RID_H
  30. #define RID_H
  31. #include "safe_refcount.h"
  32. #include "typedefs.h"
  33. #include "os/memory.h"
  34. #include "set.h"
  35. #include "list.h"
  36. /**
  37. @author Juan Linietsky <[email protected]>
  38. */
  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!=NULL; }
  71. _FORCE_INLINE_ uint32_t get_id() const { return _data?_data->get_id():0; }
  72. _FORCE_INLINE_ RID() {
  73. _data=NULL;
  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. refcount.ref();
  82. p_data->_id=refcount.get();
  83. #ifndef DEBUG_ENABLED
  84. p_data->_owner=this;
  85. #endif
  86. }
  87. #ifndef DEBUG_ENABLED
  88. _FORCE_INLINE_ bool _is_owner(RID& p_rid) const {
  89. return this==p_rid._owner;
  90. }
  91. _FORCE_INLINE_ void _remove_owner(RID& p_rid) {
  92. return p_rid._owner=NULL;
  93. }
  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