BDS Public
Beam-lib  2.16.3
This is the Beam C++ class library.
BFifo.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * BFifo.h BFifo class
3  * T.Barnaby, Beam Ltd, 2013-05-03
4  * Copyright (c) 2012 All Right Reserved, Beam Ltd, http://www.beam.ltd.uk
5  *******************************************************************************
6  *
7  * This is the BFifo class. It stores data in a ring buffer.
8  * It can store up to (size - 1) items.
9  * It has separate read and write pointers. These are thread safe.
10  * Note the read and write routines do not bounds check the Fifo. You have to use readAvailable()/writeAvailable()
11  * functions to check how much can be read/written before performing the reads or writes.
12  *
13  */
14 #ifndef BFifo_h
15 #define BFifo_h
16 
17 #include <BTypes.h>
18 #include <BError.h>
19 
20 template <class Type> class BFifo {
21 public:
22  BFifo(BUInt size);
23  ~BFifo();
24 
25  void clear();
26 
27  BUInt size();
29  BError rebase();
30 
33  BError write(const Type v);
34  BError write(const Type* data, BUInt num);
35 
36  Type* writeData();
37  Type* writeData(BUInt& num);
38  void writeDone(BUInt num);
39  void writeBackup(BUInt num);
40 
43  Type read();
44  BError read(Type* data, BUInt num);
45 
46  Type* readData();
47  Type* readData(BUInt& num);
48  void readDone(BUInt num);
49 
50  Type readPos(BUInt pos);
51  void writePos(BUInt pos, const Type& v);
52  Type& operator[](int pos);
53 
54 protected:
56  Type* odata;
57  volatile BUInt owritePos;
58  volatile BUInt oreadPos;
59 };
60 
61 #include <BFifo.inc>
62 #endif
void readDone(BUInt num)
Type * writeData()
Returns a pointer to the data.
Definition: BFifo.h:20
BUInt readAvailable()
How many items are available to read.
void clear()
Type readPos(BUInt pos)
Read item at given offset from current read position.
Type * odata
FIFO memory buffer.
Definition: BFifo.h:56
void writeBackup(BUInt num)
Backup, remove num items at end of fifo. Careful, make sure read is not already happening.
Type & operator[](int pos)
Direct access to read samples in buffer.
volatile BUInt oreadPos
The read pointer.
Definition: BFifo.h:58
void writePos(BUInt pos, const Type &v)
Write item at given offset from current read position.
volatile BUInt owritePos
The write pointer.
Definition: BFifo.h:57
BError rebase()
Rebases fifo so read pointer is at zero moving memory as needed.
BError resize(BUInt size)
Resize FIFO, clears it as well.
BUInt osize
The size of the FIFO.
Definition: BFifo.h:55
BError write(const Type v)
Write a single item.
void writeDone(BUInt num)
Indicates when write is complete.
BUInt readAvailableChunk()
How many items are available to read in a chunk.
BUInt32 BUInt
Definition: BTypes.h:30
Type read()
Read one item.
BUInt size()
Returns fifo size.
char data[8]
Definition: BoapMc1.h:21
BFifo(BUInt size)
Type * readData()
Returns a pointer to the data.
Definition: BError.h:25
BUInt writeAvailable()
How many items that can be written.
BUInt writeAvailableChunk()
How many items that can be written in a chunk.