BOSS 6.6.4.p03
BESIII Offline Software System
Loading...
Searching...
No Matches
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 <vector>
10#include <cassert>
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{
22 public:
23 FIFO (unsigned int depth = 0)
24 : vector<_T>(), m_depth(depth)
25 {
26 if (depth > 0) reserve ( depth );
27
28 // if the depth of the fifo is fixed, then reserve enough space to contain it entirely
29 // FIFO operates in two modes: one for a fixed depth,
30 // and another for a non-fixed depth (unlimited)
31 }
32
33 FIFO (const FIFO& f)
34 : vector<_T>(f), m_depth(f.m_depth)
35 {
36 }
37
38
39 // Push x into the FIFO. Returns true if x was successfully
40 // stored, false if not (FIFO was full).
41 bool push (const _T& x)
42 {
43 if ((m_depth != 0) && (size() >= m_depth)) return false;
44
45 push_back( x );
46
47 return true;
48 }
49
50 // Returns the current front of the FIFO buffer, if it
51 // exists. Caller must perform range checking to see that
52 // the FIFO is not empty prior to this call.
54 {
55 assert(size()>0);
56 _T value (*begin());
57 erase(begin());
58
59 // limited depth mode
60
61 return value;
62 }
63
64 _T& front () { return vector<_T>::front(); }
65 _T& back () { return vector<_T>::back(); }
66
67 const _T& front () const { return vector<_T>::front(); }
68 const _T& back () const { return vector<_T>::back(); }
69
70 bool empty () const { return (size() == 0); }
71
72 // Get the maximum size of this FIFO.
73 unsigned int maxSize () const
74 {
75 return m_depth;
76 }
77
78 // Return the current size of the FIFO buffer.
79 unsigned int size () const
80 {
81 return vector<_T>::size();
82 }
83
84 void clear ()
85 {
86 vector<_T>::clear();
87 }
88
89 // Writes the contents of the FIFO to the output stream.
90 void printOn (std::ostream& out = cout)
91 {
92 short i = 1;
93 for (vector<_T>::reverse_iterator it = rbegin(); it != rend(); ++it) {
94 out << (*it)();
95 if ((i % 8) == 0) out << "\n";
96 else out << "\t";
97 i++;
98 }
99 }
100
101 bool full () const { return size() == m_depth; }
102
103 bool avail () const { return size() < m_depth; }
104
105 private:
106
107 // maximum depth for the fifo
108 unsigned int m_depth;
109};
110
111#endif
#define _T(str)
Double_t x[10]
const double rend
Definition: TofCalibFit.h:19
const double rbegin
Definition: TofCalibFit.h:18
Definition: FIFO.h:21
_T & front()
Definition: FIFO.h:64
const _T & front() const
Definition: FIFO.h:67
bool push(const _T &x)
Definition: FIFO.h:41
bool full() const
Definition: FIFO.h:101
_T pop()
Definition: FIFO.h:53
FIFO(unsigned int depth=0)
Definition: FIFO.h:23
unsigned int maxSize() const
Definition: FIFO.h:73
unsigned int size() const
Definition: FIFO.h:79
void clear()
Definition: FIFO.h:84
FIFO(const FIFO &f)
Definition: FIFO.h:33
_T & back()
Definition: FIFO.h:65
bool avail() const
Definition: FIFO.h:103
const _T & back() const
Definition: FIFO.h:68
bool empty() const
Definition: FIFO.h:70
void printOn(std::ostream &out=cout)
Definition: FIFO.h:90