BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Calibration/facilities/include/facilities/FIFO.h
Go to the documentation of this file.
1// $Id: FIFO.h,v 1.1.1.1 2005/10/17 06:11:40 maqm Exp $
2#ifndef FIFO_h
3#define FIFO_h 1
4
5#ifdef _MSC_VER
6# pragma warning( disable : 4786 )
7#endif
8
9#include <cassert>
10#include <vector>
11
12// ## Class: FIFO; Parameterized Class
13// Implements the FIFO class. This is not a readout
14// component, but a standard First-In-First-Out storage
15// class.
16
17using std::vector;
18
19template <class _T>
20class FIFO : private vector<_T> // ## Inherits: _T
21{
22public:
23 FIFO( unsigned int depth = 0 ) : vector<_T>(), m_depth( depth ) {
24 if ( depth > 0 ) reserve( depth );
25
26 // if the depth of the fifo is fixed, then reserve enough space to contain it entirely
27 // FIFO operates in two modes: one for a fixed depth,
28 // and another for a non-fixed depth (unlimited)
29 }
30
31 FIFO( const FIFO& f ) : vector<_T>( f ), m_depth( f.m_depth ) {}
32
33 // Push x into the FIFO. Returns true if x was successfully
34 // stored, false if not (FIFO was full).
35 bool push( const _T& x ) {
36 if ( ( m_depth != 0 ) && ( size() >= m_depth ) ) return false;
37
38 push_back( x );
39
40 return true;
41 }
42
43 // Returns the current front of the FIFO buffer, if it
44 // exists. Caller must perform range checking to see that
45 // the FIFO is not empty prior to this call.
46 _T pop() {
47 assert( size() > 0 );
48 _T value( *begin() );
49 erase( begin() );
50
51 // limited depth mode
52
53 return value;
54 }
55
56 _T& front() { return vector<_T>::front(); }
57 _T& back() { return vector<_T>::back(); }
58
59 const _T& front() const { return vector<_T>::front(); }
60 const _T& back() const { return vector<_T>::back(); }
61
62 bool empty() const { return ( size() == 0 ); }
63
64 // Get the maximum size of this FIFO.
65 unsigned int maxSize() const { return m_depth; }
66
67 // Return the current size of the FIFO buffer.
68 unsigned int size() const { return vector<_T>::size(); }
69
70 void clear() { vector<_T>::clear(); }
71
72 // Writes the contents of the FIFO to the output stream.
73 void printOn( std::ostream& out = cout ) {
74 short i = 1;
75 for ( vector<_T>::reverse_iterator it = rbegin(); it != rend(); ++it )
76 {
77 out << ( *it )();
78 if ( ( i % 8 ) == 0 ) out << "\n";
79 else out << "\t";
80 i++;
81 }
82 }
83
84 bool full() const { return size() == m_depth; }
85
86 bool avail() const { return size() < m_depth; }
87
88private:
89 // maximum depth for the fifo
90 unsigned int m_depth;
91};
92
93#endif
#define _T(str)
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")
void printOn(std::ostream &out=cout)