00001 /* 00002 * This file is part of openMask © INRIA, CNRS, Universite de Rennes 1 1993-2002, thereinafter the Software 00003 * 00004 * The Software has been developped within the Siames Project. 00005 * INRIA, the University of Rennes 1 and CNRS jointly hold intellectual property rights 00006 * 00007 * The Software has been registered with the Agence pour la Protection des 00008 * Programmes (APP) under registration number IDDN.FR.001.510008.00.S.P.2001.000.41200 00009 * 00010 * This file may be distributed under the terms of the Q Public License 00011 * version 1.0 as defined by Trolltech AS of Norway and appearing in the file 00012 * LICENSE.QPL included in the packaging of this file. 00013 * 00014 * Licensees holding valid specific licenses issued by INRIA, CNRS or Université de Rennes 1 00015 * for the software may use this file in accordance with that specific license 00016 * 00017 */ 00018 #ifndef OMKInputHEADER 00019 #define OMKInputHEADER 00020 00021 #include "OMKAbstractInput.h" 00022 #include "OMKSimulatedObject.h" 00023 #include "OMKOutput.h" 00024 #include "OMKUnInitialisedOutputException.h" 00025 #include "OMKTracer.h" 00026 #include "OMKInvalidOutputException.h" 00027 #include "OMKInputConnectionEventListener.h" 00028 00029 namespace OMK 00030 { 00041 template< typename T > 00042 class Input : public AbstractInput< T > 00043 { 00044 public: 00045 using AbstractInput< T >::isConnectable; 00046 using AbstractInput< T >::getConnectionEventId; 00047 using AbstractInput< T >::getConnectionToControlParameterEventId; 00048 using AbstractInput< T >::getName; 00049 protected: 00050 using AbstractInput< T >::_owner ; 00051 using AbstractInput< T >::_precisionLevel; 00052 using AbstractInput< T >::_name; 00053 00054 public: 00055 00061 Input( const Name & inputName, SimulatedObject & owner, bool makeConnectable, const int precisionLevel = OMK::Type::PolatorNT::defaultPrecisionLevel ) ; 00062 00063 00064 /* Destructor 00065 */ 00066 virtual ~Input() ; 00067 00068 00074 virtual bool connect( const Name & objectName, const Name & outputName ) ; 00075 00076 00082 virtual bool connect( SimulatedObject & object, const Name & outputName ) ; 00083 00084 00090 virtual bool connect( SimulatedObject * pointerToObject, const Name & outputName ) ; 00091 00092 00099 virtual bool connect( const Name & objectName, const Name & outputName, const T & initialValue ) ; 00100 00101 00108 virtual bool connect( SimulatedObject & object, const Name & outputName, const T & initialValue ) ; 00109 00110 00117 virtual bool connect( SimulatedObject * pointerToObject, const Name & outputName, const T & initialValue ) ; 00118 00119 00125 virtual bool connectToControlParameter( const Name & objectName, const Name & controlParameterName ) ; 00126 00127 00133 virtual bool connectToControlParameter( SimulatedObject & object, const Name & controlParameterName ) ; 00134 00135 00141 virtual bool connectToControlParameter( SimulatedObject * pointerToObject, const Name & controlParameterName ) ; 00142 00143 00150 virtual bool connectToControlParameter( const Name & objectName, const Name & controlParameterName, const T & initialValue ) ; 00151 00152 00159 virtual bool connectToControlParameter( SimulatedObject & object, const Name & controlParameterName, const T & initialValue ) ; 00160 00161 00168 virtual bool connectToControlParameter( SimulatedObject * pointerToObject, const Name & controlParameterName, const T & initialValue ) ; 00169 00170 00172 virtual const OutputNT * getConnectedOutput() const ; 00173 00174 00175 00178 virtual void disconnect() ; 00179 00182 virtual bool isConnected() ; 00183 00187 virtual const T & get( int deltaT = 0) ; 00188 00189 00193 virtual int getDistanceToExactValue() const; 00194 00195 00199 virtual const T & getLastExactValue() const ; 00200 00201 00205 virtual const Date & getDateOfLastExactValue() const ; 00206 00207 00211 virtual void printDebuggingInformation( std::ostream & err ) const ; 00212 00213 00217 virtual void extract( std::istream & = std::cin ) ; 00218 00222 virtual void insertInStream( std::ostream & = std::cout ) const ; 00223 00224 00227 virtual void outputDestroyed() ; 00228 00233 virtual bool realConnect( OutputNT * output ) ; 00234 00235 protected: 00236 00240 int _distanceToExactValueOfLastGet ; 00241 00242 00245 Output<T> * _connectedOutput; 00246 00247 00251 T _calculatedResult ; 00252 00254 InputConnectionEventListener<T> * _associatedEventListener ; 00255 00256 } ; // Input 00257 00258 } // namespace OMK 00259 00260 00261 //----------------------------------------------------------------------------- 00262 //----------------------------------------------------------------------------- 00263 00264 00265 #include "OMKControlParameter.h" 00266 00267 namespace OMK 00268 { 00269 00270 //----------------------------------------------------------------------------- 00271 00272 template< typename T > 00273 Input<T>::Input( const Name & inputName, SimulatedObject & owner, 00274 bool makeConnectable, 00275 const int precisionLevel ) 00276 : AbstractInput<T>( inputName, owner, makeConnectable, precisionLevel ), 00277 _connectedOutput( 0 ) 00278 { 00279 /* the event listener has to be created even is makeConnectable 00280 * is false to enable dynamic change in the connectable state 00281 * without side effects on event listener order. This is 00282 * important to ensure consistent behaviour in way that doesn't 00283 * depend on creation and making public order. 00284 */ 00285 _associatedEventListener = new InputConnectionEventListener<T>( 00286 owner, this ) ; 00287 }; 00288 00289 //----------------------------------------------------------------------------- 00290 00291 template< typename T > 00292 Input<T>::~Input() 00293 { 00294 delete _associatedEventListener ; 00295 if( _connectedOutput ) 00296 { 00297 _connectedOutput->disconnectedMyself( this ) ; 00298 } 00299 } 00300 00301 //----------------------------------------------------------------------------- 00302 00303 template< typename T > 00304 inline void Input<T>::disconnect() 00305 { 00306 if( _connectedOutput ) 00307 { 00308 _connectedOutput->disconnectedMyself( this ) ; 00309 _connectedOutput = 0 ; 00310 } 00311 } 00312 00313 //----------------------------------------------------------------------------- 00314 00315 template< typename T > 00316 inline bool Input<T>::connect( const Name & objectName, 00317 const Name & outputName ) 00318 { 00319 OMTRACEID( OMK_DEBUG_OMK_TYPE, 00320 "Input: trying connection with object \"" << objectName 00321 << "\" on output \"" << outputName << "\"" ) ; 00322 SimulatedObject * object = 00323 _owner.getController().getPointerToSimulatedObjectNamed( objectName ) ; 00324 bool ok = false ; 00325 if( object ) 00326 { 00327 OutputNT * output = object->getPointerToOutputNamed( outputName ) ; 00328 if( output != 0 ) 00329 { 00330 ok = realConnect( output ) ; 00331 } 00332 else 00333 { 00334 OMERROR( "Output \"" << outputName 00335 << "\" not found on object \"" << objectName << "\"" ) ; 00336 } 00337 } 00338 else 00339 { 00340 std::ostringstream msg ; 00341 printDebuggingInformation( msg ) ; 00342 OMERROR( msg.str() << "Input<T>::connect \"" << objectName 00343 << "\" unknown" ) ; 00344 } 00345 return ok ; 00346 } 00347 00348 //----------------------------------------------------------------------------- 00349 00350 template< typename T > 00351 inline bool Input<T>::connect( const Name & objectName, 00352 const Name & outputName, 00353 const T & initialValue ) 00354 { 00355 bool connected = false ; 00356 if( connect( objectName, outputName ) && _connectedOutput ) 00357 { 00358 _connectedOutput->suggest( initialValue ) ; 00359 connected = true ; 00360 } 00361 return connected ; 00362 } 00363 00364 //----------------------------------------------------------------------------- 00365 00366 template< typename T > 00367 inline bool Input<T>::connect( SimulatedObject & object, 00368 const Name & outputName, 00369 const T & initialValue ) 00370 { 00371 bool connected = false ; 00372 if( connect( object, outputName ) ) 00373 { 00374 _connectedOutput->suggest( initialValue ) ; 00375 connected = true ; 00376 } 00377 return connected ; 00378 } 00379 00380 //----------------------------------------------------------------------------- 00381 00382 template< typename T > 00383 inline bool Input<T>::connect( SimulatedObject * object, 00384 const Name & outputName, 00385 const T & initialValue ) 00386 { 00387 return AbstractInput<T>::connect( object, outputName, initialValue ) ; 00388 } 00389 00390 //----------------------------------------------------------------------------- 00391 00392 template< typename T > 00393 inline bool Input<T>::connect( SimulatedObject & object, 00394 const Name & outputName ) 00395 { 00396 OMTRACEID( OMK_DEBUG_OMK_TYPE, "Input<T>::connect( \"" 00397 << object.getName() << "\", \"" << outputName << "\" )" ) ; 00398 return realConnect( object.getPointerToOutputNamed( outputName ) ) ; 00399 } 00400 00401 //----------------------------------------------------------------------------- 00402 00403 template< typename T > 00404 inline bool Input<T>::connect( SimulatedObject * object, 00405 const Name & outputName ) 00406 { 00407 return AbstractInput<T>::connect( object, outputName ) ; 00408 } 00409 00410 //----------------------------------------------------------------------------- 00411 00412 template< typename T > 00413 inline bool Input<T>::connectToControlParameter( 00414 const Name& objectName, 00415 const Name& controlParameterName ) 00416 { 00417 OMTRACEID( OMK_DEBUG_OMK_TYPE, 00418 "Input<T>::connectToControlParameter( \"" 00419 << objectName << "\", \"" << controlParameterName 00420 << "\" )" ) ; 00421 SimulatedObject * object = 00422 _owner.getController().getPointerToSimulatedObjectNamed( objectName ) ; 00423 bool ok = false ; 00424 if( object ) 00425 { 00426 ok = realConnect( object->getPointerToControlParameterNamed( 00427 controlParameterName ) ) ; 00428 } 00429 else 00430 { 00431 std::ostringstream msg ; 00432 printDebuggingInformation( msg ) ; 00433 OMERROR( msg.str() << "Input<T>::connectToControlParameter " 00434 << objectName << " unknown" ) ; 00435 } 00436 return ok ; 00437 } 00438 00439 //----------------------------------------------------------------------------- 00440 00441 template< typename T > 00442 inline bool Input<T>::connectToControlParameter( 00443 const Name& objectName, 00444 const Name& controlParameterName, 00445 const T& initialValue ) 00446 { 00447 bool ok = false ; 00448 if( connect( objectName, controlParameterName ) && _connectedOutput ) 00449 { 00450 _connectedOutput->suggest( initialValue ) ; 00451 ok = true ; 00452 } 00453 return ok ; 00454 } 00455 00456 //----------------------------------------------------------------------------- 00457 00458 template< typename T > 00459 inline bool Input<T>::connectToControlParameter( 00460 SimulatedObject& object, 00461 const Name& controlParameterName, 00462 const T& initialValue ) 00463 { 00464 bool ok = false ; 00465 if( connect( object, controlParameterName ) && _connectedOutput ) 00466 { 00467 _connectedOutput->suggest( initialValue ) ; 00468 ok = true ; 00469 } 00470 return ok ; 00471 } 00472 00473 //----------------------------------------------------------------------------- 00474 00475 template< typename T > 00476 inline bool Input<T>::connectToControlParameter( 00477 SimulatedObject* pointerToObject, 00478 const Name& controlParameterName, 00479 const T& initialValue ) 00480 { 00481 return AbstractInput<T>::connectToControlParameter( 00482 pointerToObject, controlParameterName, initialValue ) ; 00483 } 00484 00485 //----------------------------------------------------------------------------- 00486 template< typename T > 00487 inline bool Input<T>::connectToControlParameter( 00488 SimulatedObject & object, 00489 const Name & controlParameterName ) 00490 { 00491 OMTRACEID( OMK_DEBUG_OMK_TYPE, 00492 "Input<T>::connectToControlParameter( \"" 00493 << object.getName() << "\", \"" << controlParameterName 00494 << "\" )" ) ; 00495 return realConnect( object.getPointerToControlParameterNamed( 00496 controlParameterName ) ) ; 00497 } 00498 00499 //----------------------------------------------------------------------------- 00500 00501 template< typename T > 00502 inline bool Input<T>::connectToControlParameter( 00503 SimulatedObject * object, 00504 const Name & controlParameterName ) 00505 { 00506 OMTRACEID( OMK_DEBUG_OMK_TYPE, 00507 "Input<T>::connectToControlParameter( \"" 00508 << object->getName() << "\", \"" << controlParameterName 00509 << "\" )" ) ; 00510 return AbstractInput<T>::connectToControlParameter( 00511 object, controlParameterName ) ; 00512 } 00513 00514 00515 //----------------------------------------------------------------------------- 00516 00517 template< typename T > 00518 inline bool Input<T>::realConnect( OutputNT * output ) 00519 { 00520 /* if the current active object isn't initialised or is the owner 00521 * of the input, proceed with connection 00522 */ 00523 if( CurrentActiveObject::getCurrentActiveObject() == &_owner || 00524 CurrentActiveObject::getCurrentActiveObject() == &_owner.getController() || 00525 CurrentActiveObject::getCurrentActiveObject() == 0 ) 00526 { 00527 // BEGIN TDTD add because problems with local objects // 10-04-2006 00528 if( _connectedOutput != 0 ) 00529 { 00530 disconnect() ; 00531 } 00532 // END TDTD 00533 _connectedOutput = dynamic_cast< Output< T > * >( output ) ; 00534 00535 if( _connectedOutput == 0 ) 00536 { 00537 std::ostringstream msg ; 00538 printDebuggingInformation( msg ) ; 00539 if( output ) 00540 { 00541 output->printDebuggingInformation( msg ) ; 00542 } 00543 OMERROR( msg.str() 00544 << ":-( Input::realConnect outpout is of wrong type or null" ) ; 00545 return false ; 00546 } 00547 else 00548 { 00549 _connectedOutput->connectedMyself( this ) ; 00550 return true ; 00551 } 00552 } 00553 else if( isConnectable() ) 00554 { 00555 ControlParameter<T> * controlParameter = 00556 dynamic_cast<ControlParameter<T> *>( output ) ; 00557 if( controlParameter == 0 ) 00558 { 00559 CurrentActiveObject::getCurrentActiveObject()->sendValuedEvent( 00560 _owner, 00561 getConnectionEventId(), 00562 output->getName() ) ; 00563 } 00564 else 00565 { 00566 CurrentActiveObject::getCurrentActiveObject()->sendValuedEvent( 00567 _owner, 00568 getConnectionToControlParameterEventId(), 00569 output->getName() ) ; 00570 } 00571 return true ; 00572 } 00573 else 00574 { 00575 OMERROR( "Warning: " << _owner.getName() << "::" <<getName() 00576 << " is not connectable" ) ; 00577 return false ; 00578 } 00579 } 00580 00581 //----------------------------------------------------------------------------- 00582 00583 template< typename T > 00584 inline const OutputNT * Input<T>::getConnectedOutput() const 00585 { 00586 return _connectedOutput ; 00587 } 00588 00589 //----------------------------------------------------------------------------- 00590 00591 template< typename T > 00592 inline const T & Input<T>::get( int deltaT ) 00593 { 00594 OMTRACEID( OMK_DEBUG_OMK_TYPE, 00595 "Input<T>::localGet calling get of _connectedOutput" ) ; 00596 if( !_connectedOutput ) 00597 { 00598 OMERROR( "Input< " << typeid( T ).name() << " >::get \n" 00599 << "Of owner \"" << _owner.getName() 00600 << "\" named \"" << _name << "\"" ) ; 00601 throw InvalidOutputException() ; 00602 } 00603 return _connectedOutput->get( _distanceToExactValueOfLastGet, 00604 _precisionLevel, 00605 deltaT, 00606 _calculatedResult ) ; 00607 } 00608 00609 //----------------------------------------------------------------------------- 00610 00611 template< typename T > 00612 inline const T & Input<T>::getLastExactValue() const 00613 { 00614 if( !_connectedOutput ) 00615 { 00616 OMERROR( "Input< " << typeid( T ).name() 00617 << " >::getLastExactValue \n" 00618 << "Of owner \"" << _owner.getName() 00619 << "\" named \"" << _name << "\"" ) ; 00620 throw InvalidOutputException() ; 00621 } 00622 return _connectedOutput->getLastExactValue() ; 00623 } 00624 00625 //----------------------------------------------------------------------------- 00626 00627 template< typename T > 00628 inline const Date & Input<T>::getDateOfLastExactValue() const 00629 { 00630 if( !_connectedOutput ) 00631 { 00632 OMERROR( "Input< " << typeid( T ).name() 00633 << " >::getDateOfLastExactValue \n" 00634 << "Of owner \"" << _owner.getName() 00635 << "\" named \"" << _name << "\"" ) ; 00636 throw InvalidOutputException() ; 00637 } 00638 return _connectedOutput->getDateOfLastExactValue() ; 00639 } 00640 00641 //----------------------------------------------------------------------------- 00642 00643 template< typename T > 00644 inline int Input<T>::getDistanceToExactValue() const 00645 { 00646 return _distanceToExactValueOfLastGet ; 00647 } 00648 00649 //----------------------------------------------------------------------------- 00650 00651 template< typename T > 00652 inline void Input<T>::extract( std::istream & in ) 00653 { 00654 OMASSERTM( false, "shouldn't be called" ) ; 00655 in >> *_connectedOutput ; 00656 } 00657 00658 //----------------------------------------------------------------------------- 00659 00660 template< typename T > 00661 inline void Input<T>::insertInStream( std::ostream & out ) const 00662 { 00663 OMASSERTM( false, "shouldn't be called" ) ; 00664 out << *_connectedOutput << " " ; 00665 } 00666 00667 //----------------------------------------------------------------------------- 00668 00669 template< typename T > 00670 inline void Input<T>::printDebuggingInformation( 00671 std::ostream & err ) const 00672 { 00673 err << std::endl 00674 << "**********************************************************" 00675 << std::endl 00676 << "Class Input" << std::endl 00677 << "owner : " << _owner.getName() << std::endl 00678 << "polation precision level : " << _precisionLevel << std::endl 00679 << "element type : " << typeid( T ).name() << std::endl ; 00680 } 00681 00682 //----------------------------------------------------------------------------- 00683 00684 template< typename T > 00685 inline void Input<T>::outputDestroyed() 00686 { 00687 _connectedOutput = 0 ; 00688 } 00689 00690 //----------------------------------------------------------------------------- 00691 00692 template< typename T > 00693 inline bool Input<T>::isConnected() 00694 { 00695 return _connectedOutput != 0 ; 00696 } 00697 00698 //----------------------------------------------------------------------------- 00699 00700 } // namespace OMK 00701 00702 #endif
Documentation generated on Mon Jun 9 11:45:56 2008 |
Generated with doxygen by Dimitri van Heesch , 1997-2007 |