simSet.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. // [tom, 3/9/2007] To avoid having to change pretty much every source file, this
  23. // header is included from simBase.h where the original definitions of SimSet and
  24. // SimGroup were. simSet.h should not be included directly. Include dependencies
  25. // are hell.
  26. #ifndef _SIMSET_H_
  27. #define _SIMSET_H_
  28. #ifndef _TAML_CHILDREN_H_
  29. #include "persistence/taml/tamlChildren.h"
  30. #endif
  31. //---------------------------------------------------------------------------
  32. /// A set of SimObjects.
  33. ///
  34. /// It is often necessary to keep track of an arbitrary set of SimObjects.
  35. /// For instance, Torque's networking code needs to not only keep track of
  36. /// the set of objects which need to be ghosted, but also the set of objects
  37. /// which must <i>always</i> be ghosted. It does this by working with two
  38. /// sets. The first of these is the RootGroup (which is actually a SimGroup)
  39. /// and the second is the GhostAlwaysSet, which contains objects which must
  40. /// always be ghosted to the client.
  41. ///
  42. /// Some general notes on SimSets:
  43. /// - Membership is not exclusive. A SimObject may be a member of multiple
  44. /// SimSets.
  45. /// - A SimSet does not destroy subobjects when it is destroyed.
  46. /// - A SimSet may hold an arbitrary number of objects.
  47. ///
  48. /// Using SimSets, the code to work with these two sets becomes
  49. /// relatively straightforward:
  50. ///
  51. /// @code
  52. /// // (Example from netObject.cc)
  53. /// // To iterate over all the objects in the Sim:
  54. /// for (SimSetIterator obj(Sim::getRootGroup()); *obj; ++obj)
  55. /// {
  56. /// NetObject* nobj = dynamic_cast<NetObject*>(*obj);
  57. ///
  58. /// if (nobj)
  59. /// {
  60. /// // ... do things ...
  61. /// }
  62. /// }
  63. ///
  64. /// // (Example from netGhost.cc)
  65. /// // To iterate over the ghostAlways set.
  66. /// SimSet* ghostAlwaysSet = Sim::getGhostAlwaysSet();
  67. /// SimSet::iterator i;
  68. ///
  69. /// U32 sz = ghostAlwaysSet->size();
  70. /// S32 j;
  71. ///
  72. /// for(i = ghostAlwaysSet->begin(); i != ghostAlwaysSet->end(); i++)
  73. /// {
  74. /// NetObject *obj = (NetObject *)(*i);
  75. ///
  76. /// /// ... do things with obj...
  77. /// }
  78. /// @endcode
  79. ///
  80. class SimSet: public SimObject, public TamlChildren
  81. {
  82. typedef SimObject Parent;
  83. protected:
  84. SimObjectList objectList;
  85. void *mMutex;
  86. public:
  87. SimSet() {
  88. VECTOR_SET_ASSOCIATION(objectList);
  89. mMutex = Mutex::createMutex();
  90. }
  91. ~SimSet()
  92. {
  93. lock();
  94. unlock();
  95. Mutex::destroyMutex(mMutex);
  96. mMutex = NULL;
  97. }
  98. /// @name STL Interface
  99. /// @{
  100. ///
  101. typedef SimObjectList::iterator iterator;
  102. typedef SimObjectList::value_type value;
  103. SimObject* front() { return objectList.front(); }
  104. SimObject* first() { return objectList.first(); }
  105. SimObject* last() { return objectList.last(); }
  106. bool empty() { return objectList.empty(); }
  107. S32 size() const { return objectList.size(); }
  108. iterator begin() { return objectList.begin(); }
  109. iterator end() { return objectList.end(); }
  110. value operator[] (S32 index) { return objectList[U32(index)]; }
  111. inline iterator find( iterator first, iterator last, SimObject *obj ) { return ::find(first, last, obj); }
  112. inline iterator find( SimObject *obj ) { return ::find(begin(), end(), obj); }
  113. template <typename T> inline bool containsType( void )
  114. {
  115. for( iterator itr = begin(); itr != end(); ++itr )
  116. {
  117. if ( dynamic_cast<T*>(*itr) != NULL )
  118. return true;
  119. }
  120. return false;
  121. }
  122. virtual bool reOrder( SimObject *obj, SimObject *target=0 );
  123. SimObject* at(S32 index) const { return objectList.at(index); }
  124. void deleteObjects( void );
  125. void clear();
  126. /// @}
  127. virtual void onRemove();
  128. virtual void onDeleteNotify(SimObject *object);
  129. /// @name Set Management
  130. /// @{
  131. virtual void addObject(SimObject*); ///< Add an object to the set.
  132. virtual void removeObject(SimObject*); ///< Remove an object from the set.
  133. virtual void pushObject(SimObject*); ///< Add object to end of list.
  134. ///
  135. /// It will force the object to the end of the list if it already exists
  136. /// in the list.
  137. virtual void popObject(); ///< Remove an object from the end of the list.
  138. void bringObjectToFront(SimObject* obj) { reOrder(obj, front()); }
  139. void pushObjectToBack(SimObject* obj) { reOrder(obj, NULL); }
  140. /// @}
  141. virtual U32 getTamlChildCount( void ) const
  142. {
  143. return (U32)size();
  144. }
  145. virtual SimObject* getTamlChild( const U32 childIndex ) const
  146. {
  147. // Sanity!
  148. AssertFatal( childIndex < (U32)size(), "SimSet::getTamlChild() - Child index is out of range." );
  149. // For when the assert is not used.
  150. if ( childIndex >= (U32)size() )
  151. return NULL;
  152. return at( childIndex );
  153. }
  154. virtual void addTamlChild( SimObject* pSimObject )
  155. {
  156. // Sanity!
  157. AssertFatal( pSimObject != NULL, "SimSet::addTamlChild() - Cannot add a NULL child object." );
  158. addObject( pSimObject );
  159. }
  160. void callOnChildren( const char * method, S32 argc, const char *argv[], bool executeOnChildGroups = true );
  161. virtual void write(Stream &stream, U32 tabStop, U32 flags = 0);
  162. virtual SimObject *findObject(const char *name);
  163. SimObject* findObjectByInternalName(const char* internalName, bool searchChildren = false);
  164. virtual bool writeObject(Stream *stream);
  165. virtual bool readObject(Stream *stream);
  166. inline void lock()
  167. {
  168. #ifdef TORQUE_MULTITHREAD
  169. Mutex::lockMutex(mMutex);
  170. #endif
  171. }
  172. inline void unlock()
  173. {
  174. #ifdef TORQUE_MULTITHREAD
  175. Mutex::unlockMutex(mMutex);
  176. #endif
  177. }
  178. DECLARE_CONOBJECT(SimSet);
  179. #ifdef TORQUE_DEBUG
  180. inline void _setVectorAssoc( const char *file, const U32 line )
  181. {
  182. objectList.setFileAssociation( file, line );
  183. }
  184. #endif
  185. };
  186. #ifdef TORQUE_DEBUG
  187. # define SIMSET_SET_ASSOCIATION( x ) x._setVectorAssoc( __FILE__, __LINE__ )
  188. #else
  189. # define SIMSET_SET_ASSOCIATION( x )
  190. #endif
  191. /// Iterator for use with SimSets
  192. ///
  193. /// @see SimSet
  194. class SimSetIterator
  195. {
  196. protected:
  197. struct Entry {
  198. SimSet* set;
  199. SimSet::iterator itr;
  200. };
  201. class Stack: public Vector<Entry> {
  202. public:
  203. void push_back(SimSet*);
  204. };
  205. Stack stack;
  206. public:
  207. SimSetIterator(SimSet*);
  208. SimObject* operator++();
  209. SimObject* operator*() {
  210. return stack.empty()? 0: *stack.last().itr;
  211. }
  212. };
  213. //---------------------------------------------------------------------------
  214. /// A group of SimObjects.
  215. ///
  216. /// A SimGroup is a stricter form of SimSet. SimObjects may only be a member
  217. /// of a single SimGroup at a time.
  218. ///
  219. /// The SimGroup will automatically enforce the single-group-membership rule.
  220. ///
  221. /// @code
  222. /// // From engine/sim/simPath.cc - getting a pointer to a SimGroup
  223. /// SimGroup* pMissionGroup = dynamic_cast<SimGroup*>(Sim::findObject("MissionGroup"));
  224. ///
  225. /// // From game/trigger.cc:46 - iterating over a SimObject's group.
  226. /// SimObject* trigger = ...;
  227. /// SimGroup* pGroup = trigger->getGroup();
  228. /// for (SimGroup::iterator itr = pGroup->begin(); itr != pGroup->end(); itr++)
  229. /// {
  230. /// // do something with *itr
  231. /// }
  232. /// @endcode
  233. class SimGroup: public SimSet
  234. {
  235. private:
  236. friend class SimManager;
  237. friend class SimObject;
  238. typedef SimSet Parent;
  239. SimNameDictionary nameDictionary;
  240. public:
  241. ~SimGroup();
  242. /// Add an object to the group.
  243. virtual void addObject(SimObject*);
  244. void addObject(SimObject*, SimObjectId);
  245. void addObject(SimObject*, const char *name);
  246. /// Remove an object from the group.
  247. virtual void removeObject(SimObject*);
  248. virtual void onRemove();
  249. /// Find an object in the group.
  250. virtual SimObject* findObject(const char* name);
  251. bool processArguments(S32 argc, const char **argv);
  252. DECLARE_CONOBJECT(SimGroup);
  253. };
  254. inline void SimGroup::addObject(SimObject* obj, SimObjectId id)
  255. {
  256. obj->mId = id;
  257. addObject( obj );
  258. }
  259. inline void SimGroup::addObject(SimObject *obj, const char *name)
  260. {
  261. addObject( obj );
  262. obj->assignName(name);
  263. }
  264. class SimGroupIterator: public SimSetIterator
  265. {
  266. public:
  267. SimGroupIterator(SimGroup* grp): SimSetIterator(grp) {}
  268. SimObject* operator++();
  269. };
  270. #endif // _SIMSET_H_