OMKFifo.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 FifoHEADER
00019 #define FifoHEADER
00020 
00021 #include <OMKAbstractFifo.h>
00022 #include <typeinfo>
00023 #include <vector>
00024 #include "OMKTracer.h"
00025 
00026 namespace OMK
00027 {
00028 
00033 template < typename T>
00034 class Fifo : public AbstractFifo <T>
00035 {
00036  public:
00037   using AbstractFifo <T>::_fifoSize ;
00038  public :
00040    Fifo (const int taille) ;
00041 
00042 
00044    virtual ~Fifo () ;
00045 
00049    virtual const T& getPreceedingValue (const unsigned int indice) const ;
00050 
00051 
00055    virtual const Date & getPreceedingDate(const unsigned int indice) const ;
00056    
00057 
00059    virtual void set (const T& val, const Date & date) ;
00060 
00061 
00063    virtual void setInPlace ( const T& val, const Date & date) ;
00064 
00065 
00067    virtual T& getNextPlaceHolder() ;
00068 
00069 
00071    virtual void clear () ;
00072 
00073 
00076    virtual unsigned int getNumberOfPresentValues( int maxCherche = -1 ) const ;
00077 
00078 
00080    virtual void printDebuggingInformation(void) const ;
00081 
00082 protected :
00084    class ValeurDate 
00085    {
00086    public:
00087       ValeurDate() : dateValeur( -2 ) {}
00088       Date dateValeur ;
00089       T valeur ;    
00090    };
00091    
00093    std::vector<ValeurDate> _tab ;
00094 
00096    unsigned int _numberOfValues ;
00097 
00099    unsigned int _mostRecentValueIndex ;
00100 } ; // Fifo
00101 
00102 
00103 
00104 
00105 
00106 //------------------------------------------------------------------------
00107 
00108 template <typename T>
00109 Fifo<T>::Fifo ( const int size )
00110 : AbstractFifo< T >( size ),
00111 _tab( size ),
00112 _numberOfValues( 0 ),
00113 _mostRecentValueIndex( size - 1 )
00114 {
00115 }
00116 
00117 
00118 
00119 template <typename T> 
00120 Fifo<T>::~Fifo (void) 
00121 {
00122 }
00123 
00124 //------------------------------------------------------------------------
00125 
00126 template <typename T> 
00127 const Date & Fifo<T>::getPreceedingDate (unsigned int i) const 
00128 {
00129    OMASSERTM ( i < _fifoSize - 2, "" ) ;
00130    int indice = (_fifoSize + _mostRecentValueIndex - i ) % _fifoSize ;
00131    return _tab[indice].dateValeur ;
00132 }
00133 
00134 template <typename T> 
00135 const T& Fifo<T>::getPreceedingValue (unsigned int i) const {
00136    OMASSERTM( i < _fifoSize - 2, "" ) ;
00137    int indice = (_fifoSize + _mostRecentValueIndex - i ) % _fifoSize ;
00138    return _tab[indice].valeur ;
00139 }
00140 
00141 //------------------------------------------------------------------------
00142 
00143 template <typename T>
00144 void Fifo<T>::set (const T& newValue, const Date & dateOfNewValue) 
00145 {
00146    unsigned int newMostRecentValueIndex = ( _mostRecentValueIndex + 1 ) % _fifoSize ;
00147 
00148    Date & dateOfMostRecentValue( _tab[_mostRecentValueIndex].dateValeur ) ;
00149    
00150    if (dateOfMostRecentValue < dateOfNewValue ) {
00151       _tab[newMostRecentValueIndex].dateValeur = dateOfNewValue ;
00152       if ( _numberOfValues != _fifoSize ) {
00153          _numberOfValues ++ ;
00154       }
00155     }
00156 
00157    _tab[newMostRecentValueIndex].valeur = newValue ;
00158 
00159    _mostRecentValueIndex = newMostRecentValueIndex ;
00160 }
00161 
00162 template <typename T>
00163 void Fifo<T>::setInPlace ( const T& newValue, const Date & dateOfNewValue ) 
00164 {
00165    unsigned int newMostRecentValueIndex = ( _mostRecentValueIndex + 1 ) % _fifoSize ;
00166 
00167    Date & dateOfMostRecentValue(_tab[_mostRecentValueIndex].dateValeur) ;
00168    
00169    if (dateOfMostRecentValue < dateOfNewValue ) {
00170       _tab[newMostRecentValueIndex].dateValeur = dateOfNewValue ;
00171       if ( _numberOfValues != _fifoSize ) {
00172          _numberOfValues ++ ;
00173       }
00174     }
00175 
00176    //this assertion will fail if setInplace is used a a different simulation step than getNextPlaceHolder
00177    // or if newValue isn't a reference to a value of the container
00178    OMASSERTM(&(_tab[newMostRecentValueIndex].valeur) == &newValue, "" ) ;
00179 
00180    _mostRecentValueIndex = newMostRecentValueIndex ;
00181 }
00182 
00183 
00184 template <typename T>
00185 T& Fifo<T>::getNextPlaceHolder() 
00186 {
00187    return _tab[( _mostRecentValueIndex + 1 ) % _fifoSize].valeur ;
00188 }
00189 
00190 template <typename T>
00191 unsigned int Fifo<T>::getNumberOfPresentValues( int maxCherche ) const {
00192    unsigned int res ;
00193    if ( (maxCherche == -1) || (_numberOfValues < (unsigned int) maxCherche) ) {
00194       res = _numberOfValues ;
00195    }
00196    else {
00197       res = maxCherche ; 
00198    }
00199    //for multithreading reasons, limit the results to _fifoSize - 2 
00200    OMASSERTM( _fifoSize >=  2, "" ) ;
00201    if (res >= _fifoSize - 1) {
00202       res = _fifoSize - 2 ;
00203    }
00204    return res ;
00205 }
00206 
00207 //------------------------------------------------------------------------
00208 
00209 template <typename T>
00210 void Fifo<T>::clear() {
00211    _numberOfValues = 0 ;
00212 }
00213 
00214 //------------------------------------------------------------------------
00215 
00216 template <typename T>
00217 void Fifo<T>::printDebuggingInformation (void) const 
00218 {
00219    std::cerr<< "Fifo<"<<typeid(T).name()<<">::"<<this<<"::printDebuggingInformation()"<<std::endl ;
00220    AbstractFifo<T>::printDebuggingInformation() ;
00221    std::cerr << "number of valid values : " << _numberOfValues << std::endl
00222         << "last written value at : " << _mostRecentValueIndex << std::endl ;
00223 }
00224 } // namespace OMK
00225 
00226 #endif

logo OpenMask

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

Generated with doxygen by Dimitri van Heesch ,   1997-2007