3#include "BinarySerializationDetail.h"
6namespace nc::serialize::cpo
10inline constexpr bool g_alwaysFalse =
false;
13enum class Dispatch { None, Member, Adl, Default };
17concept HasSerializeMember =
requires(std::ostream& stream,
const T& obj)
19 { obj.Serialize(stream) } -> std::same_as<void>;
24concept HasSerializeAdl =
requires(std::ostream& stream,
const T& obj)
26 {
Serialize(stream, obj) } -> std::same_as<void>;
31concept HasSerializeDefault =
requires(std::ostream& stream,
const T& obj)
33 { nc::serialize::binary::Serialize(stream, obj) } -> std::same_as<void>;
43 static consteval auto GetDispatch() -> Dispatch
45 if constexpr(HasSerializeMember<T>)
46 return Dispatch::Member;
47 else if constexpr(HasSerializeAdl<T>)
49 else if constexpr(HasSerializeDefault<T>)
50 return Dispatch::Default;
52 return Dispatch::None;
56 static constexpr auto Strategy = GetDispatch<T>();
60 requires (Strategy<T> != Dispatch::None)
61 auto operator()(std::ostream& stream,
const T& obj)
const
63 constexpr auto dispatch = Strategy<T>;
64 if constexpr(dispatch == Dispatch::Member)
65 obj.Serialize(stream);
66 else if constexpr(dispatch == Dispatch::Adl)
68 else if constexpr(dispatch == Dispatch::Default)
69 nc::serialize::binary::Serialize(stream, obj);
71 static_assert(g_alwaysFalse<T>,
"Unreachable");
77concept HasDeserializeMember =
requires(std::istream& stream, T& obj)
79 { obj.Deserialize(stream) } -> std::same_as<void>;
84concept HasDeserializeAdl =
requires(std::istream& stream, T& obj)
91concept HasDeserializeDefault =
requires(std::istream& stream, T& obj)
93 { nc::serialize::binary::Deserialize(stream, obj) } -> std::same_as<void>;
103 static consteval auto GetDispatch() -> Dispatch
105 if constexpr(HasDeserializeMember<T>)
106 return Dispatch::Member;
107 else if constexpr(HasDeserializeAdl<T>)
108 return Dispatch::Adl;
109 else if constexpr(HasDeserializeDefault<T>)
110 return Dispatch::Default;
112 return Dispatch::None;
116 static constexpr auto Strategy = GetDispatch<T>();
120 requires (Strategy<T> != Dispatch::None)
121 auto operator()(std::istream& stream, T& obj)
const
123 constexpr auto dispatch = Strategy<T>;
124 if constexpr(dispatch == Dispatch::Member)
125 obj.Deserialize(stream);
126 else if constexpr(dispatch == Dispatch::Adl)
128 else if constexpr(dispatch == Dispatch::Default)
129 nc::serialize::binary::Deserialize(stream, obj);
131 static_assert(g_alwaysFalse<T>,
"Unreachable");
constexpr nc::serialize::cpo::DeserializeFn Deserialize
Deserialize an object from a stream.
Definition: BinarySerialization.h:47
constexpr nc::serialize::cpo::SerializeFn Serialize
Serialize an object to a stream.
Definition: BinarySerialization.h:41