00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "torc/generic/om/DumpRestoreConfig.hpp"
00017
00018 #ifndef HAVE_CONFIG_H
00019 #include "torc/generic/config.h"
00020 #endif
00021
00022 #ifdef GENOM_SERIALIZATION
00023 #include <boost/bind.hpp>
00024 #include <boost/mem_fn.hpp>
00025 #include <boost/archive/binary_iarchive.hpp>
00026 #include <boost/archive/binary_oarchive.hpp>
00027 #include <boost/serialization/base_object.hpp>
00028 #include <boost/serialization/is_abstract.hpp>
00029 #include <boost/serialization/map.hpp>
00030 #include <boost/serialization/shared_ptr.hpp>
00031 #endif //GENOM_SERIALIZATION
00032
00033 #include "torc/generic/om/Instance.hpp"
00034 #include "torc/generic/om/ParameterMap.hpp"
00035 #include "torc/generic/om/Parameter.hpp"
00036 #include "torc/generic/om/Port.hpp"
00037 #include "torc/generic/om/PortReference.hpp"
00038 #ifdef GENOM_SERIALIZATION
00039 #include "torc/generic/om/Library.hpp"
00040 #include "torc/generic/om/Cell.hpp"
00041 #endif //GENOM_SERIALIZATION
00042 #include "torc/generic/om/View.hpp"
00043
00044 #ifdef GENOM_SERIALIZATION
00045 BOOST_IS_ABSTRACT(
00046 torc::generic::Composite<torc::generic::Instance> )
00047 #endif //GENOM_SERIALIZATION
00048
00049 #ifdef GENOM_SERIALIZATION
00050 namespace {
00051 }
00052 #endif //GENOM_SERIALIZATION
00053
00054 namespace {
00055
00056 class PortMapper {
00057 public:
00058 void
00059 operator() (
00060 const torc::generic::PortReferenceSharedPtr &pRef
00061 ) const throw(torc::generic::Error) {
00062 torc::generic::PortSharedPtr port
00063 = mMaster->findPort( pRef->getName() );
00064 if( !port
00065 || port->getCompositionType()
00066 != pRef->getCompositionType()
00067 || port->getSize() != pRef->getSize() )
00068 {
00069
00070 }
00071 try
00072 {
00073 pRef->bindToMasterPort( port );
00074 }
00075 catch( torc::generic::Error &e )
00076 {
00077 e.setCurrentLocation( __FUNCTION__, __FILE__, __LINE__ );
00078 throw;
00079 }
00080 }
00081
00082 PortMapper(
00083 const torc::generic::ViewSharedPtr &inMaster )
00084 : mMaster( inMaster ) {
00085 }
00086
00087 private:
00088 torc::generic::ViewSharedPtr mMaster;
00089 };
00090
00091 }
00092
00093 namespace torc {
00094
00095 namespace generic {
00096
00097 ParameterContext
00098 Instance::getParameterContext() const throw() {
00099 return mMyContext;
00100 }
00101
00102 ParameterMapSharedPtr
00103 Instance::getParameters() const throw(Error) {
00104 if( !getMaster() )
00105 {
00106 return ParameterMapSharedPtr();
00107 }
00108 ParameterMapSharedPtr params = mMaster->getParameters();
00109 if( !params->isContextRegistered( mMyContext ) )
00110 {
00111 params->registerContext( mMyContext,
00112 mMaster->getParameterContext() );
00113 }
00114 return params;
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 void
00128 Instance::bindToMasterView(
00129 const ViewSharedPtr &inMaster,
00130 bool inMapPortReferences ) throw(Error) {
00131 if( !mMaster )
00132 {
00133
00134 }
00135 ViewSharedPtr oldMaster = getMaster();
00136 std::map<std::string, ParameterSharedPtr> myParams;
00137 if( oldMaster )
00138 {
00139 ParameterMapSharedPtr oldParams
00140 = oldMaster->getParameters();
00141 oldParams->getOverriddenParameters( mMyContext, myParams );
00142 oldParams->unregisterContext( mMyContext );
00143 }
00144 setMaster( inMaster );
00145 ParameterMapSharedPtr paramMap = mMaster->getParameters();
00146 mMyContext = paramMap->getNewContext();
00147
00148
00149
00150
00151 if( !myParams.empty() )
00152 {
00153 try
00154 {
00155 paramMap->registerContext( mMyContext,
00156 mMaster->getParameterContext() );
00157 for( std::map<std::string, ParameterSharedPtr>::iterator
00158 it = myParams.begin(); it != myParams.end(); ++it )
00159 {
00160 paramMap->set(
00161 mMyContext, (*it).first, (*it).second );
00162 }
00163 }
00164 catch( Error &e )
00165 {
00166 e.setCurrentLocation(
00167 __FUNCTION__, __FILE__, __LINE__ );
00168 throw;
00169 }
00170 }
00171 if( inMaster->getIsExtern() )
00172 {
00173 return;
00174 }
00175 if( inMapPortReferences )
00176 {
00177 PortMapper mapper( inMaster );
00178 try
00179 {
00180 mPortReferences.applyOnAll( mapper );
00181 }
00182 catch(Error &e)
00183 {
00184 e.setCurrentLocation(
00185 __FUNCTION__, __FILE__, __LINE__ );
00186 throw;
00187 }
00188 }
00189 }
00190
00191 void
00192 Instance::setMaster(
00193 const ViewSharedPtr &inMaster ) throw() {
00194 mMaster = inMaster;
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204 void
00205 Instance::addPortReference(
00206 const PortReferenceSharedPtr & inPortRef) throw(Error) {
00207 std::string name = inPortRef->getName();
00208 if( name.empty() )
00209 {
00210 Error e( eMessageIdErrorEmptyItemName,
00211 __FUNCTION__, __FILE__, __LINE__ );
00212 e.saveContextData("PortReference name", name);
00213 throw e;
00214 }
00215 if( false == mPortReferences.set( name, inPortRef ) )
00216 {
00217 Error e( eMessageIdErrorItemAlreadyExists,
00218 __FUNCTION__, __FILE__, __LINE__ );
00219 e.saveContextData("PortReference name", name);
00220 throw e;
00221 }
00222 inPortRef->setParent( getSharedThis() );
00223 return;
00224 }
00225
00226
00227
00228
00229
00230
00231 PortReferenceSharedPtr
00232 Instance::findPortReference(
00233 const std::string &inName) throw(Error) {
00234 PortReferenceSharedPtr portRef;
00235 mPortReferences.get( inName, portRef );
00236 return portRef;
00237 }
00238
00239
00240
00241
00242
00243
00244 NetReferenceSharedPtr
00245 Instance::findNetReference(
00246 const std::string &inName) throw(Error) {
00247 NetReferenceSharedPtr netRef;
00248 mNetReferences.get( inName, netRef );
00249 return netRef;
00250 }
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 void
00261 Instance::removePortReference(
00262 const std::string &inName) throw(Error) {
00263 if( inName.empty() )
00264 {
00265 Error e( eMessageIdErrorEmptyItemName,
00266 __FUNCTION__, __FILE__, __LINE__ );
00267 e.saveContextData("PortReference name", inName);
00268 throw e;
00269 }
00270 if( false == mPortReferences.remove( inName ) )
00271 {
00272 Error e( eMessageIdErrorItemNotFound,
00273 __FUNCTION__, __FILE__, __LINE__ );
00274 e.saveContextData("PortReference name", inName);
00275 throw e;
00276 }
00277 return;
00278 }
00279
00280
00281
00282
00283
00284
00285 void
00286 Instance::getPortReferences(
00287 std::vector< PortReferenceSharedPtr > &outPortRefs
00288 ) const throw() {
00289 mPortReferences.getValues( outPortRefs );
00290 }
00291
00292
00293
00294
00295
00296
00297 void
00298 Instance::setPortReferences(
00299 const std::vector< PortReferenceSharedPtr > &inSource
00300 ) throw( Error ) {
00301 std::vector< PortReferenceSharedPtr >::const_iterator portRef = inSource.begin();
00302 std::vector< PortReferenceSharedPtr >::const_iterator endP = inSource.end();
00303 for( ; portRef != endP; ++endP )
00304 {
00305 try
00306 {
00307 addPortReference( *portRef );
00308 }
00309 catch( Error &e )
00310 {
00311 e.setCurrentLocation(
00312 __FUNCTION__, __FILE__, __LINE__ );
00313 throw;
00314 }
00315 }
00316 return;
00317 }
00318
00319 void
00320 Instance::setDesignator(const std::string & inSource) throw() {
00321 mDesignator = inSource;
00322 }
00323
00324
00325
00326
00327
00328
00329 void
00330 Instance::setTiming(const TimingSharedPtr & inSource ) throw() {
00331 mTiming = inSource;
00332 }
00333
00334 void
00335 Instance::flatten() throw(Error) {
00336 }
00337
00338 Instance::Instance()
00339 : Composite<Instance>(),
00340 Commentable(),
00341 Nameable(),
00342 PropertyContainer(),
00343 Renamable(),
00344 Visitable(),
00345 ParentedObject<View>(),
00346 UserDataContainer(),
00347 #ifdef GENOM_SERIALIZATION
00348 mMasterData( NULL ),
00349 #endif
00350 mMaster(),
00351 mPortReferences(),
00352 mMyContext(),
00353 mDesignator() {
00354
00355 }
00356
00357 Instance::~Instance() throw() {
00358 #ifdef GENOM_SERIALIZATION
00359 delete mMasterData;
00360 #endif //GENOM_SERIALIZATION
00361 }
00362
00363 #ifdef GENOM_SERIALIZATION
00364 template<class Archive> void
00365 Instance::load( Archive &ar, unsigned int ) {
00366 ar & boost::serialization::base_object<
00367 Composite<Instance> >( *this );
00368 ar & boost::serialization::base_object<Commentable>( *this );
00369 ar & boost::serialization::base_object<Nameable>( *this );
00370 ar & boost::serialization::base_object<PropertyContainer>(*this);
00371 ar & boost::serialization::base_object<Renamable>( *this );
00372 ar & boost::serialization::base_object<Visitable>( *this );
00373 ar & mPortReferences;
00374 ar & mMasterData;
00375 mPortReferences.applyOnAll( boost::bind( boost::mem_fn(
00376 &PortReference::setParent ), _1, getSharedThis() ) );
00377 }
00378
00379 template<class Archive> void
00380 Instance::save ( Archive &ar, unsigned int ) const {
00381 ar & boost::serialization::base_object<
00382 Composite<Instance> >( *this );
00383 ar & boost::serialization::base_object<Commentable>( *this );
00384 ar & boost::serialization::base_object<Nameable>( *this );
00385 ar & boost::serialization::base_object<PropertyContainer>(*this);
00386 ar & boost::serialization::base_object<Renamable>( *this );
00387 ar & boost::serialization::base_object<Visitable>( *this );
00388 ar & mPortReferences;
00389 mMasterData = new MasterData();
00390 mMasterData->mLibrary = mMaster->getParent()
00391 ->getParent()->getName();
00392 mMasterData->mCell = mMaster->getParent()->getName();
00393 mMasterData->mView = mMaster->getName();
00394
00395 getParameters()->getOverriddenParameters(
00396 getParameterContext(), mMasterData->mParams );
00397 ar & mMasterData;
00398 }
00399
00400
00401 template void
00402 Instance::save<boost::archive::binary_oarchive>(
00403 boost::archive::binary_oarchive & ar, const unsigned int) const;
00404
00405 template void
00406 Instance::load<boost::archive::binary_iarchive>(
00407 boost::archive::binary_iarchive & ar, const unsigned int);
00408
00409 void
00410 Instance::restoreMaster() throw(Error)
00411 try
00412 {
00413 if( !mMasterData )
00414 {
00415
00416 }
00417 ViewSharedPtr view = getParent();
00418 if( !view )
00419 {
00420
00421 }
00422 CellSharedPtr cell = view->getParent();
00423 if( !cell )
00424 {
00425
00426 }
00427 LibrarySharedPtr lib = cell->getParent();
00428 if( !lib )
00429 {
00430
00431 }
00432 RootSharedPtr root = lib->getParent();
00433 if( !root )
00434 {
00435
00436 }
00437 LibrarySharedPtr targetLib
00438 = root->findLibrary( mMasterData->mLibrary );
00439 if( !targetLib )
00440 {
00441
00442 }
00443 CellSharedPtr targetCell
00444 = targetLib->findCell( mMasterData->mCell );
00445 if( !targetCell )
00446 {
00447
00448 }
00449 ViewSharedPtr targetView
00450 = targetCell->findView( mMasterData->mView );
00451 if( !targetView )
00452 {
00453
00454 }
00455 bindToMasterView( targetView );
00456
00457 std::map< std::string,ParameterSharedPtr >::iterator
00458 it = mMasterData->mParams.begin();
00459 std::map< std::string,ParameterSharedPtr >::iterator
00460 end = mMasterData->mParams.end();
00461 for(; it !=end; ++it )
00462 {
00463 getParameters()->set(
00464 mMyContext, (*it).first, (*it).second );
00465 }
00466 delete mMasterData;
00467 mMasterData = NULL;
00468 }
00469 catch( Error &e )
00470 {
00471 e.setCurrentLocation(
00472 __FUNCTION__, __FILE__, __LINE__ );
00473 throw;
00474 }
00475 #endif //GENOM_SERIALIZATION
00476
00477
00478 }
00479
00480 }