torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VectorPort.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 #include <algorithm>
21 //BOOST
22 #include <boost/bind.hpp>
23 #include <boost/mem_fn.hpp>
24 #ifdef GENOM_SERIALIZATION
25 #include <boost/archive/binary_iarchive.hpp>
26 #include <boost/archive/binary_oarchive.hpp>
27 #include <boost/serialization/base_object.hpp>
28 #include <boost/serialization/export.hpp>
29 #endif //GENOM_SERIALIZATION
31 #include "torc/generic/Log.hpp"
32 #include "torc/generic/Net.hpp"
35 
36 #ifdef GENOM_SERIALIZATION
37 BOOST_CLASS_EXPORT(torc::generic::VectorPort)
38 #endif //GENOM_SERIALIZATION
39 namespace torc {
40 namespace generic {
41 
42 /**
43  * Create a vector port.
44  *
45  * @param[in] inName Name of the vector port to be created.
46  * @param[in] inDirection Direction of port.
47  * @param[in] inViewPtr Pointer to parented(View) object.
48  * @param[in] inSize Size of the port array.
49  * @param[in] inFactory Factory for the child.
50  * @param[in] inOriginalName Original name of the vector port [optional].
51  *
52  * @return Pointer to created vector port.
53  **/
55  const EPortDirection& inDirection, const ViewSharedPtr& inViewPtr, const size_t& inSize,
56  const PortBundleSharedPtr& inParentCollection, const ChildFactorySharedPtr& inFactory,
57  const std::string& inOriginalName) throw (Error) {
58  try {
59  std::vector < size_t > limits;
60  limits.push_back(inSize);
61  return newVectorPortPtr(inName, inDirection, inViewPtr, limits, inParentCollection,
62  inFactory, inOriginalName);
63  } catch(Error& e) {
64  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
65  throw;
66  }
67 }
68 
69 /**
70  * Create a vector port.
71  *
72  * @param[in] inName Name of the vector port to be created.
73  * @param[in] inDirection Direction of port.
74  * @param[in] inViewPtr Pointer to parented(View) object.
75  * @param[in] inLimits Dimensions of the vector.
76  * @param[in] inFactory Factory for the child.
77  * @param[in] inOriginalName Original name of the vector port [optional].
78  *
79  * @return Pointer to created vector port.
80  **/
82  const EPortDirection& inDirection, const ViewSharedPtr& inViewPtr,
83  const std::vector<size_t>& inLimits, const PortBundleSharedPtr& inParentCollection,
84  const ChildFactorySharedPtr& inFactory, const std::string& inOriginalName) throw (Error) {
85  try {
86  VectorPortSharedPtr newVectorPort;
87  create(newVectorPort);
88  newVectorPort->setName(inName);
89  newVectorPort->setDirection(inDirection);
90  newVectorPort->constructChildren(inFactory,
91  inLimits);
92  newVectorPort->setOriginalName(inOriginalName);
93  if(inParentCollection) {
94  inParentCollection->addChild(newVectorPort);
95  } else {
96  inViewPtr->addPort(newVectorPort);
97  }
98  return newVectorPort;
99  } catch(Error& e) {
100  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
101  throw;
102  }
103 }
104 void VectorPort::accept(BaseVisitor& inoutVisitor) throw (Error) {
105  try {
106  runVisitor(*this, inoutVisitor);
107  } catch(Error& e) {
108  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
109  throw;
110  }
111 }
112 
113 void VectorPort::setParent(const ViewSharedPtr& inParent) {
115  BaseVectorType::List children;
116  getCreatedChildren(children);
117  std::for_each(children.begin(), children.end(),
118  boost::bind(boost::mem_fn(&Port::setParent), _1, getParent()));
119 }
120 
121 /**
122  * Connect a Net to this object.
123  *
124  * @note This metod can be overridden by derived classes. However, the method must call the
125  * on_connected() method after this. The sigConnected_ signal must also be invoked in the
126  * overriding method.
127  *
128  * @param[in] net A pointer to the Net object that eeds to be connected
129  * @return A connection that has been established. This can be used later for disconnection.
130  */
132  if(!inNet) {
133  Error e(eMessageIdErrorPointerToItemDoesNotExist, __FUNCTION__, __FILE__, __LINE__);
134  e.saveContextData("Pointer to the Net object does not exist", inNet);
135  throw e;
136  }
137  if(inNet->getSize() != getSize()) {
138  Error e(eMessageIdErrorItemSizeMismatch, __FUNCTION__, __FILE__, __LINE__);
139  e.saveContextData("Net Size", inNet->getSize());
140  e.saveContextData("Vector Net Size", getSize());
141  throw e;
142  }
143  Connectable::Connection newConnection;
144  try {
145  ConnectionHandler handler(inNet);
146  handler.connectPortToNet(getSharedThis());
147  newConnection = Connectable::connect(inNet);
148  } catch(Error& e) {
149  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
150  throw;
151  }
152  return newConnection;
153 }
154 
155 /**
156  *
157  * Disconnect a Net from this object.
158  * @note This metod can be overridden by derived classes. However, the method must call the
159  * on_connected() method after this. The sigConnected_ signal must also be invoked in the
160  * overriding method.
161  *
162  * @param[in] connection A connection as returned by the connect() method
163  * @exception Error Provided connection is invalid
164  */
165 
166 void VectorPort::disconnect(const Connectable::Connection& inConnection) throw (Error) {
167  NetSharedPtr net = *inConnection;
168  if(!net) {
169  Error e(eMessageIdErrorPointerToItemDoesNotExist, __FUNCTION__, __FILE__, __LINE__);
170  e.saveContextData("Pointer to the Net object does not exist", net);
171  throw e;
172  }
173  try {
174  ConnectionHandler handler(net);
175  handler.disconnectPortFromNet(getSharedThis());
176  Connectable::disconnect(inConnection);
177  } catch(Error& e) {
178  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
179  throw;
180  }
181 }
182 
183 void VectorPort::getConnectedNets(std::vector<NetSharedPtr>& outNets,
184  bool inSkipChildConnections) const throw (Error) {
185  Port::getConnectedNets(outNets);
186  if(!inSkipChildConnections) {
187  BaseVectorType::List children;
188  getCreatedChildren(children); //We need only the
189  // bits that have been created
190  BaseVectorType::List::iterator port = children.begin();
191  BaseVectorType::List::iterator pEnd = children.end();
192  for(; port != pEnd; ++port) {
193  (*port)->getConnectedNets(outNets, true);
194  }
195  }
196  return;
197 }
198 
200  const boost::shared_ptr<BaseVectorType::ChildType>& inCreatedChild) const throw (Error) {
201  inCreatedChild->setName(getName());
202 }
203 
205  Port(), BaseVectorType() {}
206 
208  torc::generic::log("Vector Port destroyed\n");
209 }
210 
211 #ifdef GENOM_SERIALIZATION
212 template <class Archive> void VectorPort::serialize(Archive& ar, unsigned int) {
213  ar & boost::serialization::base_object<Port>(*this);
214  ar & boost::serialization::base_object<BaseVectorType>(*this);
215 }
216 
217 //TO SATISFY THE LINKER
218 template void VectorPort::serialize<boost::archive::binary_iarchive>(
219  boost::archive::binary_iarchive& ar, const unsigned int);
220 
221 template void VectorPort::serialize<boost::archive::binary_oarchive>(
222  boost::archive::binary_oarchive& ar, const unsigned int);
223 
224 #endif //GENOM_SERIALIZATION
225 
226 }// namespace generic
227 } // namespace torc
virtual Connection connect(const NetSharedPtr &inNet)=0
Definition: Connectable.cpp:60
void log(const char *fmt,...)
Definition: Log.cpp:89
void disconnectPortFromNet(const PortSharedPtr &inPort)
boost::shared_ptr< PortBundle > PortBundleSharedPtr
std::list< NetSharedPtr >::iterator Connection
Definition: Connectable.hpp:52
void runVisitor(_Tp &inoutVisited, BaseVisitor &inoutVisitor)
Definition: VisitorType.hpp:78
std::string string
boost::shared_ptr< VectorPort > VectorPortSharedPtr
void connectPortToNet(const PortSharedPtr &inPort)
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
Represents a port array.
Definition: VectorPort.hpp:45
boost::shared_ptr< Net > NetSharedPtr
virtual VectorPortSharedPtr newVectorPortPtr(const std::string &inName, const EPortDirection &inDirection, const ViewSharedPtr &inViewPtr, const size_t &inSize, const PortBundleSharedPtr &inParentCollection=PortBundleSharedPtr(), const ChildFactorySharedPtr &inFactory=BaseVectorType::ChildFactorySharedPtr(new BaseVectorType::ChildFactory()), const std::string &inOriginalName=std::string())
Definition: VectorPort.cpp:54
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
virtual void onChildCreate(const boost::shared_ptr< BaseVectorType::ChildType > &inCreatedChild) const
Definition: VectorPort.cpp:199
virtual void getConnectedNets(std::vector< NetSharedPtr > &outNets, bool inSkipChildConnections=false) const
Definition: VectorPort.cpp:183
boost::shared_ptr< VectorPortBit::Factory > ChildFactorySharedPtr
Definition: Vector.hpp:63
Interface for an EDIF port object.
boost::shared_ptr< View > ViewSharedPtr
virtual void setParent(const ViewSharedPtr &inParent)
Definition: VectorPort.cpp:113
virtual void setParent(const boost::shared_ptr< _ParentType > &inSource)
virtual void accept(BaseVisitor &inoutVisitor)
Definition: VectorPort.cpp:104
virtual Connectable::Connection connect(const NetSharedPtr &inNet)
Definition: VectorPort.cpp:131
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73