Simple game engine with complete export to scripting language
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

249 lines
3.7 KiB

  1. #ifndef BLUECORE_REFERENCED_COUNTER_H
  2. #define BLUECORE_REFERENCED_COUNTER_H
  3. #include <list>
  4. namespace BlueCore
  5. {
  6. class Referenced
  7. {
  8. public:
  9. class DestructionListener
  10. {
  11. public:
  12. virtual void onDestruction(Referenced *referenced) = 0;
  13. };
  14. private:
  15. std::list<DestructionListener*> _DestructionListeners;
  16. unsigned int _ReferenceCount;
  17. virtual void destroyReferenced()
  18. {
  19. while (_DestructionListeners.begin() != _DestructionListeners.end())
  20. {
  21. if (_DestructionListeners.front() != 0)
  22. _DestructionListeners.front()->onDestruction(this);
  23. _DestructionListeners.pop_front();
  24. }
  25. delete this;
  26. }
  27. public:
  28. inline Referenced() :
  29. _ReferenceCount(0)
  30. {
  31. }
  32. virtual inline ~Referenced()
  33. {
  34. }
  35. inline void addReference()
  36. {
  37. _ReferenceCount += 1;
  38. }
  39. inline void removeReference()
  40. {
  41. if (_ReferenceCount > 0)
  42. {
  43. _ReferenceCount -= 1;
  44. if ( 0 == _ReferenceCount)
  45. {
  46. destroyReferenced();
  47. }
  48. }
  49. }
  50. inline unsigned int getReferenceCount()
  51. {
  52. return _ReferenceCount;
  53. }
  54. inline void addDestructionListener(DestructionListener *listener)
  55. {
  56. if (listener != 0)
  57. _DestructionListeners.push_back(listener);
  58. }
  59. inline void removeDestructionListener(DestructionListener *listener)
  60. {
  61. _DestructionListeners.remove(listener);
  62. }
  63. };
  64. template<class T> class ref_ptr
  65. {
  66. public:
  67. ref_ptr() :
  68. _Referenced(0)
  69. {
  70. }
  71. ref_ptr(T* referenced) :
  72. _Referenced(referenced)
  73. {
  74. if (_Referenced)
  75. _Referenced->addReference();
  76. }
  77. ref_ptr(const ref_ptr& op) :
  78. _Referenced(op._Referenced)
  79. {
  80. if (_Referenced)
  81. _Referenced->addReference();
  82. }
  83. ~ref_ptr()
  84. {
  85. if (_Referenced)
  86. _Referenced->removeReference();
  87. }
  88. T* operator ->() const
  89. {
  90. return _Referenced;
  91. }
  92. ref_ptr& operator =(T* op)
  93. {
  94. if (op != _Referenced)
  95. {
  96. if (_Referenced != 0)
  97. _Referenced->removeReference();
  98. _Referenced = op;
  99. if (_Referenced)
  100. _Referenced->addReference();
  101. }
  102. return *this;
  103. }
  104. bool operator ==(const ref_ptr& ref)
  105. {
  106. if (_Referenced == ref._Referenced)
  107. return true;
  108. else
  109. return false;
  110. }
  111. bool operator == (T* ref)
  112. {
  113. if (_Referenced == ref)
  114. return true;
  115. else
  116. return false;
  117. }
  118. bool valid() const
  119. {
  120. return (_Referenced != 0);
  121. }
  122. operator T*() const
  123. {
  124. return _Referenced;
  125. }
  126. T* pointer() const
  127. {
  128. return _Referenced;
  129. }
  130. private:
  131. T* _Referenced;
  132. };
  133. template<class T> class weak_ptr : public Referenced::DestructionListener
  134. {
  135. public:
  136. weak_ptr() :
  137. _Referenced(0)
  138. {
  139. }
  140. weak_ptr(T* referenced) :
  141. _Referenced(referenced)
  142. {
  143. if (_Referenced)
  144. _Referenced->addDestructionListener(this);
  145. }
  146. weak_ptr(const weak_ptr& op) :
  147. _Referenced(op._Referenced)
  148. {
  149. if (_Referenced)
  150. _Referenced->addDestructionListener(this);
  151. }
  152. virtual ~weak_ptr()
  153. {
  154. if (_Referenced)
  155. _Referenced->removeDestructionListener(this);
  156. }
  157. T* operator ->() const
  158. {
  159. return _Referenced;
  160. }
  161. weak_ptr& operator =(T* op)
  162. {
  163. if (op != _Referenced && _Referenced != 0)
  164. _Referenced->removeDestructionListener(this);
  165. _Referenced = op;
  166. _Referenced->addDestructionListener(this);
  167. return *this;
  168. }
  169. bool operator ==(const weak_ptr& ref)
  170. {
  171. if (_Referenced == ref._Referenced)
  172. return true;
  173. else
  174. return false;
  175. }
  176. bool valid() const
  177. {
  178. return (_Referenced != 0);
  179. }
  180. T* pointer() const
  181. {
  182. return _Referenced;
  183. }
  184. operator T*() const
  185. {
  186. return _Referenced;
  187. }
  188. void onDestruction(Referenced *referenced)
  189. {
  190. _Referenced = 0;
  191. }
  192. private:
  193. T* _Referenced;
  194. };
  195. } // namespace BlueCore
  196. #endif