- 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:
@@ -2,7 +2,7 @@
|
||||
#define __PROCESSOR_BUFFER_HPP__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
#include <exception>
|
||||
#include <algorithm>
|
||||
|
||||
@@ -12,7 +12,8 @@
|
||||
#define RETURN_OR_EXCEPT(code) return code
|
||||
#endif
|
||||
|
||||
|
||||
namespace Processor
|
||||
{
|
||||
class IBuffer
|
||||
{
|
||||
public:
|
||||
@@ -59,6 +60,7 @@ class Buffer : public IBuffer
|
||||
T* data_r();
|
||||
size_t write(const T* pData, size_t len);
|
||||
size_t read(T* pData, size_t len);
|
||||
Result readAt(size_t offset, T &x);
|
||||
Result consume(size_t len);
|
||||
Result produce(size_t len);
|
||||
void print(const char *prefix);
|
||||
@@ -98,5 +100,6 @@ class Buffer : public IBuffer
|
||||
};
|
||||
|
||||
#include "Buffer.tcc"
|
||||
};
|
||||
|
||||
#endif // __PROCESSOR_BUFFER_HPP__
|
||||
@@ -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