SimObjectPool.h

Go to the documentation of this file.
00001 #ifndef SIMELEMENTPOOL_H_
00002 #define SIMELEMENTPOOL_H_
00003 
00004 
00005 #include "SimObject.h"
00006 
00007 #include <vector>
00008 using std::vector;
00009 
00011 class SimObjectPool
00012 {
00013 public:
00014 
00015     SimObjectPool( int nTypes );
00016 
00017     virtual ~SimObjectPool();
00018 
00020     local_objectid_t addObject( SimObjectFactory const& objFactory, RandomEngine *rng  );
00021 
00023     local_objectid_t addObject( SimObjectFactory const& objFactory  );
00024 
00026     SimObject *getObject(object_type_t otype, local_objectid_t oid);
00027 
00029 
00031     void updateObjects( bool dtHasChanged, double dt );
00032 
00034     int numOfTypes() { return pool.size(); };
00035 
00037     int numOfObjects( object_type_t type );
00038 
00040     inline object_type_t getLastTypeId();
00041 
00042 protected:
00043     typedef vector< vector< SimObject* > > eng_type_pool_t ;
00044 
00045 public:
00046 
00048     // typedef vector<SimObject *>::const_iterator SimObjectIterator;
00049     class SimObjectIterator
00050     {
00051     public:
00052         SimObjectIterator( vector<SimObject*> *vec_=NULL, size_t idx_=0):
00053                 vec(vec_), idx(idx_)
00054         { /*NOOP*/ };
00055 
00056         SimObject** operator->()
00057         {
00058             return &((*vec)[idx]);
00059         }
00060 
00061         SimObject* operator*()
00062         {
00063             return ((*vec)[idx]);
00064         }
00065 
00066         inline bool operator!=(const SimObjectIterator& other)
00067         {
00068             return ( idx != other.idx || vec!=other.vec );
00069         }
00070 
00071         inline SimObjectIterator& operator++()
00072         {
00073             idx++;
00074             return *this;
00075         }
00076 
00077         inline local_objectid_t getLocalID(void)
00078         {
00079             return idx;
00080         };
00081 
00082     private:
00083         vector<SimObject*> *vec;
00084         size_t idx;
00085     };
00086 
00087 
00089 
00091     inline SimObjectIterator beginObject(object_type_t type )
00092     {
00093         if( type < pool.size() ) {
00094             return SimObjectIterator( &(pool[type]), 0 );
00095         } else {
00096             return NULL;
00097         }
00098     }
00100     /* \param type Integer ID specifying the type of objects
00101      */
00102     inline SimObjectIterator endObject( object_type_t type )
00103     {
00104         if( type < pool.size() )
00105             return SimObjectIterator( &(pool[type]), pool[type].size() );
00106         else
00107             return NULL;
00108     }
00109 
00110 
00111 protected:
00112 
00113     //inline local_objectid_t addObject( SimObject *o )
00114     //{
00115     //    return  addObject( o, o->getObjectTypeID() );
00116     //};
00117 
00118     local_objectid_t addObject(SimObject *o, object_type_t type);
00119 
00120     eng_type_pool_t pool;
00121 
00122 private:
00123     bool fieldsOfSomeObjectChanged;
00124 
00125     SimObject::BlockAllocator _blockAllocator;
00126 
00127 
00128 };
00129 
00130 inline local_objectid_t SimObjectPool::addObject( SimObjectFactory const& objFactory, RandomEngine *rng )
00131 {
00132     SimObject::setAllocator( _blockAllocator );
00133     return  addObject( objFactory.create( rng ), objFactory.getObjectTypeID() );
00134 }
00135 
00136 inline local_objectid_t SimObjectPool::addObject( SimObjectFactory const& objFactory  )
00137 {
00138     SimObject::setAllocator( _blockAllocator );
00139     return  addObject( objFactory.create(), objFactory.getObjectTypeID() );
00140 }
00141 
00142 inline int SimObjectPool::numOfObjects( object_type_t type )
00143 {
00144     if( type < pool.size() )
00145         return pool[type].size();
00146     else
00147         return 0;
00148 }
00149 
00150 inline object_type_t SimObjectPool::getLastTypeId()
00151 {
00152     return pool.size()-1;
00153 }
00154 
00155 #endif /* SIMELEMENTPOOL_H_ */

Generated on Wed Jul 9 16:34:38 2008 for PCSIM by  doxygen 1.5.5