From 6bc8e491376f485b2f1db424dbbe0e35052c447c Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq。com> Date: Fri, 28 Aug 2020 00:27:10 +0800 Subject: [PATCH] no message --- .vscode/settings.json | 5 ++++ general/src/pattern/ringbuffer.hpp | 47 +++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c2dcede --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "*.rh": "cpp" + } +} \ No newline at end of file diff --git a/general/src/pattern/ringbuffer.hpp b/general/src/pattern/ringbuffer.hpp index dccd6de..7dffc63 100644 --- a/general/src/pattern/ringbuffer.hpp +++ b/general/src/pattern/ringbuffer.hpp @@ -10,17 +10,29 @@ template class RingBuffer{ public: T At(uint64_t pos); RingBuffer(uint64_t size); + int Add(T *data,uint64_t len); + int Take(T *data,uint64_t data); + uint32_t CanReadCount(); + uint32_t CanWriteCount(); ~RingBuffer(); protected: T *mData; uint64_t mSize; + uint64_t mCurrentTail; + uint64_t mCurrentHead; }; +/** + * author : caiyuzheng + * function: 构造函数 +*/ template RingBuffer::RingBuffer(uint64_t size) : mData(nullptr), - mSize(0) { + mSize(0), + mCurrentTail(0), + mCurrentHead(0) { mSize = size; mData = new T[size]; } @@ -34,4 +46,37 @@ T RingBuffer::At(uint64_t pos){ return mData[pos]; } +template +int RingBuffer::Add(T *data,uint64_t len){ + /// 0->n |start|head|.....|tail| + if(mCurrentTail > mCurrentHead){ + if(mCurrentTail + len > mSize - 1){ + uint64_t tmp = mCurrentTail + len - mSize; + for(uint32_t i = 0;i < len;i++){ + if(mCurrentTail < mSize){ + data[mCurrentTail++] = data[len]; + } + } + } + } +} + +template +uint32_t RingBuffer::CanReadCount(){ + if(mCurrentHead > mCurrentTail){ + return mSize - (mCurrentHead - mCurrentTail); + }else{ + return mCurrentTail - mCurrentHead; + } +} + +template +uint32_t RingBuffer::CanWriteCount(){ + if (mCurrentTail > mCurrentHead){ + return mSize - (mCurrentTail - mCurrentHead); + } + if(mCurrentHead > mCurrentTail){ + return mCurrentHead - mCurrentTail; + } +} #endif