simSet.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. protected:
  96. SimObjectList objectList;
  97. void *mMutex;
  98. public:
  99. SimSet() {
  100. VECTOR_SET_ASSOCIATION(objectList);
  101. mMutex = Mutex::createMutex();
  102. }
  103. ~SimSet()
  104. {
  105. lock();
  106. unlock();
  107. Mutex::destroyMutex(mMutex);
  108. mMutex = NULL;
  109. }
  110. /// @name STL Interface
  111. /// @{
  112. ///
  113. typedef SimObjectList::iterator iterator;
  114. typedef SimObjectList::value_type value;
  115. SimObject* front() { return objectList.front(); }
  116. SimObject* first() { return objectList.first(); }
  117. SimObject* last() { return objectList.last(); }
  118. bool empty() { return objectList.empty(); }
  119. S32 size() const { return objectList.size(); }
  120. iterator begin() { return objectList.begin(); }
  121. iterator end() { return objectList.end(); }
  122. value operator[] (S32 index) { return objectList[U32(index)]; }
  123. inline iterator find( iterator first, iterator last, SimObject *obj ) { return ::find(first, last, obj); }
  124. inline iterator find( SimObject *obj ) { return ::find(begin(), end(), obj); }
  125. template <typename T> inline bool containsType( void )
  126. {
  127. for( iterator itr = begin(); itr != end(); ++itr )
  128. {
  129. if ( dynamic_cast<T*>(*itr) != NULL )
  130. return true;
  131. }
  132. return false;
  133. }
  134. virtual bool reOrder( SimObject *obj, SimObject *target=0 );
  135. SimObject* at(S32 index) const { return objectList.at(index); }
  136. void deleteObjects( void );
  137. void clear();
  138. /// @}
  139. virtual void onRemove();
  140. virtual void onDeleteNotify(SimObject *object);
  141. /// @name Set Management
  142. /// @{
  143. virtual void addObject(SimObject*); ///< Add an object to the set.
  144. virtual void removeObject(SimObject*); ///< Remove an object from the set.
  145. virtual void pushObject(SimObject*); ///< Add object to end of list.
  146. ///
  147. /// It will force the object to the end of the list if it already exists
  148. /// in the list.
  149. virtual void popObject(); ///< Remove an object from the end of the list.
  150. void bringObjectToFront(SimObject* obj) { reOrder(obj, front()); }
  151. void pushObjectToBack(SimObject* obj) { reOrder(obj, NULL); }
  152. /// @}
  153. virtual U32 getTamlChildCount( void ) const
  154. {
  155. return (U32)size();
  156. }
  157. virtual SimObject* getTamlChild( const U32 childIndex ) const
  158. {
  159. // Sanity!
  160. AssertFatal( childIndex < (U32)size(), "SimSet::getTamlChild() - Child index is out of range." );
  161. // For when the assert is not used.
  162. if ( childIndex >= (U32)size() )
  163. return NULL;
  164. return at( childIndex );
  165. }
  166. virtual void addTamlChild( SimObject* pSimObject )
  167. {
  168. // Sanity!
  169. AssertFatal( pSimObject != NULL, "SimSet::addTamlChild() - Cannot add a NULL child object." );
  170. addObject( pSimObject );
  171. }
  172. void callOnChildren( const char * method, S32 argc, const char *argv[], bool executeOnChildGroups = true );
  173. virtual void write(Stream &stream, U32 tabStop, U32 flags = 0);
  174. virtual SimObject *findObject(const char *name);
  175. SimObject* findObjectByInternalName(const char* internalName, bool searchChildren = false);
  176. virtual bool writeObject(Stream *stream);
  177. virtual bool readObject(Stream *stream);
  178. inline void lock()
  179. {
  180. #ifdef TORQUE_MULTITHREAD
  181. Mutex::lockMutex(mMutex);
  182. #endif
  183. }
  184. inline void unlock()
  185. {
  186. #ifdef TORQUE_MULTITHREAD
  187. Mutex::unlockMutex(mMutex);
  188. #endif
  189. }
  190. DECLARE_CONOBJECT(SimSet);
  191. #ifdef TORQUE_DEBUG
  192. inline void _setVectorAssoc( const char *file, const U32 line )
  193. {
  194. objectList.setFileAssociation( file, line );
  195. }
  196. #endif
  197. };
  198. #ifdef TORQUE_DEBUG
  199. # define SIMSET_SET_ASSOCIATION( x ) x._setVectorAssoc( __FILE__, __LINE__ )
  200. #else
  201. # define SIMSET_SET_ASSOCIATION( x )
  202. #endif
  203. /// Iterator for use with SimSets
  204. ///
  205. /// @see SimSet
  206. class SimSetIterator
  207. {
  208. protected:
  209. struct Entry {
  210. SimSet* set;
  211. SimSet::iterator itr;
  212. };
  213. class Stack: public Vector<Entry> {
  214. public:
  215. void push_back(SimSet*);
  216. };
  217. Stack stack;
  218. public:
  219. SimSetIterator(SimSet*);
  220. SimObject* operator++();
  221. SimObject* operator*() {
  222. return stack.empty()? 0: *stack.last().itr;
  223. }
  224. };
  225. //---------------------------------------------------------------------------
  226. /// A group of SimObjects.
  227. ///
  228. /// A SimGroup is a stricter form of SimSet. SimObjects may only be a member
  229. /// of a single SimGroup at a time.
  230. ///
  231. /// The SimGroup will automatically enforce the single-group-membership rule.
  232. ///
  233. /// @code
  234. /// // From engine/sim/simPath.cc - getting a pointer to a SimGroup
  235. /// SimGroup* pMissionGroup = dynamic_cast<SimGroup*>(Sim::findObject("MissionGroup"));
  236. ///
  237. /// // From game/trigger.cc:46 - iterating over a SimObject's group.
  238. /// SimObject* trigger = ...;
  239. /// SimGroup* pGroup = trigger->getGroup();
  240. /// for (SimGroup::iterator itr = pGroup->begin(); itr != pGroup->end(); itr++)
  241. /// {
  242. /// // do something with *itr
  243. /// }
  244. /// @endcode
  245. class SimGroup: public SimSet
  246. {
  247. private:
  248. friend class SimManager;
  249. friend class SimObject;
  250. typedef SimSet Parent;
  251. SimNameDictionary nameDictionary;
  252. public:
  253. ~SimGroup();
  254. /// Add an object to the group.
  255. virtual void addObject(SimObject*);
  256. void addObject(SimObject*, SimObjectId);
  257. void addObject(SimObject*, const char *name);
  258. /// Remove an object from the group.
  259. virtual void removeObject(SimObject*);
  260. virtual void onRemove();
  261. /// Find an object in the group.
  262. virtual SimObject* findObject(const char* name);
  263. bool processArguments(S32 argc, const char **argv);
  264. DECLARE_CONOBJECT(SimGroup);
  265. };
  266. inline void SimGroup::addObject(SimObject* obj, SimObjectId id)
  267. {
  268. obj->mId = id;
  269. addObject( obj );
  270. }
  271. inline void SimGroup::addObject(SimObject *obj, const char *name)
  272. {
  273. addObject( obj );
  274. obj->assignName(name);
  275. }
  276. class SimGroupIterator: public SimSetIterator
  277. {
  278. public:
  279. SimGroupIterator(SimGroup* grp): SimSetIterator(grp) {}
  280. SimObject* operator++();
  281. };
  282. #endif // _SIMSET_H_