simSet.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 _SIM_OBJECT_H_
  29. #include "sim/simObject.h"
  30. #endif
  31. #ifndef _SIM_OBJECT_LIST_H_
  32. #include "sim/SimObjectList.h"
  33. #endif
  34. #ifndef _FIND_ITERATOR_H_
  35. #include "collection/findIterator.h"
  36. #endif
  37. #ifndef _SIMDICTIONARY_H_
  38. #include "sim/simDictionary.h"
  39. #endif
  40. #ifndef _TAML_CHILDREN_H_
  41. #include "persistence/taml/tamlChildren.h"
  42. #endif
  43. //---------------------------------------------------------------------------
  44. /// A set of SimObjects.
  45. ///
  46. /// It is often necessary to keep track of an arbitrary set of SimObjects.
  47. /// For instance, Torque's networking code needs to not only keep track of
  48. /// the set of objects which need to be ghosted, but also the set of objects
  49. /// which must <i>always</i> be ghosted. It does this by working with two
  50. /// sets. The first of these is the RootGroup (which is actually a SimGroup)
  51. /// and the second is the GhostAlwaysSet, which contains objects which must
  52. /// always be ghosted to the client.
  53. ///
  54. /// Some general notes on SimSets:
  55. /// - Membership is not exclusive. A SimObject may be a member of multiple
  56. /// SimSets.
  57. /// - A SimSet does not destroy subobjects when it is destroyed.
  58. /// - A SimSet may hold an arbitrary number of objects.
  59. ///
  60. /// Using SimSets, the code to work with these two sets becomes
  61. /// relatively straightforward:
  62. ///
  63. /// @code
  64. /// // (Example from netObject.cc)
  65. /// // To iterate over all the objects in the Sim:
  66. /// for (SimSetIterator obj(Sim::getRootGroup()); *obj; ++obj)
  67. /// {
  68. /// NetObject* nobj = dynamic_cast<NetObject*>(*obj);
  69. ///
  70. /// if (nobj)
  71. /// {
  72. /// // ... do things ...
  73. /// }
  74. /// }
  75. ///
  76. /// // (Example from netGhost.cc)
  77. /// // To iterate over the ghostAlways set.
  78. /// SimSet* ghostAlwaysSet = Sim::getGhostAlwaysSet();
  79. /// SimSet::iterator i;
  80. ///
  81. /// U32 sz = ghostAlwaysSet->size();
  82. /// S32 j;
  83. ///
  84. /// for(i = ghostAlwaysSet->begin(); i != ghostAlwaysSet->end(); i++)
  85. /// {
  86. /// NetObject *obj = (NetObject *)(*i);
  87. ///
  88. /// /// ... do things with obj...
  89. /// }
  90. /// @endcode
  91. ///
  92. class SimSet: public SimObject, public TamlChildren
  93. {
  94. typedef SimObject Parent;
  95. typedef SimObject Children;
  96. protected:
  97. SimObjectList objectList;
  98. void *mMutex;
  99. public:
  100. SimSet() {
  101. VECTOR_SET_ASSOCIATION(objectList);
  102. mMutex = Mutex::createMutex();
  103. }
  104. ~SimSet()
  105. {
  106. lock();
  107. unlock();
  108. Mutex::destroyMutex(mMutex);
  109. mMutex = NULL;
  110. }
  111. /// @name STL Interface
  112. /// @{
  113. ///
  114. typedef SimObjectList::iterator iterator;
  115. typedef SimObjectList::value_type value;
  116. SimObject* front() { return objectList.front(); }
  117. SimObject* first() { return objectList.first(); }
  118. SimObject* last() { return objectList.last(); }
  119. bool empty() { return objectList.empty(); }
  120. S32 size() const { return objectList.size(); }
  121. iterator begin() { return objectList.begin(); }
  122. iterator end() { return objectList.end(); }
  123. value operator[] (S32 index) { return objectList[U32(index)]; }
  124. inline iterator find( iterator first, iterator last, SimObject *obj ) { return ::find(first, last, obj); }
  125. inline iterator find( SimObject *obj ) { return ::find(begin(), end(), obj); }
  126. template <typename T> inline bool containsType( void )
  127. {
  128. for( iterator itr = begin(); itr != end(); ++itr )
  129. {
  130. if ( dynamic_cast<T*>(*itr) != NULL )
  131. return true;
  132. }
  133. return false;
  134. }
  135. bool isMember(SimObject* obj);
  136. virtual bool reOrder( SimObject *obj, SimObject *target=0 );
  137. SimObject* at(S32 index) const { return objectList.at(index); }
  138. void deleteObjects( void );
  139. void clear();
  140. /// @}
  141. virtual void onRemove();
  142. virtual void onDeleteNotify(SimObject *object);
  143. /// @name Set Management
  144. /// @{
  145. virtual void addObject(SimObject*); ///< Add an object to the set.
  146. virtual void removeObject(SimObject*); ///< Remove an object from the set.
  147. virtual void pushObject(SimObject*); ///< Add object to end of list.
  148. ///
  149. /// It will force the object to the end of the list if it already exists
  150. /// in the list.
  151. virtual void popObject(); ///< Remove an object from the end of the list.
  152. void bringObjectToFront(SimObject* obj) { reOrder(obj, front()); }
  153. void pushObjectToBack(SimObject* obj) { reOrder(obj, NULL); }
  154. /// @}
  155. virtual U32 getTamlChildCount( void ) const
  156. {
  157. return (U32)size();
  158. }
  159. virtual SimObject* getTamlChild( const U32 childIndex ) const
  160. {
  161. // Sanity!
  162. AssertFatal( childIndex < (U32)size(), "SimSet::getTamlChild() - Child index is out of range." );
  163. // For when the assert is not used.
  164. if ( childIndex >= (U32)size() )
  165. return NULL;
  166. return at( childIndex );
  167. }
  168. virtual void addTamlChild( SimObject* pSimObject )
  169. {
  170. // Sanity!
  171. AssertFatal( pSimObject != NULL, "SimSet::addTamlChild() - Cannot add a NULL child object." );
  172. addObject( pSimObject );
  173. }
  174. void callOnChildren( const char * method, S32 argc, const char *argv[], bool executeOnChildGroups = true );
  175. virtual void write(Stream &stream, U32 tabStop, U32 flags = 0);
  176. virtual SimObject *findObject(const char *name);
  177. SimObject* findObjectByInternalName(const char* internalName, bool searchChildren = false);
  178. virtual bool writeObject(Stream *stream);
  179. virtual bool readObject(Stream *stream);
  180. inline void lock()
  181. {
  182. #ifdef TORQUE_MULTITHREAD
  183. Mutex::lockMutex(mMutex);
  184. #endif
  185. }
  186. inline void unlock()
  187. {
  188. #ifdef TORQUE_MULTITHREAD
  189. Mutex::unlockMutex(mMutex);
  190. #endif
  191. }
  192. DECLARE_CONOBJECT(SimSet);
  193. #ifdef TORQUE_DEBUG
  194. inline void _setVectorAssoc( const char *file, const U32 line )
  195. {
  196. objectList.setFileAssociation( file, line );
  197. }
  198. #endif
  199. };
  200. #ifdef TORQUE_DEBUG
  201. # define SIMSET_SET_ASSOCIATION( x ) x._setVectorAssoc( __FILE__, __LINE__ )
  202. #else
  203. # define SIMSET_SET_ASSOCIATION( x )
  204. #endif
  205. /// Iterator for use with SimSets
  206. ///
  207. /// @see SimSet
  208. class SimSetIterator
  209. {
  210. protected:
  211. struct Entry {
  212. SimSet* set;
  213. SimSet::iterator itr;
  214. };
  215. class Stack: public Vector<Entry> {
  216. public:
  217. void push_back(SimSet*);
  218. };
  219. Stack stack;
  220. public:
  221. SimSetIterator(SimSet*);
  222. SimObject* operator++();
  223. SimObject* operator*() {
  224. return stack.empty()? 0: *stack.last().itr;
  225. }
  226. };
  227. //---------------------------------------------------------------------------
  228. /// A group of SimObjects.
  229. ///
  230. /// A SimGroup is a stricter form of SimSet. SimObjects may only be a member
  231. /// of a single SimGroup at a time.
  232. ///
  233. /// The SimGroup will automatically enforce the single-group-membership rule.
  234. ///
  235. /// @code
  236. /// // From engine/sim/simPath.cc - getting a pointer to a SimGroup
  237. /// SimGroup* pMissionGroup = dynamic_cast<SimGroup*>(Sim::findObject("MissionGroup"));
  238. ///
  239. /// // From game/trigger.cc:46 - iterating over a SimObject's group.
  240. /// SimObject* trigger = ...;
  241. /// SimGroup* pGroup = trigger->getGroup();
  242. /// for (SimGroup::iterator itr = pGroup->begin(); itr != pGroup->end(); itr++)
  243. /// {
  244. /// // do something with *itr
  245. /// }
  246. /// @endcode
  247. class SimGroup: public SimSet
  248. {
  249. private:
  250. friend class SimManager;
  251. friend class SimObject;
  252. typedef SimSet Parent;
  253. SimNameDictionary nameDictionary;
  254. public:
  255. ~SimGroup();
  256. /// Add an object to the group.
  257. virtual void addObject(SimObject*);
  258. void addObject(SimObject*, SimObjectId);
  259. void addObject(SimObject*, const char *name);
  260. /// Remove an object from the group.
  261. virtual void removeObject(SimObject*);
  262. virtual void onRemove();
  263. virtual void onChildRemoved(SimObject*);
  264. /// Find an object in the group.
  265. virtual SimObject* findObject(const char* name);
  266. bool processArguments(S32 argc, const char **argv);
  267. DECLARE_CONOBJECT(SimGroup);
  268. };
  269. inline void SimGroup::addObject(SimObject* obj, SimObjectId id)
  270. {
  271. obj->mId = id;
  272. addObject( obj );
  273. }
  274. inline void SimGroup::addObject(SimObject *obj, const char *name)
  275. {
  276. addObject( obj );
  277. obj->assignName(name);
  278. }
  279. class SimGroupIterator: public SimSetIterator
  280. {
  281. public:
  282. SimGroupIterator(SimGroup* grp): SimSetIterator(grp) {}
  283. SimObject* operator++();
  284. };
  285. #endif // _SIMSET_H_