00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "torc/generic/om/Permutable.hpp"
00017 #include "torc/generic/om/Port.hpp"
00018
00019 namespace torc {
00020
00021 namespace generic {
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 PermutableSharedPtr
00033 Permutable::Factory::newPermutablePtr( const std::vector< PortSharedPtr > & inPorts,
00034 const ViewSharedPtr & inViewPtr,
00035 const PermutableSharedPtr & inParentPermutable ) throw(Error) {
00036 try
00037 {
00038 PermutableSharedPtr newPermutable;
00039 create( newPermutable );
00040 newPermutable->setPorts( inPorts );
00041 if( inParentPermutable )
00042 {
00043 inParentPermutable->addChildPermutable( newPermutable );
00044 }
00045 else if( inViewPtr )
00046 {
00047 inViewPtr->addPermutable( newPermutable );
00048 }
00049 return newPermutable;
00050 }
00051 catch( Error &e )
00052 {
00053 e.setCurrentLocation(
00054 __FUNCTION__, __FILE__, __LINE__ );
00055 throw;
00056 }
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066 void
00067 Permutable::setPorts(const std::vector< PortSharedPtr > &inSource) throw(Error) {
00068 std::vector< PortSharedPtr >::const_iterator port = inSource.begin();
00069 std::vector< PortSharedPtr >::const_iterator end = inSource.end();
00070 for(;port != end; ++port)
00071 {
00072 try
00073 {
00074 addPort( *port );
00075 }
00076 catch( Error &e )
00077 {
00078 e.setCurrentLocation(
00079 __FUNCTION__, __FILE__, __LINE__ );
00080 throw;
00081 }
00082 }
00083 }
00084
00085
00086
00087
00088
00089
00090
00091
00092 void
00093 Permutable::addPort(
00094 const PortSharedPtr &inPort) throw(Error) {
00095 if( !inPort )
00096 {
00097 return;
00098 }
00099 std::string name = inPort->getName();
00100 if( name.empty() )
00101 {
00102 Error e( eMessageIdErrorEmptyItemName,
00103 __FUNCTION__, __FILE__, __LINE__ );
00104 e.saveContextData("Port name", name);
00105 throw e;
00106 }
00107 mPorts.push_back( inPort );
00108 }
00109
00110
00111
00112
00113
00114
00115
00116 void
00117 Permutable::setChildren(
00118 const std::vector< PermutableSharedPtr > & inSource) throw(Error) {
00119 std::vector< PermutableSharedPtr >::const_iterator entry = inSource.begin();
00120 std::vector< PermutableSharedPtr >::const_iterator end = inSource.end();
00121 for(; entry != end; ++entry )
00122 {
00123 try
00124 {
00125 addChildPermutable( *entry );
00126 }
00127 catch( Error &e )
00128 {
00129 e.setCurrentLocation(
00130 __FUNCTION__, __FILE__, __LINE__ );
00131 throw;
00132 }
00133 }
00134 }
00135
00136
00137
00138
00139
00140
00141
00142 bool
00143 Permutable::addChildPermutable(
00144 const PermutableSharedPtr & inPermutable ) throw(Error) {
00145 if( !inPermutable )
00146 {
00147 Error e( eMessageIdErrorPointerToItemDoesNotExist,
00148 __FUNCTION__, __FILE__, __LINE__ );
00149 e.saveContextData("Pointer to the permutable object does not exist", inPermutable);
00150 throw e;
00151 }
00152 if( inPermutable ) {
00153 mChildren.push_back( inPermutable );
00154 return true;
00155 }
00156 else {
00157 return false;
00158 }
00159 }
00160
00161 void
00162 Permutable::setIsNonPermutable( const bool & value ) throw() {
00163 mIsNonPermutable = value;
00164 }
00165
00166 void
00167 Permutable::accept(BaseVisitor & visitor) throw(Error) {
00168 try
00169 {
00170 runVisitor( *this, visitor );
00171 }
00172 catch( Error &e )
00173 {
00174 e.setCurrentLocation(
00175 __FUNCTION__, __FILE__, __LINE__ );
00176 throw;
00177 }
00178 }
00179
00180
00181
00182
00183
00184 size_t
00185 Permutable::getSize() const throw() {
00186 size_t size = 0;
00187 size_t pSize = 0;
00188 size_t cSize = 0;
00189 Permutable::PermutableType pType = getPermutableType();
00190
00191 std::vector< PermutableSharedPtr > outPermutables;
00192 getChildren( outPermutables );
00193 std::vector< PermutableSharedPtr >::iterator permutableIt
00194 = outPermutables.begin();
00195
00196 std::vector< PortSharedPtr > outPorts;
00197 getPorts( outPorts );
00198 std::vector< PortSharedPtr >::iterator portIt
00199 = outPorts.begin();
00200
00201 if( Permutable::ePermutableParent == pType )
00202 {
00203 if( !outPorts.empty() )
00204 {
00205 pSize = (*portIt)->getSize();
00206 }
00207 else if( !outPermutables.empty() )
00208 {
00209 for( ; permutableIt != outPermutables.end() ; permutableIt ++ )
00210 {
00211 pSize += (*permutableIt)->getSize();
00212 for( ; portIt != outPorts.end(); portIt ++ )
00213 {
00214 pSize += (*portIt)->getSize();
00215 }
00216 }
00217 }
00218 else
00219 {
00220 pSize = 0;
00221 }
00222 }
00223 else
00224 {
00225 if( !outPorts.empty() )
00226 {
00227 for( ; portIt != outPorts.end(); portIt ++ )
00228 {
00229 cSize += (*portIt)->getSize();
00230 }
00231 }
00232 if( !outPermutables.empty() )
00233 {
00234 for( ; permutableIt != outPermutables.end() ; permutableIt ++ )
00235 {
00236 cSize += (*permutableIt)->getSize();
00237 }
00238 }
00239 }
00240 size = pSize + cSize;
00241 return size;
00242 }
00243
00244
00245
00246
00247
00248
00249 void
00250 Permutable::setPermutableType(const PermutableType & inSource) throw() {
00251 mPermutableType = inSource;
00252 }
00253
00254 Permutable::Permutable()
00255 : Visitable(),
00256 SelfReferencing<Permutable>(),
00257 mPorts(),
00258 mChildren(),
00259 mIsNonPermutable( false ),
00260 mPermutableType() {
00261 }
00262
00263 Permutable::~Permutable() throw() {
00264 }
00265
00266 }
00267
00268 }