rid.h 4.9 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-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 "hash_map.h"
  33. #include "list.h"
  34. #include "os/memory.h"
  35. #include "safe_refcount.h"
  36. #include "typedefs.h"
  37. /**
  38. @author Juan Linietsky <reduzio@gmail.com>
  39. */
  40. class RID_OwnerBase;
  41. typedef uint32_t ID;
  42. class RID {
  43. friend class RID_OwnerBase;
  44. ID _id;
  45. RID_OwnerBase *owner;
  46. public:
  47. _FORCE_INLINE_ ID get_id() const { return _id; }
  48. bool operator==(const RID &p_rid) const {
  49. return _id == p_rid._id;
  50. }
  51. _FORCE_INLINE_ bool operator<(const RID &p_rid) const {
  52. return _id < p_rid._id;
  53. }
  54. _FORCE_INLINE_ bool operator<=(const RID &p_rid) const {
  55. return _id <= p_rid._id;
  56. }
  57. _FORCE_INLINE_ bool operator>(const RID &p_rid) const {
  58. return _id > p_rid._id;
  59. }
  60. bool operator!=(const RID &p_rid) const {
  61. return _id != p_rid._id;
  62. }
  63. _FORCE_INLINE_ bool is_valid() const { return _id > 0; }
  64. operator const void *() const {
  65. return is_valid() ? this : 0;
  66. };
  67. _FORCE_INLINE_ RID() {
  68. _id = 0;
  69. owner = 0;
  70. }
  71. };
  72. class RID_OwnerBase {
  73. protected:
  74. friend class RID;
  75. void set_id(RID &p_rid, ID p_id) const { p_rid._id = p_id; }
  76. void set_ownage(RID &p_rid) const { p_rid.owner = const_cast<RID_OwnerBase *>(this); }
  77. ID new_ID();
  78. public:
  79. virtual bool owns(const RID &p_rid) const = 0;
  80. virtual void get_owned_list(List<RID> *p_owned) const = 0;
  81. static void init_rid();
  82. virtual ~RID_OwnerBase() {}
  83. };
  84. template <class T, bool thread_safe = false>
  85. class RID_Owner : public RID_OwnerBase {
  86. public:
  87. typedef void (*ReleaseNotifyFunc)(void *user, T *p_data);
  88. private:
  89. Mutex *mutex;
  90. mutable HashMap<ID, T *> id_map;
  91. public:
  92. RID make_rid(T *p_data) {
  93. if (thread_safe) {
  94. mutex->lock();
  95. }
  96. ID id = new_ID();
  97. id_map[id] = p_data;
  98. RID rid;
  99. set_id(rid, id);
  100. set_ownage(rid);
  101. if (thread_safe) {
  102. mutex->unlock();
  103. }
  104. return rid;
  105. }
  106. _FORCE_INLINE_ T *get(const RID &p_rid) {
  107. if (thread_safe) {
  108. mutex->lock();
  109. }
  110. T **elem = id_map.getptr(p_rid.get_id());
  111. if (thread_safe) {
  112. mutex->unlock();
  113. }
  114. ERR_FAIL_COND_V(!elem, NULL);
  115. return *elem;
  116. }
  117. virtual bool owns(const RID &p_rid) const {
  118. if (thread_safe) {
  119. mutex->lock();
  120. }
  121. T **elem = id_map.getptr(p_rid.get_id());
  122. if (thread_safe) {
  123. mutex->lock();
  124. }
  125. return elem != NULL;
  126. }
  127. virtual void free(RID p_rid) {
  128. if (thread_safe) {
  129. mutex->lock();
  130. }
  131. ERR_FAIL_COND(!owns(p_rid));
  132. id_map.erase(p_rid.get_id());
  133. }
  134. virtual void get_owned_list(List<RID> *p_owned) const {
  135. if (thread_safe) {
  136. mutex->lock();
  137. }
  138. const ID *id = NULL;
  139. while ((id = id_map.next(id))) {
  140. RID rid;
  141. set_id(rid, *id);
  142. set_ownage(rid);
  143. p_owned->push_back(rid);
  144. }
  145. if (thread_safe) {
  146. mutex->lock();
  147. }
  148. }
  149. RID_Owner() {
  150. if (thread_safe) {
  151. mutex = Mutex::create();
  152. }
  153. }
  154. ~RID_Owner() {
  155. if (thread_safe) {
  156. memdelete(mutex);
  157. }
  158. }
  159. };
  160. #endif