aboutsummaryrefslogtreecommitdiff
path: root/app/source/Cplt/Utils/IO/TslArrayIntegration.hpp
blob: b585beed833453640237e8f9f82ef7e7bee748d9 (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
#pragma once

#include <Cplt/Utils/IO/DataStream.hpp>
#include <Cplt/Utils/IO/Helper.hpp>
#include <Cplt/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& 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;
		stream.Read(size);
		map.reserve(size);

		for (uint64_t i = 0; i < size; ++i) {
			std::string key;
			stream.ReadObjectAdapted<DataStreamAdapters::String>(key);

			TValue value;
			ReadHelper<TAdapter>(stream, value);

			map.insert(key, std::move(value));
		}
	}

	template <class TValue>
	static void WriteToDataStream(OutputDataStream& stream, const tsl::array_map<char, TValue>& map)
	{
		stream.Write((uint64_t)map.size());

		for (auto it = map.begin(); it != map.end(); ++it) {
			stream.WriteObjectAdapted<DataStreamAdapters::StringView>(it.key_sv());
			WriteHelper<TAdapter>(stream, it.value());
		}
	}
};
} // namespace DataStreamAdapters