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