00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "torc/generic/om/DumpRestoreConfig.hpp"
00017
00018 #ifndef HAVE_CONFIG_H
00019 #include "torc/generic/config.h"
00020 #endif
00021
00022 #include <algorithm>
00023
00024
00025 #include <boost/bind.hpp>
00026 #include <boost/mem_fn.hpp>
00027
00028 #ifdef GENOM_SERIALIZATION
00029 #include <boost/archive/binary_iarchive.hpp>
00030 #include <boost/archive/binary_oarchive.hpp>
00031 #include <boost/serialization/list.hpp>
00032 #include <boost/serialization/shared_ptr.hpp>
00033 #endif //GENOM_SERIALIZATION
00034
00035 #include "torc/generic/om/Connectable.hpp"
00036 #include "torc/generic/om/Net.hpp"
00037 #include "torc/generic/util/Log.hpp"
00038
00039 namespace torc {
00040
00041 namespace generic {
00042
00043
00044
00045
00046
00047
00048 void
00049 Connectable::getConnectedNets(
00050 std::vector< NetSharedPtr > &outNets,
00051 bool inSkipChildConnections
00052 ) const throw(Error) {
00053 return outNets.insert( outNets.end(),
00054 mConnectedNets.begin(), mConnectedNets.end() );
00055 }
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 Connectable::Connection
00066 Connectable::connect(
00067 const NetSharedPtr & inNet) throw(Error) {
00068 Connection connection
00069 = mConnectedNets.insert(
00070 mConnectedNets.end(), inNet );
00071 try
00072 {
00073 onConnect();
00074 }
00075 catch( Error &e )
00076 {
00077 e.setCurrentLocation(
00078 __FUNCTION__, __FILE__, __LINE__ );
00079 throw;
00080 }
00081 signalNetConnected()( inNet );
00082 return connection;
00083 }
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 void
00095 Connectable::disconnect(
00096 const Connectable::Connection & inConnection) throw(Error) {
00097 if( inConnection == mConnectedNets.end() )
00098 {
00099 return;
00100 }
00101
00102 NetSharedPtr net = *inConnection;
00103 mConnectedNets.erase( inConnection );
00104 try
00105 {
00106 onDisconnect();
00107 }
00108 catch( Error &e )
00109 {
00110 e.setCurrentLocation(
00111 __FUNCTION__, __FILE__, __LINE__ );
00112 throw;
00113 }
00114 signalNetDisconnected()( net );
00115 return;
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 void
00128 Connectable::disconnect(const std::string &inName) throw(Error) {
00129 disconnect( std::find_if(mConnectedNets.begin(),
00130 mConnectedNets.end(), boost::bind<bool>(
00131 std::equal_to<std::string>(), boost::bind(
00132 boost::mem_fn( &Net::getName), _1), inName) ));
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 void Connectable::disconnect(const NetSharedPtr & inNet) throw(Error) {
00145 disconnect( std::find(mConnectedNets.begin(),
00146 mConnectedNets.end(), inNet ) );
00147 }
00148
00149 void
00150 Connectable::disconnect() throw(Error) {
00151 log("Disconnecting all\n");
00152 std::vector<NetSharedPtr> nets;
00153 getConnectedNets( nets, true );
00154 for( std::vector<NetSharedPtr>::iterator it
00155 = nets.begin(); it != nets.end(); ++it )
00156 {
00157 disconnect( *it );
00158 }
00159
00160 }
00161
00162
00163
00164
00165 void Connectable::onConnect() throw(Error) {
00166 }
00167
00168
00169
00170
00171 void Connectable::onDisconnect() throw(Error) {
00172 }
00173
00174 Connectable::Connectable()
00175 :mConnectedNets(),
00176 mSigNetConnected(),
00177 mSigNetDisconnected()
00178 {
00179 }
00180
00181 Connectable::~Connectable() throw()
00182 {
00183 mConnectedNets.clear();
00184 }
00185
00186 #ifdef GENOM_SERIALIZATION
00187 template<class Archive> void
00188 Connectable::serialize( Archive &ar, unsigned int ) {
00189 ar & mConnectedNets;
00190 }
00191
00192
00193 template void
00194 Connectable::serialize<boost::archive::binary_iarchive>(
00195 boost::archive::binary_iarchive & ar, const unsigned int);
00196
00197 template void
00198 Connectable::serialize<boost::archive::binary_oarchive>(
00199 boost::archive::binary_oarchive & ar, const unsigned int);
00200
00201
00202 #endif //GENOM_SERIALIZATION
00203
00204
00205 }
00206
00207 }