From 70e00f817e9596a746800ba4afec2b7c4ca25142 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Sat, 4 Sep 2021 17:58:56 -0700 Subject: Migrate Template and TableTemplate to use DataStream --- core/src/Utils/IO/DataStream.hpp | 73 +++++++++++++++++---- core/src/Utils/IO/StringIntegration.hpp | 6 +- core/src/Utils/IO/TslArrayIntegration.hpp | 58 +++++++++++++++++ core/src/Utils/IO/TslRobinIntegration.hpp | 102 ++++++++++++++++++++++++++++++ core/src/Utils/IO/UuidIntegration.hpp | 4 +- core/src/Utils/IO/VectorIntegration.hpp | 38 +++++++---- core/src/Utils/Vector.hpp | 24 +++++++ 7 files changed, 277 insertions(+), 28 deletions(-) create mode 100644 core/src/Utils/IO/TslArrayIntegration.hpp create mode 100644 core/src/Utils/IO/TslRobinIntegration.hpp (limited to 'core/src/Utils') diff --git a/core/src/Utils/IO/DataStream.hpp b/core/src/Utils/IO/DataStream.hpp index fa22549..58fae7d 100644 --- a/core/src/Utils/IO/DataStream.hpp +++ b/core/src/Utils/IO/DataStream.hpp @@ -60,6 +60,15 @@ public: void Read(float& n); void Read(double& n); + template + requires std::is_enum_v + void ReadEnum(TEnum& e) + { + std::underlying_type_t n; + Read(n); + e = static_cast(e); + } + template void ReadObject(TObject& obj) { @@ -72,10 +81,26 @@ public: } } - template - void ReadObjectAdapted(TObject& obj, TAdapter&& adapter) + /// Helper to invoke either Read() or ReadObject(). Note that this function doesn't account for types that needs and adapter. + /// This is intended for writing IO adapters, users that's writing IO logic shouldn't using this - it increases compile time while reducing readability. + template + void ReadGeneric(T& t) + { + if constexpr (requires(T tt, InputDataStream ss) { ss.Read(tt); }) { + Read(t); + } else if constexpr (requires(T tt, InputDataStream ss) { ss.ReadEnum(tt); }) { + ReadEnum(t); + } else if constexpr (requires(T tt, InputDataStream ss) { ss.ReadObject(tt); }) { + ReadObject(t); + } else { + static_assert(false && sizeof(T), "This type is neither a 'value' nor an 'object'."); + } + } + + template + void ReadObjectAdapted(TObject& obj) { - adapter.ReadFromDataStream(*this, obj); + TAdapter::ReadFromDataStream(*this, obj); } public: @@ -99,10 +124,10 @@ public: ReadObject(obj); } - template - void ObjectAdapted(TObject& obj, TAdapter&& adapter) + template + void ObjectAdapted(TObject& obj) { - ReadObjectAdapted(obj, adapter); + ReadObjectAdapted(obj); } }; @@ -146,6 +171,14 @@ public: void Write(float n); void Write(double n); + template + requires std::is_enum_v + void WriteEnum(TEnum e) + { + auto n = static_cast>(e); + Write(n); + } + template void WriteObject(TObject& obj) { @@ -158,10 +191,26 @@ public: } } - template - void WriteObjectAdapted(TObject& obj, TAdapter&& adapter) + /// Helper to invoke either Write() or WriteObject(). Note that this function doesn't account for types that needs and adapter. + /// This is intended for writing IO adapters, users that's writing IO logic shouldn't using this - it increases compile time while reducing readability. + template + void WriteGeneric(T& t) + { + if constexpr (requires(T tt, OutputDataStream ss) { ss.Write(tt); }) { + Write(t); + } else if constexpr (requires(T tt, OutputDataStream ss) { ss.WriteEnum(tt); }) { + WriteEnum(t); + } else if constexpr (requires(T tt, OutputDataStream ss) { ss.WriteObject(tt); }) { + WriteObject(t); + } else { + static_assert(false && sizeof(T), "This type is neither a 'value' nor an 'object'."); + } + } + + template + void WriteObjectAdapted(TObject& obj) { - adapter.WriteToDataStream(*this, obj); + TAdapter::WriteToDataStream(*this, obj); } public: @@ -185,9 +234,9 @@ public: WriteObject(obj); } - template - void ObjectAdapted(TObject& obj, TAdapter&& adapter) + template + void ObjectAdapted(TObject& obj) { - WriteObjectAdapted(obj, adapter); + WriteObjectAdapted(obj); } }; diff --git a/core/src/Utils/IO/StringIntegration.hpp b/core/src/Utils/IO/StringIntegration.hpp index 8c5588c..20dc882 100644 --- a/core/src/Utils/IO/StringIntegration.hpp +++ b/core/src/Utils/IO/StringIntegration.hpp @@ -9,7 +9,7 @@ namespace DataStreamAdapters { struct String { - void ReadFromDataStream(InputDataStream& s, std::string& str) + static void ReadFromDataStream(InputDataStream& s, std::string& str) { uint64_t size; s.Read(size); @@ -19,7 +19,7 @@ struct String s.ReadBytes(size, std::back_inserter(str)); } - void WriteToDataStream(OutputDataStream& s, const std::string& str) + static void WriteToDataStream(OutputDataStream& s, const std::string& str) { s.Write((uint64_t)str.size()); s.WriteBytes(str.size(), str.data()); @@ -28,7 +28,7 @@ struct String struct StringView { - void WriteToDataStream(OutputDataStream& s, const std::string_view& str) + static void WriteToDataStream(OutputDataStream& s, const std::string_view& str) { s.Write((uint64_t)str.size()); s.WriteBytes(str.size(), str.data()); diff --git a/core/src/Utils/IO/TslArrayIntegration.hpp b/core/src/Utils/IO/TslArrayIntegration.hpp new file mode 100644 index 0000000..eebea02 --- /dev/null +++ b/core/src/Utils/IO/TslArrayIntegration.hpp @@ -0,0 +1,58 @@ +#pragma once + +#include "Utils/IO/DataStream.hpp" +#include "Utils/IO/StringIntegration.hpp" + +#include +#include +#include +#include + +// TODO support custom key types + +namespace DataStreamAdapters { +template +struct TslArrayMap +{ + template + static void ReadFromDataStream(InputDataStream& s, tsl::robin_map& map) + { + static_assert(std::is_default_constructible_v); + static_assert(std::is_move_constructible_v); + + uint64_t size; + s.Read(size); + map.reserve(size); + + for (uint64_t i = 0; i < size; ++i) { + std::string key; + s.ReadObjectAdapted(key); + + TValue value; + if constexpr (std::is_same_v) { + s.ReadGeneric(value); + } else { + s.ReadObjectAdapted(value); + } + + map.insert(key, std::move(value)); + } + } + + template + static void WriteToDataStream(OutputDataStream& s, const tsl::robin_map& map) + { + s.Write((uint64_t)map.size()); + + for (auto it = map.begin(); it != map.end(); ++it) { + s.WriteObjectAdapted(it.key_sv()); + + if constexpr (std::is_same_v) { + s.WriteGeneric(it.value()); + } else { + s.WriteObjectAdapted(it.value()); + } + } + } +}; +} // namespace DataStreamAdapters diff --git a/core/src/Utils/IO/TslRobinIntegration.hpp b/core/src/Utils/IO/TslRobinIntegration.hpp new file mode 100644 index 0000000..c800e52 --- /dev/null +++ b/core/src/Utils/IO/TslRobinIntegration.hpp @@ -0,0 +1,102 @@ +#pragma once + +#include "Utils/IO/DataStream.hpp" + +#include +#include +#include + +namespace DataStreamAdapters { +template +struct TslRobinMap +{ + template + static void ReadFromDataStream(InputDataStream& s, tsl::robin_map& map) + { + static_assert(std::is_default_constructible_v); + static_assert(std::is_move_constructible_v); + + uint64_t size; + s.Read(size); + map.reserve(size); + + for (uint64_t i = 0; i < size; ++i) { + TKey key; + if constexpr (std::is_same_v) { + s.ReadGeneric(key); + } else { + s.ReadObjectAdapted(key); + } + + TValue value; + if constexpr (std::is_same_v) { + s.ReadGeneric(value); + } else { + s.ReadObjectAdapted(value); + } + + map.insert(std::move(key), std::move(value)); + } + } + + template + static void WriteToDataStream(OutputDataStream& s, const tsl::robin_map& map) + { + s.Write((uint64_t)map.size()); + + for (auto it = map.begin(); it != map.end(); ++it) { + if constexpr (std::is_same_v) { + s.WriteGeneric(it.key()); + } else { + s.WriteObjectAdapted(it.key()); + } + + if constexpr (std::is_same_v) { + s.WriteGeneric(it.value()); + } else { + s.WriteObjectAdapted(it.value()); + } + } + } +}; + +template +struct TslRobinSet +{ + template + static void ReadFromDataStream(InputDataStream& s, tsl::robin_set& set) + { + static_assert(std::is_default_constructible_v); + static_assert(std::is_move_constructible_v); + + uint64_t size; + s.Read(size); + set.reserve(size); + + for (uint64_t i = 0; i < size; ++i) { + TElement element; + if constexpr (std::is_same_v) { + s.ReadGeneric(element); + } else { + s.ReadObjectAdapted(element); + } + + set.insert(std::move(element)); + } + } + + template + static void WriteToDataStream(OutputDataStream& s, const tsl::array_set& set) + { + s.Write((uint64_t)set.size()); + + for (auto& element : set) { + if constexpr (std::is_same_v) { + s.WriteGeneric(element); + } else { + s.WriteObjectAdapted(element); + } + } + } +}; +} // namespace DataStreamAdapters diff --git a/core/src/Utils/IO/UuidIntegration.hpp b/core/src/Utils/IO/UuidIntegration.hpp index 605f821..d8a0304 100644 --- a/core/src/Utils/IO/UuidIntegration.hpp +++ b/core/src/Utils/IO/UuidIntegration.hpp @@ -10,7 +10,7 @@ namespace DataStreamAdapters { struct Uuid { - void ReadFromDataStream(InputDataStream& s, uuids::uuid& uuid) + static void ReadFromDataStream(InputDataStream& s, uuids::uuid& uuid) { uint8_t buffer[16]; s.ReadBytes(16, buffer); @@ -18,7 +18,7 @@ struct Uuid uuid = uuids::uuid(gsl::span{ buffer }); } - void WriteToDataStream(OutputDataStream& s, const uuids::uuid& uuid) + static void WriteToDataStream(OutputDataStream& 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 index dedbf29..da663a4 100644 --- a/core/src/Utils/IO/VectorIntegration.hpp +++ b/core/src/Utils/IO/VectorIntegration.hpp @@ -2,31 +2,47 @@ #include "Utils/IO/DataStream.hpp" +#include #include namespace DataStreamAdapters { +template struct Vector { template - void ReadFromDataStream(InputDataStream& s, std::vecetor& vec) + static void ReadFromDataStream(InputDataStream& s, std::vector& vec) { - s.Write((uint64_t)vec.size()); - for (auto& element : vec) { - // TODO - } - } + static_assert(std::is_default_constructible_v); + static_assert(std::is_move_constructible_v); - template - void WriteToDataStream(OutputDataStream& s, const std::vecetor& vec) - { uint64_t size; - s >> size; + s.Read(size); vec.clear(); vec.reserve(size); for (uint64_t i = 0; i < size; ++i) { - // TODO + TElement element; + if constexpr (std::is_same_v) { + s.ReadGeneric(element); + } else { + s.ReadObjectAdapted(element); + } + + vec.push_back(std::move(element)); + } + } + + template + static void WriteToDataStream(OutputDataStream& s, const std::vector& vec) + { + s.Write((uint64_t)vec.size()); + for (auto& element : vec) { + if constexpr (std::is_same_v) { + s.WriteGeneric(element); + } else { + s.WriteObjectAdapted(element); + } } } }; diff --git a/core/src/Utils/Vector.hpp b/core/src/Utils/Vector.hpp index 7e71b73..3d4d251 100644 --- a/core/src/Utils/Vector.hpp +++ b/core/src/Utils/Vector.hpp @@ -15,6 +15,13 @@ struct Vec2 }; } + template + void OperateIOProxy(TProxy& proxy) + { + proxy.Value(x); + proxy.Value(y); + } + friend constexpr bool operator==(const Vec2& a, const Vec2& b) = default; friend constexpr Vec2 operator+(const Vec2& a, const Vec2& b) { return { a.x + b.x, a.y + b.y }; } @@ -48,6 +55,14 @@ struct Vec3 }; } + template + void OperateIOProxy(TProxy& proxy) + { + proxy.Value(x); + proxy.Value(y); + proxy.Value(z); + } + friend constexpr bool operator==(const Vec3& a, const Vec3& b) = default; friend constexpr Vec3 operator+(const Vec3& a, const Vec3& b) { return { a.x + b.x, a.y + b.y, a.z + b.z }; } @@ -83,6 +98,15 @@ struct Vec4 }; } + template + void OperateIOProxy(TProxy& proxy) + { + proxy.Value(x); + proxy.Value(y); + proxy.Value(z); + proxy.Value(w); + } + friend constexpr bool operator==(const Vec4& a, const Vec4& b) = default; friend constexpr Vec4 operator+(const Vec4& a, const Vec4& b) { return { a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w }; } -- cgit v1.2.3-70-g09d2