diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/src/Model/Assets.cpp | 21 | ||||
-rw-r--r-- | core/src/Model/Assets.hpp | 4 | ||||
-rw-r--r-- | core/src/Model/Template/TableTemplate.cpp | 84 | ||||
-rw-r--r-- | core/src/Model/Template/TableTemplate.hpp | 15 | ||||
-rw-r--r-- | core/src/Model/Template/Template.hpp | 2 | ||||
-rw-r--r-- | core/src/Model/Template/Template_Main.cpp | 32 | ||||
-rw-r--r-- | core/src/Utils/IO/Archive.cpp | 5 | ||||
-rw-r--r-- | core/src/Utils/IO/Archive.hpp | 4 | ||||
-rw-r--r-- | core/src/Utils/IO/DataStream.hpp | 68 | ||||
-rw-r--r-- | core/src/Utils/IO/Helper.hpp | 43 | ||||
-rw-r--r-- | core/src/Utils/IO/StringIntegration.hpp | 18 | ||||
-rw-r--r-- | core/src/Utils/IO/TslArrayIntegration.hpp | 26 | ||||
-rw-r--r-- | core/src/Utils/IO/TslRobinIntegration.hpp | 54 | ||||
-rw-r--r-- | core/src/Utils/IO/UuidIntegration.hpp | 8 | ||||
-rw-r--r-- | core/src/Utils/IO/VectorIntegration.hpp | 21 | ||||
-rw-r--r-- | core/src/Utils/Vector.hpp | 50 |
16 files changed, 232 insertions, 223 deletions
diff --git a/core/src/Model/Assets.cpp b/core/src/Model/Assets.cpp index 80c2fa4..40f300e 100644 --- a/core/src/Model/Assets.cpp +++ b/core/src/Model/Assets.cpp @@ -16,21 +16,26 @@ using namespace std::literals::string_view_literals; namespace fs = std::filesystem; -template <class TProxy> -void SavedAsset::OperateIOProxy(TProxy& proxy) +template <class TSavedAsset, class TStream> +void OperateStreamForSavedAsset(TSavedAsset& cell, TStream& proxy) { - proxy.template ObjectAdapted<DataStreamAdapters::String>(Name); - proxy.template ObjectAdapted<DataStreamAdapters::Uuid>(Uuid); - proxy.Value(Payload); + proxy.template ObjectAdapted<DataStreamAdapters::String>(cell.Name); + proxy.template ObjectAdapted<DataStreamAdapters::Uuid>(cell.Uuid); + proxy.Value(cell.Payload); } -template void SavedAsset::OperateIOProxy(InputDataStream& adapter); -template void SavedAsset::OperateIOProxy(OutputDataStream& adapter); +void SavedAsset::ReadFromDataStream(InputDataStream& stream) +{ + ::OperateStreamForSavedAsset(*this, stream); +} -Asset::Asset() +void SavedAsset::WriteToDataStream(OutputDataStream& stream) const { + ::OperateStreamForSavedAsset(*this, stream); } +Asset::Asset() = default; + class AssetList::Private { public: diff --git a/core/src/Model/Assets.hpp b/core/src/Model/Assets.hpp index 3b38dff..3d90d3f 100644 --- a/core/src/Model/Assets.hpp +++ b/core/src/Model/Assets.hpp @@ -20,8 +20,8 @@ struct SavedAsset /// Extra data to be used by the AssetList/Asset implementation. uint64_t Payload; - template <class TProxy> - void OperateIOProxy(TProxy& proxy); + void ReadFromDataStream(InputDataStream& stream); + void WriteToDataStream(OutputDataStream& stream) const; }; class Asset diff --git a/core/src/Model/Template/TableTemplate.cpp b/core/src/Model/Template/TableTemplate.cpp index a5745c7..16cd333 100644 --- a/core/src/Model/Template/TableTemplate.cpp +++ b/core/src/Model/Template/TableTemplate.cpp @@ -27,22 +27,29 @@ bool TableCell::IsMergedCell() const return PrimaryCellLocation.x == -1 || PrimaryCellLocation.y == -1; } -template <class TProxy> -void TableCell::OperateIOProxy(TProxy& proxy) +template <class TTableCell, class TStream> +void OperateStreamForTableCell(TTableCell& cell, TStream& proxy) { - proxy.template ObjectAdapted<DataStreamAdapters::String>(Content); - proxy.Object(Location); - proxy.Object(PrimaryCellLocation); - proxy.Value(SpanX); - proxy.Value(SpanY); - proxy.Value(HorizontalAlignment); - proxy.Value(VerticalAlignment); - proxy.Value(Type); - proxy.Value(DataId); + proxy.template ObjectAdapted<DataStreamAdapters::String>(cell.Content); + proxy.Object(cell.Location); + proxy.Object(cell.PrimaryCellLocation); + proxy.Value(cell.SpanX); + proxy.Value(cell.SpanY); + proxy.Enum(cell.HorizontalAlignment); + proxy.Enum(cell.VerticalAlignment); + proxy.Enum(cell.Type); + proxy.Value(cell.DataId); } -template void TableCell::OperateIOProxy(InputDataStream& adapter); -template void TableCell::OperateIOProxy(OutputDataStream& adapter); +void TableCell::ReadFromDataStream(InputDataStream& stream) +{ + ::OperateStreamForTableCell(*this, stream); +} + +void TableCell::WriteToDataStream(OutputDataStream& stream) const +{ + ::OperateStreamForTableCell(*this, stream); +} Vec2i TableArrayGroup::GetLeftCell() const { @@ -86,16 +93,23 @@ bool TableArrayGroup::UpdateCellName(std::string_view oldName, std::string_view return ::UpdateElementName(mName2Cell, oldName, newName); } -template <class TProxy> -void TableArrayGroup::OperateIOProxy(TProxy& proxy) +template <class TTableArrayGroup, class TStream> +void OperateStreamForTableArrayGroup(TTableArrayGroup& group, TStream& stream) +{ + stream.Value(group.Row); + stream.Value(group.LeftCell); + stream.Value(group.RightCell); +} + +void TableArrayGroup::ReadFromDataStream(InputDataStream& stream) { - proxy.Value(Row); - proxy.Value(LeftCell); - proxy.Value(RightCell); + ::OperateStreamForTableArrayGroup(*this, stream); } -template void TableArrayGroup::OperateIOProxy(InputDataStream& adapter); -template void TableArrayGroup::OperateIOProxy(OutputDataStream& adapter); +void TableArrayGroup::WriteToDataStream(OutputDataStream& stream) const +{ + ::OperateStreamForTableArrayGroup(*this, stream); +} TableInstantiationParameters::TableInstantiationParameters(const TableTemplate& table) : mTable{ &table } @@ -549,23 +563,27 @@ lxw_worksheet* TableTemplate::InstantiateToExcelWorksheet(lxw_workbook* workbook return worksheet; } -void TableTemplate::ReadFromDataStream(InputDataStream& stream) +class TableTemplate::Private { - OperateIOProxy(stream); -} +public: + template <class TTableTemplate, class TProxy> + static void OperateStream(TTableTemplate& table, TProxy& proxy) + { + proxy.template ObjectAdapted<DataStreamAdapters::Vector<>>(table.mColumnWidths); + proxy.template ObjectAdapted<DataStreamAdapters::Vector<>>(table.mRowHeights); + proxy.template ObjectAdapted<DataStreamAdapters::Vector<>>(table.mCells); + proxy.template ObjectAdapted<DataStreamAdapters::Vector<>>(table.mArrayGroups); + proxy.template ObjectAdapted<DataStreamAdapters::TslArrayMap<>>(table.mName2Parameters); + proxy.template ObjectAdapted<DataStreamAdapters::TslArrayMap<>>(table.mName2ArrayGroups); + } +}; -void TableTemplate::WriteToDataStream(OutputDataStream& stream) +void TableTemplate::ReadFromDataStream(InputDataStream& stream) { - OperateIOProxy(stream); + Private::OperateStream(*this, stream); } -template <class TProxy> -void TableTemplate::OperateIOProxy(TProxy& proxy) +void TableTemplate::WriteToDataStream(OutputDataStream& stream) const { - proxy.template ObjectAdapted<DataStreamAdapters::Vector<>>(mColumnWidths); - proxy.template ObjectAdapted<DataStreamAdapters::Vector<>>(mRowHeights); - proxy.template ObjectAdapted<DataStreamAdapters::Vector<>>(mCells); - proxy.template ObjectAdapted<DataStreamAdapters::Vector<>>(mArrayGroups); - proxy.template ObjectAdapted<DataStreamAdapters::TslArrayMap<>>(mName2Parameters); - proxy.template ObjectAdapted<DataStreamAdapters::TslArrayMap<>>(mName2ArrayGroups); + Private::OperateStream(*this, stream); } diff --git a/core/src/Model/Template/TableTemplate.hpp b/core/src/Model/Template/TableTemplate.hpp index c6617b0..8771867 100644 --- a/core/src/Model/Template/TableTemplate.hpp +++ b/core/src/Model/Template/TableTemplate.hpp @@ -54,8 +54,8 @@ public: /// Return whether this cell is a part of a merged range or not. Includes the primary cell. bool IsMergedCell() const; - template <class TProxy> - void OperateIOProxy(TProxy& proxy); + void ReadFromDataStream(InputDataStream& stream); + void WriteToDataStream(OutputDataStream& stream) const; }; // TODO support reverse (bottom to top) filling order @@ -109,8 +109,8 @@ public: Vec2i FindCell(std::string_view name); bool UpdateCellName(std::string_view oldName, std::string_view newName); - template <class TProxy> - void OperateIOProxy(TProxy& proxy); + void ReadFromDataStream(InputDataStream& stream); + void WriteToDataStream(OutputDataStream& stream) const; }; // Forward declaration of libxlsxwriter structs @@ -148,6 +148,7 @@ class TableTemplate : public Template { friend class TableSingleParamsIter; friend class TableArrayGroupsIter; + class Private; private: /// Map from parameter name to index of the parameter cell (stored in mCells). @@ -218,9 +219,5 @@ public: lxw_worksheet* InstantiateToExcelWorksheet(lxw_workbook* workbook, const TableInstantiationParameters& params) const; void ReadFromDataStream(InputDataStream& stream) override; - void WriteToDataStream(OutputDataStream& stream) override; - -private: - template <class TProxy> - void OperateIOProxy(TProxy& proxy); + void WriteToDataStream(OutputDataStream& stream) const override; }; diff --git a/core/src/Model/Template/Template.hpp b/core/src/Model/Template/Template.hpp index c6b93e2..061cc07 100644 --- a/core/src/Model/Template/Template.hpp +++ b/core/src/Model/Template/Template.hpp @@ -36,7 +36,7 @@ public: Kind GetKind() const; virtual void ReadFromDataStream(InputDataStream& stream) = 0; - virtual void WriteToDataStream(OutputDataStream& stream) = 0; + virtual void WriteToDataStream(OutputDataStream& stream) const = 0; }; class TemplateAssetList final : public AssetListTyped<Template> diff --git a/core/src/Model/Template/Template_Main.cpp b/core/src/Model/Template/Template_Main.cpp index 19803aa..49187d3 100644 --- a/core/src/Model/Template/Template_Main.cpp +++ b/core/src/Model/Template/Template_Main.cpp @@ -4,6 +4,8 @@ #include "Model/Project.hpp" #include "UI/UI.hpp" #include "Utils/I18n.hpp" +#include "Utils/IO/DataStream.hpp" +#include "Utils/IO/FileStream.hpp" #include "Utils/UUID.hpp" #include <imgui.h> @@ -60,15 +62,11 @@ fs::path TemplateAssetList::RetrievePathFromAsset(const SavedAsset& asset) const bool TemplateAssetList::SaveInstance(const SavedAsset& assetInfo, const Asset* asset) const { auto path = RetrievePathFromAsset(assetInfo); + auto stream = OutputDataStream(OutputFileStream(path, OutputFileStream::TruncateFile)); - std::ofstream ofs(path, std::ios::binary); - if (!ofs) return false; - ofs << (uint64_t)assetInfo.Name.size(); - ofs << assetInfo.Name; - ofs << static_cast<Template::Kind>(assetInfo.Payload); - + stream.WriteObject(assetInfo); if (auto tmpl = static_cast<const Template*>(asset)) { - tmpl->WriteTo(ofs); + stream.WriteObject(*tmpl); } return true; @@ -76,24 +74,14 @@ bool TemplateAssetList::SaveInstance(const SavedAsset& assetInfo, const Asset* a static std::unique_ptr<Template> LoadTemplateFromFile(const fs::path& path) { - std::ifstream ifs(path, std::ios::binary); - if (!ifs) return nullptr; - - // TODO fix serialization - uint64_t nameSize; - ifs >> nameSize; - ifs.seekg(nameSize, std::ios::end); + auto stream = InputDataStream(InputFileStream(path)); - uint32_t iKind; - ifs >> iKind; + SavedAsset assetInfo; + stream.ReadObject(assetInfo); - auto kind = static_cast<Template::Kind>(iKind); + auto kind = static_cast<Template::Kind>(assetInfo.Payload); auto tmpl = Template::CreateByKind(kind); - - auto res = tmpl->ReadFrom(ifs); - if (res != Template::RR_Success) { - return nullptr; - } + stream.ReadObject(*tmpl); return tmpl; } diff --git a/core/src/Utils/IO/Archive.cpp b/core/src/Utils/IO/Archive.cpp index 6c4084f..aa47b67 100644 --- a/core/src/Utils/IO/Archive.cpp +++ b/core/src/Utils/IO/Archive.cpp @@ -10,6 +10,11 @@ constexpr uint8_t kByteOrderMark = []() { } }(); +std::span<const uint8_t, 8> DataArchive::GetMagicNumbers() +{ + return std::span<const uint8_t, 8>{ kMagicNumbers }; +} + std::optional<InputDataStream> DataArchive::LoadFile(const std::filesystem::path& path) { auto stream = InputDataStream(InputFileStream(path)); diff --git a/core/src/Utils/IO/Archive.hpp b/core/src/Utils/IO/Archive.hpp index 28e7c99..1bb8b59 100644 --- a/core/src/Utils/IO/Archive.hpp +++ b/core/src/Utils/IO/Archive.hpp @@ -2,12 +2,16 @@ #include "Utils/IO/DataStream.hpp" +#include <cstdint> #include <filesystem> #include <optional> +#include <span> class DataArchive { public: + static std::span<const uint8_t, 8> GetMagicNumbers(); + // TODO more complete impl static std::optional<InputDataStream> LoadFile(const std::filesystem::path& path); static std::optional<OutputDataStream> SaveFile(const std::filesystem::path& path); diff --git a/core/src/Utils/IO/DataStream.hpp b/core/src/Utils/IO/DataStream.hpp index 58fae7d..3c00f4c 100644 --- a/core/src/Utils/IO/DataStream.hpp +++ b/core/src/Utils/IO/DataStream.hpp @@ -72,29 +72,7 @@ public: template <class TObject> void ReadObject(TObject& obj) { - if constexpr (requires(TObject t) { t.ReadFromDataStream(std::declval<BaseDataStream>()); }) { - obj.ReadFromDataStream(*this); - } else if constexpr (requires(TObject t) { t.OperateIOProxy(std::declval<InputDataStream>()); }) { - obj.OperateIOProxy(*this); - } else { - static_assert(false && sizeof(TObject), "This type does not have integration with InputDataStream."); - } - } - - /// 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 <class T> - 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'."); - } + obj.ReadFromDataStream(*this); } template <class TAdapter, class TObject> @@ -104,7 +82,7 @@ public: } public: - // Proxy functions for OperateIOProxy + // Proxy functions for writing templated IO functions template <class T> void Bytes(size_t byteCount, T* buffer) @@ -119,6 +97,12 @@ public: } template <class T> + void Enum(T& t) + { + ReadEnum(t); + } + + template <class T> void Object(T& obj) { ReadObject(obj); @@ -180,41 +164,19 @@ public: } template <class TObject> - void WriteObject(TObject& obj) + void WriteObject(const TObject& obj) { - if constexpr (requires(TObject t) { t.WriteToDataStream(std::declval<BaseDataStream>()); }) { - obj.WriteToDataStream(*this); - } else if constexpr (requires(TObject t) { t.OperateIOProxy(std::declval<OutputDataStream>()); }) { - obj.OperateIOProxy(*this); - } else { - static_assert(false && sizeof(TObject), "This type does not have integration with OutputDataStream."); - } - } - - /// 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 <class T> - 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'."); - } + obj.WriteToDataStream(*this); } template <class TAdapter, class TObject> - void WriteObjectAdapted(TObject& obj) + void WriteObjectAdapted(const TObject& obj) { TAdapter::WriteToDataStream(*this, obj); } public: - // Proxy functions for OperateIOProxy + // Proxy functions for writing templated IO functions template <class T> void Bytes(size_t byteCount, T* buffer) @@ -229,6 +191,12 @@ public: } template <class T> + void Enum(T t) + { + WriteEnum(t); + } + + template <class T> void Object(T& obj) { WriteObject(obj); diff --git a/core/src/Utils/IO/Helper.hpp b/core/src/Utils/IO/Helper.hpp new file mode 100644 index 0000000..ebd47a7 --- /dev/null +++ b/core/src/Utils/IO/Helper.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include "Utils/IO/DataStream.hpp" + +namespace DataStreamAdapters { + +/// Helper to invoke either Read() or ReadObject(). +/// 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 <class TAdapter, class T> +void ReadHelper(InputDataStream& stream, T& t) +{ + if constexpr (!std::is_same_v<TAdapter, void>) { + stream.ReadObjectAdapted<TAdapter>(t); + } else if constexpr (requires(T tt, InputDataStream ss) { ss.Read(tt); }) { + stream.Read(t); + } else if constexpr (requires(T tt, InputDataStream ss) { ss.ReadEnum(tt); }) { + stream.ReadEnum(t); + } else if constexpr (requires(T tt, InputDataStream ss) { ss.ReadObject(tt); }) { + stream.ReadObject(t); + } else { + static_assert(false && sizeof(T), "This type is neither a 'value' nor an 'object'."); + } +} + +/// Helper to invoke either Write() or WriteObject(). +/// 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 <class TAdapter, class T> +void WriteHelper(OutputDataStream& stream, T& t) +{ + if constexpr (!std::is_same_v<TAdapter, void>) { + stream.WriteObjectAdapted<TAdapter>(t); + } else if constexpr (requires(T tt, OutputDataStream ss) { ss.Write(tt); }) { + stream.Write(t); + } else if constexpr (requires(T tt, OutputDataStream ss) { ss.WriteEnum(tt); }) { + stream.WriteEnum(t); + } else if constexpr (requires(T tt, OutputDataStream ss) { ss.WriteObject(tt); }) { + stream.WriteObject(t); + } else { + static_assert(false && sizeof(T), "This type is neither a 'value' nor an 'object'."); + } +} + +} // namespace DataStreamAdapters diff --git a/core/src/Utils/IO/StringIntegration.hpp b/core/src/Utils/IO/StringIntegration.hpp index 20dc882..536cb83 100644 --- a/core/src/Utils/IO/StringIntegration.hpp +++ b/core/src/Utils/IO/StringIntegration.hpp @@ -9,29 +9,29 @@ namespace DataStreamAdapters { struct String { - static void ReadFromDataStream(InputDataStream& s, std::string& str) + static void ReadFromDataStream(InputDataStream& stream, std::string& str) { uint64_t size; - s.Read(size); + stream.Read(size); str = {}; str.reserve(size); - s.ReadBytes(size, std::back_inserter(str)); + stream.ReadBytes(size, std::back_inserter(str)); } - static void WriteToDataStream(OutputDataStream& s, const std::string& str) + static void WriteToDataStream(OutputDataStream& stream, const std::string& str) { - s.Write((uint64_t)str.size()); - s.WriteBytes(str.size(), str.data()); + stream.Write((uint64_t)str.size()); + stream.WriteBytes(str.size(), str.data()); } }; struct StringView { - static void WriteToDataStream(OutputDataStream& s, const std::string_view& str) + static void WriteToDataStream(OutputDataStream& stream, const std::string_view& str) { - s.Write((uint64_t)str.size()); - s.WriteBytes(str.size(), str.data()); + stream.Write((uint64_t)str.size()); + stream.WriteBytes(str.size(), str.data()); } }; } // namespace DataStreamAdapters diff --git a/core/src/Utils/IO/TslArrayIntegration.hpp b/core/src/Utils/IO/TslArrayIntegration.hpp index eebea02..af1197c 100644 --- a/core/src/Utils/IO/TslArrayIntegration.hpp +++ b/core/src/Utils/IO/TslArrayIntegration.hpp @@ -1,6 +1,7 @@ #pragma once #include "Utils/IO/DataStream.hpp" +#include "Utils/IO/Helper.hpp" #include "Utils/IO/StringIntegration.hpp" #include <tsl/array_map.h> @@ -15,43 +16,34 @@ template <class TAdapter = void> struct TslArrayMap { template <class TValue> - static void ReadFromDataStream(InputDataStream& s, tsl::robin_map<char, TValue>& map) + static void ReadFromDataStream(InputDataStream& stream, tsl::array_map<char, TValue>& map) { static_assert(std::is_default_constructible_v<TValue>); static_assert(std::is_move_constructible_v<TValue>); uint64_t size; - s.Read(size); + stream.Read(size); map.reserve(size); for (uint64_t i = 0; i < size; ++i) { std::string key; - s.ReadObjectAdapted<DataStreamAdapters::String>(key); + stream.ReadObjectAdapted<DataStreamAdapters::String>(key); TValue value; - if constexpr (std::is_same_v<TAdapter, void>) { - s.ReadGeneric(value); - } else { - s.ReadObjectAdapted<TAdapter>(value); - } + ReadHelper<TAdapter>(stream, value); map.insert(key, std::move(value)); } } template <class TValue> - static void WriteToDataStream(OutputDataStream& s, const tsl::robin_map<char, TValue>& map) + static void WriteToDataStream(OutputDataStream& stream, const tsl::array_map<char, TValue>& map) { - s.Write((uint64_t)map.size()); + stream.Write((uint64_t)map.size()); for (auto it = map.begin(); it != map.end(); ++it) { - s.WriteObjectAdapted<DataStreamAdapters::StringView>(it.key_sv()); - - if constexpr (std::is_same_v<TAdapter, void>) { - s.WriteGeneric(it.value()); - } else { - s.WriteObjectAdapted<TAdapter>(it.value()); - } + stream.WriteObjectAdapted<DataStreamAdapters::StringView>(it.key_sv()); + WriteHelper<TAdapter>(stream, it.value()); } } }; diff --git a/core/src/Utils/IO/TslRobinIntegration.hpp b/core/src/Utils/IO/TslRobinIntegration.hpp index c800e52..50775fe 100644 --- a/core/src/Utils/IO/TslRobinIntegration.hpp +++ b/core/src/Utils/IO/TslRobinIntegration.hpp @@ -1,6 +1,7 @@ #pragma once #include "Utils/IO/DataStream.hpp" +#include "Utils/IO/Helper.hpp" #include <tsl/robin_map.h> #include <tsl/robin_set.h> @@ -11,51 +12,34 @@ template <class TKeyAdapter = void, class TValueAdapter = void> struct TslRobinMap { template <class TKey, class TValue> - static void ReadFromDataStream(InputDataStream& s, tsl::robin_map<TKey, TValue>& map) + static void ReadFromDataStream(InputDataStream& stream, tsl::robin_map<TKey, TValue>& map) { static_assert(std::is_default_constructible_v<TValue>); static_assert(std::is_move_constructible_v<TValue>); uint64_t size; - s.Read(size); + stream.Read(size); map.reserve(size); for (uint64_t i = 0; i < size; ++i) { TKey key; - if constexpr (std::is_same_v<TKeyAdapter, void>) { - s.ReadGeneric(key); - } else { - s.ReadObjectAdapted<TKeyAdapter>(key); - } + ReadHelper<TKeyAdapter>(stream, key); TValue value; - if constexpr (std::is_same_v<TValueAdapter, void>) { - s.ReadGeneric(value); - } else { - s.ReadObjectAdapted<TValueAdapter>(value); - } + ReadHelper<TValueAdapter>(stream, value); map.insert(std::move(key), std::move(value)); } } template <class TKey, class TValue> - static void WriteToDataStream(OutputDataStream& s, const tsl::robin_map<TKey, TValue>& map) + static void WriteToDataStream(OutputDataStream& stream, const tsl::robin_map<TKey, TValue>& map) { - s.Write((uint64_t)map.size()); + stream.Write((uint64_t)map.size()); for (auto it = map.begin(); it != map.end(); ++it) { - if constexpr (std::is_same_v<TKeyAdapter, void>) { - s.WriteGeneric(it.key()); - } else { - s.WriteObjectAdapted<TKeyAdapter>(it.key()); - } - - if constexpr (std::is_same_v<TValueAdapter, void>) { - s.WriteGeneric(it.value()); - } else { - s.WriteObjectAdapted<TValueAdapter>(it.value()); - } + WriteHelper<TKeyAdapter>(stream, it.key()); + WriteHelper<TValueAdapter>(stream, it.value()); } } }; @@ -64,38 +48,30 @@ template <class TAdapter = void> struct TslRobinSet { template <class TElement> - static void ReadFromDataStream(InputDataStream& s, tsl::robin_set<TElement>& set) + static void ReadFromDataStream(InputDataStream& stream, tsl::robin_set<TElement>& set) { static_assert(std::is_default_constructible_v<TElement>); static_assert(std::is_move_constructible_v<TElement>); uint64_t size; - s.Read(size); + stream.Read(size); set.reserve(size); for (uint64_t i = 0; i < size; ++i) { TElement element; - if constexpr (std::is_same_v<TAdapter, void>) { - s.ReadGeneric(element); - } else { - s.ReadObjectAdapted<TAdapter>(element); - } + ReadHelper<TAdapter>(stream, element); set.insert(std::move(element)); } } template <class TElement> - static void WriteToDataStream(OutputDataStream& s, const tsl::array_set<TElement>& set) + static void WriteToDataStream(OutputDataStream& stream, const tsl::robin_set<TElement>& set) { - s.Write((uint64_t)set.size()); + stream.Write((uint64_t)set.size()); for (auto& element : set) { - if constexpr (std::is_same_v<TAdapter, void>) { - s.WriteGeneric(element); - } else { - s.WriteObjectAdapted<TAdapter>(element); - } + WriteHelper<TAdapter>(stream, element); } } }; diff --git a/core/src/Utils/IO/UuidIntegration.hpp b/core/src/Utils/IO/UuidIntegration.hpp index d8a0304..d028c50 100644 --- a/core/src/Utils/IO/UuidIntegration.hpp +++ b/core/src/Utils/IO/UuidIntegration.hpp @@ -10,18 +10,18 @@ namespace DataStreamAdapters { struct Uuid { - static void ReadFromDataStream(InputDataStream& s, uuids::uuid& uuid) + static void ReadFromDataStream(InputDataStream& stream, uuids::uuid& uuid) { uint8_t buffer[16]; - s.ReadBytes(16, buffer); + stream.ReadBytes(16, buffer); uuid = uuids::uuid(gsl::span<uint8_t, 16>{ buffer }); } - static void WriteToDataStream(OutputDataStream& s, const uuids::uuid& uuid) + static void WriteToDataStream(OutputDataStream& stream, const uuids::uuid& uuid) { auto gslSpan = uuid.as_bytes(); - s.WriteBytes(gslSpan.size(), gslSpan.data()); + stream.WriteBytes(gslSpan.size(), gslSpan.data()); } }; } // namespace DataStreamAdapters diff --git a/core/src/Utils/IO/VectorIntegration.hpp b/core/src/Utils/IO/VectorIntegration.hpp index da663a4..3689505 100644 --- a/core/src/Utils/IO/VectorIntegration.hpp +++ b/core/src/Utils/IO/VectorIntegration.hpp @@ -1,6 +1,7 @@ #pragma once #include "Utils/IO/DataStream.hpp" +#include "Utils/IO/Helper.hpp" #include <type_traits> #include <vector> @@ -10,39 +11,31 @@ template <class TAdapter = void> struct Vector { template <class TElement> - static void ReadFromDataStream(InputDataStream& s, std::vector<TElement>& vec) + static void ReadFromDataStream(InputDataStream& stream, std::vector<TElement>& vec) { static_assert(std::is_default_constructible_v<TElement>); static_assert(std::is_move_constructible_v<TElement>); uint64_t size; - s.Read(size); + stream.Read(size); vec.clear(); vec.reserve(size); for (uint64_t i = 0; i < size; ++i) { TElement element; - if constexpr (std::is_same_v<TAdapter, void>) { - s.ReadGeneric(element); - } else { - s.ReadObjectAdapted<TAdapter>(element); - } + ReadHelper<TAdapter>(stream, element); vec.push_back(std::move(element)); } } template <class TElement> - static void WriteToDataStream(OutputDataStream& s, const std::vector<TElement>& vec) + static void WriteToDataStream(OutputDataStream& stream, const std::vector<TElement>& vec) { - s.Write((uint64_t)vec.size()); + stream.Write((uint64_t)vec.size()); for (auto& element : vec) { - if constexpr (std::is_same_v<TAdapter, void>) { - s.WriteGeneric(element); - } else { - s.WriteObjectAdapted<TAdapter>(element); - } + WriteHelper<TAdapter>(stream, element); } } }; diff --git a/core/src/Utils/Vector.hpp b/core/src/Utils/Vector.hpp index 3d4d251..4d3f3b3 100644 --- a/core/src/Utils/Vector.hpp +++ b/core/src/Utils/Vector.hpp @@ -1,5 +1,7 @@ #pragma once +#include "Utils/IO/DataStream.hpp" + template <class T> struct Vec2 { @@ -15,11 +17,16 @@ struct Vec2 }; } - template <class TProxy> - void OperateIOProxy(TProxy& proxy) + void ReadFromDataStream(InputDataStream& stream) { - proxy.Value(x); - proxy.Value(y); + stream.Value(x); + stream.Value(y); + } + + void WriteToDataStream(OutputDataStream& stream) const + { + stream.Value(x); + stream.Value(y); } friend constexpr bool operator==(const Vec2& a, const Vec2& b) = default; @@ -55,12 +62,18 @@ struct Vec3 }; } - template <class TProxy> - void OperateIOProxy(TProxy& proxy) + void ReadFromDataStream(InputDataStream& stream) + { + stream.Value(x); + stream.Value(y); + stream.Value(z); + } + + void WriteToDataStream(OutputDataStream& stream) const { - proxy.Value(x); - proxy.Value(y); - proxy.Value(z); + stream.Value(x); + stream.Value(y); + stream.Value(z); } friend constexpr bool operator==(const Vec3& a, const Vec3& b) = default; @@ -98,13 +111,20 @@ struct Vec4 }; } - template <class TProxy> - void OperateIOProxy(TProxy& proxy) + void ReadFromDataStream(InputDataStream& stream) + { + stream.Value(x); + stream.Value(y); + stream.Value(z); + stream.Value(w); + } + + void WriteToDataStream(OutputDataStream& stream) const { - proxy.Value(x); - proxy.Value(y); - proxy.Value(z); - proxy.Value(w); + stream.Value(x); + stream.Value(y); + stream.Value(z); + stream.Value(w); } friend constexpr bool operator==(const Vec4& a, const Vec4& b) = default; |