torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PortList.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 #ifdef GENOM_SERIALIZATION
22 #include <boost/archive/binary_iarchive.hpp>
23 #include <boost/archive/binary_oarchive.hpp>
24 #include <boost/serialization/base_object.hpp>
25 #include <boost/serialization/list.hpp>
26 #endif //GENOM_SERIALIZATION
28 #include "torc/generic/Net.hpp"
29 #include "torc/generic/Port.hpp"
32 
33 namespace torc {
34 namespace generic {
35 
36 /**
37  * Create a port list.
38  *
39  * @param[in] inPorts List of ports to this composition.
40  * @param[in] inPortReferences List of port references to this composition.
41  *
42  * @return Pointer to created port list.
43  */
44 
45 PortListSharedPtr PortList::Factory::newPortListPtr(const std::list<PortSharedPtr>& inPorts,
46  const std::list<PortReferenceSharedPtr>& inPortReferences) throw (Error) {
47  try {
48  PortListSharedPtr newPortList;
49  create(newPortList);
50  std::list<PortSharedPtr>::const_iterator portIt = inPorts.begin();
51  for(; portIt != inPorts.end(); portIt++) {
52  newPortList->addChildPort(*portIt);
53  }
54  std::list<PortReferenceSharedPtr>::const_iterator portRefIt = inPortReferences.begin();
55  for(; portRefIt != inPortReferences.end(); portRefIt++) {
56  newPortList->addChildPortReference(*portRefIt);
57  }
58  return newPortList;
59  } catch(Error& e) {
60  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
61  throw;
62  }
63 }
64 
65 void PortList::accept(BaseVisitor& inoutVisitor) throw (Error) {
66  try {
67  runVisitor(*this, inoutVisitor);
68  } catch(Error& e) {
69  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
70  throw;
71  }
72 }
73 
74 template <typename _Tp> void PortList::connectElementToNet(const NetSharedPtr& inNet,
75  const boost::shared_ptr<_Tp>& inType, size_t& inoutCurrentWidth) throw (Error) {
76  try {
77  typedef _Tp Type;
78  typedef boost::shared_ptr<_Tp> Pointer;
79  size_t updatedSize = inoutCurrentWidth + inType->getSize();
80  if(updatedSize > inNet->getSize()) {
81  //TBD::ERROR
82  }
83  //Element can be connected to scalar or Vector only
84  switch(inNet->getCompositionType()) {
87  if(1 == inType->getSize()) {
88  Connectable::Connection conn = inType->connect(inNet);
89  inoutCurrentWidth++;
90  (void) conn; // NJS
91  }
92  break;
93  }
95  //We explode the vector net
96  std::vector<NetSharedPtr> children;
97  inNet->getChildren(children);
98 
99  switch(inType->getCompositionType()) {
102  //One element vector net to a scalar port
103  {
104  connectElementToNet(children[inoutCurrentWidth], inType, inoutCurrentWidth);
105  break;
106  }
108  //Part of vector net to vector port
109  {
110  std::vector<Pointer> cTypes;
111  inType->getChildren(cTypes);
112  size_t j = 0;
113  for(size_t i = inoutCurrentWidth; i < updatedSize; i++, j++) {
114  connectElementToNet(children[i], cTypes[j], inoutCurrentWidth);
115  }
116  break;
117  }
118  case eCompositionTypeBundle: {
119  std::vector<Pointer> cTypes;
120  inType->getChildren(cTypes);
121  for(typename std::vector<Pointer>::iterator it = cTypes.begin(); it != cTypes.end();
122  ++it) {
123  connectElementToNet(inNet, *it, inoutCurrentWidth);
124  }
125  break;
126  }
127  }
128  }
130  default: {
131  //NOT POSSIBLE
132  }
133  }
134  } catch(Error& e) {
135  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
136  throw;
137  }
138 }
139 
141  size_t count = 0;
142  for(std::list<PortListElement>::iterator it = mElements.begin(); it != mElements.end(); ++it) {
143  try {
144  switch((*it).getType()) {
145  case PortListElement::eElementTypePort: {
146  connectElementToNet(inNet, (*it).getPort(), count);
147  break;
148  }
149  case PortListElement::eElementTypePortReference: {
150  connectElementToNet(inNet, (*it).getPortReference(), count);
151  break;
152  }
153  }
154  } catch(Error& e) {
155  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
156  throw;
157  }
158  }
159  Connectable::Connection newConnection;
160  try {
161  ConnectionHandler handler(inNet);
162  handler.connectPortListToNet(getSharedThis());
163  newConnection = Connectable::connect(inNet);
164  } catch(Error& e) {
165  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
166  throw;
167  }
168  return newConnection;
169 }
170 
171 void PortList::disconnect(const Connectable::Connection& inConnection) throw (Error) {
172  NetSharedPtr inNet = *inConnection;
173  //Do not clear components from here ...
174  //They are connected to individual bits of some net
175  // and would be cleared in due course
176  try {
177  ConnectionHandler handler(inNet);
178  handler.disconnectPortListFromNet(getSharedThis());
179  } catch(Error& e) {
180  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
181  throw;
182  }
183 }
184 
185 size_t PortList::getSize() const {
186  size_t size = 0;
187  for(std::list<PortListElement>::const_iterator it = mElements.begin(); it != mElements.end();
188  ++it) {
189  switch((*it).getType()) {
191  size += (*it).getPort()->getSize();
192  break;
193  }
195  size += (*it).getPortReference()->getSize();
196  break;
197  }
198  }
199  }
200  return size;
201 }
202 
204  mElements.push_back(PortListElement(inPort));
205 }
206 
208  mElements.push_back(PortListElement(inPortRef));
209 }
210 
211 void PortList::getChildren(std::list<PortListElement>& outElements) {
212  outElements.insert(outElements.end(), mElements.begin(), mElements.end());
213 }
214 
215 PortList::PortList() : Connectable(), mElements() {}
216 
218  mElements.clear();
219 }
220 
221 #ifdef GENOM_SERIALIZATION
222 
223 template <class Archive> void PortList::PortListElement::serialize(Archive& ar, unsigned int ) {
224  ar & mType;
225  ar & mPort;
226  ar & mPortReference;
227 }
228 
229 template <class Archive> void PortList::serialize(Archive& ar, unsigned int) {
230  ar & boost::serialization::base_object<Connectable>(*this);
231  ar & boost::serialization::base_object<SelfReferencing<PortList> >(*this);
232  ar & mElements;
233 }
234 
235 //TO SATISFY THE LINKER
236 template void PortList::serialize<boost::archive::binary_iarchive>(
237  boost::archive::binary_iarchive& ar, const unsigned int);
238 
239 template void PortList::serialize<boost::archive::binary_oarchive>(
240  boost::archive::binary_oarchive& ar, const unsigned int);
241 
242 #endif //GENOM_SERIALIZATION
243 
244 } // namespace generic
245 } // namespace torc
virtual Connection connect(const NetSharedPtr &inNet)=0
Definition: Connectable.cpp:60
void connectPortListToNet(const PortListSharedPtr &inPortList)
void getChildren(std::list< PortListElement > &outPorts)
Definition: PortList.cpp:211
PortReferenceSharedPtr mPortReference
Definition: PortElement.hpp:83
An object that is connectable to a Net.
Definition: Connectable.hpp:44
virtual PortListSharedPtr newPortListPtr(const std::list< PortSharedPtr > &inPorts, const std::list< PortReferenceSharedPtr > &inPortReferences)
Definition: PortList.cpp:45
std::list< NetSharedPtr >::iterator Connection
Definition: Connectable.hpp:52
size_t getSize() const
Definition: PortList.cpp:185
void runVisitor(_Tp &inoutVisited, BaseVisitor &inoutVisitor)
Definition: VisitorType.hpp:78
void connectElementToNet(const NetSharedPtr &inNet, const boost::shared_ptr< _Tp > &inType, size_t &inoutCurrentWidth)
Definition: PortList.cpp:74
void addChildPortReference(const PortReferenceSharedPtr &inPortRef)
Definition: PortList.cpp:207
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
boost::shared_ptr< PortReference > PortReferenceSharedPtr
boost::shared_ptr< PortList > PortListSharedPtr
Represents an ordered list of port references.
Definition: PortList.hpp:43
virtual void accept(BaseVisitor &inoutVisitor)
Definition: PortList.cpp:65
boost::shared_ptr< Port > PortSharedPtr
virtual Connection connect(const NetSharedPtr &inNet)
Definition: PortList.cpp:140
void disconnectPortListFromNet(const PortListSharedPtr &inPortList)
void addChildPort(const PortSharedPtr &inPort)
Definition: PortList.cpp:203
PortElement PortListElement
Definition: PortList.hpp:51
std::list< PortListElement > mElements
Definition: PortList.hpp:145
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73