2,180
社区成员
发帖
与我相关
我的任务
分享
#include "VxWorks.h"
#include "loglib.h"
#include "BtTypes.hpp"
namespace Bt {
template<typename Type, UInt32 FixedSize = 32>
class AbstractBuffer {
protected:
AbstractBuffer() : m_Size(0) {}
virtual ~AbstractBuffer() = 0;
void copy(const AbstractBuffer& other) {
for (UInt32 i = 0; i < FixedSize; i++) {
m_Buffer[i] = other.m_Buffer[i];
}
m_Size = other.m_Size;
}
UInt32 m_Size;
Type m_Buffer[FixedSize];
};
template<typename Type, UInt32 FixedSize>
inline AbstractBuffer<Type, FixedSize>::~AbstractBuffer() {}
template<typename Type, UInt32 FixedSize = 32>
class SimpleBuffer: public AbstractBuffer<Type, FixedSize> {
typedef AbstractBuffer<Type, FixedSize> Base;
public:
SimpleBuffer() : AbstractBuffer<Type, FixedSize>() {
}
virtual ~SimpleBuffer() {
}
SimpleBuffer(const SimpleBuffer& other) {
copy(other);
}
SimpleBuffer& operator=(const SimpleBuffer& other) {
copy(other);
return (*this);
}
void Push(Type item) {
if (Base::m_Size < FixedSize) {
Base::m_Buffer[Base::m_Size++] = item;
} else {
// TODO : Shall I throw an exception?
}
}
void Assign(UInt32 offset, Type item) {
if (offset < Base::m_Size) {
Base::m_Buffer[offset] = item;
}
}
Type operator[](UInt32 offset) {
if (offset < Base::m_Size) {
return Base::m_Buffer[offset];
} else {
// TODO : Shall I throw an exception, too?
return (Type) Null;
}
}
UInt32 Length() const {
return Base::m_Size;
}
void Clear() {
Base::m_Size = 0;
}
void Reverse() {
}
void Insert(UInt32 offset, Type item) {
}
Bool Remove(Type item) {
if (Base::m_Size > 0) {
UInt32 offset = 0;
while ((offset < Base::m_Size) && (item != Base::m_Buffer[offset])) {
offset++;
}
if (offset < Base::m_Size) {
while (offset < Base::m_Size - 1) {
Base::m_Buffer[offset] = Base::m_Buffer[offset + 1];
offset++;
}
if (Base::m_Size > 0) Base::m_Size--;
return True;
} else {
return False;
}
} else {
return False;
}
}
};
}
Bt::SimpleBuffer<int> Buffer;
void init_buffer() {
for (int i = 0; i < 10; i++) {
Buffer.Push(i);
}
}
void check_buffer() {
for (int i = 0; i < Buffer.Length(); i++) {
if (Buffer[i] == i) {
logMsg("%d Checked\n", i, Buffer[i], 0, 0, 0, 0);
}
}
}