torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
generic/Instance.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/bind.hpp>
22 #include <boost/mem_fn.hpp>
23 #include <boost/archive/binary_iarchive.hpp>
24 #include <boost/archive/binary_oarchive.hpp>
25 #include <boost/serialization/base_object.hpp>
26 #include <boost/serialization/is_abstract.hpp>
27 #include <boost/serialization/map.hpp>
28 #include <boost/serialization/shared_ptr.hpp>
29 #endif //GENOM_SERIALIZATION
33 #include "torc/generic/Port.hpp"
35 #ifdef GENOM_SERIALIZATION
36 #include "torc/generic/Library.hpp"
37 #include "torc/generic/Cell.hpp"
38 #endif //GENOM_SERIALIZATION
39 #include "torc/generic/View.hpp"
40 
41 #ifdef GENOM_SERIALIZATION
43 #endif //GENOM_SERIALIZATION
44 #ifdef GENOM_SERIALIZATION
45 namespace {}
46 #endif //GENOM_SERIALIZATION
47 namespace {
48 
49 class PortMapper {
50 public:
51  void operator()(const torc::generic::PortReferenceSharedPtr& pRef) const
52  throw (torc::generic::Error) {
53  torc::generic::PortSharedPtr port = mMaster->findPort(pRef->getName());
54  if(!port || port->getCompositionType() != pRef->getCompositionType()
55  || port->getSize() != pRef->getSize()) {
56  //TBD::ERROR
57  }
58  try {
59  pRef->bindToMasterPort(port);
60  } catch(torc::generic::Error& e) {
61  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
62  throw;
63  }
64  }
65 
66  PortMapper(const torc::generic::ViewSharedPtr& inMaster) : mMaster(inMaster) {
67  }
68 
69 private:
71 };
72 
73 }
74 
75 namespace torc {
76 namespace generic {
77 
79  return mMyContext;
80 }
81 
83  if(!getMaster()) {
84  return ParameterMapSharedPtr();
85  }
86  ParameterMapSharedPtr params = mMaster->getParameters();
87  if(!params->isContextRegistered(mMyContext)) {
88  params->registerContext(mMyContext, mMaster->getParameterContext());
89  }
90  return params;
91 }
92 
93 /**
94  * Bind an instance to it's master. Instance binding consists of two steps:
95  * <ul>
96  * <li> Bind instance to cell </li>
97  * <li> Bind port references to ports of the cell </li>
98  * </ul>
99  *
100  * @param[in] inMaster Source Master view for this instance.
101  * @exception Error If there is any kind of mismatch between ports etc.
102  */
103 void Instance::bindToMasterView(const ViewSharedPtr& inMaster, bool inMapPortReferences)
104  throw (Error) {
105  if(!mMaster) {
106  //TDB::ERROR MASTER REBINDING NOT ALLOWED
107  }
108  ViewSharedPtr oldMaster = getMaster();
109  std::map<std::string, ParameterSharedPtr> myParams;
110  if(oldMaster) {
111  ParameterMapSharedPtr oldParams = oldMaster->getParameters();
112  oldParams->getOverriddenParameters(mMyContext, myParams);
113  oldParams->unregisterContext(mMyContext);
114  }
115  setMaster(inMaster);
116  ParameterMapSharedPtr paramMap = mMaster->getParameters();
117  mMyContext = paramMap->getNewContext();
118  //We do not create params here, they will be created only when
119  //someone asks for params
120  //For rebinding, however, if some params were there
121  //.. we have to override them on the new master
122  if(!myParams.empty()) {
123  try {
124  paramMap->registerContext(mMyContext, mMaster->getParameterContext());
125  for(std::map<std::string, ParameterSharedPtr>::iterator it = myParams.begin();
126  it != myParams.end(); ++it) {
127  paramMap->set(mMyContext, (*it).first, (*it).second);
128  }
129  } catch(Error& e) {
130  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
131  throw;
132  }
133  }
134  if(inMaster->getIsExtern()) {
135  return; //No port binding for extern
136  }
137  if(inMapPortReferences) {
138  PortMapper mapper(inMaster);
139  try {
140  mPortReferences.applyOnAll(mapper);
141  } catch(Error& e) {
142  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
143  throw;
144  }
145  }
146 }
147 
148 void Instance::setMaster(const ViewSharedPtr& inMaster) {
149  mMaster = inMaster;
150 }
151 
152 /**
153  * Add a port reference to this master. When a master is set using the set_master(), the list of port references is not created. It has to be extrinsically set.
154  *
155  * @param[in] inPortRef A port reference.
156  *
157  * @exception Error The port ref could not be added.
158  */
160  std::string name = inPortRef->getName();
161  if(name.empty()) {
162  Error e(eMessageIdErrorEmptyItemName, __FUNCTION__, __FILE__, __LINE__);
163  e.saveContextData("PortReference name", name);
164  throw e;
165  }
166  if(false == mPortReferences.set(name, inPortRef)) {
167  Error e(eMessageIdErrorItemAlreadyExists, __FUNCTION__, __FILE__, __LINE__);
168  e.saveContextData("PortReference name", name);
169  throw e;
170  }
171  inPortRef->setParent(getSharedThis());
172  return;
173 }
174 
175 /**
176  * Find a port reference.
177  *
178  * @param[in] inName String containing the name of the port.
179  */
181  PortReferenceSharedPtr portRef;
182  mPortReferences.get(inName, portRef);
183  return portRef;
184 }
185 
186 /**
187  * Find a Net reference.
188  *
189  * @param[in] inName String containing the name of the Net.
190  */
192  NetReferenceSharedPtr netRef;
193  mNetReferences.get(inName, netRef);
194  return netRef;
195 }
196 
197 /**
198  * Remove a given port reference.
199  *
200  * @param inName Name of the object to be delete
201  *
202  * @exception Error PortReference name is empty
203  * @exception Error PortReference name could not be found
204  */
205 void Instance::removePortReference(const std::string& inName) throw (Error) {
206  if(inName.empty()) {
207  Error e(eMessageIdErrorEmptyItemName, __FUNCTION__, __FILE__, __LINE__);
208  e.saveContextData("PortReference name", inName);
209  throw e;
210  }
211  if(false == mPortReferences.remove(inName)) {
212  Error e(eMessageIdErrorItemNotFound, __FUNCTION__, __FILE__, __LINE__);
213  e.saveContextData("PortReference name", inName);
214  throw e;
215  }
216  return;
217 }
218 
219 /**
220  * Get list of all port references.
221  *
222  * @param[out] outPortRefs List of port references
223  */
224 void Instance::getPortReferences(std::vector<PortReferenceSharedPtr>& outPortRefs) const {
225  mPortReferences.getValues(outPortRefs);
226 }
227 
228 /**
229  * Set list of all port references.
230  *
231  * @param[in] inSource List of port references
232  */
233 void Instance::setPortReferences(const std::vector<PortReferenceSharedPtr>& inSource) throw (Error) {
234  std::vector<PortReferenceSharedPtr>::const_iterator portRef = inSource.begin();
235  std::vector<PortReferenceSharedPtr>::const_iterator endP = inSource.end();
236  for(; portRef != endP; ++endP) {
237  try {
238  addPortReference(*portRef);
239  } catch(Error& e) {
240  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
241  throw;
242  }
243  }
244  return;
245 }
246 
247 void Instance::setDesignator(const std::string& inSource) {
248  mDesignator = inSource;
249 }
250 
251 /**
252  * Set the pointer to the timing object
253  *
254  * @param[in] inSource Pointer to the timing object
255  */
256 void Instance::setTiming(const TimingSharedPtr& inSource) {
257  mTiming = inSource;
258 }
259 
262  View>(), UserDataContainer(),
263 #ifdef GENOM_SERIALIZATION
264  mMasterData(NULL),
265 #endif //GENOM_SERIALIZATION
266  mMaster(),
267  mPortReferences(),
268  mMyContext(),
269  mDesignator() {}
270 
272 #ifdef GENOM_SERIALIZATION
273  delete mMasterData;
274 #endif //GENOM_SERIALIZATION
275 }
276 
277 #ifdef GENOM_SERIALIZATION
278 template <class Archive> void Instance::load(Archive& ar, unsigned int) {
279  ar & boost::serialization::base_object < Composite<Instance> > (*this);
280  ar & boost::serialization::base_object < Commentable > (*this);
281  ar & boost::serialization::base_object < Nameable > (*this);
282  ar & boost::serialization::base_object < PropertyContainer > (*this);
283  ar & boost::serialization::base_object < Renamable > (*this);
284  ar & boost::serialization::base_object < Visitable > (*this);
285  ar & mPortReferences;
286  ar & mMasterData; //We restore master from view
287  mPortReferences.applyOnAll(
288  boost::bind(boost::mem_fn(&PortReference::setParent), _1, getSharedThis()));
289 }
290 
291 template <class Archive> void Instance::save(Archive& ar, unsigned int) const {
292  ar & boost::serialization::base_object < Composite<Instance> > (*this);
293  ar & boost::serialization::base_object < Commentable > (*this);
294  ar & boost::serialization::base_object < Nameable > (*this);
295  ar & boost::serialization::base_object < PropertyContainer > (*this);
296  ar & boost::serialization::base_object < Renamable > (*this);
297  ar & boost::serialization::base_object < Visitable > (*this);
298  ar & mPortReferences;
299  mMasterData = new MasterData();
300  mMasterData->mLibrary = mMaster->getParent()->getParent()->getName();
301  mMasterData->mCell = mMaster->getParent()->getName();
302  mMasterData->mView = mMaster->getName();
303  //We save only our overridden params
304  getParameters()->getOverriddenParameters(getParameterContext(), mMasterData->mParams);
305  ar & mMasterData;
306 }
307 
308 //TO SATISFY THE LINKER
309 template void Instance::save<boost::archive::binary_oarchive>(boost::archive::binary_oarchive& ar,
310  const unsigned int) const;
311 
312 template void Instance::load<boost::archive::binary_iarchive>(boost::archive::binary_iarchive& ar,
313  const unsigned int);
314 
315 void Instance::restoreMaster() throw (Error) {
316  try {
317  if(!mMasterData) {
318  //TBD::ERROR
319  }
320  ViewSharedPtr view = getParent();
321  if(!view) {
322  //TBD::ERROR
323  }
324  CellSharedPtr cell = view->getParent();
325  if(!cell) {
326  //TBD::ERROR
327  }
328  LibrarySharedPtr lib = cell->getParent();
329  if(!lib) {
330  //TBD::ERROR
331  }
332  RootSharedPtr root = lib->getParent();
333  if(!root) {
334  //TBD::ERROR
335  }
336  LibrarySharedPtr targetLib = root->findLibrary(mMasterData->mLibrary);
337  if(!targetLib) {
338  //TBD::ERROR
339  }
340  CellSharedPtr targetCell = targetLib->findCell(mMasterData->mCell);
341  if(!targetCell) {
342  //TBD::ERROR
343  }
344  ViewSharedPtr targetView = targetCell->findView(mMasterData->mView);
345  if(!targetView) {
346  //TBD::ERROR
347  }
348  bindToMasterView(targetView);
349  //Restore params
350  std::map<std::string, ParameterSharedPtr>::iterator it = mMasterData->mParams.begin();
351  std::map<std::string, ParameterSharedPtr>::iterator end = mMasterData->mParams.end();
352  for(; it != end; ++it) {
353  getParameters()->set(mMyContext, (*it).first, (*it).second);
354  }
355  delete mMasterData;
356  mMasterData = NULL;
357  } catch(Error& e) {
358  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
359  throw;
360  }
361 }
362 #endif //GENOM_SERIALIZATION
363 
364 } // namespace generic
365 } // namespace torc
void getValues(std::vector< ValueType > &outValues) const
Definition: SymTab.hpp:158
Represents an instantiation of a cell view in the view of another cell.
An object that has a parent.
virtual void getPortReferences(std::vector< PortReferenceSharedPtr > &) const
Represents objects that have properties.
Represents and EDIF View.
Definition: View.hpp:61
Interface for objects that can be composed within each other.
Definition: Composite.hpp:45
virtual void bindToMasterView(const ViewSharedPtr &inMaster, bool inMapPortReferences=true)
virtual ParameterContext getParameterContext() const
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
SymTab< std::string, PortReferenceSharedPtr > mPortReferences
std::string string
boost::shared_ptr< NetReference > NetReferenceSharedPtr
void setMaster(const ViewSharedPtr &inMaster)
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
virtual void removePortReference(const std::string &inName)
boost::shared_ptr< Library > LibrarySharedPtr
boost::shared_ptr< PortReference > PortReferenceSharedPtr
virtual ParameterMapSharedPtr getParameters() const
const boost::shared_ptr< View > getParent() const
void saveContextData(const std::string &inName, const boost::any &inSource)
Definition: Error.cpp:79
void setDesignator(const std::string &inSource)
ViewSharedPtr getMaster() const
void setTiming(const TimingSharedPtr &inSource)
virtual NetReferenceSharedPtr findNetReference(const std::string &inNetRef)
boost::shared_ptr< View > ViewSharedPtr
boost::shared_ptr< Cell > CellSharedPtr
An object that has a name.
Definition: Nameable.hpp:34
virtual PortReferenceSharedPtr findPortReference(const std::string &inPortRef)
boost::shared_ptr< Port > PortSharedPtr
virtual void setParent(const boost::shared_ptr< Instance > &inSource)
Represents objects that can be renamed.
virtual void setPortReferences(const std::vector< PortReferenceSharedPtr > &inSource)
boost::shared_ptr< Timing > TimingSharedPtr
virtual void addPortReference(const PortReferenceSharedPtr &inPortRef)
boost::shared_ptr< Root > RootSharedPtr
boost::shared_ptr< ParameterMap > ParameterMapSharedPtr
An object that receives an inoutVisitor.
Definition: Visitable.hpp:38
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73