From c51a61c0f0de65a3e64f589816a56f21ed4e8528 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Sun, 15 Aug 2021 17:14:06 -0700 Subject: Initial work on data streams --- core/src/Utils/IO/Adapter.cpp | 1 + core/src/Utils/IO/Adapter.hpp | 87 ++++++++++++++++++++++ core/src/Utils/IO/DataStream.cpp | 1 + core/src/Utils/IO/DataStream.hpp | 123 ++++++++++++++++++++++++++++++++ core/src/Utils/IO/StringIntegration.hpp | 29 ++++++++ core/src/Utils/IO/UuidIntegration.hpp | 22 ++++++ core/src/Utils/IO/VectorIntegration.hpp | 28 ++++++++ core/src/Utils/IO/fwd.hpp | 8 +++ core/src/Utils/fwd.hpp | 2 + 9 files changed, 301 insertions(+) create mode 100644 core/src/Utils/IO/Adapter.cpp create mode 100644 core/src/Utils/IO/Adapter.hpp create mode 100644 core/src/Utils/IO/DataStream.cpp create mode 100644 core/src/Utils/IO/DataStream.hpp create mode 100644 core/src/Utils/IO/StringIntegration.hpp create mode 100644 core/src/Utils/IO/UuidIntegration.hpp create mode 100644 core/src/Utils/IO/VectorIntegration.hpp create mode 100644 core/src/Utils/IO/fwd.hpp (limited to 'core/src/Utils') diff --git a/core/src/Utils/IO/Adapter.cpp b/core/src/Utils/IO/Adapter.cpp new file mode 100644 index 0000000..bd094d4 --- /dev/null +++ b/core/src/Utils/IO/Adapter.cpp @@ -0,0 +1 @@ +#include "Adapter.hpp" diff --git a/core/src/Utils/IO/Adapter.hpp b/core/src/Utils/IO/Adapter.hpp new file mode 100644 index 0000000..e9e8fb2 --- /dev/null +++ b/core/src/Utils/IO/Adapter.hpp @@ -0,0 +1,87 @@ +#pragma once + +#include "Utils/IO/DataStream.hpp" + +#include + +class SerializationAdapter +{ +public: + static constexpr bool IsSerializer() + { + return true; + } + +public: + DataStream* Stream; + + template + void Bytes(size_t byteCount, T* buffer) const + { + Stream->WriteBytes(byteCount, buffer); + } + + template + void Value(T t) const + { + Stream->Write(t); + } + + template + void Object(TObject& obj) const + { + Stream->WriteObject(obj); + } +}; + +class DeserializationAdapter +{ +public: + static constexpr bool IsSerializer() + { + return false; + } + +public: + DataStream* Stream; + + template + void Bytes(size_t byteCount, T* buffer) const + { + Stream->WriteBytes(byteCount, buffer); + } + + template + void Value(T& t) const + { + Stream->Read(t); + } + + template + void Object(TObject& obj) const + { + Stream->ReadObject(obj); + } +}; + +template +requires requires(T t) +{ + t.OperateIOAdapter(std::declval()); +} +void ReadFromDataStream(DataStream& stream, T& obj) +{ + DeserializationAdapter adapter{ &stream }; + obj.OperateIOAdapter(adapter); +} + +template +requires requires(T t) +{ + t.OperateIOAdapter(std::declval()); +} +void WriteToDataStream(DataStream& stream, T& obj) +{ + SerializationAdapter adapter{ &stream }; + obj.OperateIOAdapter(adapter); +} diff --git a/core/src/Utils/IO/DataStream.cpp b/core/src/Utils/IO/DataStream.cpp new file mode 100644 index 0000000..bca357a --- /dev/null +++ b/core/src/Utils/IO/DataStream.cpp @@ -0,0 +1 @@ +#include "DataStream.hpp" diff --git a/core/src/Utils/IO/DataStream.hpp b/core/src/Utils/IO/DataStream.hpp new file mode 100644 index 0000000..6babf63 --- /dev/null +++ b/core/src/Utils/IO/DataStream.hpp @@ -0,0 +1,123 @@ +#pragma once + +#include +#include +#include + +class DataStream +{ +public: + enum Endianness + { + BigEndian, + LittleEndian, + }; + +private: + Endianness mEndian; + +public: + DataStream(); + ~DataStream(); + + Endianness GetEndianness() const; + void SetEndianness(Endianness endianness); + + void ReadBytes(size_t byteCount, std::byte* buffer); + void ReadBytes(size_t byteCount, char* buffer); + void ReadBytes(size_t byteCount, signed char* buffer); + void ReadBytes(size_t byteCount, unsigned char* buffer); + + template + void ReadBytes(size_t byteCount, TInserter&& inserter) + { + for (size_t i = 0; i < byteCount; ++i) { + uint8_t byte; + Read(byte); + + inserter = byte; + } + } + + void WriteBytes(size_t byteCount, const std::byte* buffer); + void WriteBytes(size_t byteCount, const char* buffer); + void WriteBytes(size_t byteCount, const signed char* buffer); + void WriteBytes(size_t byteCount, const unsigned char* buffer); + + template + void WriteBytes(TIterator&& begin, TIterator&& end) + { + for (; begin != end; ++begin) { + uint8_t byte = *begin; + Write(byte); + } + } + + void Write(int8_t n); + void Write(int16_t n); + void Write(int32_t n); + void Write(int64_t n); + + void Read(int8_t& n); + void Read(int16_t& n); + void Read(int32_t& n); + void Read(int64_t& n); + + void Write(uint8_t n); + void Write(uint16_t n); + void Write(uint32_t n); + void Write(uint64_t n); + + void Read(uint8_t& n); + void Read(uint16_t& n); + void Read(uint32_t& n); + void Read(uint64_t& n); + + void Write(float n); + void Write(double n); + + void Read(float& n); + void Read(double& n); + + template + requires requires(TObject obj) + { + obj.ReadFromDataStream(std::declval()); + } + void ReadObject(TObject& obj) + { + obj.ReadFromDataStream(*this); + } + + template + requires requires(TObject obj) + { + // Let ADL happen + ReadFromDataStream(std::declval(), obj); + } + void ReadObject(TObject& obj) + { + ReadFromDataStream(*this, obj); + } + + template + requires requires(TObject obj) + { + obj.WriteToDataStream(std::declval()); + } + void WriteObject(TObject& obj) + { + obj.ReadFromDataStream(*this); + } + + template + requires requires(TObject obj) + { + // Let ADL happen + WriteToDataStream(std::declval(), obj); + } + void WriteObject(TObject& obj) + { + WriteToDataStream(*this, obj); + } +}; diff --git a/core/src/Utils/IO/StringIntegration.hpp b/core/src/Utils/IO/StringIntegration.hpp new file mode 100644 index 0000000..d4be23a --- /dev/null +++ b/core/src/Utils/IO/StringIntegration.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include "Utils/IO/DataStream.hpp" + +#include +#include +#include + +void ReadFromDataStream(DataStream& s, std::string& str) +{ + uint64_t size; + s.Read(size); + + str = {}; + str.reserve(size); + s.ReadBytes(size, std::back_inserter(str)); +} + +void WriteToDataStream(DataStream& s, const std::string& str) +{ + s.Write((uint64_t)str.size()); + s.WriteBytes(str.size(), str.data()); +} + +void WriteToDataStream(DataStream& s, const std::string_view& str) +{ + s.Write((uint64_t)str.size()); + s.WriteBytes(str.size(), str.data()); +} \ No newline at end of file diff --git a/core/src/Utils/IO/UuidIntegration.hpp b/core/src/Utils/IO/UuidIntegration.hpp new file mode 100644 index 0000000..202e021 --- /dev/null +++ b/core/src/Utils/IO/UuidIntegration.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "Utils/IO/DataStream.hpp" + +#include +#include +#include +#include + +void ReadFromDataStream(DataStream& s, uuids::uuid& uuid) +{ + uint8_t buffer[16]; + s.ReadBytes(16, buffer); + + uuid = uuids::uuid(gsl::span{ buffer }); +} + +void WriteToDataStream(DataStream& s, const uuids::uuid& uuid) +{ + auto gslSpan = uuid.as_bytes(); + s.WriteBytes(gslSpan.size(), gslSpan.data()); +} diff --git a/core/src/Utils/IO/VectorIntegration.hpp b/core/src/Utils/IO/VectorIntegration.hpp new file mode 100644 index 0000000..b2c751c --- /dev/null +++ b/core/src/Utils/IO/VectorIntegration.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include "Utils/IO/DataStream.hpp" + +#include + +template +void ReadFromDataStream(DataStream& s, std::vecetor& vec) +{ + s.Write((uint64_t)vec.size()); + for (auto& element : vec) { + // TODO + } +} + +template +void WriteToDataStream(DataStream& s, const std::vecetor& vec) +{ + uint64_t size; + s >> size; + + vec.clear(); + vec.reserve(size); + + for (uint64_t i = 0; i < size; ++i) { + // TODO + } +} diff --git a/core/src/Utils/IO/fwd.hpp b/core/src/Utils/IO/fwd.hpp new file mode 100644 index 0000000..48d8cfb --- /dev/null +++ b/core/src/Utils/IO/fwd.hpp @@ -0,0 +1,8 @@ +#pragma onceDeserializationAdapter + +// Adapter.hpp +class SerializationAdapter; +class DeserializationAdapter; + +// DataStream.hpp +class DataStream; diff --git a/core/src/Utils/fwd.hpp b/core/src/Utils/fwd.hpp index 5d1fe45..c740bd1 100644 --- a/core/src/Utils/fwd.hpp +++ b/core/src/Utils/fwd.hpp @@ -1,5 +1,7 @@ #pragma once +#include "Utils/IO/fwd.hpp" + // Color.hpp class RgbaColor; class HsvColor; -- cgit v1.2.3-70-g09d2