00001 #include "SimEngine.h"
00002 #include "PCSIMException.h"
00003 #include "SimObjectRegistry.h"
00004
00005 #include <iostream>
00006 using std::cerr;
00007 using std::endl;
00008
00009 SimObjectRegistry *theSimObjectRegistry;
00010
00011
00012 SimObjectRegistry::SimObjectRegistry()
00013 {
00014 reg_per_phase.clear();
00015 reg_orderd.clear();
00016 type_id_map.clear();
00017 simObjInfo.clear();
00018 }
00019
00020 SimObjectRegistry::~SimObjectRegistry()
00021 {
00022 for( size_t i=0; i<simObjInfo.size(); i++ ) {
00023 delete simObjInfo[i];
00024 }
00025 }
00026
00027 object_type_t SimObjectRegistry::registerSimObject( SimObject *obj, AdvancePhase::enum_t phase, string const& name, string const& description)
00028 {
00029 if( phase >= (int)reg_per_phase.size() ) {
00030 reg_per_phase.resize( phase + 1 );
00031 reg_per_phase[phase].resize(0);
00032 }
00033 if( type_id_map.find( typeid( *obj ).name() ) == type_id_map.end() ) {
00034 type_id_map[ typeid( *obj ).name() ] = reg_per_phase[phase].size();
00035 reg_per_phase[phase].push_back( obj );
00036 SimObjectInformation *soi = new SimObjectInformation( name, description );
00037 simObjInfo.push_back( soi );
00038 obj->setSimObjectInformation( soi );
00039
00040 name_factory_map[ name ] = obj;
00041 }
00042 return INVALID_OBJECT_TYPE_ID;
00043 }
00044
00045
00046 void SimObjectRegistry::finalize()
00047 {
00048 SimObject *obj;
00049 object_type_t tid;
00050
00051 #ifdef DEBUG
00052 cerr << endl;
00053 cerr << "---------------------------------------------------------" << endl;
00054 #endif
00055 reg_orderd.clear();
00056 type_id_map.clear();
00057 for( size_t p=0; p < reg_per_phase.size(); p++ ) {
00058 tid = reg_orderd.size();
00059
00060 if( (int)p == (int)AdvancePhase::One::enumval )
00061 SimObjectRegistry::FirstIndexOf::AdvancePhase1 = tid;
00062 else if ( (int)p == (int)AdvancePhase::SpikeDriven::enumval )
00063 SimObjectRegistry::FirstIndexOf::SpikeDriven = tid;
00064 else if ( (int)p == (int)AdvancePhase::Two::enumval )
00065 SimObjectRegistry::FirstIndexOf::AdvancePhase2 = tid;
00066 else if ( (int)p == (int)AdvancePhase::None::enumval )
00067 SimObjectRegistry::FirstIndexOf::NoAdvance = tid;
00068
00069
00070 for( size_t i=0; i < reg_per_phase[p].size(); i++ ) {
00071 tid = reg_orderd.size();
00072 obj = reg_per_phase[p][i];
00073 type_id_map[ typeid( *obj ).name() ] = tid;
00074 obj->setObjectTypeID( tid );
00075 #ifdef DEBUG
00076 cerr << "registerd " << typeid( *obj ).name() << "with tid=" << (int)obj->getObjectTypeID() << endl;
00077 #endif
00078 reg_orderd.push_back( obj );
00079 }
00080 }
00081
00082 #ifdef DEBUG
00083 cerr << "SimObjectRegistry::FirstIndexOf::AdvancePhase1=" << (int)SimObjectRegistry::FirstIndexOf::AdvancePhase1 << endl;
00084 cerr << "SimObjectRegistry::FirstIndexOf::SpikeDriven=" << (int)SimObjectRegistry::FirstIndexOf::SpikeDriven << endl;
00085 cerr << "SimObjectRegistry::FirstIndexOf::AdvancePhase2=" << (int)SimObjectRegistry::FirstIndexOf::AdvancePhase2 << endl;
00086 cerr << "SimObjectRegistry::FirstIndexOf::NoAdvance=" << (int)SimObjectRegistry::FirstIndexOf::NoAdvance << endl;
00087 cerr << "---------------------------------------------------------" << endl << endl;
00088 #endif
00089
00090 }
00091
00092 object_type_t registerSimObject( SimObject *obj, AdvancePhase::enum_t phase, string const& name, string const& description )
00093 {
00094 static bool first = true;
00095 if( first ) {
00096 theSimObjectRegistry = new SimObjectRegistry;
00097 first = false;
00098 }
00099 return theSimObjectRegistry->registerSimObject(obj,phase,name,description);
00100 }
00101
00102 SimObject const& SimObjectRegistry::getObject( object_type_t tid )
00103 {
00104 if( tid < reg_orderd.size() ) {
00105 return *reg_orderd[tid];
00106 } else {
00107 throw( PCSIM::ObjectRegistryException( "getObject", "No object with given type ID registered." ) );
00108 }
00109 }
00110
00111 SimObjectFactory const& SimObjectRegistry::getSimObjectFactory( string name )
00112 {
00113
00114
00115
00116
00117 if( name_factory_map.find( name ) == name_factory_map.end() ) {
00118
00119 throw PCSIM::ObjectRegistryException( "getSimObjectFactory", str( boost::format( "No object with given name %1% registered." ) % name ) );
00120 } else {
00121 return *name_factory_map[ name ];
00122 }
00123 }