00001 #include "SimObjectPool.h"
00002 #include <iostream>
00003 using std::cerr;
00004 using std::endl;
00005
00006 #include "PCSIMException.h"
00007
00008 #include <boost/format.hpp>
00009
00010 #include <string>
00011 using std::string;
00012
00013 SimObjectPool::SimObjectPool( int num_obj_types ) :
00014 pool(0), fieldsOfSomeObjectChanged(false)
00015 {
00016 pool.resize(num_obj_types);
00017 fieldsOfSomeObjectChanged = false;
00018 }
00019
00024 local_objectid_t SimObjectPool::addObject(SimObject *o, object_type_t type )
00025 {
00026
00027
00028
00029
00030
00031
00032 if (pool.size() <= type ) {
00033 pool.resize(type+1);
00034 }
00035
00036
00037 local_objectid_t new_loid = pool[type].size() ;
00038
00039
00040 pool[type].push_back(o);
00041
00042 return new_loid ;
00043 }
00044
00045
00046 void SimObjectPool::updateObjects( bool dtChanged, double dt )
00047 {
00048 if ( fieldsOfSomeObjectChanged || dtChanged ) {
00049 for( size_t t = 0; t < pool.size(); t++ ) {
00050 for( size_t i = 0; i < pool[t].size(); i++ ) {
00051 pool[t][i]->adjust( dt );
00052 }
00053 }
00054 }
00055 }
00056
00061 SimObject *SimObjectPool::getObject(object_type_t otype, local_objectid_t oid)
00062 {
00063
00064 if (otype >= pool.size()) {
00065 throw( PCSIM::ConstructionException( "SimObjectPool::getObject", str( boost::format( "Trying to access nonexistent object type=%1%, localid=%2%") % (int)otype % oid) ) );
00066 }
00067 if (oid >= pool[otype].size())
00068 throw( PCSIM::ConstructionException( "SimObjectPool::getObject", str( boost::format( "Trying to access nonexistent object type=%1%, localid=%2%") % (int)otype % oid) ) );
00069 return pool[otype][oid];
00070 }
00071
00076 SimObjectPool::~SimObjectPool()
00077 {
00078 vector< vector<SimObject *> >::const_iterator tit;
00079 vector<SimObject* >::const_iterator oit ;
00080
00081 for (tit = pool.begin() ; tit != pool.end() ; ++tit) {
00082 for (oit = tit->begin() ; oit != tit->end() ; ++oit) {
00083 if ((*oit)->owned_by_net())
00084 delete *oit ;
00085 }
00086 }
00087
00088 }