aboutsummaryrefslogtreecommitdiff
path: root/core/src/Utils/IO/TslArrayIntegration.hpp
blob: eebea022089edbe727c79ff7cb4e55c1b3d6f660 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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