aboutsummaryrefslogtreecommitdiff
path: root/core/src/Utils/IO/DataStream.hpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2021-09-04 17:58:56 -0700
committerrtk0c <[email protected]>2021-09-04 17:58:56 -0700
commit70e00f817e9596a746800ba4afec2b7c4ca25142 (patch)
tree52ca5f993034c6dcb7353805450e66cefc5a0481 /core/src/Utils/IO/DataStream.hpp
parent500aa5130f3f5ad211749018d7be9b0ab46c12b4 (diff)
Migrate Template and TableTemplate to use DataStream
Diffstat (limited to 'core/src/Utils/IO/DataStream.hpp')
-rw-r--r--core/src/Utils/IO/DataStream.hpp73
1 files changed, 61 insertions, 12 deletions
diff --git a/core/src/Utils/IO/DataStream.hpp b/core/src/Utils/IO/DataStream.hpp
index fa22549..58fae7d 100644
--- a/core/src/Utils/IO/DataStream.hpp
+++ b/core/src/Utils/IO/DataStream.hpp
@@ -60,6 +60,15 @@ public:
void Read(float& n);
void Read(double& n);
+ template <class TEnum>
+ requires std::is_enum_v<TEnum>
+ void ReadEnum(TEnum& e)
+ {
+ std::underlying_type_t<TEnum> n;
+ Read(n);
+ e = static_cast<TEnum>(e);
+ }
+
template <class TObject>
void ReadObject(TObject& obj)
{
@@ -72,10 +81,26 @@ public:
}
}
- template <class TObject, class TAdapter>
- void ReadObjectAdapted(TObject& obj, TAdapter&& adapter)
+ /// Helper to invoke either Read() or ReadObject(). Note that this function doesn't account for types that needs and adapter.
+ /// 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 T>
+ void ReadGeneric(T& t)
+ {
+ if constexpr (requires(T tt, InputDataStream ss) { ss.Read(tt); }) {
+ Read(t);
+ } else if constexpr (requires(T tt, InputDataStream ss) { ss.ReadEnum(tt); }) {
+ ReadEnum(t);
+ } else if constexpr (requires(T tt, InputDataStream ss) { ss.ReadObject(tt); }) {
+ ReadObject(t);
+ } else {
+ static_assert(false && sizeof(T), "This type is neither a 'value' nor an 'object'.");
+ }
+ }
+
+ template <class TAdapter, class TObject>
+ void ReadObjectAdapted(TObject& obj)
{
- adapter.ReadFromDataStream(*this, obj);
+ TAdapter::ReadFromDataStream(*this, obj);
}
public:
@@ -99,10 +124,10 @@ public:
ReadObject(obj);
}
- template <class TObject, class TAdapter>
- void ObjectAdapted(TObject& obj, TAdapter&& adapter)
+ template <class TAdapter, class TObject>
+ void ObjectAdapted(TObject& obj)
{
- ReadObjectAdapted(obj, adapter);
+ ReadObjectAdapted<TAdapter>(obj);
}
};
@@ -146,6 +171,14 @@ public:
void Write(float n);
void Write(double n);
+ template <class TEnum>
+ requires std::is_enum_v<TEnum>
+ void WriteEnum(TEnum e)
+ {
+ auto n = static_cast<std::underlying_type_t<TEnum>>(e);
+ Write(n);
+ }
+
template <class TObject>
void WriteObject(TObject& obj)
{
@@ -158,10 +191,26 @@ public:
}
}
- template <class TObject, class TAdapter>
- void WriteObjectAdapted(TObject& obj, TAdapter&& adapter)
+ /// Helper to invoke either Write() or WriteObject(). Note that this function doesn't account for types that needs and adapter.
+ /// 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 T>
+ void WriteGeneric(T& t)
+ {
+ if constexpr (requires(T tt, OutputDataStream ss) { ss.Write(tt); }) {
+ Write(t);
+ } else if constexpr (requires(T tt, OutputDataStream ss) { ss.WriteEnum(tt); }) {
+ WriteEnum(t);
+ } else if constexpr (requires(T tt, OutputDataStream ss) { ss.WriteObject(tt); }) {
+ WriteObject(t);
+ } else {
+ static_assert(false && sizeof(T), "This type is neither a 'value' nor an 'object'.");
+ }
+ }
+
+ template <class TAdapter, class TObject>
+ void WriteObjectAdapted(TObject& obj)
{
- adapter.WriteToDataStream(*this, obj);
+ TAdapter::WriteToDataStream(*this, obj);
}
public:
@@ -185,9 +234,9 @@ public:
WriteObject(obj);
}
- template <class TObject, class TAdapter>
- void ObjectAdapted(TObject& obj, TAdapter&& adapter)
+ template <class TAdapter, class TObject>
+ void ObjectAdapted(TObject& obj)
{
- WriteObjectAdapted(obj, adapter);
+ WriteObjectAdapted<TAdapter>(obj);
}
};