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

#include <Cplt/Utils/IO/DataStream.hpp>

namespace DataStreamAdapters {

/// Helper to invoke either Read() or ReadObject().
/// This is intended for writing IO adapters, users that's writing IO logic shouldn't using this - it increases compile time while reducing readability.
template <class TAdapter, class T>
void ReadHelper(InputDataStream& stream, T& t) {
	if constexpr (!std::is_same_v<TAdapter, void>) {
		stream.ReadObjectAdapted<TAdapter>(t);
	} else if constexpr (requires(T tt, InputDataStream ss) { ss.Read(tt); }) {
		stream.Read(t);
	} else if constexpr (requires(T tt, InputDataStream ss) { ss.ReadEnum(tt); }) {
		stream.ReadEnum(t);
	} else if constexpr (requires(T tt, InputDataStream ss) { ss.ReadObject(tt); }) {
		stream.ReadObject(t);
	} else {
		static_assert(false && sizeof(T), "This type is neither a 'value' nor an 'object'.");
	}
}

/// Helper to invoke either Write() or WriteObject().
/// This is intended for writing IO adapters, users that's writing IO logic shouldn't using this - it increases compile time while reducing readability.
template <class TAdapter, class T>
void WriteHelper(OutputDataStream& stream, T& t) {
	if constexpr (!std::is_same_v<TAdapter, void>) {
		stream.WriteObjectAdapted<TAdapter>(t);
	} else if constexpr (requires(T tt, OutputDataStream ss) { ss.Write(tt); }) {
		stream.Write(t);
	} else if constexpr (requires(T tt, OutputDataStream ss) { ss.WriteEnum(tt); }) {
		stream.WriteEnum(t);
	} else if constexpr (requires(T tt, OutputDataStream ss) { ss.WriteObject(tt); }) {
		stream.WriteObject(t);
	} else {
		static_assert(false && sizeof(T), "This type is neither a 'value' nor an 'object'.");
	}
}

} // namespace DataStreamAdapters