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/TslArrayIntegration.hpp | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 core/src/Utils/IO/TslArrayIntegration.hpp (limited to 'core/src/Utils/IO/TslArrayIntegration.hpp') 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 -- cgit v1.2.3-70-g09d2