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
00023 #include <boost/bind.hpp>
00024 #ifdef GENOM_SERIALIZATION
00025 #include <boost/archive/binary_iarchive.hpp>
00026 #include <boost/archive/binary_oarchive.hpp>
00027 #include <boost/serialization/is_abstract.hpp>
00028 #include <boost/serialization/base_object.hpp>
00029 #include <boost/serialization/shared_ptr.hpp>
00030 #include <boost/serialization/list.hpp>
00031 #endif //GENOM_SERIALIZATION
00032
00033 #include "torc/generic/util/Log.hpp"
00034 #include "torc/generic/om/Net.hpp"
00035 #include "torc/generic/om/Port.hpp"
00036 #include "torc/generic/om/PortReference.hpp"
00037 #include "torc/generic/om/PortList.hpp"
00038 #include "torc/generic/om/View.hpp"
00039
00040 #ifdef GENOM_SERIALIZATION
00041 BOOST_IS_ABSTRACT( torc::generic::Composite< torc::generic::Net > )
00042 #endif //GENOM_SERIALIZATION
00043
00044 namespace torc {
00045
00046 namespace generic {
00047
00048 Net::Net()
00049 : Composite<Net>(),
00050 Commentable(),
00051 Connectable(),
00052 Nameable(),
00053 PropertyContainer(),
00054 Renamable(),
00055 Visitable(),
00056 ParentedObject<View>(),
00057 UserDataContainer(),
00058 mAttributes() {
00059 }
00060
00061 Net::~Net() throw() {
00062 try
00063 {
00064 disconnect();
00065 }
00066 catch( Error &e )
00067 {
00068 log("Cannot propagate received error\n");
00069 }
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 void
00081 Net::addSubnet(
00082 const NetSharedPtr &inSubnet) throw(Error) {
00083 std::string name = inSubnet->getName();
00084 if( name.empty() )
00085 {
00086 Error e( eMessageIdErrorEmptyItemName,
00087 __FUNCTION__, __FILE__, __LINE__ );
00088 e.saveContextData("Subnet name", name);
00089 throw e;
00090 }
00091 if( false == mSubnets.set( name, inSubnet ) )
00092 {
00093 Error e( eMessageIdErrorItemAlreadyExists,
00094 __FUNCTION__, __FILE__, __LINE__ );
00095 e.saveContextData("Subnet name", name);
00096 throw e;
00097 }
00098 inSubnet->setParentNet( getSharedThis() );
00099 return;
00100 }
00101
00102
00103
00104
00105
00106
00107
00108 NetSharedPtr
00109 Net::findSubnet(const std::string &inName) throw(Error) {
00110 NetSharedPtr subnet;
00111 mSubnets.get( inName, subnet );
00112 return subnet;
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 void
00124 Net::removeSubnet(const std::string &inName) throw(Error) {
00125 if( inName.empty() )
00126 {
00127 Error e( eMessageIdErrorEmptyItemName,
00128 __FUNCTION__, __FILE__, __LINE__ );
00129 e.saveContextData("Subnet name", inName);
00130 throw e;
00131 }
00132 if( false == mSubnets.remove( inName ) )
00133 {
00134 Error e( eMessageIdErrorItemNotFound,
00135 __FUNCTION__, __FILE__, __LINE__ );
00136 e.saveContextData("Subnet name", inName);
00137 throw e;
00138 }
00139 return;
00140 }
00141
00142 void
00143 Net::setParentNet( const NetSharedPtr &inParent ) throw() {
00144 mParentNet = inParent;
00145 }
00146
00147
00148
00149
00150
00151
00152 void
00153 Net::setAttributes(
00154 const NetAttributesSharedPtr & inSource) throw() {
00155 mAttributes = inSource;
00156 }
00157
00158 void
00159 Net::setSubnets(
00160 const std::vector< NetSharedPtr > & inSource) throw(Error) {
00161 std::vector< NetSharedPtr >::const_iterator subnet
00162 = inSource.begin();
00163 std::vector< NetSharedPtr >::const_iterator subEnd
00164 = inSource.end();
00165 for(; subnet != subEnd; ++subnet )
00166 {
00167 try
00168 {
00169 addSubnet( *subnet );
00170 }
00171 catch( Error &e )
00172 {
00173 e.setCurrentLocation(
00174 __FUNCTION__, __FILE__, __LINE__ );
00175 throw;
00176 }
00177 }
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 void
00189 Net::addConnectedPort(
00190 const PortSharedPtr &inPort) throw(Error) {
00191 if( !inPort )
00192 {
00193 return;
00194 }
00195 std::string name = inPort->getName();
00196 if( name.empty() )
00197 {
00198 Error e( eMessageIdErrorEmptyItemName,
00199 __FUNCTION__, __FILE__, __LINE__ );
00200 e.saveContextData("Port name", name);
00201 throw e;
00202 }
00203 mConnectedPorts.push_back( inPort );
00204 return;
00205 }
00206
00207
00208
00209
00210
00211
00212 PortSharedPtr
00213 Net::findConnectedPort( const std::string &inName) throw(Error) {
00214 PortSharedPtr port;
00215 std::list< PortSharedPtr >::iterator it
00216 = find_if( mConnectedPorts.begin(), mConnectedPorts.end(),
00217 boost::bind<bool>( std::equal_to<std::string>(),
00218 boost::bind( boost::mem_fn( &Port::getName ), _1),
00219 inName));
00220 if( it != mConnectedPorts.end() )
00221 {
00222 port = *it;
00223 }
00224 return port;
00225 }
00226
00227 void
00228 Net::getConnectedPorts(
00229 std::vector< PortSharedPtr > &outPorts,
00230 bool inSkipChildConnections ) const throw() {
00231 outPorts.insert( outPorts.end(),
00232 mConnectedPorts.begin(), mConnectedPorts.end() );
00233 return;
00234 }
00235
00236
00237 void
00238 Net::removeConnectedPort( const PortSharedPtr &inPort) throw(Error) {
00239 std::list< PortSharedPtr >::iterator it
00240 = find( mConnectedPorts.begin(), mConnectedPorts.end(),
00241 inPort);
00242 if( it != mConnectedPorts.end() )
00243 {
00244 mConnectedPorts.erase( it );
00245 }
00246 else
00247 {
00248 Error e( eMessageIdErrorItemNotFound,
00249 __FUNCTION__, __FILE__, __LINE__ );
00250 e.saveContextData("Name", inPort->getName() );
00251 throw e;
00252 }
00253 return;
00254
00255 }
00256
00257 void
00258 Net::setConnectedPorts(
00259 const std::vector< PortSharedPtr > &inSource) throw(Error) {
00260 std::vector< PortSharedPtr >::const_iterator port
00261 = inSource.begin();
00262 std::vector< PortSharedPtr >::const_iterator portEnd
00263 = inSource.end();
00264 for( ; port != portEnd; ++port )
00265 {
00266 try
00267 {
00268 addConnectedPort( *port );
00269 }
00270 catch( Error &e )
00271 {
00272 e.setCurrentLocation(
00273 __FUNCTION__, __FILE__, __LINE__ );
00274 throw;
00275 }
00276 }
00277
00278 }
00279
00280 void
00281 Net::getConnectedPortLists(
00282 std::vector< PortListSharedPtr > &outPortList) const throw() {
00283 outPortList.insert( outPortList.end(),
00284 mConnectedPortLists.begin(), mConnectedPortLists.end() );
00285 return;
00286 }
00287
00288 void
00289 Net::addConnectedPortList(
00290 const PortListSharedPtr & inPortList) throw(Error) {
00291 if( !inPortList )
00292 {
00293 return;
00294 }
00295 mConnectedPortLists.push_back( inPortList );
00296 return;
00297 }
00298
00299 void
00300 Net::removeConnectedPortList(
00301 const PortListSharedPtr &inPortList) throw(Error) {
00302 std::list< PortListSharedPtr >::iterator it
00303 = find( mConnectedPortLists.begin(), mConnectedPortLists.end(),
00304 inPortList);
00305 if( it != mConnectedPortLists.end() )
00306 {
00307 mConnectedPortLists.erase( it );
00308 }
00309 else
00310 {
00311 Error e( eMessageIdErrorItemNotFound,
00312 __FUNCTION__, __FILE__, __LINE__ );
00313 e.saveContextData("Name", std::string("PortList") );
00314 throw e;
00315 }
00316 return;
00317
00318
00319 }
00320
00321
00322
00323
00324
00325
00326
00327
00328 void
00329 Net::addConnectedPortReference(
00330 const PortReferenceSharedPtr & inPortRef) throw(Error) {
00331 if( !inPortRef )
00332 {
00333 return;
00334 }
00335 std::string name = inPortRef->getName();
00336 if( name.empty() )
00337 {
00338 Error e( eMessageIdErrorEmptyItemName,
00339 __FUNCTION__, __FILE__, __LINE__ );
00340 e.saveContextData("PortReference name", name);
00341 throw e;
00342 }
00343 mConnectedPortRefs.push_back( inPortRef );
00344 return;
00345 }
00346
00347
00348
00349
00350
00351
00352 PortReferenceSharedPtr
00353 Net::findConnectedPortReference(
00354 const std::string &inName) throw(Error) {
00355 PortReferenceSharedPtr portRef;
00356 std::list< PortReferenceSharedPtr >::iterator it
00357 = find_if(
00358 mConnectedPortRefs.begin(), mConnectedPortRefs.end(),
00359 boost::bind<bool>( std::equal_to<std::string>(),
00360 boost::bind(
00361 boost::mem_fn(&PortReference::getName ), _1),
00362 inName));
00363 if( it != mConnectedPortRefs.end() )
00364 {
00365 portRef = *it;
00366 }
00367 return portRef;
00368 }
00369
00370 void
00371 Net::getConnectedPortRefs(
00372 std::vector< PortReferenceSharedPtr > &outPortRefs,
00373 bool inSkipChildConnections ) const throw() {
00374 outPortRefs.insert( outPortRefs.end(),
00375 mConnectedPortRefs.begin(), mConnectedPortRefs.end() );
00376 return;
00377 }
00378
00379 void
00380 Net::removeConnectedPortReference(
00381 const PortReferenceSharedPtr &inPortRef) throw(Error) {
00382 std::list< PortReferenceSharedPtr >::iterator it
00383 = find( mConnectedPortRefs.begin(), mConnectedPortRefs.end(),
00384 inPortRef );
00385 if( it != mConnectedPortRefs.end() )
00386 {
00387 mConnectedPortRefs.erase(it);
00388 }
00389 else
00390 {
00391 Error e( eMessageIdErrorItemNotFound,
00392 __FUNCTION__, __FILE__, __LINE__ );
00393 e.saveContextData("PortReference name", inPortRef->getName());
00394 throw e;
00395 }
00396 return;
00397 }
00398
00399 void
00400 Net::setConnectedPortRefs(
00401 const std::vector< PortReferenceSharedPtr > & inSource) throw(Error) {
00402 std::vector< PortReferenceSharedPtr >::const_iterator portRef = inSource.begin();
00403 std::vector< PortReferenceSharedPtr >::const_iterator portRefEnd = inSource.end();
00404 for( ; portRef != portRefEnd; ++portRef )
00405 {
00406 try
00407 {
00408 addConnectedPortReference( *portRef );
00409 }
00410 catch( Error &e )
00411 {
00412 e.setCurrentLocation(
00413 __FUNCTION__, __FILE__, __LINE__ );
00414 throw;
00415 }
00416 }
00417 }
00418
00419 void
00420 Net::disconnect() throw(Error) {
00421 if( !getName().empty() )
00422 {
00423 log("Clearing net %s\n", getName().c_str() );
00424 }
00425 mSubnets.clear();
00426
00427
00428
00429 std::vector< PortSharedPtr > ports;
00430 getConnectedPorts( ports, true );
00431 void (Connectable::*fp)(const NetSharedPtr & net)
00432 = &Connectable::disconnect;
00433 std::for_each( ports.begin(), ports.end(),
00434 boost::bind( fp, _1, getSharedThis()));
00435 std::vector< PortReferenceSharedPtr > portRefs;
00436 getConnectedPortRefs( portRefs, true );
00437 std::for_each( portRefs.begin(), portRefs.end(),
00438 boost::bind( fp, _1, getSharedThis()));
00439 std::vector< PortListSharedPtr > portLists;
00440 getConnectedPortLists( portLists );
00441 std::for_each( portLists.begin(), portLists.end(),
00442 boost::bind( fp, _1, getSharedThis()));
00443 Connectable::disconnect();
00444 }
00445
00446 #ifdef GENOM_SERIALIZATION
00447 template<class Archive> void
00448 Net::serialize( Archive &ar, unsigned int ) {
00449 ar & boost::serialization::base_object< Commentable >( *this );
00450 ar & boost::serialization::base_object< Connectable >( *this );
00451 ar & boost::serialization::base_object< Nameable >( *this );
00452 ar & boost::serialization::base_object< PropertyContainer >(
00453 *this );
00454 ar & boost::serialization::base_object< Renamable >( *this );
00455 ar & boost::serialization::base_object< Visitable >( *this );
00456 ar & boost::serialization::base_object<
00457 Composite<Net> >( *this );
00458 ar & mSubnets;
00459 ar & mConnectedPorts;
00460 ar & mConnectedPortRefs;
00461 ar & mConnectedPortLists;
00462 }
00463
00464 template void
00465 Net::serialize<boost::archive::binary_iarchive>(
00466 boost::archive::binary_iarchive & ar, const unsigned int);
00467
00468 template void
00469 Net::serialize<boost::archive::binary_oarchive>(
00470 boost::archive::binary_oarchive & ar, const unsigned int);
00471
00472 #endif //GENOM_SERIALIZATION
00473
00474 }
00475
00476 }