Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

PsInput.h

Go to the documentation of this file.
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 PsInputHEADER
00019 #define PsInputHEADER
00020 
00021 #include <PsAbstractInput.h>
00022 #include <PsSimulatedObject.h>
00023 #include <PsOutput.h>
00024 #include <PsUnInitialisedOutputException.h>
00025 
00026 //-----------------------------------------------------------------
00027 #include "PsInputConnectionEventListener.h"
00028 
00040 template <typename Type>
00041 class PsInput : public PsAbstractInput <Type>
00042 {
00043 public:
00044 
00050    PsInput (const PsName & inputName, PsSimulatedObject & owner, bool makeConnectable, const int precisionLevel = PsPolatorNT::defaultPrecisionLevel) ;
00051 
00052 
00053    /* Destructor
00054     */
00055    virtual ~PsInput (void) ;
00056 
00057 
00063    virtual bool connect (const PsName & objectName, const PsName & outputName ) ;
00064 
00065 
00071    virtual bool connect (PsSimulatedObject & object, const PsName & outputName ) ;
00072 
00073 
00079    virtual bool connect (PsSimulatedObject * pointerToObject, const PsName & outputName ) ;
00080 
00081 
00088    virtual bool connect (const PsName & objectName, const PsName & outputName, const Type & initialValue) ;
00089 
00090 
00097    virtual bool connect (PsSimulatedObject & object, const PsName & outputName, const Type & initialValue) ;
00098 
00099 
00106    virtual bool connect (PsSimulatedObject * pointerToObject, const PsName & outputName, const Type & initialValue) ;
00107 
00108 
00114    virtual bool connectToControlParameter (const PsName & objectName, const PsName & controlParameterName ) ;
00115 
00116 
00122    virtual bool connectToControlParameter (PsSimulatedObject & object, const PsName & controlParameterName )  ;
00123 
00124 
00130    virtual bool connectToControlParameter (PsSimulatedObject * pointerToObject, const PsName & controlParameterName ) ;
00131 
00132 
00139    virtual bool connectToControlParameter (const PsName & objectName, const PsName & controlParameterName, const Type & initialValue)  ;
00140 
00141 
00148    virtual bool connectToControlParameter (PsSimulatedObject & object, const PsName & controlParameterName, const Type & initialValue)  ;
00149 
00150 
00157    virtual bool connectToControlParameter (PsSimulatedObject * pointerToObject, const PsName & controlParameterName, const Type & initialValue) ;
00158 
00159 
00161    virtual const PsOutputNT * getConnectedOutput () const ;
00162 
00163 
00164 
00167    virtual void disconnect () ;
00168 
00169 
00173    virtual const Type & get (int deltaT = 0); 
00174 
00175 
00179    virtual int getDistanceToExactValue () const;
00180 
00181 
00185    virtual const Type & getLastExactValue() const ;
00186 
00187 
00191    virtual const PsDate & getDateOfLastExactValue() const ;
00192 
00193 
00197    virtual void printDebuggingInformation (ostream & err ) const ;
00198 
00199 
00203    virtual void extract (istream & = cin) ;
00204    
00208    virtual void insertInStream (ostream & = cout) const ;
00209 
00210 
00213    virtual void outputDestroyed () ; 
00214 
00219    virtual bool realConnect (PsOutputNT * output ) ;
00220 
00221 protected:
00222 
00226    int _distanceToExactValueOfLastGet ; 
00227 
00228 
00231    PsOutput<Type> * _connectedOutput;
00232 
00233    
00237    Type _calculatedResult ;
00238 
00240    PsInputConnectionEventListener<Type> * _associatedEventListener ;
00241    
00242 } ; // PsInput
00243 
00244 
00245 
00246 
00247 
00248 
00249 //---------------------------- IMPLEMENTATION -----------------------------
00250 
00251 
00252 #include "PsNullOutputCreator.h"
00253 #include "PsControlParameter.h"
00254 
00255 template <typename Type>
00256 PsInput<Type>::PsInput (const PsName & inputName, PsSimulatedObject & owner, bool makeConnectable, const int precisionLevel)
00257    : PsAbstractInput<Type>( inputName, owner, makeConnectable, precisionLevel ),
00258    _connectedOutput (  PsNullOutputCreator::template getNullOutput <Type> () )
00259 {
00260    // the event listener has to be created even is makeConnectable is false to enable dynamic change in the connectable state without side effects on event listener order. This is important to ensure consistent behaviour in way that doesn't depend on creation and making public order.
00261    _associatedEventListener = new PsInputConnectionEventListener<Type> ( owner, this ) ;
00262    owner.addEventListener( *_associatedEventListener ) ;     
00263 };
00264 
00265 
00266 template <typename Type>
00267 PsInput<Type>::~PsInput () 
00268 {
00269    if ( _associatedEventListener != NULL ) delete _associatedEventListener ;
00270    _connectedOutput -> disconnectedMyself (this) ;
00271 }
00272 
00273 
00274 //-----------------------------------------------------------------
00275 
00276 
00277 template <typename Type>
00278 inline void PsInput<Type>::disconnect () 
00279 {
00280    _connectedOutput -> disconnectedMyself (this) ;
00281    _connectedOutput = PsNullOutputCreator::template getNullOutput<Type> () ;
00282 }
00283 
00284 
00285 //-----------------------------------------------------------------
00286 
00287 
00288 template <typename Type>
00289 inline bool PsInput<Type>::connect (const PsName & objectName,
00290                                     const PsName & outputName) {
00291 #ifdef _DEBUGTYPEUTIL
00292   cout << "PsInput<Type>::connect" << endl;
00293 #endif
00294   PsSimulatedObject * object = _owner.getController ().getPointerToSimulatedObjectNamed (objectName) ;
00295   if (object != NULL) 
00296      {
00297 #ifdef DEBUG
00298           cerr << "PsInput<Type>::connect, objet : " << objectName 
00299                     << ", sur sortie : " << outputName << endl
00300                     << "pointeur de la sortie de l'objet : " << object->getPointerToOutputNamed ( outputName ) << endl ;
00301 #endif
00302         return realConnect ( object->getPointerToOutputNamed ( outputName ) ) ;
00303      } 
00304   else 
00305      {
00306         printDebuggingInformation ( cerr ) ;
00307         cerr<< "PsInput<Type>::connect " <<objectName<<" unknown"<< endl ;
00308         return false ;
00309      }
00310 }
00311 
00312 
00313 template <typename Type>
00314 inline bool PsInput<Type>::connect (const PsName & objectName,
00315                                      const PsName & outputName,
00316                                      const Type & initialValue) 
00317 {
00318    if ( connect (objectName, outputName) )
00319       {
00320          _connectedOutput->suggest (initialValue) ;
00321          return true ;
00322       }
00323    else 
00324       {
00325          return false ;
00326       }
00327 }
00328 
00329 //-----------------------------------------------------------------
00330 
00331 template <typename Type>
00332 inline bool PsInput<Type>::connect (PsSimulatedObject & object,
00333                                     const PsName & outputName,
00334                                     const Type & initialValue) 
00335 {
00336    if ( connect (object, outputName) )
00337       {
00338          _connectedOutput->suggest (initialValue) ;
00339          return true ;
00340       }
00341    else 
00342       {
00343          return false ;
00344       }
00345 }
00346 
00347 template <typename Type>
00348 inline bool PsInput<Type>::connect (PsSimulatedObject * object,
00349                                     const PsName & outputName,
00350                                     const Type & initialValue) 
00351 {
00352    return PsAbstractInput<Type>::connect (object, outputName, initialValue );
00353 }
00354 
00355 //-----------------------------------------------------------------
00356 template <typename Type>
00357 inline bool PsInput<Type>::connect (PsSimulatedObject & object,
00358                                     const PsName & outputName) 
00359 {
00360   
00361 #ifdef _DEBUGTYPEUTIL
00362    cout << "PsInput<Type>::connect" << endl;
00363 #endif
00364    return realConnect (object.getPointerToOutputNamed (outputName)) ;
00365 }
00366 
00367 //-----------------------------------------------------------------
00368 template <typename Type>
00369 inline bool PsInput<Type>::connect (PsSimulatedObject * object,
00370                                     const PsName & outputName) 
00371 {
00372    return PsAbstractInput<Type>::connect(object, outputName) ;
00373 }
00374 
00375 
00376 //-----------------------------------------------------------------
00377 
00378 
00379 template <typename Type>
00380 inline bool PsInput<Type>::connectToControlParameter (const PsName & objectName,
00381                                                       const PsName & controlParameterName) {
00382 #ifdef _DEBUGTYPEUTIL
00383    cout << "PsInput<Type>::connectToControlParameter" << endl;
00384 #endif
00385    PsSimulatedObject * object = _owner.getController ().getPointerToSimulatedObjectNamed (objectName) ;
00386    if (object != NULL) 
00387       {
00388          return realConnect ( object->getPointerToControlParameterNamed ( controlParameterName ) ) ;
00389       } 
00390    else 
00391       {
00392          printDebuggingInformation ( cerr ) ;
00393          cerr<< "PsInput<Type>::connectToControlParameter " <<objectName<<" unknown"<< endl ;
00394          return false ;
00395       }
00396 }
00397 
00398 
00399 template <typename Type>
00400 inline bool PsInput<Type>::connectToControlParameter (const PsName & objectName,
00401                                                       const PsName & controlParameterName,
00402                                                       const Type & initialValue) 
00403 {
00404    if ( connect (objectName, controlParameterName) )
00405       {
00406          _connectedOutput->suggest (initialValue) ;
00407          return true ;
00408       }
00409    else 
00410       {
00411          return false ;
00412       }
00413 }
00414 
00415 //-----------------------------------------------------------------
00416 
00417 template <typename Type>
00418 inline bool PsInput<Type>::connectToControlParameter (PsSimulatedObject & object,
00419                                                       const PsName & controlParameterName,
00420                                                       const Type & initialValue) 
00421 {
00422    if ( connect (object, controlParameterName) )
00423       {
00424          _connectedOutput->suggest (initialValue) ;
00425          return true ;
00426       }
00427    else 
00428       {
00429          return false ;
00430       }
00431 }
00432 
00433 template <typename Type>
00434 inline bool PsInput<Type>::connectToControlParameter (PsSimulatedObject * pointerToObject,
00435                                                       const PsName & controlParameterName,
00436                                                       const Type & initialValue) 
00437 {
00438    return PsAbstractInput<Type>::connectToControlParameter ( pointerToObject, controlParameterName, initialValue ) ;
00439 }
00440 
00441 
00442 
00443 //-----------------------------------------------------------------
00444 template <typename Type>
00445 inline bool PsInput<Type>::connectToControlParameter (PsSimulatedObject & object,
00446                                                       const PsName & controlParameterName) 
00447 {
00448 #ifdef _DEBUGTYPEUTIL
00449    cerr << "PsInput<Type>::connect" << endl;
00450 #endif
00451    return realConnect (object.getPointerToControlParameterNamed (controlParameterName)) ;
00452 }
00453 
00454 
00455 //-----------------------------------------------------------------
00456 template <typename Type>
00457 inline bool PsInput<Type>::connectToControlParameter (PsSimulatedObject * object,
00458                                                       const PsName & controlParameterName) 
00459 {
00460 #ifdef _DEBUGTYPEUTIL
00461    cerr << "PsInput<Type>::connect" << endl;
00462 #endif
00463    return PsAbstractInput<Type>::connectToControlParameter (object, controlParameterName) ;
00464 }
00465 
00466 
00467 //-----------------------------------------------------------------
00468 
00469 template <typename Type>
00470 inline bool PsInput<Type>::realConnect (PsOutputNT * output) 
00471 {
00472    /* if the current active object isn't initialised or is the owner of the input, proceed with connection*/
00473    if ( PsnCurrentActiveObject::getCurrentActiveObject() == &_owner ||
00474         PsnCurrentActiveObject::getCurrentActiveObject() == &_owner.getController() ||
00475         PsnCurrentActiveObject::getCurrentActiveObject() == NULL )
00476       {
00477          disconnect() ;
00478          _connectedOutput = dynamic_cast <PsOutput<Type> *> (output) ;
00479          
00480          if ( _connectedOutput == NULL ) 
00481             {
00482                printDebuggingInformation ( cerr ) ;
00483                cerr<<"PsInput::realConnect outpout is of wrong type"<<endl;
00484                return false ;
00485             }
00486          else 
00487             {
00488                _connectedOutput->connectedMyself(this) ;
00489                return true ;
00490             }
00491       }
00492    else if ( isConnectable() )
00493       {
00494          PsControlParameter<Type> * controlParameter = dynamic_cast<PsControlParameter<Type> *> (output) ;
00495          if ( controlParameter == NULL )
00496             {
00497                PsnCurrentActiveObject::getCurrentActiveObject()->sendValuedEvent(_owner,
00498                                                                                  getConnectionEventId(),
00499                                                                                  output->getName() ) ;
00500             }
00501          else
00502             {
00503                PsnCurrentActiveObject::getCurrentActiveObject()->sendValuedEvent(_owner,
00504                                                                                  getConnectionToControlParameterEventId(),
00505                                                                                  output->getName() ) ;
00506             }
00507          return true ;
00508       }
00509    else
00510       {
00511          cerr<<"Warning: "
00512              <<_owner.getName()<<"::"<<getName()<<" "
00513              <<"is not connectable"<<endl;
00514          return false ;
00515       }
00516 }
00517 
00518 
00519 
00520 template <typename Type>
00521 inline const PsOutputNT * PsInput<Type>::getConnectedOutput () const
00522 {
00523    return _connectedOutput ;
00524 }
00525 
00526 //-----------------------------------------------------------------
00527 
00528 template <typename Type>
00529 inline const Type & PsInput<Type>::get (int deltaT ) 
00530 {
00531 #ifdef _DEBUGTYPEUTIL
00532    cout << "PsInput<Type>::localGet calling get of _connectedOutput " <<refOutput<< endl;
00533 #endif
00534    try {
00535       return _connectedOutput->get (_distanceToExactValueOfLastGet, 
00536                               _precisionLevel, 
00537                               deltaT, 
00538                               _calculatedResult);
00539    }
00540    catch (PsUnInitialisedOutputException & s ) {
00541       s<<"PsInput<"<<typeid(Type).name()<<">::get \n";
00542       s<<"Owner "<< _owner.getName() <<"de nom "<<_name<<"\n";
00543       throw ;
00544    }
00545 }
00546 
00547 
00548 template <typename Type>
00549 inline const Type & PsInput<Type>::getLastExactValue() const {
00550    return _connectedOutput->getLastExactValue () ;
00551 }
00552 
00553 
00554 template <typename Type>
00555 inline const PsDate & PsInput<Type>::getDateOfLastExactValue() const {
00556    return _connectedOutput->getDateOfLastExactValue () ;
00557 }
00558 
00559 //-----------------------------------------------------------------
00560 
00561 template <typename Type>
00562 inline int PsInput<Type>::getDistanceToExactValue () const
00563 {
00564    return _distanceToExactValueOfLastGet ;
00565 }
00566 
00567 //-----------------------------------------------------------------
00568 
00569 template <typename Type>
00570 inline void PsInput<Type>::extract (istream & in)
00571 {
00572    assert ( false ) ; //shouldn't be called
00573    in >> *_connectedOutput ;
00574 }
00575 
00576 //-----------------------------------------------------------------
00577 
00578 template <typename Type>
00579 inline void PsInput<Type>::insertInStream (ostream & out) const
00580 {
00581    assert ( false ) ; //shouldn't be called
00582    out << *_connectedOutput << " " ;
00583 }
00584 
00585 //-----------------------------------------------------------------
00586 
00587 template <typename Type>
00588 inline void PsInput<Type>::printDebuggingInformation (ostream & err) const
00589 {
00590   err << "**********************************************************" << endl;
00591   err << "Class PsInput" << endl;
00592   err << "owner : " << _owner.getName() << endl;
00593   err << "polation precision level : " << _precisionLevel << endl;
00594   err << "element type : " << typeid(Type).name() << endl;
00595 }
00596 
00597 template <typename Type>
00598 void PsInput<Type>::outputDestroyed () 
00599 {
00600    _connectedOutput = PsNullOutputCreator::template getNullOutput<Type> () ;
00601 } 
00602 
00603 #endif
00604 
00605 

logo OpenMask

Documentation generated on Mon Nov 25 15:25:00 2002

Generated with doxygen 1.2.12 by Dimitri van Heesch ,   1997-2001