torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
generic/Net.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 #ifdef GENOM_SERIALIZATION
23 #include <boost/archive/binary_iarchive.hpp>
24 #include <boost/archive/binary_oarchive.hpp>
25 #include <boost/serialization/is_abstract.hpp>
26 #include <boost/serialization/base_object.hpp>
27 #include <boost/serialization/shared_ptr.hpp>
28 #include <boost/serialization/list.hpp>
29 #endif //GENOM_SERIALIZATION
30 #include "torc/generic/Log.hpp"
31 #include "torc/generic/Net.hpp"
32 #include "torc/generic/Port.hpp"
35 #include "torc/generic/View.hpp"
36 
37 #ifdef GENOM_SERIALIZATION
39 #endif //GENOM_SERIALIZATION
40 namespace torc {
41 namespace generic {
42 
45  View>(), UserDataContainer(), mSubnets(), mConnectedPorts(), mConnectedPortLists(), mConnectedPortRefs(), mParentNet(), mAttributes() {}
46 
47 Net::~Net() throw () {
48  try {
49  disconnect();
50  } catch(Error& e) {
51  log("Cannot propagate received error\n");
52  }
53 }
54 
55 /**
56  * Add a subnet to this net.
57  *
58  * @param[in] inSubnet Subnet to be added
59  *
60  * @exception Error Could not add subnet, because subnet name is empty
61  * @exception Error Could not add subnet, because subnet name is already exists
62  */
63 void Net::addSubnet(const NetSharedPtr& inSubnet) throw (Error) {
64  std::string name = inSubnet->getName();
65  if(name.empty()) {
66  Error e(eMessageIdErrorEmptyItemName, __FUNCTION__, __FILE__, __LINE__);
67  e.saveContextData("Subnet name", name);
68  throw e;
69  }
70  if(false == mSubnets.set(name, inSubnet)) {
71  Error e(eMessageIdErrorItemAlreadyExists, __FUNCTION__, __FILE__, __LINE__);
72  e.saveContextData("Subnet name", name);
73  throw e;
74  }
75  inSubnet->setParentNet(getSharedThis());
76  return;
77 }
78 
79 /**
80  * Find a subnet from belonging to this net.
81  *
82  * @param[in] inName Name of the subnet. If not found, empty pointer is returned
83  *
84  */
86  NetSharedPtr subnet;
87  mSubnets.get(inName, subnet);
88  return subnet;
89 }
90 
91 /**
92  * Remove a subnet from this net.
93  *
94  * @param inName Name of the object to be delete
95  *
96  * @exception Error Could not remove subnet, because subnet name is empty
97  * @exception Error Could not remove subnet, because subnet not present.
98  */
99 void Net::removeSubnet(const std::string& inName) throw (Error) {
100  if(inName.empty()) {
101  Error e(eMessageIdErrorEmptyItemName, __FUNCTION__, __FILE__, __LINE__);
102  e.saveContextData("Subnet name", inName);
103  throw e;
104  }
105  if(false == mSubnets.remove(inName)) {
106  Error e(eMessageIdErrorItemNotFound, __FUNCTION__, __FILE__, __LINE__);
107  e.saveContextData("Subnet name", inName);
108  throw e;
109  }
110  return;
111 }
112 
113 void Net::setParentNet(const NetSharedPtr& inParent) {
114  mParentNet = inParent;
115 }
116 
117 /**
118  * Set the attributes of the net. Attributes include criticality, netDelay etc.
119  *
120  * @param[in] inSource Pointer to NetAttributes object.
121  */
123  mAttributes = inSource;
124 }
125 
126 void Net::setSubnets(const std::vector<NetSharedPtr>& inSource) throw (Error) {
127  std::vector<NetSharedPtr>::const_iterator subnet = inSource.begin();
128  std::vector<NetSharedPtr>::const_iterator subEnd = inSource.end();
129  for(; subnet != subEnd; ++subnet) {
130  try {
131  addSubnet(*subnet);
132  } catch(Error& e) {
133  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
134  throw;
135  }
136  }
137 }
138 
139 /**
140  * Connect a port to this net.
141  *
142  * @param[in] inPort port to be added. Empty pointer is ignored.
143  *
144  * @exception Error Could not add Port, because Port name is empty
145  * @exception Error Could not add Port, because Port name is already exists
146  */
147 void Net::addConnectedPort(const PortSharedPtr& inPort) throw (Error) {
148  if(!inPort) {
149  return;
150  }
151  std::string name = inPort->getName();
152  if(name.empty()) {
153  Error e(eMessageIdErrorEmptyItemName, __FUNCTION__, __FILE__, __LINE__);
154  e.saveContextData("Port name", name);
155  throw e;
156  }
157  mConnectedPorts.push_back(inPort);
158  return;
159 }
160 
161 /**
162  * Find a port connected to this net.
163  *
164  * @param[in] inName Name of the port. If not found, empty pointer is returned
165  */
167  PortSharedPtr port;
168  std::list<PortSharedPtr>::iterator it = find_if(
169  mConnectedPorts.begin(),
170  mConnectedPorts.end(),
171  boost::bind<bool>(std::equal_to<std::string>(),
172  boost::bind(boost::mem_fn(&Port::getName), _1), inName));
173  if(it != mConnectedPorts.end()) {
174  port = *it;
175  }
176  return port;
177 }
178 
179 void Net::getConnectedPorts(std::vector<PortSharedPtr>& outPorts,
180  bool inSkipChildConnections) const {
181  outPorts.insert(outPorts.end(), mConnectedPorts.begin(), mConnectedPorts.end());
182  return;
183 }
184 
185 void Net::removeConnectedPort(const PortSharedPtr& inPort) throw (Error) {
186  std::list<PortSharedPtr>::iterator it = find(mConnectedPorts.begin(), mConnectedPorts.end(),
187  inPort);
188  if(it != mConnectedPorts.end()) {
189  mConnectedPorts.erase(it);
190  } else {
191  Error e(eMessageIdErrorItemNotFound, __FUNCTION__, __FILE__, __LINE__);
192  e.saveContextData("Name", inPort->getName());
193  throw e;
194  }
195  return;
196 
197 }
198 
199 void Net::setConnectedPorts(const std::vector<PortSharedPtr>& inSource) throw (Error) {
200  std::vector<PortSharedPtr>::const_iterator port = inSource.begin();
201  std::vector<PortSharedPtr>::const_iterator portEnd = inSource.end();
202  for(; port != portEnd; ++port) {
203  try {
204  addConnectedPort(*port);
205  } catch(Error& e) {
206  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
207  throw;
208  }
209  }
210 
211 }
212 
213 void Net::getConnectedPortLists(std::vector<PortListSharedPtr>& outPortList) const {
214  outPortList.insert(outPortList.end(), mConnectedPortLists.begin(), mConnectedPortLists.end());
215  return;
216 }
217 
218 void Net::addConnectedPortList(const PortListSharedPtr& inPortList) throw (Error) {
219  if(!inPortList) {
220  return;
221  }
222  mConnectedPortLists.push_back(inPortList);
223  return;
224 }
225 
226 void Net::removeConnectedPortList(const PortListSharedPtr& inPortList) throw (Error) {
227  std::list<PortListSharedPtr>::iterator it = find(mConnectedPortLists.begin(),
228  mConnectedPortLists.end(), inPortList);
229  if(it != mConnectedPortLists.end()) {
230  mConnectedPortLists.erase(it);
231  } else {
232  Error e(eMessageIdErrorItemNotFound, __FUNCTION__, __FILE__, __LINE__);
233  e.saveContextData("Name", std::string("PortList"));
234  throw e;
235  }
236  return;
237 
238 }
239 
240 /**
241  * Connect a port reference to this net.
242  *
243  * @param[in] inPortRef Port reference to be added
244  *
245  * @exception Error Could not add port reference
246  */
248  if(!inPortRef) {
249  return;
250  }
251  std::string name = inPortRef->getName();
252  if(name.empty()) {
253  Error e(eMessageIdErrorEmptyItemName, __FUNCTION__, __FILE__, __LINE__);
254  e.saveContextData("PortReference name", name);
255  throw e;
256  }
257  mConnectedPortRefs.push_back(inPortRef);
258  return;
259 }
260 
261 /**
262  * Find a port reference connected to this net.
263  *
264  * @param[in] inName Name of the port reference. If not found, empty pointer is returned
265  */
267  PortReferenceSharedPtr portRef;
268  std::list<PortReferenceSharedPtr>::iterator it = find_if(
269  mConnectedPortRefs.begin(),
270  mConnectedPortRefs.end(),
271  boost::bind<bool>(std::equal_to<std::string>(),
272  boost::bind(boost::mem_fn(&PortReference::getName), _1), inName));
273  if(it != mConnectedPortRefs.end()) {
274  portRef = *it;
275  }
276  return portRef;
277 }
278 
279 void Net::getConnectedPortRefs(std::vector<PortReferenceSharedPtr>& outPortRefs,
280  bool inSkipChildConnections) const {
281  outPortRefs.insert(outPortRefs.end(), mConnectedPortRefs.begin(), mConnectedPortRefs.end());
282  return;
283 }
284 
286  std::list<PortReferenceSharedPtr>::iterator it = find(mConnectedPortRefs.begin(),
287  mConnectedPortRefs.end(), inPortRef);
288  if(it != mConnectedPortRefs.end()) {
289  mConnectedPortRefs.erase(it);
290  } else {
291  Error e(eMessageIdErrorItemNotFound, __FUNCTION__, __FILE__, __LINE__);
292  e.saveContextData("PortReference name", inPortRef->getName());
293  throw e;
294  }
295  return;
296 }
297 
298 void Net::setConnectedPortRefs(const std::vector<PortReferenceSharedPtr>& inSource) throw (Error) {
299  std::vector<PortReferenceSharedPtr>::const_iterator portRef = inSource.begin();
300  std::vector<PortReferenceSharedPtr>::const_iterator portRefEnd = inSource.end();
301  for(; portRef != portRefEnd; ++portRef) {
302  try {
303  addConnectedPortReference(*portRef);
304  } catch(Error& e) {
305  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
306  throw;
307  }
308  }
309 }
310 
311 void Net::disconnect() throw (Error) {
312  if(!getName().empty()) {
313  log("Clearing net %s\n", getName().c_str());
314  }
315  mSubnets.clear();
316  //Don't try to optimize the following by using applyOnAll
317  // it causes memory corruption as the SymTab itself changes
318  // during disconnection
319  std::vector<PortSharedPtr> ports;
320  getConnectedPorts(ports, true);
321  void (Connectable::*fp)(const NetSharedPtr& net)
323  std::for_each(ports.begin(), ports.end(), boost::bind(fp, _1, getSharedThis()));
324  std::vector<PortReferenceSharedPtr> portRefs;
325  getConnectedPortRefs(portRefs, true);
326  std::for_each(portRefs.begin(), portRefs.end(), boost::bind(fp, _1, getSharedThis()));
327  std::vector<PortListSharedPtr> portLists;
328  getConnectedPortLists(portLists);
329  std::for_each(portLists.begin(), portLists.end(), boost::bind(fp, _1, getSharedThis()));
331 }
332 
333 #ifdef GENOM_SERIALIZATION
334 template <class Archive> void Net::serialize(Archive& ar, unsigned int) {
335  ar & boost::serialization::base_object < Commentable > (*this);
336  ar & boost::serialization::base_object < Connectable > (*this);
337  ar & boost::serialization::base_object < Nameable > (*this);
338  ar & boost::serialization::base_object < PropertyContainer > (*this);
339  ar & boost::serialization::base_object < Renamable > (*this);
340  ar & boost::serialization::base_object < Visitable > (*this);
341  ar & boost::serialization::base_object < Composite<Net> > (*this);
342  ar & mSubnets;
343  ar & mConnectedPorts;
344  ar & mConnectedPortRefs;
345  ar & mConnectedPortLists;
346 }
347 //TO SATISFY THE LINKER
348 template void Net::serialize<boost::archive::binary_iarchive>(boost::archive::binary_iarchive& ar,
349  const unsigned int);
350 
351 template void Net::serialize<boost::archive::binary_oarchive>(boost::archive::binary_oarchive& ar,
352  const unsigned int);
353 
354 #endif //GENOM_SERIALIZATION
355 } // namespace generic
356 } // namespace torc
void log(const char *fmt,...)
Definition: Log.cpp:89
void removeConnectedPort(const PortSharedPtr &inPort)
An object that has a parent.
std::list< PortReferenceSharedPtr > mConnectedPortRefs
boost::shared_ptr< NetAttributes > NetAttributesSharedPtr
Represents objects that have properties.
void setParentNet(const NetSharedPtr &inParent)
Represents and EDIF View.
Definition: View.hpp:61
void setConnectedPorts(const std::vector< PortSharedPtr > &inSource)
Interface for objects that can be composed within each other.
Definition: Composite.hpp:45
void removeSubnet(const std::string &inName)
Definition: generic/Net.cpp:99
void removeConnectedPortReference(const PortReferenceSharedPtr &inPortRef)
An object that is connectable to a Net.
Definition: Connectable.hpp:44
PortSharedPtr findConnectedPort(const std::string &inName)
PortReferenceSharedPtr findConnectedPortReference(const std::string &inName)
Represents an EDIF Net.
Definition: generic/Net.hpp:58
Represents all classes that can hold user comments.
Definition: Commentable.hpp:36
std::list< PortSharedPtr > mConnectedPorts
Represents class that can hold userData.
virtual void getConnectedPorts(std::vector< PortSharedPtr > &outPort, bool inSkipChildConnections=false) const
std::string string
virtual void getConnectedPortRefs(std::vector< PortReferenceSharedPtr > &outPortRefs, bool inSkipChildConnections=false) const
void addSubnet(const NetSharedPtr &inSubnet)
Definition: generic/Net.cpp:63
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
boost::shared_ptr< Net > NetSharedPtr
boost::shared_ptr< PortReference > PortReferenceSharedPtr
boost::shared_ptr< PortList > PortListSharedPtr
void saveContextData(const std::string &inName, const boost::any &inSource)
Definition: Error.cpp:79
void addConnectedPort(const PortSharedPtr &inPort)
void addConnectedPortList(const PortListSharedPtr &inPort)
virtual void getConnectedPortLists(std::vector< PortListSharedPtr > &outPortList) const
virtual void disconnect()
NetAttributesSharedPtr mAttributes
NetSharedPtr findSubnet(const std::string &inName)
Definition: generic/Net.cpp:85
void removeConnectedPortList(const PortListSharedPtr &inList)
virtual const std::string getName() const
Definition: Nameable.hpp:89
An object that has a name.
Definition: Nameable.hpp:34
void setAttributes(const NetAttributesSharedPtr &inSource)
boost::shared_ptr< Port > PortSharedPtr
Represents objects that can be renamed.
void setConnectedPortRefs(const std::vector< PortReferenceSharedPtr > &inSource)
SymTab< std::string, NetSharedPtr, true > mSubnets
void addConnectedPortReference(const PortReferenceSharedPtr &portRef)
std::list< PortListSharedPtr > mConnectedPortLists
void setSubnets(const std::vector< NetSharedPtr > &inSource)
An object that receives an inoutVisitor.
Definition: Visitable.hpp:38
NetSharedPtr mParentNet
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73