torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Connectable.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 
22 //BOOST
23 #include <boost/bind.hpp>
24 #include <boost/mem_fn.hpp>
25 
26 #ifdef GENOM_SERIALIZATION
27 #include <boost/archive/binary_iarchive.hpp>
28 #include <boost/archive/binary_oarchive.hpp>
29 #include <boost/serialization/list.hpp>
30 #include <boost/serialization/shared_ptr.hpp>
31 #endif //GENOM_SERIALIZATION
33 #include "torc/generic/Net.hpp"
34 #include "torc/generic/Log.hpp"
35 
36 namespace torc {
37 namespace generic {
38 
39 /**
40  * Get the vector of Nets that are Connected to the current object. The connected elements are
41  * appended to the given vector
42  *
43  * @return A vector of Connected nets
44  */
45 void Connectable::getConnectedNets(std::vector<NetSharedPtr>& outNets, bool inSkipChildConnections)
46  const throw (Error) {
47  outNets.insert(outNets.end(), mConnectedNets.begin(), mConnectedNets.end());
48 }
49 
50 /**
51  * Connect a Net to this object.
52  *
53  * @note This metod can be overridden by derived classes. However, the method must call the
54  * on_connected() method after this. The sigConnected_ signal must also be invoked in the
55  * overriding method.
56  *
57  * @param[in] inNet A pointer to the Net object that eeds to be Connected
58  * @return A connection that has been established. This can be used later for disconnection.
59  */
61  Connection connection = mConnectedNets.insert(mConnectedNets.end(), inNet);
62  try {
63  onConnect();
64  } catch(Error& e) {
65  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
66  throw;
67  }
68  signalNetConnected()(inNet);
69  return connection;
70 }
71 
72 /**
73  *
74  * Disconnect a Net from this object.
75  * @note This metod can be overridden by derived classes. However, the method must call the
76  * on_connected() method after this. The sigConnected_ signal must also be invoked in the
77  * overriding method.
78  *
79  * @param[in] inConnection A connection as returned by the connect() method
80  * @exception Error Provided connection is invalid
81  *
82  */
83 void Connectable::disconnect(const Connectable::Connection& inConnection) throw (Error) {
84  if(inConnection == mConnectedNets.end()) {
85  return;
86  }
87 
88  NetSharedPtr net = *inConnection;
89  mConnectedNets.erase(inConnection);
90  try {
91  onDisconnect();
92  } catch(Error& e) {
93  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
94  throw;
95  }
96  signalNetDisconnected()(net);
97  return;
98 }
99 
100 /**
101  *
102  * Disconnect the named Net from this object.
103  * @note This metod can be overridden by derived classes. However, the method must call the
104  * on_connected() method after this. The sigConnected_ signal must also be invoked in the
105  * overriding method.
106  *
107  * @param[in] inNname Name of the net to be Disconnected
108  * @exception Error Provided net was not found
109  *
110  */
111 void Connectable::disconnect(const std::string& inName) throw (Error) {
112  disconnect(std::find_if(mConnectedNets.begin(), mConnectedNets.end(),
113  boost::bind<bool>(std::equal_to<std::string>(),
114  boost::bind(boost::mem_fn(&Net::getName), _1), inName)));
115 }
116 
117 /**
118  *
119  * Disconnect the given Net from this object.
120  * @note This metod can be overridden by derived classes. However, the method must call the
121  * on_disconnected() method after this. The sigDisconnected_ signal must also be invoked in the
122  * overriding method.
123  *
124  * @param[in] inNet Pointer to a net
125  * @exception Error Provided net was not found
126  *
127  */
128 void Connectable::disconnect(const NetSharedPtr& inNet) throw (Error) {
129  disconnect(std::find(mConnectedNets.begin(), mConnectedNets.end(), inNet));
130 }
131 
133  log("Disconnecting all\n");
134  std::vector<NetSharedPtr> nets;
135  getConnectedNets(nets, true);
136  for(std::vector<NetSharedPtr>::iterator it = nets.begin(); it != nets.end(); ++it) {
137  disconnect(*it);
138  }
139 }
140 
141 /**
142  * A polymorphic function that is called after a net is Connected to this object
143  */
144 void Connectable::onConnect() throw (Error) {}
145 
146 /**
147  * A polymorphic function that is called after a net is Disconnected from this object
148  */
150 
151 Connectable::Connectable() : mConnectedNets(), mSigNetConnected(), mSigNetDisconnected() {}
152 
154  mConnectedNets.clear();
155 }
156 
157 #ifdef GENOM_SERIALIZATION
158 template <class Archive> void Connectable::serialize(Archive& ar, unsigned int) {
159  ar & mConnectedNets;
160 }
161 
162 //TO SATISFY THE LINKER
163 template void Connectable::serialize<boost::archive::binary_iarchive>(
164  boost::archive::binary_iarchive& ar, const unsigned int);
165 
166 template void Connectable::serialize<boost::archive::binary_oarchive>(
167  boost::archive::binary_oarchive& ar, const unsigned int);
168 
169 #endif //GENOM_SERIALIZATION
170 
171 } // namespace generic
172 } // namespace torc
virtual Connection connect(const NetSharedPtr &inNet)=0
Definition: Connectable.cpp:60
void log(const char *fmt,...)
Definition: Log.cpp:89
std::list< NetSharedPtr >::iterator Connection
Definition: Connectable.hpp:52
std::list< NetSharedPtr > mConnectedNets
std::string string
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
boost::shared_ptr< Net > NetSharedPtr
virtual const std::string getName() const
Definition: Nameable.hpp:89
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73