OMKIAttribute.cpp

Go to the documentation of this file.
00001 
00002 /************************************************************************/
00003 /* This file is part of openMask(c) INRIA, CNRS, Universite de Rennes 1 */
00004 /* 1993-2002, thereinafter the Software                                 */
00005 /*                                                                      */
00006 /* The Software has been developped within the Siames Project.          */
00007 /* INRIA, the University of Rennes 1 and CNRS jointly hold intellectual */
00008 /* property rights                                                      */
00009 /*                                                                      */
00010 /* The Software has been registered with the Agence pour la Protection  */
00011 /* des Programmes (APP) under registration number                       */
00012 /* IDDN.FR.001.510008.00.S.P.2001.000.41200                             */
00013 /*                                                                      */
00014 /* This file may be distributed under the terms of the Q Public License */
00015 /* version 1.0 as defined by Trolltech AS of Norway and appearing in    */
00016 /* the file LICENSE.QPL included in the packaging of this file.         */
00017 /*                                                                      */
00018 /* Licensees holding valid specific licenses issued by INRIA, CNRS or   */
00019 /* Universite Rennes 1 for the software may use this file in            */
00020 /* acordance with that specific license                                 */
00021 /************************************************************************/
00022                 
00023 #include "OMKIAttribute.h"
00024 #include "OMKExtensibleSimulatedObject.h"
00025 #include "OMKInputNT.h"
00026 #include "OMKOutputNT.h"
00027 #include "OMKEventListenerCallBack.h"
00028 #include "OMKParametersAccessor.inl"
00029 #include "OMKSimpleTypeT.inl"
00030 
00031 
00032 namespace OMK 
00033 {
00034 namespace EventId 
00035 {
00036 
00037 //-----------------------------------------------------------------
00038 EventIdentifier CURRENT_ATTRIBUTE_VALUE( "current_attribute_value" ) ;
00039 //-----------------------------------------------------------------
00040 EventIdentifier getCurrentValueEventId( const std::string& id ) 
00041 { 
00042   return EventIdentifier( ( "current_value_of_attribute_" + id ).c_str() ) ; 
00043 }
00044 //-----------------------------------------------------------------
00045 EventIdentifier getGetValueEventId( const std::string& id ) 
00046 { 
00047   return EventIdentifier( ( "get_value_of_attribute_" + id ).c_str() ) ; 
00048 }
00049 //-----------------------------------------------------------------
00050 EventIdentifier getConnectToEventId( const std::string& id ) 
00051 { 
00052   return EventIdentifier( ( "connect_to_attribute_" + id ).c_str() ) ; 
00053 }
00054 //-----------------------------------------------------------------
00055 EventIdentifier getDisconnectEventId( const std::string& id ) 
00056 { 
00057   return EventIdentifier( ( "disconnect_attribute_" + id ).c_str() ) ; 
00058 }
00059 //-----------------------------------------------------------------
00060 
00061 } // namespace EventId
00062 } // namespace OMK
00063 
00064 using namespace OMK ;
00065 //=================================================================
00066 
00067 //-----------------------------------------------------------------
00068 IAttribute::IAttribute( const Name& id ) 
00069 : _owner( 0 ),
00070   _id( id ),
00071   _updated( false ),
00072   _connectToListener     ( 0 ),
00073   _disconnectListener    ( 0 ),
00074   _connectToId     ( EventId::getConnectToEventId   ( id.getCString() ) ),
00075   _disconnectId    ( EventId::getDisconnectEventId  ( id.getCString() ) ),
00076   _getValueListener      ( 0 ),
00077   _currentValueListener  ( 0 ),
00078   _currentValueOfListener( 0 ),
00079   _getValueId      ( EventId::getGetValueEventId    ( id.getCString() ) ),
00080   _currentValueOfId( EventId::getCurrentValueEventId( id.getCString() ) )
00081 {
00082 }
00083 
00084 //-----------------------------------------------------------------
00085 IAttribute::~IAttribute() 
00086 {
00087   // Delete the event listeners
00088   delete _connectToListener ;
00089   delete _disconnectListener ;
00090   delete _getValueListener ;
00091   delete _currentValueListener ;
00092   delete _currentValueOfListener ;
00093   if( _owner ) _owner->removeAttribute( getId() ) ;
00094 }
00095 
00096 //-----------------------------------------------------------------
00097 const ExtensibleSimulatedObject* const
00098 IAttribute::getOwner() const
00099 {
00100         return _owner ;
00101 }
00102 
00103 //-----------------------------------------------------------------
00104 void IAttribute::setOwner( ExtensibleSimulatedObject* owner, bool withOutput ) 
00105 {
00106   // Set the owner
00107   OMASSERTM( !_owner, "cannot be intialised twice !" ) ;
00108   _owner = owner ;
00109   OMASSERTM( _owner, "must be valid !" ) ;
00110 
00111   // Look for the creation of the associated output
00112   bool createOutputCfg = false ;
00113   std::string strId( getId().getCString() ) ;
00114   ParametersAccessor::get( _owner->getConfigurationParameters(), strId + "Output" , createOutputCfg ) ;
00115 
00116   if( withOutput || createOutputCfg )
00117   {
00118     createOutput() ;
00119   }
00120 }
00121 //-----------------------------------------------------------------
00122 bool IAttribute::loadParameters( const ConfigurationParameterDescriptor * node, 
00123                                  const std::string* aliasName ) 
00124 {
00125   std::string strId( aliasName ? *aliasName : getId().getCString() ) ;
00126   // Read parameters to connect the input, ignores if cannot
00127   // to get the parameters
00128   std::pair< std::string, std::string > connectParam; // first = target name / second = output name
00129   if( ParametersAccessor::get( node, strId + "Connect", connectParam ) )
00130   {
00131     connectTo( connectParam.first, connectParam.second ) ;
00132   }
00133   // Event activation
00134   bool activate = false ;
00135   std::vector< Name > emitters ;
00136 
00137   activate = false ;
00138   ParametersAccessor::get( node, strId + "ConnectByEvent", activate ) ;
00139   if( activate ) activateConnectByEvent()  ;
00140   activate = false ;
00141   emitters.clear() ;
00142   if( ParametersAccessor::get( node, strId + "ConnectBySignal", activate )
00143    && activate ) 
00144   {
00145     activateConnectBySignal()  ;
00146   }
00147   else if( ParametersAccessor::get( node, strId + "ConnectBySignal", emitters ) )
00148   {
00149     for( size_t i = 0 ; i < emitters.size() ; i++ )
00150     {
00151       activateConnectBySignal( emitters[ i ] ) ;
00152     }
00153   }
00154 
00155   activate = false ;
00156   ParametersAccessor::get( node, strId + "GetByEvent", activate ) ;
00157   if( activate ) activateGetByEvent() ;
00158   activate = false ;
00159   emitters.clear() ;
00160   if( ParametersAccessor::get( node, strId + "GetBySignal", activate ) 
00161    && activate ) 
00162   {
00163     activateGetBySignal() ;
00164   }
00165   else if( ParametersAccessor::get( node, strId + "GetBySignal", emitters ) )
00166   {
00167     for( size_t i = 0 ; i < emitters.size() ; i++ )
00168     {
00169       activateGetBySignal( emitters[ i ] ) ;
00170     }
00171   }
00172 
00173 
00174   activate = false ;
00175   ParametersAccessor::get( node, strId + "SetByEvent", activate ) ;
00176   if( activate ) activateSetByEvent() ;
00177   activate = false ;
00178   emitters.clear() ;
00179   if( ParametersAccessor::get( node, strId + "SetBySignal", activate ) 
00180    && activate ) 
00181   {
00182     activateSetBySignal() ;
00183   }
00184   else if( ParametersAccessor::get( node, strId + "SetBySignal", emitters ) )
00185   {
00186     for( size_t i = 0 ; i < emitters.size() ; i++ )
00187     {
00188       activateSetBySignal( emitters[ i ] ) ;
00189     }
00190   }
00191 
00192   activate = false ;
00193   ParametersAccessor::get( node, strId + "SetByEventOf", activate ) ;
00194   if( activate ) activateSetByEventOf() ;
00195   activate = false ;
00196   emitters.clear() ;
00197   if( ParametersAccessor::get( node, strId + "SetBySignalOf", activate )
00198    && activate ) 
00199   {
00200     activateSetBySignalOf() ;
00201   }
00202   else if( ParametersAccessor::get( node, strId + "SetBySignalOf", emitters ) )
00203   {
00204     for( size_t i = 0 ; i < emitters.size() ; i++ )
00205     {
00206       activateSetBySignalOf( emitters[ i ] ) ;
00207     }
00208   }
00209 
00210   return true ;
00211 }
00212 
00213 //-----------------------------------------------------------------
00214 void IAttribute::activateConnectByEvent() 
00215 {
00216   if( !_connectToListener )
00217   {
00218     _connectToListener = new ConnectToEventListener
00219       ( *_owner, this, 
00220         (ConnectToEventListener::CallBackFct)&IAttribute::processConnectTo, 
00221         _connectToId ) ;
00222     _disconnectListener = new EventListenerCallBack< IAttribute >
00223       ( *_owner, this, 
00224         (EventListenerCallBack< IAttribute >::CallBackFct)&IAttribute::processDisconnect, 
00225         _disconnectId ) ;
00226     OMTRACEID( OMK_DEBUG_OMK_ATTR, 
00227       "Information for " << OMK::debugMsg( this ) << std::endl
00228       << ">>> :-) Connection by event is activated!" ) ;
00229   }
00230 }
00231 //-----------------------------------------------------------------
00232 void IAttribute::activateConnectBySignal( const Name& emitter ) 
00233 {
00234   activateConnectByEvent() ;
00235   if( emitter.undefined() )
00236   {
00237     _owner->registerForSignal( _connectToId ) ;
00238     _owner->registerForSignal( _disconnectId ) ;
00239   }
00240   else
00241   {
00242     _owner->registerForSignalBy( _connectToId, emitter ) ;
00243     _owner->registerForSignalBy( _disconnectId, emitter ) ;
00244   }
00245 }
00246 
00247 //-----------------------------------------------------------------
00248 void IAttribute::activateGetByEvent() 
00249 {
00250   if( !_getValueListener )
00251   {
00252     _getValueListener = new EventListenerCallBack< IAttribute >
00253       ( *_owner, this, 
00254         (EventListenerCallBack< IAttribute >::CallBackFct)&IAttribute::processGetValue, 
00255         _getValueId ) ;
00256     OMTRACEID( OMK_DEBUG_OMK_ATTR, 
00257       "Information for " << OMK::debugMsg( this ) << std::endl
00258       << ">>> :-) Get by event is activated!" ) ;
00259   }
00260 }
00261 //-----------------------------------------------------------------
00262 void IAttribute::activateGetBySignal( const Name& emitter ) 
00263 {
00264   activateGetByEvent() ;
00265   if( emitter.undefined() )
00266   {
00267     _owner->registerForSignal( _getValueId ) ;
00268   }
00269   else
00270   {
00271     _owner->registerForSignalBy( _getValueId, emitter ) ;
00272   }
00273 }
00274 
00275 //-----------------------------------------------------------------
00276 bool IAttribute::connectTo( const Name &objectName, const Name &outputName ) 
00277 {
00278   InputNT *associatedInput = getInput() ;
00279   if( !associatedInput )
00280   { // No input defined => create it and try again
00281     createInput() ;
00282     associatedInput = getInput() ;
00283   }
00284   bool ok = associatedInput && associatedInput->connect( objectName, outputName ) ;
00285   if( !ok )
00286   {
00287     // No associated input or unable to connect
00288     OMTRACEID( OMK_DEBUG_OMK_ATTR, 
00289                "Error for " << OMK::debugMsg( this ) << std::endl
00290                    << ">>> :-( Unable to connect the attribute to the output \"" << outputName.getString() 
00291                    << "\" of the object \"" << objectName.getString() << "\" !" ) ;
00292   }
00293   else
00294   {
00295     // No associated input or unable to connect
00296     OMTRACEID( OMK_DEBUG_OMK_ATTR, 
00297                "Information for " << OMK::debugMsg( this ) << std::endl
00298                    << ">>> :-) Connect the attribute to the output \"" << outputName.getString() 
00299                    << "\" of the object \"" << objectName.getString() << "\" !" ) ;
00300   }
00301   return ok ;
00302 }
00303 
00304 //-----------------------------------------------------------------
00305 void IAttribute::disconnect() 
00306 {
00307   InputNT *associatedInput = getInput() ;
00308   if( associatedInput ) associatedInput->disconnect() ;
00309 }
00310 
00311 //-----------------------------------------------------------------
00312 bool IAttribute::processConnectTo( ConnectToEvent* e ) 
00313 {
00314   OMASSERTM( _connectToId == e->eventId, "Not the good event id !" ) ;
00315   return connectTo( e->value.getValue().first, e->value.getValue().second ) ;
00316 }
00317 
00318 //-----------------------------------------------------------------
00319 bool IAttribute::processDisconnect( Event* e ) 
00320 {
00321   disconnect() ;
00322   return true ;
00323 }
00324 
00325 //-----------------------------------------------------------------
00326 void IAttribute::activateSetBySignal( const Name& emitter ) 
00327 {
00328   activateSetByEvent() ;
00329   if( emitter.undefined() )
00330   {
00331     _owner->registerForSignal( EventId::CURRENT_ATTRIBUTE_VALUE ) ;
00332   }
00333   else
00334   {
00335     _owner->registerForSignalBy( EventId::CURRENT_ATTRIBUTE_VALUE, emitter ) ;
00336   }
00337 }
00338 //-----------------------------------------------------------------
00339 void IAttribute::activateSetBySignalOf( const Name& emitter ) 
00340 {
00341   activateSetByEventOf() ;
00342   if( emitter.undefined() )
00343   {
00344     _owner->registerForSignal( _currentValueOfId ) ;
00345   }
00346   else
00347   {
00348     _owner->registerForSignalBy( _currentValueOfId, emitter ) ;
00349   }
00350 }
00351 //-----------------------------------------------------------------
00352 bool IAttribute::processGetValue( Event* e ) 
00353 {
00354   ValuedEvent< Name >* eventName = dynamic_cast< ValuedEvent< Name >* >( e ) ;
00355   if( eventName )
00356   {
00357     sendValue( e->sender, EventId::getCurrentValueEventId( eventName->value.getString() ) ) ;
00358   }
00359   else
00360   {
00361     sendValue( e->sender, _currentValueOfId ) ;
00362   }
00363   return true ;
00364 }
00365 //-----------------------------------------------------------------
00366 void IAttribute::sendValueTo( const Name &receiver, const Name &attributeId, bool genericId )
00367 {
00368   if( genericId )
00369   {
00370     sendIdAndValue( receiver, EventId::CURRENT_ATTRIBUTE_VALUE, attributeId ) ;
00371   }
00372   else
00373   {
00374     sendValue( receiver, EventId::getCurrentValueEventId( attributeId.getString() ) ) ;
00375   }
00376 }
00377 //-----------------------------------------------------------------
00378 void IAttribute::sendValueOf( const Name &receiver, bool genericId ) 
00379 {
00380   if( genericId )
00381   {
00382     sendIdAndValue( receiver, EventId::CURRENT_ATTRIBUTE_VALUE, getId() ) ;
00383   }
00384   else
00385   {
00386     sendValue( receiver, _currentValueOfId ) ;
00387   }
00388 }
00389 //-----------------------------------------------------------------
00390 void IAttribute::fireValueTo( const Name &attributeId, bool genericId )
00391 {
00392   if( genericId )
00393   {
00394     fireIdAndValue( EventId::CURRENT_ATTRIBUTE_VALUE, attributeId ) ;
00395   }
00396   else
00397   {
00398     fireValue(  EventId::getCurrentValueEventId( attributeId.getString() ) ) ;
00399   }
00400 }
00401 //-----------------------------------------------------------------
00402 void IAttribute::fireValueOf( bool genericId )
00403 {
00404   if( genericId )
00405   {
00406     fireIdAndValue( EventId::CURRENT_ATTRIBUTE_VALUE, getId() ) ;
00407   }
00408   else
00409   {
00410     fireValue( _currentValueOfId ) ;
00411   }
00412 }
00413 //-----------------------------------------------------------------
00414 

logo OpenMask

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

Generated with doxygen by Dimitri van Heesch ,   1997-2007