From 22ad0c7c81824e1f41c099c5b33d2ca8085c5ee9 Mon Sep 17 00:00:00 2001 From: a7458969 <290198252@qq.com> Date: Fri, 15 May 2020 00:33:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0ringbuffer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- general/src/algorithm/ringbuffer.hpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/general/src/algorithm/ringbuffer.hpp b/general/src/algorithm/ringbuffer.hpp index 473f09c..a40abd2 100644 --- a/general/src/algorithm/ringbuffer.hpp +++ b/general/src/algorithm/ringbuffer.hpp @@ -5,13 +5,13 @@ using namespace std; template class RingBuffer{ public: - typedef enum Error{ + enum Error{ Error = -10, ERROR_FULL = -11, ERROR_NO_ENOUGH_WRITE = -12, }; - RingBuffer(uint16_t len){ - mBuffer = new[len]; + RingBuffer(uint16_t len) { + mBuffer = new T[len]; mHead = mTail = mBuffer; if (mBuffer == nullptr){ this->mLen = 0; @@ -35,7 +35,7 @@ public: { int copy_sz = min(count, this->ReadableCnt()); memcpy(data, mHead, copy_sz * sizeof(T)); - rb->rb_head += copy_sz * sizeof(T); + mHead += copy_sz * sizeof(T); return copy_sz; } else // 头部大于尾部 @@ -43,8 +43,8 @@ public: if (count < mLen -(mHead - mBuffer)) // (rb->rb_head - rb->rb_buff) 是从头部到数组物理结束的长度,如果小于说明不需要读取数组头部的,只需要copy一次 { int copy_sz = count*sizeof(T); - memcpy(data, rb->rb_head, copy_sz*sizeof(T)); - rb->rb_head += copy_sz; + memcpy(data, mHead, copy_sz*sizeof(T)); + mHead += copy_sz; return copy_sz; } else @@ -59,14 +59,14 @@ public: } } int16_t Write(const T *in,uint16_t len){ - if (len >= WriteableCnt(rb)) + if (len >= WriteableCnt()) return ERROR_NO_ENOUGH_WRITE; if (mHead <= mTail) // 头部小于尾部 { int tail_avail_sz = mLen - (mTail - this->mBuffer); if (len <= tail_avail_sz) // 是否需要循环到0开始写 { - memcpy(rb->rb_tail, in, len*sizeof(T)); + memcpy(mTail, in, len*sizeof(T)); mTail += len; if (mTail == mBuffer + mLen) // 刚好相等的情况 mTail = mBuffer; @@ -75,8 +75,8 @@ public: else { memcpy(mTail, in, tail_avail_sz*sizeof(T)); - mTail = rb->rb_buff; - return tail_avail_sz + Write(rb, (char*)in + tail_avail_sz, (len - tail_avail_sz)*sizeof(T)); + mTail = mBuffer; + return tail_avail_sz + Write( (T*)in + tail_avail_sz* sizeof(T), (len - tail_avail_sz)); } } else