torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VectorPortReference.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 //BOOST
21 #include <boost/bind.hpp>
22 #include <boost/mem_fn.hpp>
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/export.hpp>
28 #endif //GENOM_SERIALIZATION
30 #include "torc/generic/Net.hpp"
31 #include "torc/generic/Port.hpp"
34 
35 #ifdef GENOM_SERIALIZATION
36 BOOST_CLASS_EXPORT(torc::generic::VectorPortReference)
37 #endif //GENOM_SERIALIZATION
38 namespace {
39 
40 class PortBinder : public torc::generic::VectorPortBitReference::Visitor {
41 public:
42  void visit(torc::generic::VectorPortBitReference& portBit) throw (torc::generic::Error) {
43  torc::generic::PortSharedPtr childPort = mMasterVector->get(portBit.getIndices());
44  try {
45  portBit.bindToMasterPort(childPort);
46  } catch(torc::generic::Error& e) {
47  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
48  throw;
49  }
50  return;
51  }
52 
53  PortBinder(const torc::generic::PortSharedPtr& inMasterVector) :
54  mMasterVector(inMasterVector) {
55  }
56 
57  ~PortBinder() throw () {
58  }
59 
60 private:
61  torc::generic::PortSharedPtr mMasterVector;
62 };
63 
64 }
65 namespace torc {
66 namespace generic {
67 
68 /**
69  * Create a vector port ref.
70  *
71  * @param[in] inInstancePtr Pointer to parented(Instance) object.
72  * @param[in] inPortPtr Pointer to master(Port) object.
73  * @param[in] inSize Size of the net array.
74  * @param[in] inFactory Factory for the child.
75  *
76  * @return Pointer to created scalar port ref.
77  **/
79  const InstanceSharedPtr& inInstancePtr, const PortSharedPtr& inPortPtr, const size_t& inSize,
80  const PortBundleReferenceSharedPtr& inParentCollection, const ChildFactorySharedPtr& inFactory)
81  throw (Error) {
82  try {
83  std::vector < size_t > limits;
84  limits.push_back(inSize);
85  return newVectorPortReferencePtr(inInstancePtr, inPortPtr, limits, inParentCollection,
86  inFactory);
87  } catch(Error& e) {
88  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
89  throw;
90  }
91 }
92 
93 /**
94  * Create a vector port ref.
95  *
96  * @param[in] inInstancePtr Pointer to parented(Instance) object.
97  * @param[in] inPortPtr Pointer to master(Port) object.
98  * @param[in] inLimits Dimensions of the vector.
99  * @param[in] inFactory Factory for the child.
100  *
101  * @return Pointer to created scalar port ref.
102  **/
104  const InstanceSharedPtr& inInstancePtr, const PortSharedPtr& inPortPtr,
105  const std::vector<size_t>& inLimits, const PortBundleReferenceSharedPtr& inParentCollection,
106  const ChildFactorySharedPtr& inFactory) throw (Error) {
107  try {
108  VectorPortReferenceSharedPtr newVectorPortReference;
109  create(newVectorPortReference);
110  newVectorPortReference->constructChildren(inFactory, inLimits);
111  newVectorPortReference->bindToMasterPort(inPortPtr);
112  if(inParentCollection) {
113  inParentCollection->addChild(newVectorPortReference);
114  } else {
115  inInstancePtr->addPortReference(newVectorPortReference);
116  }
117  return newVectorPortReference;
118  } catch(Error& e) {
119  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
120  throw;
121  }
122 }
123 
124 void VectorPortReference::accept(BaseVisitor& inoutVisitor) throw (Error) {
125  try {
126  runVisitor(*this, inoutVisitor);
127  } catch(Error& e) {
128  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
129  throw;
130  }
131 }
132 
135  BaseType::List children;
136  getCreatedChildren(children);
137  std::for_each(children.begin(), children.end(),
138  boost::bind(boost::mem_fn(&PortReference::setParent), _1, getParent()));
139 }
140 
141 /**
142  * Connect a Net to this object.
143  *
144  * @note This metod can be overridden by derived classes. However, the method must call the
145  * on_connected() method after this. The sigConnected_ signal must also be invoked in the
146  * overriding method.
147  *
148  * @param[in] inNet A pointer to the Net object that eeds to be connected
149  * @return A connection that has been established. This can be used later for disconnection.
150  *
151  * @exception Error Pointer to the Net object does not exist
152  * @exception Error Net size does not match with VectorPortReference size
153  */
155  if(!inNet) {
156  Error e(eMessageIdErrorPointerToItemDoesNotExist, __FUNCTION__, __FILE__, __LINE__);
157  e.saveContextData("Pointer to Net", inNet);
158  throw e;
159  }
160  if(inNet->getSize() != getSize()) {
161  Error e(eMessageIdErrorItemSizeMismatch, __FUNCTION__, __FILE__, __LINE__);
162  e.saveContextData("Net Size", inNet->getSize());
163  e.saveContextData("VectorPortReference Size", getSize());
164  throw e;
165  }
166  Connectable::Connection newConnection;
167  try {
168  ConnectionHandler handler(inNet);
169  handler.connectPortRefToNet(getSharedThis());
170  newConnection = Connectable::connect(inNet);
171  } catch(Error& e) {
172  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
173  throw;
174  }
175  return newConnection;
176 }
177 
178 /**
179  * Disconnect a Net from this object.
180  * @note This metod can be overridden by derived classes. However, the method must call the
181  * on_connected() method after this. The sigConnected_ signal must also be invoked in the
182  * overriding method.
183  *
184  * @param[in] connection A connection as returned by the connect() method
185  * @exception Error Provided connection is invalid
186  *
187  */
189  NetSharedPtr connNet = *inConnection;
190  if(!connNet) {
191  Error e(eMessageIdErrorPointerToItemDoesNotExist, __FUNCTION__, __FILE__, __LINE__);
192  e.saveContextData("Pointer to Net", connNet);
193  throw e;
194  }
195  try {
196  BaseType::List children;
197  getCreatedChildren(children);
198  for_each(children.begin(), children.end(),
199  boost::bind(boost::mem_fn(&Connectable::disconnect), _1));
200  ConnectionHandler handler(connNet);
201  handler.disconnectPortRefFromNet(getSharedThis());
202  Connectable::disconnect(inConnection);
203  } catch(Error& e) {
204  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
205  throw;
206  }
207  return;
208 }
209 
210 void VectorPortReference::getConnectedNets(std::vector<NetSharedPtr>& outNets,
211  bool inSkipChildConnections) const throw (Error) {
213  if(!inSkipChildConnections) {
214  BaseType::List children;
215  getCreatedChildren(children); //We need only the
216  // bits that have been created
217  BaseType::List::iterator pRef = children.begin();
218  BaseType::List::iterator nEnd = children.end();
219  for(; pRef != nEnd; ++pRef) {
220  (*pRef)->getConnectedNets(outNets, true);
221  }
222  }
223  return;
224 }
225 
226 /**
227  * Set master port.
228  *
229  * @param[in] inSource Set the master port.
230  */
233 
234 }
235 
237  throw (Error) {
238  PortSharedPtr masterPort = getMaster();
239  if(masterPort) {
240  PortBinder binder(masterPort);
241  //We have now a handle to a bit whose parent's master is set
242  //But it does not have a master itself. So we get the
243  //corresponding bit from master
244  try {
245  inCreatedChild->accept(binder);
246  } catch(Error& e) {
247  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
248  throw;
249  }
250  }
251 }
252 
254 
256 
257 #ifdef GENOM_SERIALIZATION
258 template <class Archive> void
259 VectorPortReference::serialize(Archive& ar, unsigned int) {
260  ar & boost::serialization::base_object< PortReference >(*this);
261  ar & boost::serialization::base_object< BaseType >(*this);
262 }
263 
264 //TO SATISFY THE LINKER
265 template void
266 VectorPortReference::serialize<boost::archive::binary_iarchive>(
267  boost::archive::binary_iarchive& ar, const unsigned int);
268 
269 template void
270 VectorPortReference::serialize<boost::archive::binary_oarchive>(
271  boost::archive::binary_oarchive& ar, const unsigned int);
272 
273 #endif //GENOM_SERIALIZATION
274 
275 }// namespace generic
276 } // namespace torc
virtual Connection connect(const NetSharedPtr &inNet)=0
Definition: Connectable.cpp:60
virtual void setParent(const InstanceSharedPtr &inParent)
An acyclic inoutVisitor implementation.
Definition: VisitorType.hpp:57
virtual Connectable::Connection connect(const NetSharedPtr &inNet)
boost::shared_ptr< Instance > InstanceSharedPtr
Represents a reference to a port array.
Represents the usable instance of a port of a cell in another cell.
boost::shared_ptr< VectorPortBitReference > VectorPortBitReferenceSharedPtr
virtual VectorPortReferenceSharedPtr newVectorPortReferencePtr(const InstanceSharedPtr &inInstancePtr, const PortSharedPtr &inPortPtr, const size_t &inSize, const PortBundleReferenceSharedPtr &inParentCollection=PortBundleReferenceSharedPtr(), const ChildFactorySharedPtr &inFactory=BaseType::ChildFactorySharedPtr(new BaseType::ChildFactory()))
std::list< NetSharedPtr >::iterator Connection
Definition: Connectable.hpp:52
void connectPortRefToNet(const PortReferenceSharedPtr &inPort)
void runVisitor(_Tp &inoutVisited, BaseVisitor &inoutVisitor)
Definition: VisitorType.hpp:78
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
virtual void getConnectedNets(std::vector< NetSharedPtr > &outNets, bool inSkipChildConnections=false) const
Definition: Connectable.cpp:45
boost::shared_ptr< Net > NetSharedPtr
A base class for Visitor.
Definition: VisitorType.hpp:31
virtual void bindToMasterPort(const PortSharedPtr &inMaster)
const boost::shared_ptr< Instance > getParent() const
void saveContextData(const std::string &inName, const boost::any &inSource)
Definition: Error.cpp:79
virtual void accept(BaseVisitor &inoutVisitor)
virtual void getConnectedNets(std::vector< NetSharedPtr > &outNets, bool inSkipChildConnections=false) const
Represents a reference to a bit of a port.
boost::shared_ptr< Port > PortSharedPtr
virtual void setParent(const boost::shared_ptr< _ParentType > &inSource)
virtual void bindToMasterPort(const PortSharedPtr &inMaster)
boost::shared_ptr< VectorPortReference > VectorPortReferenceSharedPtr
virtual void onChildCreate(const VectorPortBitReferenceSharedPtr &inCreatedChild) const
boost::shared_ptr< PortBundleReference > PortBundleReferenceSharedPtr
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73