- fixed buffer resize
- added readAt() git-svn-id: http://moon:8086/svn/software/trunk/libsrc/cpp@949 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
@@ -1,6 +1,3 @@
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
#include <cstring>
|
||||
|
||||
template<typename T>
|
||||
Buffer<T>::Buffer(size_t capacity)
|
||||
@@ -17,20 +14,33 @@ Buffer<T>::Buffer(size_t capacity)
|
||||
template<typename T>
|
||||
Buffer<T>::~Buffer()
|
||||
{
|
||||
delete [] m_data[0];
|
||||
delete [] m_data[1];
|
||||
resize(0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Buffer<T>::resize(size_t size)
|
||||
{
|
||||
delete [] m_data[0];
|
||||
delete [] m_data[1];
|
||||
m_data[0] = new T[size];
|
||||
m_data[1] = new T[size];
|
||||
const size_t N = sizeof(m_data)/sizeof(m_data[0]);
|
||||
for (int i=0; i < N; i++)
|
||||
{
|
||||
if (m_data[i] != nullptr)
|
||||
{
|
||||
delete m_data[i];
|
||||
m_data[i] = nullptr;
|
||||
}
|
||||
}
|
||||
m_capacity = size;
|
||||
|
||||
clear();
|
||||
if (size == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i=0; i < N; i++)
|
||||
{
|
||||
m_data[i] = new T[size];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@@ -116,6 +126,23 @@ size_t Buffer<T>::read(T* pData, size_t len)
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename Buffer<T>::Result Buffer<T>::readAt(size_t offset, T &x)
|
||||
{
|
||||
if (m_len == 0)
|
||||
{
|
||||
RETURN_OR_EXCEPT(Result::Err_InvalidSize);
|
||||
}
|
||||
size_t n = (m_ri+offset) % m_capacity;
|
||||
x = data_r()[n];
|
||||
if (offset)
|
||||
{
|
||||
consume(1);
|
||||
}
|
||||
|
||||
return Result::Ok;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename Buffer<T>::Result Buffer<T>::consume(size_t len)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user