SimObjectPool.cpp

Go to the documentation of this file.
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     /* check if the vectors have the appropriate size, if not then resize them
00028      * This is an exceptional situation, should not happen!  */
00029 
00030 //    cerr << "adding new object: " << typeid(*o).name() << ", type=" << (int)type << endl;
00031 
00032     if (pool.size() <= type ) {
00033         pool.resize(type+1);
00034     }
00035 
00036     /* set the local id of the object to be the next index in the  */
00037     local_objectid_t new_loid = pool[type].size() ;
00038     //    o->setLocalID(new_loid);
00039 
00040     pool[type].push_back(o);
00041 //    cerr << "Done new_loid = " << new_loid << endl;
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 //      cerr << " object pool otype = " << (int)otype << " oid = "  << (int)oid << endl;
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 }

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