PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Core/Dependencies/OgreCollada/FCollada/FCollada/FUtils/FUTracker.h

https://bitbucket.org/barakianc/nvidia-physx-and-apex-in-gge
C Header | 378 lines | 190 code | 51 blank | 137 comment | 27 complexity | 5d6cc757bdd1efda5ad38aa568628b45 MD5 | raw file
  1. /*
  2. Copyright (C) 2005-2007 Feeling Software Inc.
  3. Portions of the code are:
  4. Copyright (C) 2005-2007 Sony Computer Entertainment America
  5. MIT License: http://www.opensource.org/licenses/mit-license.php
  6. */
  7. #ifndef _FU_TRACKER_H_
  8. #define _FU_TRACKER_H_
  9. /**
  10. @file FUTracker.h
  11. This file contains the FUTrackable base class and the related tracker template classes.
  12. */
  13. #ifndef _FU_OBJECT_H_
  14. #include "FUtils/FUObject.h"
  15. #endif // _FU_OBJECT_H_
  16. class FUTracker;
  17. /**
  18. A trackable object.
  19. Each object holds a pointer to the trackers that track it.
  20. This pointer is useful so that the trackers can be notified if the object
  21. is released.
  22. @ingroup FUtils
  23. */
  24. class FCOLLADA_EXPORT FUTrackable : public FUObject
  25. {
  26. private:
  27. DeclareObjectType(FUObject);
  28. // The objects tracking this one.
  29. typedef fm::pvector<FUTracker> FUTrackerList;
  30. FUTrackerList trackers;
  31. public:
  32. /** Constructor.
  33. Although it is not an abstract class, this class is
  34. not meant to be used directly. */
  35. FUTrackable();
  36. /** Destructor.
  37. This function informs the trackers of this object's release. */
  38. virtual ~FUTrackable();
  39. /** Retrieves the number of tracker tracking the object.
  40. This can be used as an expensive reference counting mechanism.
  41. @return The number of trackers tracking the object. */
  42. size_t GetTrackerCount() const { return trackers.size(); }
  43. protected:
  44. /** Detaches all the trackers of this object.
  45. The trackers will be notified that this object has been released.
  46. It is not recommended to call this function outside of the Release() function. */
  47. void Detach();
  48. private:
  49. friend class FUTracker;
  50. void AddTracker(FUTracker* tracker);
  51. void RemoveTracker(FUTracker* tracker);
  52. bool HasTracker(const FUTracker* tracker) const;
  53. };
  54. /**
  55. An object set
  56. Each set has access to a list of unique objects.
  57. When the objects are created/released: they will inform the
  58. list.
  59. @ingroup FUtils
  60. */
  61. class FCOLLADA_EXPORT FUTracker
  62. {
  63. public:
  64. /** Destructor. */
  65. virtual ~FUTracker() {}
  66. /** Callback when an object tracked by this tracker
  67. is being released.
  68. @param object A tracked object. */
  69. virtual void OnObjectReleased(FUTrackable* object) = 0;
  70. /** Retrieves whether an object is tracked by this tracker.
  71. @param object An object. */
  72. virtual bool TracksObject(const FUTrackable* object) const { return object != NULL ? object->HasTracker(this) : false; }
  73. protected:
  74. /** Adds an object to be tracked.
  75. @param object The object to track. */
  76. void TrackObject(FUTrackable* object) { if (object) object->AddTracker(this); }
  77. /** Stops tracking an object
  78. @param object The object to stop tracking. */
  79. void UntrackObject(FUTrackable* object) { if (object) object->RemoveTracker(this); }
  80. };
  81. /**
  82. A tracked object pointer
  83. The reverse idea of a smart pointer: if the object pointed
  84. to by the pointer is released, the pointer will become NULL.
  85. @ingroup FUtils
  86. */
  87. template <class ObjectClass = FUTrackable>
  88. class FUTrackedPtr : public FUTracker
  89. {
  90. protected:
  91. /** The tracked pointer. */
  92. ObjectClass* ptr;
  93. public:
  94. /** Copy constructor.
  95. @param _ptr The object to track. This pointer can be NULL to indicate
  96. that no object should be tracked at this time. */
  97. FUTrackedPtr(ObjectClass* _ptr = NULL) : ptr(_ptr)
  98. {
  99. if (ptr != NULL) FUTracker::TrackObject((FUTrackable*) ptr);
  100. ptr = ptr;
  101. }
  102. /** Destructor.
  103. Stops the tracking of the pointer. */
  104. ~FUTrackedPtr()
  105. {
  106. if (ptr != NULL) FUTracker::UntrackObject((FUTrackable*) ptr);
  107. ptr = NULL;
  108. }
  109. /** Assigns this tracking pointer a new object to track.
  110. @param _ptr The new object to track.
  111. @return This reference. */
  112. FUTrackedPtr& operator=(ObjectClass* _ptr)
  113. {
  114. if (ptr != NULL) FUTracker::UntrackObject((FUTrackable*) ptr);
  115. ptr = _ptr;
  116. if (ptr != NULL) FUTracker::TrackObject((FUTrackable*) ptr);
  117. return *this;
  118. }
  119. inline FUTrackedPtr& operator=(const FUTrackedPtr& _ptr) { return operator=(_ptr.ptr); } /**< See above. */
  120. /** Retrieves whether an object is tracked by this tracker.
  121. @param object An object. */
  122. virtual bool TracksObject(const FUTrackable* object) const { return (FUTrackable*) ptr == object; }
  123. /** Accesses the tracked object.
  124. @return The tracked object. */
  125. inline ObjectClass& operator*() { FUAssert(ptr != NULL, return *ptr); return *ptr; }
  126. inline const ObjectClass& operator*() const { FUAssert(ptr != NULL, return *ptr); return *ptr; } /**< See above. */
  127. inline ObjectClass* operator->() { return ptr; } /**< See above. */
  128. inline const ObjectClass* operator->() const { return ptr; } /**< See above. */
  129. inline operator ObjectClass*() { return ptr; } /**< See above. */
  130. inline operator const ObjectClass*() const { return ptr; } /**< See above. */
  131. protected:
  132. /** Callback when an object tracked by this tracker
  133. is being released.
  134. @param object A contained object. */
  135. virtual void OnObjectReleased(FUTrackable* object)
  136. {
  137. FUAssert(TracksObject(object), return);
  138. ptr = NULL;
  139. }
  140. };
  141. /**
  142. An object list.
  143. Based on top of our modified version of the STL vector class,
  144. this contained object list holds pointers to some FUTrackable derived class
  145. and automatically removes objects when they are deleted.
  146. @ingroup FUtils
  147. */
  148. template <class ObjectClass = FUTrackable>
  149. class FUTrackedList : private fm::pvector<ObjectClass>, FUTracker
  150. {
  151. public:
  152. typedef fm::pvector<ObjectClass> Parent;
  153. typedef ObjectClass* item;
  154. typedef const ObjectClass* const_item;
  155. typedef item* iterator;
  156. typedef const_item* const_iterator;
  157. /** Destructor. */
  158. virtual ~FUTrackedList() { clear(); }
  159. /** Clears the object tracked by this object list. */
  160. void clear()
  161. {
  162. for (iterator it = begin(); it != end(); ++it)
  163. {
  164. FUTracker::UntrackObject((FUTrackable*) (*it));
  165. }
  166. Parent::clear();
  167. }
  168. /** Retrieves the first element of the container.
  169. @return The first element in the container. */
  170. ObjectClass*& front() { return (ObjectClass*&) Parent::front(); }
  171. const ObjectClass*& front() const { return (const ObjectClass*&) Parent::front(); } /**< See above. */
  172. /** Retrieves the last element of the container.
  173. @return The last element in the container. */
  174. ObjectClass*& back() { return (ObjectClass*&) Parent::back(); }
  175. const ObjectClass*& back() const { return (const ObjectClass*&) Parent::back(); } /**< See above. */
  176. /** Retrieves an indexed object in the list.
  177. @param index An index.
  178. @return The given object. */
  179. inline ObjectClass* at(size_t index) { return (ObjectClass*) Parent::at(index); }
  180. inline const ObjectClass* at(size_t index) const { return (const ObjectClass*) Parent::at(index); } /**< See above. */
  181. template <class INTEGER> inline ObjectClass* operator[](INTEGER index) { return at(index); } /**< See above. */
  182. template <class INTEGER> inline const ObjectClass* operator[](INTEGER index) const { return at(index); } /**< See above. */
  183. /** Retrieves an iterator for the first element in the list.
  184. @return an iterator for the first element in the list. */
  185. inline iterator begin() { return (iterator) Parent::begin(); }
  186. inline const_iterator begin() const { return (const_iterator) Parent::begin(); } /**< See above. */
  187. /** Retrieves an iterator for the element after the last element in the list.
  188. @return an iterator for the element after the last element in the list. */
  189. inline iterator end() { return (iterator) Parent::end(); }
  190. inline const_iterator end() const { return (const_iterator) Parent::end(); } /**< See above. */
  191. /** Retrieves an iterator for a given element in the list.
  192. @param item An item of the list.
  193. @return An iterator for the given item. If the item is not
  194. found in the list, the end() iterator is returned. */
  195. inline iterator find(const ObjectClass* item) { return (iterator) Parent::find(item); }
  196. inline const_iterator find(const ObjectClass* item) const { return (const_iterator) Parent::find(item); } /**< See above. */
  197. /** Adds an object to the container's containment list.
  198. @param object An object to contain. */
  199. inline void push_back(ObjectClass* object)
  200. {
  201. FUTracker::TrackObject((FUTrackable*) object);
  202. Parent::push_back(object);
  203. }
  204. /** Inserts an object in the container's containment list.
  205. @param _iterator The iterator after which to insert the object.
  206. @param object An object to insert.
  207. @return The iterator to the inserted object. */
  208. iterator insert(iterator _iterator, ObjectClass* object)
  209. {
  210. FUTracker::TrackObject(object);
  211. return (iterator) Parent::insert(_iterator, object);
  212. }
  213. /** Inserts an object in the container's containment list.
  214. @param index Where to insert the object.
  215. @param object An object to insert. */
  216. inline void insert(size_t index, ObjectClass* object) { insert(begin() + index, object); }
  217. /** Inserts a list of object in the container's containment list.
  218. @param _where The iterator after which to insert the object.
  219. @param _startIterator The iterator for the first object to insert.
  220. @param _endIterator The iterator just passed the last object.
  221. This object will not be inserted. */
  222. template <class _It>
  223. void insert(iterator _where, _It _startIterator, _It _endIterator)
  224. {
  225. if (_startIterator < _endIterator)
  226. {
  227. size_t relativeWhere = _where - begin();
  228. size_t count = _endIterator - _startIterator;
  229. Parent::insert(Parent::begin() + relativeWhere, count);
  230. _where = begin() + relativeWhere;
  231. for (; _startIterator != _endIterator; ++_startIterator, ++_where)
  232. {
  233. *_where = const_cast<ObjectClass*>((const ObjectClass*)(*_startIterator));
  234. FUTracker::TrackObject(const_cast<FUTrackable*>((const FUTrackable*) (*_startIterator)));
  235. }
  236. }
  237. }
  238. /** Removes the last value of the tracked object list. */
  239. void pop_back()
  240. {
  241. if (!Parent::empty())
  242. {
  243. FUTracker::UntrackObject(back());
  244. Parent::pop_back();
  245. }
  246. }
  247. /** Removes the value at the given position within the list.
  248. @param _it The list position for the value to remove. */
  249. iterator erase(iterator _it)
  250. {
  251. FUTracker::UntrackObject((FUTrackable*) *_it);
  252. return (iterator) Parent::erase(_it);
  253. }
  254. /** Removes a range of values from the list.
  255. @param first The list position of the first value to remove.
  256. @param last The list position just passed the last value to remove. */
  257. inline void erase(iterator first, iterator last)
  258. {
  259. for (iterator it = first; it != last; ++it) FUTracker::UntrackObject((FUTrackable*) *it);
  260. Parent::erase(first, last);
  261. }
  262. /** Removes a range of values from the list.
  263. @param first The index of the first value to remove.
  264. @param last The index just passed the last value to remove. */
  265. inline void erase(size_t first, size_t last) { erase(begin() + first, begin() + last); }
  266. /** Removes a value contained within the list, once.
  267. @param value The value, contained within the list, to erase from it.
  268. @return Whether the value was found and erased from the list. */
  269. inline bool erase(const ObjectClass* value)
  270. {
  271. iterator it = Parent::find(value);
  272. if (it != Parent::end())
  273. {
  274. FUTracker::UntrackObject((FUTrackable*) *it);
  275. Parent::erase(it);
  276. return true;
  277. }
  278. return false;
  279. }
  280. /** Removes an indexed value contained within the list.
  281. @param index The index of the value to erase. */
  282. inline void erase(size_t index) { erase(begin() + index); }
  283. /** Retrieves whether an object is contained by this container.
  284. @param object An object. */
  285. virtual bool TracksObject(const FUTrackable* object) const { return Parent::contains((ObjectClass*) object); }
  286. /** Clones a list of tracked objects.
  287. This list will stop tracking all its current tracked objects
  288. and will start tracking the objects within the other list.
  289. @param other A second list of tracked objects.
  290. @return This list. */
  291. FUTrackedList<ObjectClass>& operator= (const FUTrackedList<ObjectClass>& other) { clear(); insert(end(), other.begin(), other.end()); return *this; }
  292. inline bool empty() const { return Parent::empty(); } /**< Inherited from pvector. */
  293. inline size_t size() const { return Parent::size(); } /**< Inherited from pvector. */
  294. void reserve(size_t count) { Parent::reserve(count); } /**< Inherited from pvector. */
  295. inline bool contains(const ObjectClass* value) const { return Parent::contains(value); } /**< Inherited from pvector. */
  296. /** Releases a value contained within a list.
  297. Use this function only if there is no duplicate pointers within the list.
  298. @param value The value, contained within the list, to release.
  299. @return Whether the value was found and released. */
  300. inline bool release(const ObjectClass* value)
  301. {
  302. ObjectClass** it = find(value);
  303. if (it != Parent::end()) { erase(it); ((FUTrackable*) value)->Release(); return true; }
  304. return false;
  305. }
  306. /** Removes the first value of the tracked object list. */
  307. void pop_front()
  308. {
  309. if (!Parent::empty())
  310. {
  311. FUTracker::UntrackObject(front());
  312. Parent::pop_front();
  313. }
  314. }
  315. protected:
  316. /** Removes an object from the container's containment list.
  317. @param object A contained object. */
  318. virtual void OnObjectReleased(FUTrackable* object)
  319. {
  320. FUAssert(TracksObject(object), return);
  321. Parent::erase((ObjectClass*) object);
  322. }
  323. };
  324. #endif // _FU_TRACKER_H_