torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PortBundleReference.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::PortBundleReference)
37 #endif //GENOM_SERIALIZATION
38 #include <iostream>
39 
40 namespace torc {
41 namespace generic {
42 
44  const InstanceSharedPtr& inInstancePtr, const PortSharedPtr& inPortPtr,
45  const ObjectFactorySharedPtr& inFactory, const PortBundleReferenceSharedPtr& inParentCollection)
46  throw (Error) {
47  try {
48  PortBundleReferenceSharedPtr newPortBundleReference;
49  PortRefCreator<PortBundleReferenceSharedPtr> creator(inFactory, inInstancePtr,
50  inParentCollection);
51  inPortPtr->getParent()->applyOnAllPorts(
53  newPortBundleReference = creator.getReturnValue();
54  newPortBundleReference->bindToMasterPort(inPortPtr);
55  return newPortBundleReference;
56  } catch(Error& e) {
57  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
58  throw;
59  }
60 
61 }
62 
63 void PortBundleReference::accept(BaseVisitor& inoutVisitor) throw (Error) {
64  try {
65  runVisitor(*this, inoutVisitor);
66  } catch(Error& e) {
67  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
68  throw;
69  }
70 }
71 
74  applyOnAllChildren(boost::bind(boost::mem_fn(&PortReference::setParent), _1, getParent()));
75 }
76 
78  if(getSize() != getMaster()->getSize()) {
79  Error e(eMessageIdErrorItemSizeMismatch, __FUNCTION__, __FILE__, __LINE__);
80  e.saveContextData("PortBundleReference Size", getSize());
81  e.saveContextData("Master Port Size", getMaster()->getSize());
82  throw e;
83  }
84  if(inNet->getSize() != getSize()) {
85  Error e(eMessageIdErrorItemSizeMismatch, __FUNCTION__, __FILE__, __LINE__);
86  e.saveContextData("Net Size", inNet->getSize());
87  e.saveContextData("PortBundleReference Size", getSize());
88  throw e;
89  }
92  PortBundleReference> flattener;
93  accept(flattener);
94  flattener.getChildren(children);
95 
96  Bundle<Net>::List netChildren;
97  inNet->getChildren(netChildren);
98  Bundle<PortReference>::List::iterator childPortRef = children.begin();
99  Bundle<PortReference>::List::iterator childEnd = children.end();
100  Bundle<Net>::List::iterator net = netChildren.begin();
101  for(; childPortRef != childEnd; ++childPortRef, ++net) {
102  try {
103  (*childPortRef)->connect(*net);
104  } catch(Error& e) {
105  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
106  throw;
107  }
108  }
109  Connectable::Connection newConnection;
110  try {
111  ConnectionHandler handler(inNet);
112  handler.connectPortRefToNet(getSharedThis());
113  newConnection = Connectable::connect(inNet);
114  } catch(Error& e) {
115  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
116  throw;
117  }
118  return newConnection;
119 }
120 
121 void PortBundleReference::disconnect(const Connection& inConnection) throw (Error) {
122  NetSharedPtr connNet = *inConnection;
123  if(!connNet) {
124  Error e(eMessageIdErrorConnectionInvalid, __FUNCTION__, __FILE__, __LINE__);
125  e.saveContextData("Pointer to Net", connNet);
126  throw e;
127  }
130  PortBundleReference> flattener;
131  accept(flattener);
132  flattener.getChildren(children);
133 
134  Bundle<Net>::List netChildren;
135  connNet->getChildren(netChildren);
136  Bundle<PortReference>::List::iterator childPortRef = children.begin();
137  Bundle<PortReference>::List::iterator childEnd = children.end();
138  Bundle<Net>::List::iterator net = netChildren.begin();
139  //We find all bits from the connected net and remove them
140  //from the children nets. This disconnect is a little slow
141  //as lookup is performed for each net. Otherwise we
142  //would have to save the iterators (Connection objects)
143  //for each child connection
144  for(; childPortRef != childEnd; ++childPortRef, ++net) {
145  try {
146  (*childPortRef)->disconnect(*net);
147  } catch(Error& e) {
148  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
149  throw;
150  }
151  }
152  try {
153  ConnectionHandler handler(connNet);
154  handler.disconnectPortRefFromNet(getSharedThis());
155  Connectable::disconnect(inConnection);
156  } catch(Error& e) {
157  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
158  throw;
159  }
160  return;
161 }
162 
163 /**
164  * Set master port.
165  *
166  * @param[in] inSource Set the master port
167  *
168  */
170  if(inMaster->getSize() != getSize()) {
171  Error e(eMessageIdErrorItemSizeMismatch, __FUNCTION__, __FILE__, __LINE__);
172  e.saveContextData("Master Port Size", inMaster->getSize());
173  e.saveContextData("Port bundle reference Size", getSize());
174  throw e;
175  }
177 #if 0 //Bind during reference creation
178  std::vector< PortReferenceSharedPtr > children;
179  getChildren(children);
180  std::vector< PortSharedPtr > childrenPort;
181  inMaster->getChildren(childrenPort); //TBD::OPTMIZE
182  std::vector< PortReferenceSharedPtr >::iterator pRef = children.begin();
183  std::vector< PortReferenceSharedPtr >::iterator pRefEnd = children.end();
184  std::vector< PortSharedPtr >::iterator pIt = childrenPort.begin();
185  for(; pRef != pRefEnd; ++pRef, ++pIt ) {
186  PortReferenceSharedPtr childRef;
187  PortSharedPtr childPort = *pIt;
188  try {
189  childRef->bindToMasterPort(childPort);
190  } catch(Error& e) {
191  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
192  throw;
193  }
194  }
195 #endif
196 }
197 
199 
201 
202 #ifdef GENOM_SERIALIZATION
203 template <class Archive> void PortBundleReference::serialize(Archive& ar, unsigned int) {
204  ar & boost::serialization::base_object<PortReference >(*this);
205  ar & boost::serialization::base_object<Bundle<PortReference> >(*this);
206 }
207 
208 //TO SATISFY THE LINKER
209 template void PortBundleReference::serialize<boost::archive::binary_iarchive>(
210  boost::archive::binary_iarchive& ar, const unsigned int);
211 
212 template void PortBundleReference::serialize<boost::archive::binary_oarchive>(
213  boost::archive::binary_oarchive& ar, const unsigned int);
214 
215 #endif //GENOM_SERIALIZATION
216 }// namespace generic
217 } // namespace torc
virtual Connection connect(const NetSharedPtr &inNet)=0
Definition: Connectable.cpp:60
boost::shared_ptr< Instance > InstanceSharedPtr
Represents areference to a standalone port.
Represents a reference to a port array.
_ReturnType getReturnValue() const
Represents the usable instance of a port of a cell in another cell.
virtual Connection connect(const NetSharedPtr &inNet)
std::list< NetSharedPtr >::iterator Connection
Definition: Connectable.hpp:52
boost::shared_ptr< ObjectFactory > ObjectFactorySharedPtr
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
boost::shared_ptr< Net > NetSharedPtr
A base class for Visitor.
Definition: VisitorType.hpp:31
virtual void bindToMasterPort(const PortSharedPtr &inMaster)
boost::shared_ptr< PortReference > PortReferenceSharedPtr
const boost::shared_ptr< Instance > getParent() const
void saveContextData(const std::string &inName, const boost::any &inSource)
Definition: Error.cpp:79
void applyOnAllChildren(const _Action &action)
void disconnectPortRefFromNet(const PortReferenceSharedPtr &inPort)
virtual void accept(BaseVisitor &inoutVisitor)
Create port reference.
Represents a reference to a bit of a port.
virtual void getChildren(List &outChildren) const
Definition: Bundle.hpp:156
virtual void bindToMasterPort(const PortSharedPtr &inMaster)
boost::shared_ptr< Port > PortSharedPtr
virtual void setParent(const boost::shared_ptr< _ParentType > &inSource)
virtual void setParent(const InstanceSharedPtr &inParent)
Represents a reference to a bundle of ports.
Represents a "bundle" in the EDIF sense.
Definition: Bundle.hpp:43
virtual PortBundleReferenceSharedPtr newPortBundleReferencePtr(const InstanceSharedPtr &inInstancePtr, const PortSharedPtr &inPortPtr, const ObjectFactorySharedPtr &inFactory, const PortBundleReferenceSharedPtr &inParentCollection=PortBundleReferenceSharedPtr())
boost::shared_ptr< PortBundleReference > PortBundleReferenceSharedPtr
Flatten a bundle to bits.
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73