diff options
author | rtk0c <[email protected]> | 2021-08-15 17:14:06 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2021-08-15 17:14:06 -0700 |
commit | c51a61c0f0de65a3e64f589816a56f21ed4e8528 (patch) | |
tree | ba1737b64d5307f9dedc934d9398297d3fb8ee82 /core/src/Utils/IO | |
parent | 64a6dbcfdb89a5f57d93d47a2be0c741dda4662d (diff) |
Initial work on data streams
Diffstat (limited to 'core/src/Utils/IO')
-rw-r--r-- | core/src/Utils/IO/Adapter.cpp | 1 | ||||
-rw-r--r-- | core/src/Utils/IO/Adapter.hpp | 87 | ||||
-rw-r--r-- | core/src/Utils/IO/DataStream.cpp | 1 | ||||
-rw-r--r-- | core/src/Utils/IO/DataStream.hpp | 123 | ||||
-rw-r--r-- | core/src/Utils/IO/StringIntegration.hpp | 29 | ||||
-rw-r--r-- | core/src/Utils/IO/UuidIntegration.hpp | 22 | ||||
-rw-r--r-- | core/src/Utils/IO/VectorIntegration.hpp | 28 | ||||
-rw-r--r-- | core/src/Utils/IO/fwd.hpp | 8 |
8 files changed, 299 insertions, 0 deletions
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 <utility> + +class SerializationAdapter +{ +public: + static constexpr bool IsSerializer() + { + return true; + } + +public: + DataStream* Stream; + + template <class T> + void Bytes(size_t byteCount, T* buffer) const + { + Stream->WriteBytes(byteCount, buffer); + } + + template <class T> + void Value(T t) const + { + Stream->Write(t); + } + + template <class TObject> + void Object(TObject& obj) const + { + Stream->WriteObject(obj); + } +}; + +class DeserializationAdapter +{ +public: + static constexpr bool IsSerializer() + { + return false; + } + +public: + DataStream* Stream; + + template <class T> + void Bytes(size_t byteCount, T* buffer) const + { + Stream->WriteBytes(byteCount, buffer); + } + + template <class T> + void Value(T& t) const + { + Stream->Read(t); + } + + template <class TObject> + void Object(TObject& obj) const + { + Stream->ReadObject(obj); + } +}; + +template <class T> +requires requires(T t) +{ + t.OperateIOAdapter(std::declval<DeserializationAdapter>()); +} +void ReadFromDataStream(DataStream& stream, T& obj) +{ + DeserializationAdapter adapter{ &stream }; + obj.OperateIOAdapter(adapter); +} + +template <class T> +requires requires(T t) +{ + t.OperateIOAdapter(std::declval<SerializationAdapter>()); +} +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 <cstddef> +#include <cstdint> +#include <span> + +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 <class TInserter> + 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 <class TIterator> + 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 <class TObject> + requires requires(TObject obj) + { + obj.ReadFromDataStream(std::declval<DataStream>()); + } + void ReadObject(TObject& obj) + { + obj.ReadFromDataStream(*this); + } + + template <class TObject> + requires requires(TObject obj) + { + // Let ADL happen + ReadFromDataStream(std::declval<DataStream>(), obj); + } + void ReadObject(TObject& obj) + { + ReadFromDataStream(*this, obj); + } + + template <class TObject> + requires requires(TObject obj) + { + obj.WriteToDataStream(std::declval<DataStream>()); + } + void WriteObject(TObject& obj) + { + obj.ReadFromDataStream(*this); + } + + template <class TObject> + requires requires(TObject obj) + { + // Let ADL happen + WriteToDataStream(std::declval<DataStream>(), 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 <iterator> +#include <string> +#include <string_view> + +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 <uuid/uuid.h> +#include <cstddef> +#include <cstdint> +#include <iterator> + +void ReadFromDataStream(DataStream& s, uuids::uuid& uuid) +{ + uint8_t buffer[16]; + s.ReadBytes(16, buffer); + + uuid = uuids::uuid(gsl::span<uint8_t, 16>{ 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 <vector> + +template <class TElement> +void ReadFromDataStream(DataStream& s, std::vecetor<TElement>& vec) +{ + s.Write((uint64_t)vec.size()); + for (auto& element : vec) { + // TODO + } +} + +template <class TElement> +void WriteToDataStream(DataStream& s, const std::vecetor<TElement>& 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; |