OMKSimulatedObject.inl

Go to the documentation of this file.
00001 #ifndef OMKSimulatedObjectINL
00002 #define OMKSimulatedObjectINL
00003 
00004 #include "OMKController.h"
00005 #include "OMKValuedEvent.h"
00006 #include "OMKObjectHandle.h"
00007 #include "OMKUnallowedOverloadingException.h"
00008 
00009 //  Implementation of template member functions
00010 
00011 namespace OMK
00012 {
00013 //-----------------------------------------------------------------------------
00014 template < typename Type >
00015 std::list< const ObjectDescriptor * > * SimulatedObject::listSonsOfType()
00016 {
00017   return getObjectDescriptor().template listSonsOfType< Type >() ;
00018 }
00019 //-----------------------------------------------------------------------------
00020 template < typename Type >
00021 std::list< const ObjectDescriptor * > * SimulatedObject::listBrothersOfType()
00022 {
00023    OMASSERT( getObjectDescriptor().getFathersObjectDescriptor() != 0 ) ;
00024    return getObjectDescriptor().
00025              getFathersObjectDescriptor()->template listSonsOfType< Type >() ;
00026 }
00027 //-----------------------------------------------------------------------------
00028 template < typename Type >
00029 std::list< const ObjectDescriptor * > * SimulatedObject::listDescendantsOfType()
00030 {
00031   return getObjectDescriptor().template listDescendantsOfType< Type >();
00032 }
00033 //-----------------------------------------------------------------------------
00034 template < typename UserType >
00035 void SimulatedObject::fireValuedSignal( const EventIdentifier &sig,
00036                                         const UserType &value )
00037 {
00038   ValuedEvent< UserType > event( sig, getSimulatedDate(), getName(),
00039                                  getName(), value) ;
00040   if( getObjectHandle() != 0 )
00041   {
00042     getObjectHandle()->sendEventsForSignal( event , sig ) ;
00043   }
00044   else
00045   {
00046     getController().broadcastEventsForSignal( event, sig ) ;
00047   }
00048 }
00049 //-----------------------------------------------------------------------------
00050 template < typename Type >
00051 Input< Type > & SimulatedObject::addInput( const Name &inputName,
00052                                            bool makeConnectable,
00053                                            int precisionLevel ) 
00054 {
00055   InputNT *redefinedInput( _inputTable.getObjectOfIndex( inputName ) ) ;
00056   Input< Type > * input( 0 ) ;
00057   if( redefinedInput == 0 )
00058   {
00059     input = _controller.template createInput< Type >( inputName, *this,
00060                                                       makeConnectable,
00061                                                       precisionLevel ) ;
00062   }
00063   else 
00064   {
00065     input = dynamic_cast< Input< Type > * >( redefinedInput ) ;
00066     if( input == 0 ) 
00067     {
00068       throw UnallowedOverloadingException( inputName, getName() ) ;
00069     }
00070     else 
00071     {
00072       OMTRACEID( OMK_DEBUG_OMK_EXEC, "input \"" << inputName
00073                  << "\" of " << OMK::debugMsg( this )
00074                  <<" already defined. No new input created : reference to old one used" ) ;
00075     }
00076   }
00077   return *input ;
00078 }
00079 //-----------------------------------------------------------------------------
00080 template < typename Type >
00081 SensitiveInput<Type> & SimulatedObject::addSensitiveInput( const Name &inputName,
00082                                                            bool makeConnectable,
00083                                                            int precisionLevel ) 
00084 {
00085   InputNT *redefinedInput( _inputTable.getObjectOfIndex( inputName ) ) ;
00086   SensitiveInput< Type > *input( 0 ) ;
00087   if( redefinedInput == 0 )
00088   {
00089     input = _controller.template createSensitiveInput< Type >( inputName,
00090                                                                *this,
00091                                                                makeConnectable,
00092                                                                precisionLevel );
00093   }
00094   else
00095   {
00096     input = dynamic_cast< SensitiveInput< Type > * >( redefinedInput ) ;
00097     if( input == 0 )
00098     {
00099       throw UnallowedOverloadingException( inputName, getName() ) ;
00100     }
00101     else 
00102     {
00103       OMTRACEID( OMK_DEBUG_OMK_EXEC, "sensitiveInput \"" << inputName
00104                  << "\" of " << OMK::debugMsg( this )
00105                  <<" already defined. No new sensitiveInput created : reference to old one used" ) ;
00106     }
00107   }
00108   return *input ;
00109 }
00110 //-----------------------------------------------------------------------------
00111 template < typename Type >
00112 SensitiveNotifyingInput< Type > & SimulatedObject::addSensitiveNotifyingInput(
00113   const Name &inputName,
00114   bool makeConnectable,
00115   int precisionLevel ) 
00116 {
00117   InputNT *redefinedInput( _inputTable.getObjectOfIndex( inputName ) ) ;
00118   SensitiveNotifyingInput< Type > *input( 0 ) ;
00119   if( redefinedInput == 0 ) 
00120   {
00121     input = _controller.template createSensitiveNotifyingInput< Type >(
00122       inputName, *this, makeConnectable, precisionLevel ) ;
00123   }
00124   else 
00125   {
00126     input = dynamic_cast< SensitiveNotifyingInput< Type > * >( redefinedInput ) ;
00127     if( input == 0 )
00128     {
00129       throw UnallowedOverloadingException( inputName, getName() ) ;
00130     }
00131     else 
00132     {
00133       OMTRACEID( OMK_DEBUG_OMK_EXEC, "sensitiveNotifyingInput \"" << inputName
00134                  << "\" of " << OMK::debugMsg( this ) 
00135                  << " already defined. No new sensitiveNotifyingInput created : reference to old one used" ) ;
00136     }
00137   }
00138   return *input ; 
00139 }
00140 //-----------------------------------------------------------------------------
00141 template < typename Type >
00142 Output< Type > & SimulatedObject::addOutput( const Name &outputName,
00143                                              OMK::Type::PolatorNT *polator )
00144 {
00145   OutputNT *redefinedOutput( _outputTable.getObjectOfIndex( outputName ) ) ;
00146   Output< Type > *output( 0 );
00147   if( redefinedOutput == 0 )
00148   {
00149     output = _controller.template createOutput< Type >( outputName, *this,
00150                                                         polator ) ;
00151   }
00152   else 
00153   {
00154     output = dynamic_cast< Output< Type > * >( redefinedOutput ) ;
00155     if( output == 0 )
00156     {
00157       throw UnallowedOverloadingException( outputName, getName() ) ;
00158     }
00159     else 
00160     {
00161       OMTRACEID( OMK_DEBUG_OMK_EXEC, "output \"" << outputName << "\" of "
00162                  << OMK::debugMsg( this )
00163                  << " already defined. No new output created : reference to old one used" ) ;
00164     }
00165   } 
00166   _outputTable.addObjectWithIndex( outputName, output ) ;
00167   return *output ;
00168 }
00169 //-----------------------------------------------------------------------------
00170 template < typename Type >
00171 ControlParameter< Type > & SimulatedObject::addControlParameter(
00172   const Name &controlParameterName,
00173   OMK::Type::PolatorNT *polator )
00174 {
00175   OutputNT *redefinedControlParameter( _controlParameterTable.getObjectOfIndex(
00176                                          controlParameterName ) ) ;
00177   ControlParameter< Type > *controlParameter( 0 ) ;
00178   if( redefinedControlParameter == 0 )
00179   {
00180     controlParameter = _controller.template createControlParameter< Type >(
00181       controlParameterName, *this, polator ) ;
00182   }
00183   else 
00184   {
00185     controlParameter = dynamic_cast < ControlParameter<Type> * >(
00186       redefinedControlParameter ) ;
00187     if( controlParameter == 0 )
00188     {
00189       throw UnallowedOverloadingException( controlParameterName, getName() ) ;
00190     }
00191     else 
00192     {
00193       OMTRACEID( OMK_DEBUG_OMK_EXEC, "controlParameter \"" <<
00194                  controlParameterName << "\" of " << OMK::debugMsg( this )
00195                  <<" already defined. No new controlParameter created : reference to old one used" ) ;
00196     }
00197   }
00198   OutputNT *realControlParameter( dynamic_cast< OutputNT * >( controlParameter ) ) ;
00199   OMASSERT( realControlParameter != 0 ) ; 
00200   _controlParameterTable.addObjectWithIndex( controlParameterName,
00201                                              realControlParameter ) ;
00202   return *controlParameter ;
00203 }
00204 //-----------------------------------------------------------------------------
00205 template < typename Type >
00206 InputAlias< Type > & SimulatedObject::addInputAlias( const Name &aliasName,
00207                                                      const Name &aliasedObjectName,
00208                                                      const Name &aliasedInputName,
00209                                                      int precisionLevel )
00210 {
00211   InputNT *redefinedInput( _inputTable.getObjectOfIndex( aliasName ) ) ;
00212   InputAlias< Type > *alias( 0 ) ;
00213   if( redefinedInput == 0 )
00214   {
00215     // make sure aliasedObjectName is an ancestor of object
00216     if( isAncestor( aliasedObjectName ) )
00217     {
00218       SimulatedObject *obj( _controller.getPointerToSimulatedObjectNamed(
00219                               aliasedObjectName ) ) ;
00220       InputNT *input( obj->_inputTable.getObjectOfIndex( aliasedInputName ) ) ;
00221       AbstractInput< Type > *realInput( dynamic_cast< AbstractInput< Type > * >(
00222                                           input ) ) ;
00223       if( realInput != 0 )
00224       {
00225         if( obj != 0 )
00226         {
00227           alias = _controller.template createInputAlias< Type >( aliasName,
00228                                                                  *this,
00229                                                                  realInput,
00230                                                                  precisionLevel ) ;
00231         }
00232         else
00233         {
00234           Controller::error( "imposible aliasing " ) ;
00235         }
00236       }
00237       else
00238       {
00239         Controller::error( "impossible aliasing, probably because of type mismatch" ) ;
00240       }
00241     }
00242     else
00243     {
00244       OMERROR( "SimulatedObject::createInputAlias \"" << aliasedObjectName
00245                << "\" isn't the father of " 
00246                << OMK::debugMsg( this )
00247                << " : aliasing is therefore impossible for alias named \""
00248                << aliasedInputName << "\"" ) ;
00249       Controller::error( "" ) ;
00250     }
00251   }
00252   else 
00253   {
00254     alias = dynamic_cast< InputAlias< Type > * >( redefinedInput ) ;
00255     if( alias == 0 )
00256     {
00257       throw UnallowedOverloadingException( aliasName, getName() ) ;
00258     }
00259     else
00260     {
00261       OMTRACEID( OMK_DEBUG_OMK_EXEC, "inputAlias \"" << aliasName
00262                  << "\" of " << OMK::debugMsg( this )
00263                  <<" already defined. No new inputAlias created : reference to old one used" ) ;
00264     }
00265   }
00266   return *alias ;
00267 }
00268 //-----------------------------------------------------------------------------
00269 template < typename Type >
00270 SensitiveInputAlias< Type > & SimulatedObject::addSensitiveInputAlias(
00271   const Name &aliasName,
00272   const Name &aliasedObjectName,
00273   const Name &aliasedInputName,
00274   int precisionLevel )
00275 {
00276   InputNT *redefinedInput( _inputTable.getObjectOfIndex( aliasName ) ) ;
00277   SensitiveInputAlias< Type > *alias( 0 ) ;
00278   if( redefinedInput == 0 )
00279   {
00280     // make sure aliasedObjectName is an ancestor of object
00281     if( isAncestor( aliasedObjectName ) )
00282     {
00283       SimulatedObject *obj( _controller.getPointerToSimulatedObjectNamed(
00284                               aliasedObjectName ) ) ;
00285       InputNT * input( obj->_inputTable.getObjectOfIndex( aliasedInputName ) ) ;
00286       AbstractInput< Type > *realInput( dynamic_cast< AbstractInput< Type > * >(
00287                                           input ) ) ;
00288       if( realInput != 0 )
00289       {
00290         if( obj != 0 )
00291         {
00292           alias = _controller.template createSensitiveInputAlias< Type >(
00293             aliasName, *this, realInput, precisionLevel ) ;
00294         }
00295         else
00296         {
00297           Controller::error( "Impossible aliasing " ) ;
00298         }
00299       }
00300       else
00301       {
00302         Controller::error( "impossible aliasing, probably because of type mismatch" ) ;
00303       }
00304     }
00305     else
00306     {
00307       OMERROR( "SimulatedObject::addSensitiveInputAlias \n" << aliasedObjectName
00308                << "\n isn't the father of " << OMK::debugMsg( this )
00309                << " : cannot create an alias of \""
00310                << aliasedInputName << "\"" ) ;
00311       throw UserException( "" ) ;
00312     }
00313   }
00314   else 
00315   {
00316     alias = dynamic_cast< SensitiveInputAlias< Type > * >( redefinedInput ) ;
00317     if( alias == 0 )
00318     {
00319       throw UnallowedOverloadingException( aliasName, getName() ) ;
00320     }
00321     else
00322     {
00323       OMTRACEID( OMK_DEBUG_OMK_EXEC, "sensitiveInputAlias \"" << aliasName
00324                  << "\" of " << OMK::debugMsg( this )
00325                  <<" already defined. No new sensitiveInputAlias created : reference to old one used" ) ;
00326     }
00327   }
00328   return *alias ;
00329 }
00330 //-----------------------------------------------------------------------------
00331 template < typename Type > 
00332 SensitiveNotifyingInputAlias< Type > & 
00333 SimulatedObject::addSensitiveNotifyingInputAlias( const Name &aliasName,
00334                                                   const Name &aliasedObjectName,
00335                                                   const Name &aliasedInputName,
00336                                                   int precisionLevel )
00337 {
00338   InputNT *redefinedInput( _inputTable.getObjectOfIndex( aliasName ) ) ;
00339   SensitiveNotifyingInputAlias< Type > *alias( 0 ) ;
00340   if( redefinedInput == 0 )
00341   {
00342     // make sure aliasedObjectName is an ancestor of object
00343     if( isAncestor( aliasedObjectName ) )
00344     {
00345       SimulatedObject *obj( _controller.getPointerToSimulatedObjectNamed(
00346                               aliasedObjectName ) ) ;
00347       InputNT *input( obj->_inputTable.getObjectOfIndex( aliasedInputName ) ) ;
00348       AbstractInput< Type > *realInput( dynamic_cast < AbstractInput< Type > * >(
00349                                           input ) ) ;
00350       if( realInput != 0 )
00351       {
00352         if( obj != 0 )
00353         {
00354           alias =
00355             _controller.template createSensitiveNotifyingInputAlias< Type >(
00356               aliasName, *this, realInput, precisionLevel ) ;
00357         }
00358         else
00359         {
00360           throw UserException( "Impossible aliasing " ) ;
00361         }
00362       }
00363       else
00364       {
00365         Controller::error( "alias type mismatch" ) ;
00366       }
00367     }
00368     else
00369     {
00370       OMERROR( "SimulatedObject::addSensitiveSeignalingInputAlias \""
00371                << aliasedObjectName << "\" isn' the father of "
00372                << OMK::debugMsg( this )
00373                << " : creation of alias named \"" << aliasedInputName
00374                << "\" impossible" ) ;
00375       throw UserException( "" );
00376     }
00377   }
00378   else
00379   {
00380     alias = dynamic_cast< SensitiveNotifyingInputAlias< Type > * >(
00381       redefinedInput ) ;
00382     if( alias == 0 )
00383     {
00384       throw UnallowedOverloadingException( aliasName, getName() ) ;
00385     }
00386     else
00387     {
00388       OMTRACEID( OMK_DEBUG_OMK_EXEC, "sensitiveNotifyingInputAlias \""
00389                  << aliasName << " of " << OMK::debugMsg( this )
00390                  <<" already defined. No new sensitiveNotifyingInputAlias : reference to old one used" ) ;
00391     }
00392   }
00393   return *alias ;
00394 }
00395 //-----------------------------------------------------------------------------
00396 template < typename Type > 
00397 OutputAlias< Type > & SimulatedObject::addOutputAlias(
00398   const Name &aliasName,
00399   const Name &aliasedObjectName,
00400   const Name &aliasedOutputName,
00401   OMK::Type::PolatorNT *interp )
00402 {
00403   OutputNT *redefinedOutput( _outputTable.getObjectOfIndex( aliasName ) ) ;
00404   OutputAlias< Type > *alias( 0 ) ;
00405   std::list< const ObjectDescriptor * > *listeFils(
00406     getController().template listDescendantsOfType< SimulatedObject >(
00407       aliasedObjectName ) ) ;
00408   bool aliasedObjectPresent( false ) ;
00409    
00410   if( redefinedOutput == 0 ) 
00411   {
00412     // make sure aliasedObjectName is an ancestor of object
00413     if( listeFils != 0 )
00414     {
00415       std::list< const ObjectDescriptor * >::const_iterator listeFilsIterator(
00416         listeFils->begin() ) ;
00417       while( listeFilsIterator != listeFils->end() && !aliasedObjectPresent )
00418       {
00419         aliasedObjectPresent = ( (*listeFilsIterator)->getName() == getName() ) ;
00420         ++listeFilsIterator ;
00421       }       
00422       delete listeFils ;
00423     }
00424     if( aliasedObjectPresent )
00425     {
00426       SimulatedObject *obj( _controller.getPointerToSimulatedObjectNamed(
00427                               aliasedObjectName ) ) ;
00428       OutputNT *Outputnt( obj->getPointerToOutputNamed( aliasedOutputName ) ) ;
00429       Output< Type > *refOutput( dynamic_cast< Output< Type > * >( Outputnt ) ) ;
00430       if( refOutput != 0 )
00431       {
00432         if( obj != 0 ) 
00433         {
00434           alias = _controller.template createOutputAlias< Type >(
00435             aliasName, *this, refOutput, interp ) ;
00436         }
00437         else
00438         {
00439           OMERROR( "SimulatedObject::addOutputAlias " << std::endl 
00440                    << "alias \"" << aliasName
00441                    << "\" cannot be made on inexistant \""
00442                    << aliasedOutputName << "\"" ) ;
00443           throw UserException( "" ) ;
00444         }
00445       }
00446       else
00447       {
00448         throw UserException( "Impossible output aliasing : type mismatch" ) ;
00449       }
00450     }
00451     else
00452     {
00453       OMERROR( "SimulatedObject::addOutputAlias \"" << aliasedObjectName
00454                << "\" isn't the father of "  << OMK::debugMsg( this )
00455                << " : not possible to alias \""
00456                <<  aliasedOutputName << "\"" ) ;
00457       Controller::error( "" ) ;
00458     }
00459   }
00460   else
00461   {
00462     alias = dynamic_cast< OutputAlias< Type > * >( redefinedOutput ) ;
00463     if( alias == 0 )
00464     {
00465       throw UnallowedOverloadingException( aliasName, getName() ) ;
00466     }
00467     else
00468     {
00469       OMTRACEID( OMK_DEBUG_OMK_EXEC,
00470                  "outputAlias already defined. No new outputAlias created : reference to old one used" ) ;
00471     }
00472   }
00473   _outputTable.addObjectWithIndex( aliasName, alias ) ;
00474   return *alias ;
00475 }
00476 //-----------------------------------------------------------------------------
00477 template < typename UserType >
00478 void SimulatedObject::sendValuedEvent( const Name &receiver,
00479                                        const EventIdentifier &eventId,
00480                                        const UserType &userInfo )
00481 {
00482   SimulatedObject *receiverObject(
00483     _controller.getPointerToSimulatedObjectNamed( receiver ) ) ;
00484   if( receiverObject == 0 )
00485   {
00486     getController().addToPendingEvents(
00487       new ValuedEvent< UserType >( eventId,
00488                                    _controller.getSimulatedDate(),
00489                                    getName(),
00490                                    receiver,
00491                                    userInfo ) ) ;
00492   }
00493   else
00494   {
00495     sendValuedEvent( receiverObject, eventId, userInfo ) ;
00496   }
00497 }
00498 //-----------------------------------------------------------------------------
00499 template < typename UserType >
00500 void SimulatedObject::sendValuedEvent( SimulatedObject *receiver,
00501                                        const EventIdentifier &eventId,
00502                                        const UserType &userInfo)
00503 {
00504   if( receiver->getObjectHandle() == 0 )
00505   {
00506     getController().addToPendingEvents(
00507       new ValuedEvent< UserType >( eventId,
00508                                    _controller.getSimulatedDate(),
00509                                    getName(),
00510                                    receiver->getName(),
00511                                    userInfo ) ) ;
00512   }
00513   else
00514   {
00515     receiver->getObjectHandle()->receiveEvent(
00516       new ValuedEvent< UserType >( eventId,
00517                                    _controller.getSimulatedDate(),
00518                                    getName(),
00519                                    receiver->getName(),
00520                                    userInfo ) ) ;
00521   }
00522 }
00523 //-----------------------------------------------------------------------------
00524 template < typename UserType >
00525 void SimulatedObject::sendValuedEvent( SimulatedObject &receiver,
00526                                        const EventIdentifier &eventId,
00527                                        const UserType &userInfo )
00528 {
00529   sendValuedEvent( &receiver, eventId, userInfo ) ;
00530 }
00531 //-----------------------------------------------------------------------------
00532 } // namespace OMK
00533 
00534 #endif // OMKSimulatedObjectINL

logo OpenMask

Documentation generated on Mon Jun 9 11:45:57 2008

Generated with doxygen by Dimitri van Heesch ,   1997-2007