torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PortBundle.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/Log.hpp"
32 #include "torc/generic/Net.hpp"
33 
34 #ifdef GENOM_SERIALIZATION
35 BOOST_CLASS_EXPORT(torc::generic::PortBundle)
36 #endif //GENOM_SERIALIZATION
37 namespace torc {
38 namespace generic {
39 
40 void PortBundle::setParent(const ViewSharedPtr& inParent) {
42  applyOnAllChildren(boost::bind(boost::mem_fn(&Port::setParent), _1, getParent()));
43 }
44 
45 /**
46  * Connect a Net to this object.
47  *
48  * @note This metod can be overridden by derived classes. However, the method must call the
49  * on_connected() method after this. The sigConnected_ signal must also be invoked in the
50  * overriding method.
51  *
52  * @param[in] inNet A pointer to the Net object that eeds to be connected
53  * @return A connection that has been established. This can be used later for disconnection.
54  */
56  if(!inNet) {
57  Error e(eMessageIdErrorPointerToItemDoesNotExist, __FUNCTION__, __FILE__, __LINE__);
58  e.saveContextData("Pointer to the Net object does not exists", inNet);
59  throw e;
60  }
61  if(inNet->getSize() != getSize()) {
62  Error e(eMessageIdErrorItemSizeMismatch, __FUNCTION__, __FILE__, __LINE__);
63  e.saveContextData("Net Size", inNet->getSize());
64  e.saveContextData("Port Size", getSize());
65  throw e;
66  }
67  Bundle<Port>::List children;
69  accept(flattener);
70  flattener.getChildren(children);
71 
72  Bundle<Net>::List netChildren;
73  inNet->getChildren(netChildren);
74  Bundle<Port>::List::iterator childPort = children.begin();
75  Bundle<Port>::List::iterator childEnd = children.end();
76  Bundle<Net>::List::iterator net = netChildren.begin();
77  for(; childPort != childEnd; ++childPort, ++net) {
78  try {
79  (*childPort)->connect(*net);
80  } catch(Error& e) {
81  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
82  throw;
83  }
84  }
85  Connectable::Connection newConnection;
86  try {
87  ConnectionHandler handler(inNet);
88  handler.connectPortToNet(getSharedThis());
89  newConnection = Connectable::connect(inNet);
90  } catch(Error& e) {
91  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
92  throw;
93  }
94  return newConnection;
95 }
96 
97 /**
98  * Disconnect a Net from this object.
99  * @note This metod can be overridden by derived classes. However, the method must call the
100  * on_connected() method after this. The sigConnected_ signal must also be invoked in the
101  * overriding method.
102  *
103  * @param[in] connection A connection as returned by the connect() method
104  * @exception Error Provided connection is invalid, because pointer to the Net does not exist
105  */
106 void PortBundle::disconnect(const Connectable::Connection& inConnection) throw (Error) {
107  NetSharedPtr connNet = *inConnection;
108  if(!connNet) {
109  Error e(eMessageIdErrorConnectionInvalid, __FUNCTION__, __FILE__, __LINE__);
110  e.saveContextData("Pointer to Net", connNet);
111  throw e;
112  }
113  Bundle<Port>::List children;
115  accept(flattener);
116  flattener.getChildren(children);
117  Bundle<Net>::List netChildren;
118  connNet->getChildren(netChildren);
119  Bundle<Port>::List::iterator childPort = children.begin();
120  Bundle<Port>::List::iterator childEnd = children.end();
121  Bundle<Net>::List::iterator net = netChildren.begin();
122  //We find all bits from the connected net and remove them
123  //from the children nets. This disconnect is a little slow
124  //as lookup is performed for each net. Otherwise we
125  //would have to save the iterators (Connection objects)
126  //for each child connection
127  for(; childPort != childEnd; ++childPort, ++net) {
128  try {
129  (*childPort)->disconnect(*net);
130  } catch(Error& e) {
131  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
132  throw;
133  }
134  }
135  try {
136  ConnectionHandler handler(connNet);
137  handler.disconnectPortFromNet(getSharedThis());
138  Connectable::disconnect(inConnection);
139  } catch(Error& e) {
140  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
141  throw;
142  }
143 }
144 
145 /**
146  * Create a port bundle.
147  *
148  * @param[in] inName Name of the port bundle to be created.
149  * @param[in] inDirection Direction of port.
150  * @param[in] inViewPtr Pointer to parented(View) object.
151  * @param[in] inParentCollection Pointer to parent bundle.
152  * @param[in] inOriginalName Original name of the port bundle [optional].
153  *
154  * @return Pointer to created port bundle.
155  **/
157  const ViewSharedPtr& inViewPtr, const PortBundleSharedPtr& inParentCollection,
158  const std::string& inOriginalName) throw (Error) {
159  try {
160  PortBundleSharedPtr newPortBundle;
161  create(newPortBundle);
162  newPortBundle->setName(inName);
163  newPortBundle->setOriginalName(inOriginalName);
164  if(inParentCollection) {
165  inParentCollection->addChild(newPortBundle);
166  } else {
167  inViewPtr->addPort(newPortBundle);
168  }
169  return newPortBundle;
170  } catch(Error& e) {
171  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
172  throw;
173  }
174 }
175 
176 void PortBundle::accept(BaseVisitor& inoutVisitor) throw (Error) {
177  try {
178  runVisitor(*this, inoutVisitor);
179  } catch(Error& e) {
180  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
181  throw;
182  }
183 }
184 
186 
188  torc::generic::log("Port bundle destroyed\n");
189 }
190 
191 #ifdef GENOM_SERIALIZATION
192 template <class Archive> void PortBundle::serialize(Archive& ar, unsigned int) {
193  ar & boost::serialization::base_object< Port >(*this);
194  ar & boost::serialization::base_object< Bundle<Port> >(*this);
195 }
196 
197 //TO SATISFY THE LINKER
198 template void PortBundle::serialize<boost::archive::binary_iarchive>(
199  boost::archive::binary_iarchive& ar, const unsigned int);
200 
201 template void PortBundle::serialize<boost::archive::binary_oarchive>(
202  boost::archive::binary_oarchive& ar, const unsigned int);
203 
204 #endif //GENOM_SERIALIZATION
205 
206 } // namespace generic
207 } // namespace torc
virtual Connection connect(const NetSharedPtr &inNet)=0
Definition: Connectable.cpp:60
void log(const char *fmt,...)
Definition: Log.cpp:89
virtual void accept(BaseVisitor &inoutVisitor)
Definition: PortBundle.cpp:176
Represents a bundle of ports.
Definition: PortBundle.hpp:44
virtual void setParent(const ViewSharedPtr &inParent)
Definition: PortBundle.cpp:40
void disconnectPortFromNet(const PortSharedPtr &inPort)
boost::shared_ptr< PortBundle > PortBundleSharedPtr
std::list< NetSharedPtr >::iterator Connection
Definition: Connectable.hpp:52
virtual Connectable::Connection connect(const NetSharedPtr &inNet)
Definition: PortBundle.cpp:55
void runVisitor(_Tp &inoutVisited, BaseVisitor &inoutVisitor)
Definition: VisitorType.hpp:78
std::string string
void connectPortToNet(const PortSharedPtr &inPort)
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
const boost::shared_ptr< View > getParent() const
void saveContextData(const std::string &inName, const boost::any &inSource)
Definition: Error.cpp:79
void applyOnAllChildren(const _Action &action)
void getChildren(typename Bundle< _BaseType >::List &outChildren) const
virtual PortBundleSharedPtr newPortBundlePtr(const std::string &inName, const ViewSharedPtr &inViewPtr, const PortBundleSharedPtr &inParentCollection=PortBundleSharedPtr(), const std::string &inOriginalName=std::string())
Definition: PortBundle.cpp:156
virtual void getChildren(List &outChildren) const
Definition: Bundle.hpp:156
Interface for an EDIF port object.
boost::shared_ptr< View > ViewSharedPtr
virtual void setParent(const boost::shared_ptr< _ParentType > &inSource)
Represents a "bundle" in the EDIF sense.
Definition: Bundle.hpp:43
Flatten a bundle to bits.
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73