torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Cell.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 #ifdef GENOM_SERIALIZATION
21 #include <boost/archive/binary_iarchive.hpp>
22 #include <boost/archive/binary_oarchive.hpp>
23 #include <boost/serialization/is_abstract.hpp>
24 #include <boost/serialization/shared_ptr.hpp>
25 #endif //GENOM_SERIALIZATION
26 #include "torc/generic/Cell.hpp"
27 #include "torc/generic/Library.hpp"
29 #include "torc/generic/View.hpp"
30 
31 namespace torc {
32 namespace generic {
33 
34 #ifdef GENOM_SERIALIZATION
35 class RestoredViewUpdater {
36 public:
37  void operator()(const ViewSharedPtr& inView) const throw (Error) {
38  try {
39  inView->setParent(mCell);
40  inView->restoreActions();
41  } catch(Error& e) {
42  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
43  throw;
44  }
45  }
46 
47  RestoredViewUpdater(const CellSharedPtr& inCell) : mCell(inCell) {
48  }
49 private:
50  CellSharedPtr mCell;
51 };
52 #endif //GENOM_SERIALIZATION
53 /**
54  * Create a cell
55  *
56  * @param[in] inName Name of the cell to be created.
57  * @param[in] inLibraryPtr Pointer to parented(Library) object.
58  * @param[in] inCellType Cell Type
59  * @param[in] inOriginalName Original name of the cell [optional]
60  *
61  * @return Pointer to created cell.
62  **/
64  const LibrarySharedPtr& inLibraryPtr, const Cell::Type& inCellType,
65  const std::string& inOriginalName) throw (Error) {
66  try {
67  CellSharedPtr newCell;
68  create(newCell);
69  newCell->setName(inName);
70  newCell->setParent(inLibraryPtr);
71  inLibraryPtr->addCell(newCell);
72  newCell->setType(inCellType);
73  newCell->setOriginalName(inOriginalName);
74  return newCell;
75  } catch(Error& e) {
76  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
77  throw;
78  }
79 }
80 
81 void Cell::accept(BaseVisitor& inoutVisitor) throw (Error) {
82  try {
83  runVisitor(*this, inoutVisitor);
84  } catch(Error& e) {
85  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
86  throw;
87  }
88 }
89 
90 /**
91  * Set the type of cell
92  *
93  * @param[in] inSource Type of cell
94  */
95 void Cell::setType(const Cell::Type& inSource) {
96  mType = inSource;
97 }
98 
99 /**
100  * Add a view to the list of views. An empty pointer will be ignored.
101  *
102  * @param[in] inView Pointer to a view.
103  *
104  * @exception Error Empty View type
105  * @exception Error View name already exists
106  *
107  */
108 void Cell::addView(const ViewSharedPtr& inView) throw (Error) {
109  if(!inView) {
110  return;
111  }
112  std::string name = inView->getName();
113  if(name.empty()) {
114  Error e(eMessageIdErrorEmptyItemName, __FUNCTION__, __FILE__, __LINE__);
115  e.saveContextData("Empty View type", name);
116  throw e;
117  }
118  if(false == mViewSymTab.set(name, inView)) {
119  Error e(eMessageIdErrorItemAlreadyExists, __FUNCTION__, __FILE__, __LINE__);
120  e.saveContextData("View name", name);
121  throw e;
122  }
123  inView->setParent(getSharedThis());
124 }
125 
126 /**
127  * Find a view by name.
128  *
129  * @param[in] inName Name of the cell to be found.
130  *
131  * @return Pointer to cell if found, empty pointer otherwise.
132  */
134  if(inName.empty()) {
135  Error e(eMessageIdErrorEmptyItemName, __FUNCTION__, __FILE__, __LINE__);
136  e.saveContextData("Empty Cell name", inName);
137  throw e;
138  }
139  ViewSharedPtr view;
140  mViewSymTab.get(inName, view);
141  return view;
142 }
143 
144 /**
145  * Remove the specified pointer from the view. Empty pointer is ignored.
146  *
147  * @param[in] inName name of object to be removed
148  *
149  * @exception Error View is not a member of the views list.
150  */
151 void Cell::removeView(const std::string& inName) throw (Error) {
152  if(inName.empty()) {
153  Error e(eMessageIdErrorEmptyItemName, __FUNCTION__, __FILE__, __LINE__);
154  e.saveContextData("View name", inName);
155  throw e;
156  }
157  if(false == mViewSymTab.remove(inName)) {
158  Error e(eMessageIdErrorItemNotFound, __FUNCTION__, __FILE__, __LINE__);
159  e.saveContextData("View name", inName);
160  throw e;
161  }
162  return;
163 }
164 
167  mViewSymTab(), mType(eTypeGeneric) {}
168 
169 Cell::~Cell() throw () {}
170 
171 #ifdef GENOM_SERIALIZATION
172 template <class Archive> void Cell::serialize(Archive& ar, unsigned int) {
173  ar & boost::serialization::base_object < Commentable > (*this);
174  ar & boost::serialization::base_object < Extern > (*this);
175  ar & boost::serialization::base_object < Nameable > (*this);
176  ar & boost::serialization::base_object < PropertyContainer > (*this);
177  ar & boost::serialization::base_object < Renamable > (*this);
178  ar & boost::serialization::base_object < Visitable > (*this);
179  ar & boost::serialization::base_object < SelfReferencing<Cell> > (*this);
180  ar & mViewSymTab;
181  //TBD:: ar & mParameters;
182 }
183 
184 void Cell::restoreActions() throw (Error) {
185  try {
186  mViewSymTab.applyOnAll(RestoredViewUpdater(getSharedThis()));
187  } catch(Error& e) {
188  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
189  throw;
190  }
191 }
192 
193 //TO SATISFY THE LINKER
194 template void Cell::serialize<boost::archive::binary_iarchive>(boost::archive::binary_iarchive& ar,
195  const unsigned int);
196 
197 template void Cell::serialize<boost::archive::binary_oarchive>(boost::archive::binary_oarchive& ar,
198  const unsigned int);
199 
200 #endif //GENOM_SERIALIZATION
201 
202 } // namespace generic
203 } // namespace torc
Represents an EDIF cell.
Definition: Cell.hpp:55
An object that has a parent.
void addView(const ViewSharedPtr &inView)
Definition: Cell.cpp:108
SymTab< std::string, ViewSharedPtr > mViewSymTab
Definition: Cell.hpp:201
void removeView(const std::string &inName)
Definition: Cell.cpp:151
Represents objects that have properties.
An EDIF cell library.
Definition: Library.hpp:60
Represents all classes that can hold user comments.
Definition: Commentable.hpp:36
Represents class that can hold userData.
void applyOnAll(const _Action &action)
Definition: SymTab.hpp:192
void runVisitor(_Tp &inoutVisited, BaseVisitor &inoutVisitor)
Definition: VisitorType.hpp:78
std::string string
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
bool get(const KeyType &inKey, ValueType &outValue) const
Definition: SymTab.hpp:121
virtual ~Cell()
Definition: Cell.cpp:169
void saveContextData(const std::string &inName, const boost::any &inSource)
Definition: Error.cpp:79
virtual CellSharedPtr newCellPtr(const std::string &inName, const LibrarySharedPtr &inLibraryPtr, const Cell::Type &inCellType=Cell::eTypeGeneric, const std::string &inOriginalName=std::string())
Definition: Cell.cpp:63
ViewSharedPtr findView(const std::string &inName)
Definition: Cell.cpp:133
void setType(const Type &inSource)
Definition: Cell.cpp:95
boost::shared_ptr< View > ViewSharedPtr
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.
virtual void accept(BaseVisitor &inoutVisitor)
Definition: Cell.cpp:81
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