一种C++11风格的FIFO,支持任意长度的数据读写。
做单元测试的时候发现支持的类型不够通用,所以改为模板类型。
当时用的编辑器不支持中文,没办法,就用英文写了点简单的注释,代码也很好懂,不多说,直接上代码。
template <typename T> //para T is fifo store type
class XyFifo
{
public:
// initial fifo size
XyFifo(unsigned int size)
{
_fifoInputIndex = 0;
_fifoOutputIndex = 0;
_currentFifoHead = 0;
_bufferSize = size+1;
_fifoBuffer = new T[_bufferSize];
};
~XyFifo()
{
delete[]_fifoBuffer;
_fifoBuffer = nullptr;
};
//check whether the fifo is write full
bool isWriteFull()
{
auto inputIndex = _fifoInputIndex;
auto temp = (++inputIndex >= _bufferSize) ? 0 : inputIndex;
if (temp == _fifoOutputIndex)
{
return true;
}
return false;
};
//check whether the fif