aboutsummaryrefslogtreecommitdiff
path: root/core/src/Utils/IO/Adapter.hpp
blob: e9e8fb23a3dfdefac15622f18ef18100229b74be (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#pragma once

#include "Utils/IO/DataStream.hpp"

#include <utility>

class SerializationAdapter
{
public:
	static constexpr bool IsSerializer()
	{
		return true;
	}

public:
	DataStream* Stream;

	template <class T>
	void Bytes(size_t byteCount, T* buffer) const
	{
		Stream->WriteBytes(byteCount, buffer);
	}

	template <class T>
	void Value(T t) const
	{
		Stream->Write(t);
	}

	template <class TObject>
	void Object(TObject& obj) const
	{
		Stream->WriteObject(obj);
	}
};

class DeserializationAdapter
{
public:
	static constexpr bool IsSerializer()
	{
		return false;
	}

public:
	DataStream* Stream;

	template <class T>
	void Bytes(size_t byteCount, T* buffer) const
	{
		Stream->WriteBytes(byteCount, buffer);
	}

	template <class T>
	void Value(T& t) const
	{
		Stream->Read(t);
	}

	template <class TObject>
	void Object(TObject& obj) const
	{
		Stream->ReadObject(obj);
	}
};

template <class T>
requires requires(T t)
{
	t.OperateIOAdapter(std::declval<DeserializationAdapter>());
}
void ReadFromDataStream(DataStream& stream, T& obj)
{
	DeserializationAdapter adapter{ &stream };
	obj.OperateIOAdapter(adapter);
}

template <class T>
requires requires(T t)
{
	t.OperateIOAdapter(std::declval<SerializationAdapter>());
}
void WriteToDataStream(DataStream& stream, T& obj)
{
	SerializationAdapter adapter{ &stream };
	obj.OperateIOAdapter(adapter);
}