00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef HAVE_CONFIG_H
00017 #include "torc/generic/config.h"
00018 #endif
00019
00020 #include "torc/generic/om/DumpRestoreConfig.hpp"
00021
00022
00023 #ifdef GENOM_SERIALIZATION
00024 #include <boost/archive/binary_iarchive.hpp>
00025 #include <boost/archive/binary_oarchive.hpp>
00026 #include <boost/serialization/base_object.hpp>
00027 #include <boost/serialization/list.hpp>
00028 #endif //GENOM_SERIALIZATION
00029
00030 #include "torc/generic/om/PortList.hpp"
00031 #include "torc/generic/om/Net.hpp"
00032 #include "torc/generic/om/Port.hpp"
00033 #include "torc/generic/om/PortReference.hpp"
00034 #include "torc/generic/om/ConnectionHandler.hpp"
00035
00036 namespace torc {
00037
00038 namespace generic {
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 PortListSharedPtr
00050 PortList::Factory::newPortListPtr( const std::list< PortSharedPtr > & inPorts,
00051 const std::list< PortReferenceSharedPtr > & inPortReferences ) throw(Error) {
00052 try
00053 {
00054 PortListSharedPtr newPortList;
00055 create( newPortList );
00056 std::list< PortSharedPtr >::const_iterator portIt = inPorts.begin();
00057 for( ; portIt != inPorts.end(); portIt++ )
00058 {
00059 newPortList->addChildPort( *portIt );
00060 }
00061 std::list< PortReferenceSharedPtr >::const_iterator portRefIt
00062 = inPortReferences.begin();
00063 for( ; portRefIt != inPortReferences.end(); portRefIt++ )
00064 {
00065 newPortList->addChildPortReference( *portRefIt );
00066 }
00067 return newPortList;
00068 }
00069 catch( Error &e )
00070 {
00071 e.setCurrentLocation(
00072 __FUNCTION__, __FILE__, __LINE__ );
00073 throw;
00074 }
00075 }
00076
00077 void
00078 PortList::accept(BaseVisitor & inoutVisitor) throw(Error) {
00079 try
00080 {
00081 runVisitor( *this, inoutVisitor );
00082 }
00083 catch( Error &e )
00084 {
00085 e.setCurrentLocation(
00086 __FUNCTION__, __FILE__, __LINE__ );
00087 throw;
00088 }
00089 }
00090
00091 template<typename _Tp>
00092 void
00093 PortList::connectElementToNet(
00094 const NetSharedPtr &inNet,
00095 const boost::shared_ptr<_Tp> &inType,
00096 size_t &inoutCurrentWidth ) throw(Error)
00097 {
00098 try
00099 {
00100 typedef _Tp Type;
00101 typedef boost::shared_ptr<_Tp> Pointer;
00102 size_t updatedSize = inoutCurrentWidth + inType->getSize();
00103 if( updatedSize > inNet->getSize() )
00104 {
00105
00106 }
00107
00108 switch( inNet->getCompositionType() )
00109 {
00110 case eCompositionTypeScalar:
00111 case eCompositionTypeVectorBit:
00112 {
00113 if( 1 == inType->getSize() )
00114 {
00115 inType->connect( inNet );
00116 inoutCurrentWidth++;
00117 }
00118 break;
00119 }
00120 case eCompositionTypeVector:
00121 {
00122
00123 std::vector< NetSharedPtr > children;
00124 inNet->getChildren( children );
00125
00126 switch( inType->getCompositionType() )
00127 {
00128 case eCompositionTypeScalar:
00129 case eCompositionTypeVectorBit:
00130
00131 {
00132 connectElementToNet(
00133 children[ inoutCurrentWidth ],
00134 inType, inoutCurrentWidth);
00135 break;
00136 }
00137 case eCompositionTypeVector:
00138
00139 {
00140 std::vector< Pointer > cTypes;
00141 inType->getChildren( cTypes );
00142 size_t j = 0;
00143 for( size_t i = inoutCurrentWidth;
00144 i < updatedSize; i++, j++ )
00145 {
00146 connectElementToNet(
00147 children[i], cTypes[j],
00148 inoutCurrentWidth );
00149 }
00150 break;
00151 }
00152 case eCompositionTypeBundle:
00153 {
00154 std::vector<Pointer> cTypes;
00155 inType->getChildren( cTypes );
00156 for( typename std::vector<Pointer>::iterator it
00157 = cTypes.begin();
00158 it != cTypes.end(); ++it )
00159 {
00160 connectElementToNet(
00161 inNet, *it, inoutCurrentWidth);
00162 }
00163 break;
00164 }
00165 }
00166 }
00167 case eCompositionTypeBundle:
00168 default:
00169 {
00170
00171 }
00172 }
00173 }
00174 catch( Error &e )
00175 {
00176 e.setCurrentLocation(
00177 __FUNCTION__, __FILE__, __LINE__ );
00178 throw;
00179 }
00180 }
00181
00182 Connectable::Connection
00183 PortList::connect(const NetSharedPtr & inNet) throw(Error) {
00184 size_t count = 0;
00185 for( std::list< PortListElement >::iterator it
00186 = mElements.begin(); it != mElements.end(); ++it )
00187 {
00188 try
00189 {
00190 switch( (*it).getType() )
00191 {
00192 case PortListElement::eElementTypePort:
00193 {
00194 connectElementToNet(
00195 inNet, (*it).getPort(), count);
00196 break;
00197 }
00198 case PortListElement::eElementTypePortReference:
00199 {
00200 connectElementToNet(
00201 inNet, (*it).getPortReference(), count);
00202 break;
00203 }
00204 }
00205 }
00206 catch( Error &e )
00207 {
00208 e.setCurrentLocation(
00209 __FUNCTION__, __FILE__, __LINE__ );
00210 throw;
00211 }
00212 }
00213 Connectable::Connection newConnection;
00214 try
00215 {
00216 ConnectionHandler handler( inNet );
00217 handler.connectPortListToNet( getSharedThis() );
00218 newConnection = Connectable::connect( inNet );
00219 }
00220 catch( Error &e )
00221 {
00222 e.setCurrentLocation(
00223 __FUNCTION__, __FILE__, __LINE__ );
00224 throw;
00225 }
00226 return newConnection;
00227 }
00228
00229 void
00230 PortList::disconnect(
00231 const Connectable::Connection & inConnection) throw(Error) {
00232 NetSharedPtr inNet = *inConnection;
00233
00234
00235
00236 try
00237 {
00238 ConnectionHandler handler( inNet );
00239 handler.disconnectPortListFromNet( getSharedThis() );
00240 }
00241 catch( Error &e )
00242 {
00243 e.setCurrentLocation(
00244 __FUNCTION__, __FILE__, __LINE__ );
00245 throw;
00246 }
00247 }
00248
00249 size_t
00250 PortList::getSize() const throw() {
00251 size_t size = 0;
00252 for( std::list< PortListElement >::const_iterator it
00253 = mElements.begin(); it != mElements.end(); ++ it )
00254 {
00255 switch( (*it).getType() )
00256 {
00257 case PortListElement::eElementTypePort:
00258 {
00259 size += (*it).getPort()->getSize();
00260 break;
00261 }
00262 case PortListElement::eElementTypePortReference:
00263 {
00264 size += (*it).getPortReference()->getSize();
00265 break;
00266 }
00267 }
00268 }
00269 return size;
00270 }
00271
00272 void
00273 PortList::addChildPort( const PortSharedPtr &inPort ) throw() {
00274 mElements.push_back( PortListElement( inPort ) );
00275 }
00276
00277 void
00278 PortList::addChildPortReference(
00279 const PortReferenceSharedPtr &inPortRef ) throw() {
00280 mElements.push_back( PortListElement( inPortRef ) );
00281 }
00282
00283 void
00284 PortList::getChildren(
00285 std::list< PortListElement > &outElements ) throw() {
00286 outElements.insert( outElements.end(),
00287 mElements.begin(), mElements.end());
00288 }
00289
00290
00291 PortList::PortList()
00292 : Connectable(),
00293 mElements() {
00294 }
00295
00296 PortList::~PortList() throw() {
00297 mElements.clear();
00298 }
00299
00300 #ifdef GENOM_SERIALIZATION
00301
00302 template<class Archive> void
00303 PortList::PortListElement::serialize(
00304 Archive &ar, unsigned int ) {
00305 ar & mType;
00306 ar & mPort;
00307 ar & mPortReference;
00308 }
00309
00310 template<class Archive> void
00311 PortList::serialize( Archive &ar, unsigned int ) {
00312 ar & boost::serialization::base_object<Connectable>(
00313 *this );
00314 ar & boost::serialization::base_object<
00315 SelfReferencing<PortList> >( *this );
00316 ar & mElements;
00317 }
00318
00319
00320 template void
00321 PortList::serialize<boost::archive::binary_iarchive>(
00322 boost::archive::binary_iarchive & ar, const unsigned int);
00323
00324 template void
00325 PortList::serialize<boost::archive::binary_oarchive>(
00326 boost::archive::binary_oarchive & ar, const unsigned int);
00327
00328 #endif //GENOM_SERIALIZATION
00329
00330
00331 }
00332
00333 }