generated from Persson-dev/Godot-Xmake
ByteBuffer: handle read errors
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <godot_cpp/variant/packed_byte_array.hpp>
|
||||
#include <godot_cpp/variant/string.hpp>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
namespace blitz {
|
||||
@@ -9,25 +10,17 @@ namespace protocol {
|
||||
|
||||
class PlayerInfo;
|
||||
|
||||
#define Operators(Type, GodotType) \
|
||||
ByteBuffer& operator>>(Type& a_Data) { \
|
||||
a_Data = m_Buffer.decode_##GodotType(m_ReadOffset); \
|
||||
m_ReadOffset += sizeof(a_Data); \
|
||||
return *this; \
|
||||
} \
|
||||
\
|
||||
ByteBuffer& operator<<(Type a_Data) { \
|
||||
m_Buffer.resize(m_Buffer.size() + sizeof(a_Data)); \
|
||||
m_Buffer.encode_##GodotType(m_Buffer.size() - sizeof(a_Data), a_Data); \
|
||||
return *this; \
|
||||
}
|
||||
|
||||
class ByteBuffer {
|
||||
private:
|
||||
godot::PackedByteArray m_Buffer;
|
||||
std::size_t m_ReadOffset;
|
||||
|
||||
public:
|
||||
class ReadError : public std::runtime_error {
|
||||
public:
|
||||
ReadError(const std::string& msg) : std::runtime_error(msg) {}
|
||||
};
|
||||
|
||||
ByteBuffer(godot::PackedByteArray&& a_Buffer) : m_Buffer(std::move(a_Buffer)), m_ReadOffset(0) {}
|
||||
ByteBuffer() : m_ReadOffset(0) {
|
||||
m_Buffer.resize(0);
|
||||
@@ -42,18 +35,30 @@ class ByteBuffer {
|
||||
}
|
||||
|
||||
// Integers
|
||||
Operators(int8_t, s8);
|
||||
Operators(uint8_t, u8);
|
||||
Operators(int16_t, s16);
|
||||
Operators(uint16_t, u16);
|
||||
Operators(int32_t, s32);
|
||||
Operators(uint32_t, u32);
|
||||
Operators(int64_t, s64);
|
||||
Operators(uint64_t, u64);
|
||||
ByteBuffer& operator<<(int8_t a_Data);
|
||||
ByteBuffer& operator>>(int8_t& a_Data);
|
||||
ByteBuffer& operator<<(uint8_t a_Data);
|
||||
ByteBuffer& operator>>(uint8_t& a_Data);
|
||||
|
||||
// Reals
|
||||
Operators(float, float);
|
||||
Operators(double, double);
|
||||
ByteBuffer& operator<<(int16_t a_Data);
|
||||
ByteBuffer& operator>>(int16_t& a_Data);
|
||||
ByteBuffer& operator<<(uint16_t a_Data);
|
||||
ByteBuffer& operator>>(uint16_t& a_Data);
|
||||
|
||||
ByteBuffer& operator<<(int32_t a_Data);
|
||||
ByteBuffer& operator>>(int32_t& a_Data);
|
||||
ByteBuffer& operator<<(uint32_t a_Data);
|
||||
ByteBuffer& operator>>(uint32_t& a_Data);
|
||||
|
||||
ByteBuffer& operator<<(int64_t a_Data);
|
||||
ByteBuffer& operator>>(int64_t& a_Data);
|
||||
ByteBuffer& operator<<(uint64_t a_Data);
|
||||
ByteBuffer& operator>>(uint64_t& a_Data);
|
||||
|
||||
ByteBuffer& operator<<(float a_Data);
|
||||
ByteBuffer& operator>>(float& a_Data);
|
||||
ByteBuffer& operator<<(double a_Data);
|
||||
ByteBuffer& operator>>(double& a_Data);
|
||||
|
||||
ByteBuffer& operator<<(const godot::String& a_Data);
|
||||
ByteBuffer& operator>>(godot::String& a_Data);
|
||||
|
||||
Reference in New Issue
Block a user