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 #include <boost/mem_fn.hpp>
00025 #ifdef GENOM_SERIALIZATION
00026 #include <boost/archive/binary_iarchive.hpp>
00027 #include <boost/archive/binary_oarchive.hpp>
00028 #include <boost/serialization/base_object.hpp>
00029 #include <boost/serialization/export.hpp>
00030 #endif //GENOM_SERIALIZATION
00031
00032 #include "torc/generic/om/ConnectionHandler.hpp"
00033 #include "torc/generic/util/Log.hpp"
00034 #include "torc/generic/om/PortBundle.hpp"
00035 #include "torc/generic/om/Net.hpp"
00036
00037 #ifdef GENOM_SERIALIZATION
00038 BOOST_CLASS_EXPORT( torc::generic::PortBundle )
00039 #endif //GENOM_SERIALIZATION
00040
00041 namespace torc {
00042
00043 namespace generic {
00044
00045 void
00046 PortBundle::setParent(
00047 const ViewSharedPtr &inParent ) throw() {
00048 ParentedObject<View>::setParent( inParent );
00049 applyOnAllChildren(
00050 boost::bind( boost::mem_fn(
00051 &Port::setParent), _1, getParent() ));
00052 }
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 Connectable::Connection
00063 PortBundle::connect(
00064 const NetSharedPtr & inNet) throw(Error) {
00065 if( !inNet )
00066 {
00067 Error e( eMessageIdErrorPointerToItemDoesNotExist,
00068 __FUNCTION__, __FILE__, __LINE__ );
00069 e.saveContextData("Pointer to the Net object does not exists", inNet);
00070 throw e;
00071 }
00072 if( inNet->getSize() != getSize() )
00073 {
00074 Error e( eMessageIdErrorItemSizeMismatch,
00075 __FUNCTION__, __FILE__, __LINE__ );
00076 e.saveContextData("Net Size", inNet->getSize());
00077 e.saveContextData("Port Size", getSize() );
00078 throw e;
00079 }
00080 Bundle<Port>::List children;
00081 BundleFlattener<Port, ScalarPort, VectorPort,
00082 VectorPortBit, PortBundle> flattener;
00083 accept( flattener );
00084 flattener.getChildren( children );
00085
00086 Bundle<Net>::List netChildren;
00087 inNet->getChildren( netChildren );
00088 Bundle<Port>::List::iterator childPort = children.begin();
00089 Bundle<Port>::List::iterator childEnd = children.end();
00090 Bundle<Net>::List::iterator net = netChildren.begin();
00091 for(; childPort != childEnd; ++childPort, ++net )
00092 {
00093 try
00094 {
00095 (*childPort)->connect( *net );
00096 }
00097 catch( Error &e )
00098 {
00099 e.setCurrentLocation(
00100 __FUNCTION__, __FILE__, __LINE__ );
00101 throw;
00102 }
00103 }
00104 Connectable::Connection newConnection;
00105 try
00106 {
00107 ConnectionHandler handler( inNet );
00108 handler.connectPortToNet( getSharedThis() );
00109 newConnection = Connectable::connect( inNet );
00110 }
00111 catch( Error &e )
00112 {
00113 e.setCurrentLocation(
00114 __FUNCTION__, __FILE__, __LINE__ );
00115 throw;
00116 }
00117 return newConnection;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127 void
00128 PortBundle::disconnect(
00129 const Connectable::Connection &inConnection) throw(Error) {
00130 NetSharedPtr connNet = *inConnection;
00131 if( !connNet )
00132 {
00133 Error e( eMessageIdErrorConnectionInvalid,
00134 __FUNCTION__, __FILE__, __LINE__ );
00135 e.saveContextData("Pointer to Net", connNet);
00136 throw e;
00137 }
00138 Bundle<Port>::List children;
00139 BundleFlattener<Port, ScalarPort, VectorPort,
00140 VectorPortBit, PortBundle> flattener;
00141 accept( flattener );
00142 flattener.getChildren( children );
00143 Bundle<Net>::List netChildren;
00144 connNet->getChildren( netChildren );
00145 Bundle<Port>::List::iterator childPort = children.begin();
00146 Bundle<Port>::List::iterator childEnd = children.end();
00147 Bundle<Net>::List::iterator net = netChildren.begin();
00148
00149
00150
00151
00152
00153 for(; childPort != childEnd; ++childPort, ++net )
00154 {
00155 try
00156 {
00157 (*childPort)->disconnect( *net );
00158 }
00159 catch( Error &e )
00160 {
00161 e.setCurrentLocation(
00162 __FUNCTION__, __FILE__, __LINE__ );
00163 throw;
00164 }
00165 }
00166 try
00167 {
00168 ConnectionHandler handler( connNet );
00169 handler.disconnectPortFromNet( getSharedThis() );
00170 Connectable::disconnect( inConnection );
00171 }
00172 catch( Error &e )
00173 {
00174 e.setCurrentLocation(
00175 __FUNCTION__, __FILE__, __LINE__ );
00176 throw;
00177 }
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 PortBundleSharedPtr
00192 PortBundle::Factory::newPortBundlePtr( const std::string &inName,
00193 const ViewSharedPtr &inViewPtr,
00194 const PortBundleSharedPtr &inParentCollection,
00195 const std::string &inOriginalName ) throw(Error) {
00196 try
00197 {
00198 PortBundleSharedPtr newPortBundle;
00199 create( newPortBundle );
00200 newPortBundle->setName(inName);
00201 newPortBundle->setOriginalName( inOriginalName );
00202 if( inParentCollection )
00203 {
00204 inParentCollection->addChild( newPortBundle );
00205 }
00206 else
00207 {
00208 inViewPtr->addPort( newPortBundle );
00209 }
00210 return newPortBundle;
00211 }
00212 catch( Error &e )
00213 {
00214 e.setCurrentLocation(
00215 __FUNCTION__, __FILE__, __LINE__ );
00216 throw;
00217 }
00218 }
00219
00220 void
00221 PortBundle::accept(
00222 BaseVisitor & inoutVisitor) throw(Error) {
00223 try
00224 {
00225 runVisitor( *this, inoutVisitor );
00226 }
00227 catch( Error &e )
00228 {
00229 e.setCurrentLocation(
00230 __FUNCTION__, __FILE__, __LINE__ );
00231 throw;
00232 }
00233 }
00234
00235 PortBundle::PortBundle()
00236 :Port(),
00237 Bundle<Port>() {
00238 }
00239
00240 PortBundle::~PortBundle() throw() {
00241 torc::generic::log("Port bundle destroyed\n");
00242 }
00243
00244 #ifdef GENOM_SERIALIZATION
00245 template<class Archive> void
00246 PortBundle::serialize( Archive &ar, unsigned int ) {
00247 ar & boost::serialization::base_object< Port >( *this );
00248 ar & boost::serialization::base_object< Bundle<Port> >(*this);
00249 }
00250
00251
00252 template void
00253 PortBundle::serialize<boost::archive::binary_iarchive>(
00254 boost::archive::binary_iarchive & ar, const unsigned int);
00255
00256 template void
00257 PortBundle::serialize<boost::archive::binary_oarchive>(
00258 boost::archive::binary_oarchive & ar, const unsigned int);
00259
00260 #endif //GENOM_SERIALIZATION
00261
00262 }
00263
00264 }