C++11风格的FIFO
来源:互联网 发布:都玩网络ko三国 编辑:程序博客网 时间:2024/05/17 02:11
一种C++11风格的FIFO,支持任意长度的数据读写。
做单元测试的时候发现支持的类型不够通用,所以改为模板类型。
当时用的编辑器不支持中文,没办法,就用英文写了点简单的注释,代码也很好懂,不多说,直接上代码。
template <typename T> //para T is fifo store typeclass 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 fifo is read empty bool isReadEmpty() { return (_fifoInputIndex == _fifoOutputIndex) ? true : false; }; //write data to fifo,if the fifo is write full return false ,others return true //parameter bIntact ,true :write all data in once,if the fifo space not enough return false,or else return true //if bIntact is false ,write data one by one,if fifo write full return false,or else return true. bool writeData(const T* pdata, unsigned int len, bool bIntact = false) { auto inData = pdata; if (bIntact) { if (!isWriteSpaceEnough(len)) { return false; } for (unsigned int i=0; i < len; i++) { _fifoBuffer[_fifoInputIndex++] = *inData++; if (_fifoInputIndex >= _bufferSize) { _fifoInputIndex = 0; } } } else { for (unsigned int i=0; i < len; i++) { if (!isWriteFull()) { _fifoBuffer[_fifoInputIndex++] = *inData++; if (_fifoInputIndex >= _bufferSize) { _fifoInputIndex = 0; } } else { return false; } } } return true; }; //write one data only bool writeOneData(const T data) { if (!isWriteFull()) { _fifoBuffer[_fifoInputIndex++] = data; if (_fifoInputIndex >= _bufferSize) { _fifoInputIndex = 0; } return true; } else { return false; } } //read data from fifo ,if it is not read empty,return true ,or else return false bool readData(T* pdata,unsigned int len) { if (!isReadSpaceEnough(len)) { return false; } auto pOutBuffer = pdata; for (unsigned int i = 0; i < len;i++) { *pOutBuffer++ = _fifoBuffer[_fifoOutputIndex++]; if (_fifoOutputIndex>=_bufferSize) { _fifoOutputIndex = 0; } } return true; }; //read one data only bool readOneData(T& outData) { if (!isReadSpaceEnough(1)) { return false; } outData = _fifoBuffer[_fifoOutputIndex++]; if (_fifoOutputIndex >= _bufferSize) { _fifoOutputIndex = 0; } return true; }; //get fifo size const unsigned int getBufferSize(){ return _bufferSize; }; //clear fifo void resetFifo() { _fifoInputIndex = 0; _fifoOutputIndex = 0; memset(_fifoBuffer, 0, sizeof(_fifoBuffer)); } //resize the fifo size void resizeFifo(unsigned int len) { resetFifo(); if (len+1 == _bufferSize) { return; } if (_fifoBuffer) { delete[]_fifoBuffer; } _bufferSize = len+1; _fifoBuffer = new T[_bufferSize]; }private: T *_fifoBuffer; unsigned int _fifoInputIndex; unsigned int _fifoOutputIndex; unsigned int _currentFifoHead; unsigned int _bufferSize; const unsigned int getWriteSpace() { if (isWriteFull()) { return 0; } auto surplus = (_fifoInputIndex >= _fifoOutputIndex) ? (_bufferSize - _fifoInputIndex + _fifoOutputIndex) : (_fifoOutputIndex - _fifoInputIndex); return surplus; } const bool isWriteSpaceEnough(unsigned int len) { return (getWriteSpace() >= len) ? true : false; } const unsigned int getReadSpace() { if (isReadEmpty()) { return 0; } auto surplus = (_fifoInputIndex >= _fifoOutputIndex) ? (_fifoInputIndex - _fifoOutputIndex) : (_bufferSize - _fifoOutputIndex + _fifoInputIndex); return surplus; } const bool isReadSpaceEnough(unsigned int len) { return (getReadSpace() >= len) ? true : false; }};
0 0
- C++11风格的FIFO
- fifo的c语言实现
- fifo.c
- C风格的字符串
- c风格的字符串
- C风格的字符串
- c风格的字符串
- Linux C 的风格
- c风格的控制台
- c语言的编程风格
- c语言的编程风格
- c语言的编程风格
- c语言的编程风格
- c语言的编程风格
- C语言的编程风格
- c语言的编程风格
- C风格字符串的类型
- c语言的编程风格
- 如何去自律(时刻警醒!!!!!!)
- HDOJ -- 1874畅通工程续
- **YII** 系列八应用结构
- OpenCV学习笔记——视频的边缘检测
- Eclipse工程运行时出现Select Java Application的问题
- C++11风格的FIFO
- android App 禁止系统修改字体
- POJ 2778 DNA Sequence
- android关于jni的使用步骤
- jquery实现动态生成和删除输入框
- 淘宝druid数据库连接池使用示例
- Redis cluster:redis集群架构说明
- iOS 认识CoreData-基础
- TextView设置边框,EditText常见问题