diff options
author | rtk0c <[email protected]> | 2021-09-04 17:58:56 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2021-09-04 17:58:56 -0700 |
commit | 70e00f817e9596a746800ba4afec2b7c4ca25142 (patch) | |
tree | 52ca5f993034c6dcb7353805450e66cefc5a0481 /core/src/Utils/IO/TslArrayIntegration.hpp | |
parent | 500aa5130f3f5ad211749018d7be9b0ab46c12b4 (diff) |
Migrate Template and TableTemplate to use DataStream
Diffstat (limited to 'core/src/Utils/IO/TslArrayIntegration.hpp')
-rw-r--r-- | core/src/Utils/IO/TslArrayIntegration.hpp | 58 |
1 files changed, 58 insertions, 0 deletions
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 <tsl/array_map.h> +#include <tsl/array_set.h> +#include <string> +#include <type_traits> + +// TODO support custom key types + +namespace DataStreamAdapters { +template <class TAdapter = void> +struct TslArrayMap +{ + template <class TValue> + static void ReadFromDataStream(InputDataStream& s, tsl::robin_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); + map.reserve(size); + + for (uint64_t i = 0; i < size; ++i) { + std::string key; + s.ReadObjectAdapted<DataStreamAdapters::String>(key); + + TValue value; + if constexpr (std::is_same_v<TAdapter, void>) { + s.ReadGeneric(value); + } else { + s.ReadObjectAdapted<TAdapter>(value); + } + + map.insert(key, std::move(value)); + } + } + + template <class TValue> + static void WriteToDataStream(OutputDataStream& s, const tsl::robin_map<char, TValue>& map) + { + s.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()); + } + } + } +}; +} // namespace DataStreamAdapters |