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 #include <algorithm>
00023 #include <fstream>
00024
00025 #ifdef GENOM_SERIALIZATION
00026 #include <boost/archive/binary_iarchive.hpp>
00027 #include <boost/archive/binary_oarchive.hpp>
00028 #include <boost/serialization/base_object.hpp>
00029 #include <boost/serialization/list.hpp>
00030 #include <boost/serialization/shared_ptr.hpp>
00031 #include <boost/serialization/string.hpp>
00032 #endif //GENOM_SERIALIZATION
00033
00034 #include "torc/generic/util/Log.hpp"
00035 #include "torc/generic/om/Root.hpp"
00036 #include "torc/generic/om/Library.hpp"
00037 #include "torc/generic/om/Design.hpp"
00038
00039 namespace torc {
00040
00041 namespace generic {
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 void
00052 Root::addLibrary(
00053 const LibrarySharedPtr &inLibrary) throw(Error) {
00054 if( !inLibrary )
00055 {
00056 return;
00057 }
00058 std::string name = inLibrary->getName();
00059 if( name.empty() )
00060 {
00061 Error e( eMessageIdErrorEmptyItemName,
00062 __FUNCTION__, __FILE__, __LINE__ );
00063 e.saveContextData("Library name", name);
00064 throw e;
00065 }
00066 #ifdef GENOM_SERIALIZATION
00067 std::list< std::string >::iterator it
00068 = std::find( mDumpedLibraries.begin(),
00069 mDumpedLibraries.end(), name );
00070 if( it != mDumpedLibraries.end() )
00071 {
00072 Error e( eMessageIdErrorItemAlreadyExists,
00073 __FUNCTION__, __FILE__, __LINE__ );
00074 e.saveContextData("Library name", name);
00075 throw e;
00076 }
00077 #endif //GENOM_SERIALIZATION
00078 if( false == mLibraries.set( name, inLibrary ) )
00079 {
00080 Error e( eMessageIdErrorItemAlreadyExists,
00081 __FUNCTION__, __FILE__, __LINE__ );
00082 e.saveContextData("Library name", name);
00083 throw e;
00084 }
00085 inLibrary->setParent( getSharedThis() );
00086 return;
00087 }
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 void
00098 Root::removeLibrary(const std::string &inName) throw(Error) {
00099 if( inName.empty() )
00100 {
00101 Error e( eMessageIdErrorEmptyItemName,
00102 __FUNCTION__, __FILE__, __LINE__ );
00103 e.saveContextData("Library name", inName);
00104 throw e;
00105 }
00106 #ifdef GENOM_SERIALIZATION
00107 std::list< std::string >::iterator it
00108 = std::find( mDumpedLibraries.begin(),
00109 mDumpedLibraries.end(), inName );
00110 if( it == mDumpedLibraries.end() )
00111 {
00112 Error e( eMessageIdErrorItemNotFound,
00113 __FUNCTION__, __FILE__, __LINE__ );
00114 e.saveContextData("Library name", inName);
00115 throw e;
00116 }
00117 else
00118 {
00119 mDumpedLibraries.erase( it );
00120 }
00121 #endif //GENOM_SERIALIZATION
00122
00123 if( false == mLibraries.remove( inName ) )
00124 {
00125 Error e( eMessageIdErrorItemNotFound,
00126 __FUNCTION__, __FILE__, __LINE__ );
00127 e.saveContextData("Library name", inName);
00128 throw e;
00129 }
00130 return;
00131 }
00132
00133
00134
00135
00136
00137
00138 void
00139 Root::getLibraries(
00140 std::vector< LibrarySharedPtr > &outLibraries) throw() {
00141 #ifdef GENOM_SERIALIZATION
00142 restoreAllLibraries();
00143 #endif //GENOM_SERIALIZATION
00144 mLibraries.getValues( outLibraries );
00145 }
00146
00147
00148
00149
00150
00151
00152
00153
00154 LibrarySharedPtr
00155 Root::findLibrary(const std::string &inName) throw() {
00156 LibrarySharedPtr library;
00157 mLibraries.get( inName, library );
00158 #ifdef GENOM_SERIALIZATION
00159 if( !library && !mDumpedLibraries.empty() )
00160 {
00161 library = restoreSingleLibrary( inName );
00162 }
00163 #endif //GENOM_SERIALIZATION
00164 return library;
00165 }
00166
00167
00168
00169
00170 void
00171 Root::addDesign(
00172 const DesignSharedPtr &inDesign) throw(Error) {
00173 if( !inDesign )
00174 {
00175 return;
00176 }
00177 std::string name = inDesign->getName();
00178 if( name.empty() )
00179 {
00180 Error e( eMessageIdErrorEmptyItemName,
00181 __FUNCTION__, __FILE__, __LINE__ );
00182 e.saveContextData("Design name", name);
00183 throw e;
00184 }
00185 if( false == mDesignSymTab.set( name, inDesign ) )
00186 {
00187 Error e( eMessageIdErrorItemAlreadyExists,
00188 __FUNCTION__, __FILE__, __LINE__ );
00189 e.saveContextData("Design name", name);
00190 throw e;
00191 }
00192 inDesign->setParent( getSharedThis() );
00193 return;
00194 }
00195
00196
00197
00198
00199
00200 void
00201 Root::removeDesign(const std::string &inName) throw(Error) {
00202 if( inName.empty() )
00203 {
00204 Error e( eMessageIdErrorEmptyItemName,
00205 __FUNCTION__, __FILE__, __LINE__ );
00206 e.saveContextData("Design name", inName);
00207 throw e;
00208 }
00209 if( false == mDesignSymTab.remove( inName ) )
00210 {
00211 Error e( eMessageIdErrorItemNotFound,
00212 __FUNCTION__, __FILE__, __LINE__ );
00213 e.saveContextData("Design name", inName);
00214 throw e;
00215 }
00216 return;
00217 }
00218
00219
00220
00221
00222
00223
00224 void
00225 Root::getDesigns(
00226 std::vector< DesignSharedPtr > &outDesigns) throw() {
00227 mDesignSymTab.getValues( outDesigns );
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237 DesignSharedPtr
00238 Root::findDesign(const std::string &inName) throw() {
00239 DesignSharedPtr design;
00240 mDesignSymTab.get( inName, design );
00241 return design;
00242 }
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 RootSharedPtr
00254 Root::Factory::newRootPtr( const std::string &inName,
00255 const EdifLevel &inEdifLevel,
00256 const std::string &inOriginalName ) throw(Error) {
00257 try
00258 {
00259 RootSharedPtr newRoot;
00260 create( newRoot );
00261 newRoot->setName( inName );
00262 newRoot->setLevel( inEdifLevel );
00263 newRoot->setOriginalName( inOriginalName );
00264 return newRoot;
00265 }
00266 catch( Error &e )
00267 {
00268 e.setCurrentLocation(
00269 __FUNCTION__, __FILE__, __LINE__ );
00270 throw;
00271 }
00272 }
00273
00274 void
00275 Root::accept(BaseVisitor & inoutVisitor) throw(Error) {
00276 try
00277 {
00278 runVisitor( *this, inoutVisitor );
00279 }
00280 catch( Error &e )
00281 {
00282 e.setCurrentLocation(
00283 __FUNCTION__, __FILE__, __LINE__ );
00284 throw;
00285 }
00286 }
00287
00288
00289
00290
00291
00292
00293 void
00294 Root::setVersion(const EdifVersion & inSource) throw() {
00295 mVersion = inSource;
00296 }
00297
00298
00299
00300
00301
00302
00303
00304
00305 void
00306 Root::setLevel(const EdifLevel & inSource) throw() {
00307 mLevel = inSource;
00308 }
00309
00310 #ifdef GENOM_SERIALIZATION
00311
00312 void
00313 Root::setDumpRestoreData(
00314 const DumpRestoreData &inDumpRestoreData ) throw() {
00315 mDumpRestoreData = inDumpRestoreData;
00316 }
00317
00318 void
00319 Root::handleNewDumpRestoreData(
00320 const DumpRestoreData &inDumpRestoreData ) throw(Error) {
00321 mDumpRestoreData = inDumpRestoreData;
00322 if( mDumpRestoreData.getRestoreAllComponents() )
00323 {
00324 try
00325 {
00326 restoreAllLibraries();
00327 }
00328 catch( Error &e )
00329 {
00330 e.setCurrentLocation(
00331 __FUNCTION__, __FILE__, __LINE__ );
00332 throw;
00333 }
00334 }
00335 }
00336
00337 #endif //GENOM_SERIALIZATION
00338
00339 Root::Root()
00340 :Commentable(),
00341 Nameable(),
00342 Renamable(),
00343 Visitable(),
00344 SelfReferencing<Root>(),
00345 UserDataContainer(),
00346 StatusContainer(),
00347 mLevel(),
00348 mLibraries(),
00349 mVersion()
00350 #ifdef GENOM_SERIALIZATION
00351 , mDumpRestoreData(),
00352 mDumpedLibraries()
00353 #endif
00354 {
00355 }
00356
00357 Root::~Root() throw() {
00358 log("Root destroyed\n");
00359 }
00360
00361 #ifdef GENOM_SERIALIZATION
00362 template<class Archive> void
00363 Root::load( Archive &ar, unsigned int ) {
00364 ar & boost::serialization::base_object<Commentable>( *this );
00365 ar & boost::serialization::base_object<Nameable>( *this );
00366 ar & boost::serialization::base_object<Renamable>( *this );
00367 ar & boost::serialization::base_object<Visitable>( *this );
00368 ar & boost::serialization::base_object<
00369 SelfReferencing<Root> >( *this );
00370 ar & mLevel;
00371 ar & mVersion;
00372
00373 ar & mDumpedLibraries;
00374
00375
00376 }
00377
00378 template<class Archive> void
00379 Root::save( Archive &ar, unsigned int ) const {
00380 typedef std::vector< LibrarySharedPtr > Libs;
00381 ar & boost::serialization::base_object<Commentable>( *this );
00382 ar & boost::serialization::base_object<Nameable>( *this );
00383 ar & boost::serialization::base_object<Renamable>( *this );
00384 ar & boost::serialization::base_object<Visitable>( *this );
00385 ar & boost::serialization::base_object<
00386 SelfReferencing<Root> >( *this );
00387 ar & mLevel;
00388 ar & mVersion;
00389
00390 Libs libs;
00391 mLibraries.getValues( libs );
00392 Libs::iterator lib = libs.begin();
00393 Libs::iterator end = libs.end();
00394 for(; lib != end; ++lib )
00395 {
00396 log("HERE\n");
00397 try
00398 {
00399 dump( *lib );
00400 }
00401 catch( Error &e )
00402 {
00403 e.setCurrentLocation(
00404 __FUNCTION__, __FILE__, __LINE__ );
00405 throw;
00406 }
00407 mDumpedLibraries.push_back( (*lib)->getName() );
00408 }
00409 ar & mDumpedLibraries;
00410 mDumpedLibraries.clear();
00411
00412
00413
00414
00415 log("Root dumped");
00416 }
00417
00418 LibrarySharedPtr
00419 Root::restoreSingleLibrary( const std::string &inName ) throw(Error) {
00420 LibrarySharedPtr library;
00421 std::list< std::string >::iterator lib
00422 = find( mDumpedLibraries.begin(),
00423 mDumpedLibraries.end(), inName );
00424 if( lib == mDumpedLibraries.end() )
00425 {
00426 return library;
00427 }
00428 try
00429 {
00430 library = restore( *lib, getSharedThis() );
00431 if( library )
00432 {
00433 mDumpedLibraries.erase( lib );
00434 addLibrary( library );
00435 }
00436 }
00437 catch( Error &e )
00438 {
00439 e.setCurrentLocation(
00440 __FUNCTION__, __FILE__, __LINE__ );
00441 throw;
00442 }
00443 return library;
00444 }
00445
00446 void
00447 Root::restoreAllLibraries() throw(Error)
00448 try
00449 {
00450 std::vector<std::string> libs;
00451 libs.insert( libs.end(),
00452 mDumpedLibraries.begin(), mDumpedLibraries.end());
00453 for( std::vector<std::string>::iterator lib = libs.begin();
00454 lib != libs.end(); ++lib)
00455 {
00456 try
00457 {
00458 restoreSingleLibrary( *lib );
00459 }
00460 catch( Error &e )
00461 {
00462 e.setCurrentLocation(
00463 __FUNCTION__, __FILE__, __LINE__ );
00464 throw;
00465 }
00466 }
00467 }
00468 catch( Error &e )
00469 {
00470 e.setCurrentLocation(
00471 __FUNCTION__, __FILE__, __LINE__ );
00472 throw;
00473 }
00474
00475
00476 template void
00477 Root::load<boost::archive::binary_iarchive>(
00478 boost::archive::binary_iarchive & ar, const unsigned int);
00479
00480 template void
00481 Root::save<boost::archive::binary_oarchive>(
00482 boost::archive::binary_oarchive & ar, const unsigned int) const;
00483
00484 void
00485 dump( const RootSharedPtr &inRoot ) throw(Error) {
00486 if( !inRoot )
00487 {
00488
00489 }
00490 try
00491 {
00492 dump( inRoot, inRoot->getDumpRestoreData() );
00493 }
00494 catch( Error &e )
00495 {
00496 e.setCurrentLocation(
00497 __FUNCTION__, __FILE__, __LINE__ );
00498 throw;
00499 }
00500 }
00501
00502 void
00503 dump( const RootSharedPtr &inRoot,
00504 const DumpRestoreData &inData ) throw(Error) {
00505 if( !inRoot )
00506 {
00507
00508 }
00509 if( inRoot->getName().empty() )
00510 {
00511
00512 }
00513 if( inData.getDumpPath().empty() )
00514 {
00515
00516 }
00517 inRoot->setDumpRestoreData( inData );
00518 std::string fileName = inData.getDumpPath() + "/";
00519 fileName += inRoot->getName() + ".root";
00520 std::ofstream dout( fileName.c_str(),
00521 std::ios::out | std::ios::binary );
00522 if( !dout )
00523 {
00524
00525 }
00526 boost::archive::binary_oarchive rootAr( dout );
00527 rootAr & inRoot;
00528 }
00529
00530 RootSharedPtr
00531 restore( const std::string &inName,
00532 const DumpRestoreData &inData ) throw(Error) {
00533 RootSharedPtr root;
00534 if( inName.empty() )
00535 {
00536
00537 }
00538 if( inData.getDumpPath().empty() )
00539 {
00540
00541 }
00542 std::string fileName = inData.getDumpPath() + "/";
00543 fileName += inName + ".root";
00544 std::ifstream din( fileName.c_str(),
00545 std::ios::out | std::ios::binary );
00546 if( !din )
00547 {
00548
00549 }
00550 boost::archive::binary_iarchive rootAr( din );
00551 rootAr & root;
00552 if( root )
00553 {
00554 try
00555 {
00556 root->handleNewDumpRestoreData( inData );
00557 }
00558 catch( Error &e )
00559 {
00560 e.setCurrentLocation(
00561 __FUNCTION__, __FILE__, __LINE__ );
00562 throw;
00563 }
00564 }
00565 return root;
00566 }
00567 #endif //GENOM_SERIALIZATION
00568
00569 }
00570
00571 }