rid.h 4.9 KB

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