00001 // Torc - Copyright 2011 University of Southern California. All Rights Reserved. 00002 // $HeadURL: https://torc-isi.svn.sourceforge.net/svnroot/torc-isi/branches/staging/0.9/src/torc/generic/om/Connectable.hpp $ 00003 // $Id: Connectable.hpp 10 2011-10-12 18:40:16Z nsteiner $ 00004 00005 // This program is free software: you can redistribute it and/or modify it under the terms of the 00006 // GNU General Public License as published by the Free Software Foundation, either version 3 of the 00007 // License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 00010 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 00011 // the GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License along with this program. If 00014 // not, see <http://www.gnu.org/licenses/>. 00015 00016 #ifndef TORC_GENERIC_OM_CONNECTABLE_HPP 00017 #define TORC_GENERIC_OM_CONNECTABLE_HPP 00018 00019 #include "torc/generic/om/DumpRestoreConfig.hpp" 00020 00021 #include <list> 00022 #include <vector> 00023 00024 //BOOST 00025 #include <boost/signal.hpp> 00026 #ifdef GENOM_SERIALIZATION 00027 #include <boost/serialization/access.hpp> 00028 #include <boost/serialization/is_abstract.hpp> 00029 #endif //GENOM_SERIALIZATION 00030 00031 #include "torc/generic/om/PointerTypes.hpp" 00032 #include "torc/generic/util/Error.hpp" 00033 00034 namespace torc { namespace generic { class Net; } } 00035 00036 namespace torc { 00037 00038 namespace generic { 00039 00040 /** 00041 * @brief An object that is connectable to a Net 00042 * 00043 * The Connectable class can act as a base for objects that can connect to a Net. This interface provides event functionality for connection and disconnection. It also provides polymorphic methods to implement connectivity beween different components in a design. 00044 */ 00045 class Connectable 00046 { 00047 #ifdef GENOM_SERIALIZATION 00048 friend class boost::serialization::access; 00049 #endif //GENOM_SERIALIZATION 00050 00051 public: 00052 /** 00053 * A connection between a net and this object 00054 */ 00055 typedef std::list< NetSharedPtr >::iterator Connection; 00056 00057 /** 00058 * A signal to indicate that a new connection has been made 00059 */ 00060 typedef boost::signal<void (const NetSharedPtr &)> Connected; 00061 00062 /** 00063 * A signal to indicate that a connection has been removed 00064 */ 00065 typedef boost::signal<void (const NetSharedPtr &)> Disconnected; 00066 00067 public: 00068 /** 00069 * A signal to indicate that a new connection has been made 00070 */ 00071 inline Connected & 00072 signalNetConnected() throw(); 00073 00074 /** 00075 * A signal to indicate that a new connection has been made 00076 */ 00077 inline Disconnected & 00078 signalNetDisconnected() throw(); 00079 00080 /** 00081 * Get the vector of Nets that are Connected to the current object. The connected elements are appended to the given vector 00082 * 00083 * @param[out] outNets A vector of Connected nets 00084 */ 00085 virtual void 00086 getConnectedNets( 00087 std::vector< NetSharedPtr > &outNets, 00088 bool inSkipChildConnections = false ) const throw(Error); 00089 00090 /** 00091 * Connect a Net to this object. 00092 * 00093 * @note This metod can be overridden by derived classes. However, the method must call the on_connected() method after this. The sigConnected_ signal must also be invoked in the overriding method. 00094 * 00095 * @param[in] inNet A pointer to the Net object that needs to be Connected 00096 * @return A connection that has been established. This can be used later for disconnection. 00097 */ 00098 virtual Connection 00099 connect(const NetSharedPtr & inNet) throw(Error) = 0; 00100 00101 /** 00102 * Disconnect a Net from this object. 00103 * @note This metod can be overridden by derived classes. However, the method must call the on_connected() method after this. The sigConnected_ signal must also be invoked in the overriding method. 00104 00105 * @param[in] inConnection A connection as returned by the connect() method 00106 * @exception Error Provided connection is invalid 00107 */ 00108 virtual void 00109 disconnect(const Connection & inConnection) throw(Error) = 0; 00110 00111 /** 00112 * Disconnect the named Net from this object. 00113 @note This metod can be overridden by derived classes. However, the method must call the on_connected() method after this. The sigConnected_ signal must also be invoked in the overriding method. 00114 00115 * @param[in] inName Name of the net to be Disconnected 00116 * @exception Error Provided net was not found 00117 */ 00118 void 00119 disconnect(const std::string &inName) throw(Error); 00120 00121 /** 00122 * Disconnect the given Net from this object. 00123 @note This metod can be overridden by derived classes. However, the method must call the on_disconnected() method after this. The sigDisconnected_ signal must also be invoked in the overriding method. 00124 00125 * @param[in] net Pointer to a net 00126 * @exception Error Provided net was not found 00127 */ 00128 void 00129 disconnect(const NetSharedPtr & net) throw(Error); 00130 00131 /** 00132 * Disconnect all connections to this port. 00133 * 00134 */ 00135 virtual void 00136 disconnect() throw(Error); 00137 00138 protected: 00139 /** 00140 * A polymorphic function that is called after a net is Connected to this object 00141 */ 00142 virtual void 00143 onConnect() throw(Error); 00144 00145 /** 00146 * A polymorphic function that is called after a net is Disconnected from this object 00147 */ 00148 virtual void 00149 onDisconnect() throw(Error); 00150 00151 Connectable(); 00152 00153 virtual 00154 ~Connectable() throw(); 00155 00156 private: 00157 Connectable( const Connectable &rhs ); 00158 00159 Connectable & 00160 operator =( const Connectable &rhs ); 00161 00162 #ifdef GENOM_SERIALIZATION 00163 template<class Archive> void 00164 serialize( Archive &ar, unsigned int ); 00165 #endif //GENOM_SERIALIZATION 00166 00167 private: 00168 std::list< NetSharedPtr > mConnectedNets; 00169 Connected mSigNetConnected; 00170 Disconnected mSigNetDisconnected; 00171 }; 00172 00173 /** 00174 * A signal to indicate that a new connection has been made 00175 */ 00176 inline Connectable::Connected & 00177 Connectable::signalNetConnected() throw() { 00178 return mSigNetConnected; 00179 } 00180 00181 /** 00182 * A signal to indicate that a new connection has been made 00183 */ 00184 inline Connectable::Disconnected & 00185 Connectable::signalNetDisconnected() throw() { 00186 return mSigNetDisconnected; 00187 } 00188 00189 } // namespace torc::generic 00190 00191 } // namespace torc 00192 00193 #ifdef GENOM_SERIALIZATION 00194 BOOST_IS_ABSTRACT( torc::generic::Connectable ) 00195 #endif //GENOM_SERIALIZATION 00196 00197 #endif // TORC_GENERIC_OM_CONNECTABLE_HPP