torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Library.cpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 #ifndef HAVE_CONFIG_H
17 #include "torc/generic/config.h"
18 #endif
19 
20 #include <fstream>
21 
22 //BOOST
23 #ifdef GENOM_SERIALIZATION
24 #include <boost/archive/binary_iarchive.hpp>
25 #include <boost/archive/binary_oarchive.hpp>
26 #include <boost/serialization/base_object.hpp>
27 #include <boost/serialization/is_abstract.hpp>
28 #include <boost/serialization/list.hpp>
29 #include <boost/serialization/shared_ptr.hpp>
30 #endif //GENOM_SERIALIZATION
31 #include "torc/generic/Cell.hpp"
32 #include "torc/generic/Library.hpp"
33 #include "torc/generic/Log.hpp"
35 #ifdef GENOM_SERIALIZATION
37 #endif //GENOM_SERIALIZATION
38 namespace torc {
39 namespace generic {
40 
41 #ifdef GENOM_SERIALIZATION
42 class RestoredCellUpdater {
43 public:
44  void operator()(const CellSharedPtr& inCell) const throw (Error) {
45  try {
46  inCell->setParent(mLibrary);
47  inCell->restoreActions();
48  } catch(Error& e) {
49  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
50  throw;
51  }
52  }
53 
54  RestoredCellUpdater(const LibrarySharedPtr& inLibrary) :
55  mLibrary(inLibrary) {
56  }
57 private:
58  LibrarySharedPtr mLibrary;
59 };
60 #endif //GENOM_SERIALIZATION
61 /**
62  * Create a library
63  *
64  * @param[in] inName Name of the library to be created.
65  * @param[in] inRootPtr Pointer to parented(Root) object.
66  * @param[in] inEdifLevel Edif level.
67  * @param[in] inOriginalName Original name of the library [optional]
68  *
69  * @return Pointer to created library.
70  **/
72  const RootSharedPtr& inRootPtr, const EdifLevel& inEdifLevel, const std::string& inOriginalName)
73  throw (Error) {
74  try {
75  LibrarySharedPtr newLibrary;
76  create(newLibrary);
77  newLibrary->setName(inName);
78  newLibrary->setParent(inRootPtr);
79  inRootPtr->addLibrary(newLibrary);
80  newLibrary->setLevel(inEdifLevel);
81  newLibrary->setOriginalName(inOriginalName);
82  return newLibrary;
83  } catch(Error& e) {
84  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
85  throw;
86  }
87 }
88 
89 void Library::accept(BaseVisitor& inoutVisitor) throw (Error) {
90  try {
91  runVisitor(*this, inoutVisitor);
92  } catch(Error& e) {
93  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
94  throw;
95  }
96 }
97 
98 /**
99  * Set the EDIF level.
100  *
101  * @note Current parser supports LEVEL_1 edif only.
102  *
103  * @param[in] inSource EdifLevel object
104  */
105 void Library::setLevel(const EdifLevel& inSource) {
106  mLevel = inSource;
107 }
108 
109 /**
110  * Find a scale factor for the given unit.
111  *
112  * @param[in] inUnit Units for which scale factor is to be found.
113  * @param[out] outResult The scale factor if found.
114  *
115  * @return true if found, false otherwise
116  */
117 bool Library::findScaleFactor(Unit inUnit, ScaleFactor& outResult) const {
118  return mScaleFactors.get(inUnit, outResult);
119 }
120 
121 /**
122  * Set a scale factor for the given unit.
123  *
124  * @param[in] inUnit Units for which scale factor is to be set.
125  * @param[out] inScale The scale factor to store.
126  *
127  */
128 bool Library::setScaleFactor(Unit inUnit, const ScaleFactor& inScale) {
129  return mScaleFactors.set(inUnit, inScale);
130 }
131 
132 /**
133  * Add a cell to the list of cells. If an empty pointer is supplied, it returns without doing anything.
134  *
135  * @param[in] inCell A pointer to a cell object.
136  *
137  * @exception Error Cell could not be added.
138  */
139 void Library::addCell(const CellSharedPtr& inCell) throw (Error) {
140  if(!inCell) {
141  return;
142  }
143  std::string cellName = inCell->getName();
144  if(cellName.empty()) {
145  Error e(eMessageIdErrorEmptyItemName, __FUNCTION__, __FILE__, __LINE__);
146  e.saveContextData("Cell name", cellName);
147  throw e;
148  }
149 #ifdef GENOM_SERIALIZATION
150  std::list<std::string>::iterator it = std::find(mDumpedCells.begin(), mDumpedCells.end(),
151  cellName);
152  if(it != mDumpedCells.end()) {
153  Error e(eMessageIdErrorItemAlreadyExists, __FUNCTION__, __FILE__, __LINE__);
154  e.saveContextData("Cell name", cellName);
155  throw e;
156  }
157 #endif //GENOM_SERIALIZATION
158  if(false == mCellSymTab.set(cellName, inCell)) {
159  Error e(eMessageIdErrorItemAlreadyExists, __FUNCTION__, __FILE__, __LINE__);
160  e.saveContextData("Cell name", cellName);
161  throw e;
162  }
163  inCell->setParent(getSharedThis());
164 }
165 
166 /**
167  * Find a cell by name, in the list of cells.
168  *
169  * @param[in] inName String inSource specifying the name of the cell.
170  *
171  * @return A pointer to the cell if found, an empty pointer otherwise.
172  */
174  if(inName.empty()) {
175  Error e(eMessageIdErrorEmptyItemName, __FUNCTION__, __FILE__, __LINE__);
176  e.saveContextData("Cell name", inName);
177  throw e;
178  }
179  CellSharedPtr cell;
180  mCellSymTab.get(inName, cell);
181 #ifdef GENOM_SERIALIZATION
182  if(!cell && !mDumpedCells.empty()) {
183  cell = restoreSingleCell(inName);
184  }
185 #endif //GENOM_SERIALIZATION
186  return cell;
187 }
188 
189 /**
190  * Remove the specified cell from the list of cells. If an empty pointer is passed, it returns without doing anything
191  *
192  * @param inCell Pointer to a cell object.
193  *
194  * @exception Error Cell name is empty
195  * @exception Error Cell name not preset in collection.
196  */
197 void Library::removeCell(const std::string& inName) throw (Error) {
198  if(inName.empty()) {
199  Error e(eMessageIdErrorEmptyItemName, __FUNCTION__, __FILE__, __LINE__);
200  e.saveContextData("Cell name", inName);
201  throw e;
202  }
203 
204 #ifdef GENOM_SERIALIZATION
205  std::list<std::string>::iterator it = std::find(mDumpedCells.begin(), mDumpedCells.end(),
206  inName);
207  if(it == mDumpedCells.end()) {
208  Error e(eMessageIdErrorItemNotFound, __FUNCTION__, __FILE__, __LINE__);
209  e.saveContextData("Cell name", inName);
210  throw e;
211  } else {
212  mDumpedCells.erase(it);
213  }
214 #endif //GENOM_SERIALIZATION
215  if(false == mCellSymTab.remove(inName)) {
216  Error e(eMessageIdErrorItemNotFound, __FUNCTION__, __FILE__, __LINE__);
217  e.saveContextData("Cell name", inName);
218  throw e;
219  }
220 }
221 
222 /**
223  * Get the list of cells.
224  *
225  * @return List of cells in the library.
226  */
227 void Library::getCells(std::vector<CellSharedPtr>& outCells) {
228 #ifdef GENOM_SERIALIZATION
229  restoreAllCells();
230 #endif //GENOM_SERIALIZATION
231  mCellSymTab.getValues(outCells);
232 }
233 
234 /**
235  * Set the pointer to the simulation info.
236  *
237  * @param[in] inSource Pointer to the simulation info
238  */
240  mSimulationInfo = inSource;
241 }
242 
245  mScaleFactors(), mLevel(), mCellSymTab(), mSimulationInfo() {}
246 
247 Library::~Library() throw () {
248  log("Library destroyed \n");
249 }
250 
251 #ifdef GENOM_SERIALIZATION
252 template <class Archive> void Library::save(Archive& ar, unsigned int) const {
253  typedef std::vector<CellSharedPtr> Cells;
254  ar & boost::serialization::base_object < Commentable > (*this);
255  ar & boost::serialization::base_object < Extern > (*this);
256  ar & boost::serialization::base_object < Nameable > (*this);
257  ar & boost::serialization::base_object < Renamable > (*this);
258  ar & boost::serialization::base_object < Visitable > (*this);
259 // ar & boost::serialization::base_object<
260 // ParentedObject<Root> >(*this);
261  ar & boost::serialization::base_object < SelfReferencing<Library> > (*this);
262  ar & mScaleFactors;
263  ar & mLevel;
264  Cells cells;
265  mCellSymTab.getValues(cells);
266  Cells::iterator cell = cells.begin();
267  Cells::iterator end = cells.end();
268  for(; cell != end; ++cell) {
269  std::string fileName = getParent()->getDumpRestoreData().getDumpPath() + "/";
270  fileName += getName() + "_";
271  fileName += (*cell)->getName() + ".cell";
272  std::ofstream fout(fileName.c_str(), std::ios::out | std::ios::binary);
273  if(!fout) {
274  //TBD::ERROR
275  }
276  boost::archive::binary_oarchive cellAr(fout);
277 
278  (*cell)->setParent(LibrarySharedPtr());
279  //We reset the parent to avoid dumping the library again
280  cellAr & (*cell); //Dump cell
281  (*cell)->setParent(getSharedThis()); //For the rest of
282  //this session
283  mDumpedCells.push_back((*cell)->getName());
284  }
285  ar & mDumpedCells; //Dump list of cell names for this lib
286  mDumpedCells.clear(); //We clear this list now, since
287  //the cells are still in memory
288  //Common sense dictates that we remove the dumped cells from lib
289  //.. however, they may have been instantiated else where.
290  //So we have to keep them in memory and connected to this lib
291 }
292 
293 template <class Archive> void Library::load(Archive& ar, unsigned int) {
294  ar & boost::serialization::base_object < Commentable > (*this);
295  ar & boost::serialization::base_object < Extern > (*this);
296  ar & boost::serialization::base_object < Nameable > (*this);
297  ar & boost::serialization::base_object < Renamable > (*this);
298  ar & boost::serialization::base_object < Visitable > (*this);
299 // ar & boost::serialization::base_object< ParentedObject<Root> >(*this);
300  ar & boost::serialization::base_object < SelfReferencing<Library> > (*this);
301  ar & mScaleFactors;
302  ar & mLevel;
303  ar & mDumpedCells;
304  //We do not retrieve cells now
305  //We retreive when required
306 }
307 
308 CellSharedPtr Library::restoreSingleCell(const std::string& inName) throw (Error) {
309  CellSharedPtr cell;
310  std::list<std::string>::iterator it = std::find(mDumpedCells.begin(), mDumpedCells.end(),
311  inName);
312  if(it == mDumpedCells.end()) {
313  return cell;
314  }
315  std::string fileName = getParent()->getDumpRestoreData().getDumpPath() + "/";
316  fileName += getName() + "_";
317  fileName += inName + ".cell";
318  std::ifstream fin(fileName.c_str(), std::ios::in | std::ios::binary);
319  if(!fin) {
320  //TBD::ERROR
321  }
322  boost::archive::binary_iarchive cellAr(fin);
323  cellAr & cell;
324  if(cell) {
325  mDumpedCells.erase(it);
326  RestoredCellUpdater updater(getSharedThis());
327  updater(cell);
328  addCell(cell);
329  }
330  return cell;
331 }
332 
333 void Library::restoreAllCells() throw (Error)
334 try {
335  std::vector < std::string > cells;
336  cells.insert(cells.end(), mDumpedCells.begin(), mDumpedCells.end());
337  for(std::vector<std::string>::iterator cell = cells.begin(); cell != cells.end(); ++cell) {
338  try {
339  restoreSingleCell(*cell);
340  } catch(Error& e) {
341  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
342  throw;
343  }
344  }
345 }
346 catch(Error& e) {
347  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
348  throw;
349 }
350 
351 //TO SATISFY THE LINKER
352 template void
353 Library::load<boost::archive::binary_iarchive>(boost::archive::binary_iarchive& ar,
354  const unsigned int);
355 
356 template void
357 Library::save<boost::archive::binary_oarchive>(boost::archive::binary_oarchive& ar,
358  const unsigned int) const;
359 
360 void dump(const LibrarySharedPtr& inLibrary) throw (Error) {
361  if(!inLibrary) {
362  //TBD::ERROR
363  }
364  if(inLibrary->getName().empty()) {
365  //TBD::ERROR
366  }
367  if(!inLibrary->getParent()) {
368  //TBD::ERROR
369  }
370  try {
371  dump(inLibrary, inLibrary->getParent()->getDumpRestoreData());
372  } catch(Error& e) {
373  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
374  throw;
375  }
376 }
377 
378 void dump(const LibrarySharedPtr& inLibrary, const DumpRestoreData& inData) throw (Error) {
379  if(!inLibrary) {
380  //TBD::ERROR
381  }
382  if(inLibrary->getName().empty()) {
383  //TBD::ERROR
384  }
385  if(inData.getDumpPath().empty()) {
386  //TBD::ERROR
387  }
388  std::string fileName = inData.getDumpPath() + "/";
389  fileName += inLibrary->getName() + ".lib";
390  std::ofstream fout(fileName.c_str(), std::ios::out | std::ios::binary);
391  if(!fout) {
392  //TBD::ERROR
393  }
394  boost::archive::binary_oarchive libAr(fout);
395 
396  libAr & inLibrary; //Dump lib
397 }
398 
399 LibrarySharedPtr restore(const std::string& inName, const RootSharedPtr& inParent) throw (Error) {
400  LibrarySharedPtr lib;
401  if(!inParent) {
402  //TBD::ERROR
403  }
404  try {
405  lib = restore(inName, inParent, inParent->getDumpRestoreData());
406  } catch(Error& e) {
407  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
408  throw;
409  }
410  return lib;
411 }
412 
413 LibrarySharedPtr restore(const std::string& inName, const RootSharedPtr& inParent,
414  const DumpRestoreData& inData) throw (Error) {
415  if(inName.empty()) {
416  //TBD::ERROR
417  }
418  LibrarySharedPtr library;
419  std::string fileName = inData.getDumpPath() + "/";
420  fileName += inName + ".lib";
421  std::ifstream fin(fileName.c_str(), std::ios::in | std::ios::binary);
422  if(!fin) {
423  //TBD::ERROR
424  }
425  boost::archive::binary_iarchive libAr(fin);
426  libAr & library;
427  if(library) {
428  library->setParent(inParent);
429  }
430  return library;
431 }
432 
433 #endif //GENOM_SERIALIZATION
434 
435 } // namespace generic
436 } // namespace torc
void getValues(std::vector< ValueType > &outValues) const
Definition: SymTab.hpp:158
void log(const char *fmt,...)
Definition: Log.cpp:89
void addCell(const CellSharedPtr &inCell)
Definition: Library.cpp:139
bool setScaleFactor(Unit inUnit, const ScaleFactor &inScale)
Definition: Library.cpp:128
An object that has a parent.
An EDIF cell library.
Definition: Library.hpp:60
void setSimulationInfo(const SimulationInfoSharedPtr &inSource)
Definition: Library.cpp:239
virtual void accept(BaseVisitor &inoutVisitor)
Definition: Library.cpp:89
Represents all classes that can hold user comments.
Definition: Commentable.hpp:36
Represents class that can hold userData.
virtual void getCells(std::vector< CellSharedPtr > &outCells)
Definition: Library.cpp:227
void runVisitor(_Tp &inoutVisited, BaseVisitor &inoutVisitor)
Definition: VisitorType.hpp:78
std::string string
bool findScaleFactor(Unit inUnit, ScaleFactor &outResult) const
Definition: Library.cpp:117
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
boost::shared_ptr< Library > LibrarySharedPtr
A base class for Visitor.
Definition: VisitorType.hpp:31
boost::shared_ptr< SimulationInfo > SimulationInfoSharedPtr
SymTab< std::string, CellSharedPtr > mCellSymTab
Definition: Library.hpp:293
const boost::shared_ptr< Root > getParent() const
void saveContextData(const std::string &inName, const boost::any &inSource)
Definition: Error.cpp:79
SimulationInfoSharedPtr mSimulationInfo
Definition: Library.hpp:294
virtual CellSharedPtr findCell(const std::string &inName)
Definition: Library.cpp:173
void removeCell(const std::string &inName)
Definition: Library.cpp:197
void setLevel(const EdifLevel &inSource)
Definition: Library.cpp:105
SymTab< Unit, ScaleFactor > mScaleFactors
Definition: Library.hpp:291
virtual LibrarySharedPtr newLibraryPtr(const std::string &inName, const RootSharedPtr &inRootPtr, const EdifLevel &inEdifLevel=eEdifLevel0, const std::string &inOriginalName=std::string())
Definition: Library.cpp:71
Root of the EDIF Object Model.
Definition: Root.hpp:66
virtual const std::string getName() const
Definition: Nameable.hpp:89
boost::shared_ptr< Cell > CellSharedPtr
An object that has a name.
Definition: Nameable.hpp:34
Represents objects that can be renamed.
Represents objects that have status.
boost::shared_ptr< Root > RootSharedPtr
An object that receives an inoutVisitor.
Definition: Visitable.hpp:38
Used to implement external object referencing.
Definition: Extern.hpp:36
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73