JD2022-TU1/main/extern/dtest/dtcore/inc/dtbytebuffer.h

137 lines
3.6 KiB
C++

#pragma once
#include "dtcore/inc/dtstring.h"
namespace dt
{
class ByteBuffer
{
public:
DT_CORE_DLL_ENTRY ByteBuffer();
DT_CORE_DLL_ENTRY ByteBuffer(const ByteBuffer& buf);
DT_CORE_DLL_ENTRY ByteBuffer(ByteBuffer&& buf);
DT_CORE_DLL_ENTRY ByteBuffer(const void* buffer, SizeT size);
DT_CORE_DLL_ENTRY ~ByteBuffer();
DT_CORE_DLL_ENTRY ByteBuffer& operator=(const ByteBuffer& buf);
DT_CORE_DLL_ENTRY bool operator==(const ByteBuffer& rhs) const;
DT_CORE_DLL_ENTRY bool operator!=(const ByteBuffer& rhs) const;
DT_CORE_DLL_ENTRY void Reserve(SizeT capacity);
DT_CORE_DLL_ENTRY SizeT Capacity() const;
DT_CORE_DLL_ENTRY void Resize(SizeT size);
DT_CORE_DLL_ENTRY SizeT Size() const;
DT_CORE_DLL_ENTRY const void *Front() const;
DT_CORE_DLL_ENTRY void *Front();
DT_CORE_DLL_ENTRY void WriteBack(SizeT size, const void *in);
template <typename T> void WriteBack(const T& in)
{
in.SerializeToBuffer(*this);
}
DT_CORE_DLL_ENTRY void WriteBackStringVector(const Strings& values);
DT_CORE_DLL_ENTRY void WriteBackString(const String& value);
DT_CORE_DLL_ENTRY void WriteBackBuffer(const ByteBuffer& value);
DT_CORE_DLL_ENTRY void WriteBackS32(S32 value);
DT_CORE_DLL_ENTRY void WriteBackS64(S64 value);
DT_CORE_DLL_ENTRY void WriteBackS32Vector(const Vector<S32>& values);
template <typename T> void WriteBackVector(const Vector<T>& values)
{
WriteBackS64(values.Size());
for (auto i = values.Begin(); i != values.End(); ++i)
{
(*i).SerializeToBuffer(*this);
}
}
DT_CORE_DLL_ENTRY void WriteBackU32(U32 value);
DT_CORE_DLL_ENTRY void WriteBackU64(U64 value);
DT_CORE_DLL_ENTRY void WriteBackBool(bool value);
DT_CORE_DLL_ENTRY SizeT Read(SizeT size, void *out);
template <typename T> bool Read(T& out)
{
return out.DeserializeFromBuffer(*this);
}
DT_CORE_DLL_ENTRY bool ReadStringVector(Strings& out);
template <typename T> bool ReadVector(Vector<T>& out)
{
SizeT size(0);
if (!ReadS64(size))
{
return false;
}
out.Resize(size);
for (auto i = out.Begin(); i != out.End(); ++i)
{
if (!(*i).DeserializeFromBuffer(*this))
{
return false;
}
}
return true;
}
DT_CORE_DLL_ENTRY bool ReadString(String& out);
DT_CORE_DLL_ENTRY bool ReadBuffer(ByteBuffer& out);
DT_CORE_DLL_ENTRY bool ReadS32(S32& out);
DT_CORE_DLL_ENTRY bool ReadS64(S64& out);
DT_CORE_DLL_ENTRY bool ReadS32Vector(Vector<S32>& out);
DT_CORE_DLL_ENTRY bool ReadU32(U32& out);
DT_CORE_DLL_ENTRY bool ReadU64(U64& out);
DT_CORE_DLL_ENTRY bool ReadBool(bool& out);
DT_CORE_DLL_ENTRY bool SetPosition(SizeT position);
DT_CORE_DLL_ENTRY SizeT Position() const;
DT_CORE_DLL_ENTRY SizeT Remain() const;
DT_CORE_DLL_ENTRY void Clear();
DT_CORE_DLL_ENTRY void Swap(ByteBuffer& rhs);
private:
Byte *m_Buffer;
SizeT m_Capacity;
SizeT m_Position;
SizeT m_Size;
bool m_ReadOnly;
};
DT_CORE_DLL_ENTRY String& operator<<(String& out, const ByteBuffer& buffer);
DT_CORE_DLL_ENTRY ByteBuffer& operator>>(String& in, ByteBuffer& buffer);
}