/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Calibration/facilities/facilities-00-00-04/facilities/FIFO.h

Go to the documentation of this file.
00001 //      $Id: FIFO.h,v 1.1.1.1 2005/10/17 06:11:40 maqm Exp $
00002 #ifndef FIFO_h
00003 #define FIFO_h 1
00004 
00005 #ifdef _MSC_VER
00006 # pragma warning(disable: 4786)
00007 #endif
00008 
00009 #include <vector>
00010 #include <cassert>
00011 
00012 //## Class: FIFO; Parameterized Class
00013 //      Implements the FIFO class. This is not a readout
00014 //      component, but a standard First-In-First-Out storage
00015 //      class.
00016 
00017 using std::vector;
00018 
00019 template <class _T>
00020 class FIFO : private vector<_T>  //## Inherits: _T
00021 {
00022   public:
00023     FIFO (unsigned int depth = 0)   
00024         : vector<_T>(), m_depth(depth)    
00025     {
00026         if (depth > 0)  reserve ( depth );
00027 
00028         // if the depth of the fifo is fixed, then reserve enough space to contain it entirely
00029         // FIFO operates in two modes: one for a fixed depth,
00030         // and another for a non-fixed depth (unlimited)
00031     }
00032 
00033     FIFO (const FIFO& f)
00034         : vector<_T>(f), m_depth(f.m_depth)
00035     {
00036     }
00037 
00038 
00039       //        Push x into the FIFO. Returns true if x was successfully
00040       //        stored, false if not (FIFO was full).
00041       bool push (const _T& x)
00042       {
00043           if ((m_depth != 0) && (size() >= m_depth))    return false;
00044 
00045           push_back( x );
00046 
00047           return true;
00048       }
00049 
00050       //        Returns the current front of the FIFO buffer, if it
00051       //        exists. Caller must perform range checking to see that
00052       //        the FIFO is not empty prior to this call.
00053       _T pop ()
00054       {
00055           assert(size()>0);
00056           _T    value (*begin());
00057           erase(begin());
00058           
00059           // limited depth mode
00060           
00061           return value;
00062       }
00063 
00064       _T&   front   () { return vector<_T>::front(); }
00065       _T&   back    () { return vector<_T>::back(); }
00066 
00067       const _T&   front   () const      { return vector<_T>::front(); }
00068       const _T&   back    () const      { return vector<_T>::back(); }
00069 
00070       bool  empty   () const    { return (size() == 0); }
00071 
00072       //        Get the maximum size of this FIFO.
00073       unsigned int maxSize () const
00074       {
00075           return m_depth;
00076       }
00077 
00078       //        Return the current size of the FIFO buffer.
00079       unsigned int size () const
00080       {
00081           return vector<_T>::size();
00082       }
00083 
00084       void clear ()
00085       {
00086          vector<_T>::clear();
00087       }
00088 
00089       //        Writes the contents of the FIFO to the output stream.
00090       void printOn (std::ostream& out = cout)
00091       {
00092           short i = 1;
00093           for (vector<_T>::reverse_iterator it = rbegin(); it != rend(); ++it) {
00094               out << (*it)();
00095               if ((i % 8) == 0) out << "\n";
00096               else  out << "\t";
00097               i++;
00098           }
00099       }
00100 
00101       bool  full () const { return size() == m_depth; }
00102 
00103       bool  avail () const { return size() < m_depth; }
00104 
00105   private: 
00106 
00107       //        maximum depth for the fifo
00108       unsigned int m_depth;
00109 };
00110 
00111 #endif

Generated on Tue Nov 29 22:57:55 2016 for BOSS_7.0.2 by  doxygen 1.4.7