From cef8259bb1f057f2cf09752fae5552f527fdb278 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Tue, 24 Oct 2023 11:46:09 +0100 Subject: [PATCH] [+] ByteBuf: support for T::Serialize and T::Deserialize detection --- Include/Aurora/Memory/ByteBuffer.hpp | 3 + .../Memory/ByteBuffer_TypedReadWrite.inl | 128 +++++++++++++++++- 2 files changed, 128 insertions(+), 3 deletions(-) diff --git a/Include/Aurora/Memory/ByteBuffer.hpp b/Include/Aurora/Memory/ByteBuffer.hpp index fd9f8974..0d5bdb6a 100644 --- a/Include/Aurora/Memory/ByteBuffer.hpp +++ b/Include/Aurora/Memory/ByteBuffer.hpp @@ -529,6 +529,9 @@ namespace Aurora::Memory template bool Write(const T &in); + template + bool Write(T &in); + template bool Read(T &out); diff --git a/Include/Aurora/Memory/ByteBuffer_TypedReadWrite.inl b/Include/Aurora/Memory/ByteBuffer_TypedReadWrite.inl index affc9779..a7271b3a 100644 --- a/Include/Aurora/Memory/ByteBuffer_TypedReadWrite.inl +++ b/Include/Aurora/Memory/ByteBuffer_TypedReadWrite.inl @@ -17,6 +17,54 @@ namespace Aurora::Memory namespace __detail { + template + struct AuHasSerializeBool + { + template static constexpr AuTrueType Test(decltype(static_cast(&C::Serialize))); + template static constexpr AuFalseType Test(...); + using type = decltype(Test(0)); + }; + + template + struct AuHasSerializeBool2 + { + template static constexpr AuTrueType Test(decltype(static_cast(&C::Serialize))); + template static constexpr AuFalseType Test(...); + using type = decltype(Test(0)); + }; + + template + struct AuHasDeserializeBool + { + template static constexpr AuTrueType Test(decltype(static_cast(&C::Deserialize))); + template static constexpr AuFalseType Test(...); + using type = decltype(Test(0)); + }; + + template + struct AuHasSerialize + { + template static constexpr AuTrueType Test(decltype(static_cast(&C::Serialize))); + template static constexpr AuFalseType Test(...); + using type = decltype(Test(0)); + }; + + template + struct AuHasSerialize2 + { + template static constexpr AuTrueType Test(decltype(static_cast(&C::Serialize))); + template static constexpr AuFalseType Test(...); + using type = decltype(Test(0)); + }; + + template + struct AuHasDeserialize + { + template static constexpr AuTrueType Test(decltype(static_cast(&C::Deserialize))); + template static constexpr AuFalseType Test(...); + using type = decltype(Test(0)); + }; + template static constexpr AuUInt8 TypeID_v = (AuUInt8)Data::EDataType::kTypeEND; @@ -53,7 +101,16 @@ namespace Aurora::Memory template static constexpr AuUInt8 TypeToID() { - if constexpr (AuIsTuple_v>) + if constexpr (__detail::AuHasDeserializeBool>::type::value || + __detail::AuHasDeserialize>::type::value || + __detail::AuHasSerializeBool>::type::value || + __detail::AuHasSerialize>::type::value || + __detail::AuHasSerializeBool2>::type::value || + __detail::AuHasSerialize2>::type::value) + { + return (AuUInt8)Data::EDataType::kTypeSpecialReserved1; //obj+1 + } + else if constexpr (AuIsTuple_v>) { return (AuUInt8)Data::EDataType::kTypeSpecialReserved4; } @@ -88,7 +145,24 @@ namespace Aurora::Memory { if constexpr (AuIsClass_v) { - if constexpr (AuIsTuple_v>) + if constexpr (__detail::AuHasDeserializeBool>::type::value) + { + if (!out.Deserialize(*this)) + { + this->flagReadError = true; + return false; + } + else + { + return !this->flagReadError; + } + } + else if constexpr (__detail::AuHasDeserialize>::type::value) + { + out.Deserialize(*this); + return !this->flagReadError; + } + else if constexpr (AuIsTuple_v>) { if (this->ReadTagged != AuTupleCountOf_v>) @@ -247,12 +321,60 @@ namespace Aurora::Memory return a; } + template + bool ByteBuffer::Write(T &in) + { + if constexpr (AuIsClass_v) + { + if constexpr (__detail::AuHasSerializeBool>::type::value || + __detail::AuHasSerializeBool2>::type::value) + { + if (!in.Serialize(*this)) + { + this->flagWriteError = true; + return false; + } + else + { + return !this->flagWriteError; + } + } + else if constexpr (__detail::AuHasSerialize>::type::value || + __detail::AuHasSerialize2>::type::value) + { + in.Serialize(*this); + return !this->flagReadError; + } + } + else + { + return this->Write(AuConstReference(in)); + } + } + template bool ByteBuffer::Write(const T &in) { if constexpr (AuIsClass_v) { - if constexpr (AuIsTuple_v>) + if constexpr (__detail::AuHasSerializeBool2>::type::value) + { + if (!in.Serialize(*this)) + { + this->flagWriteError = true; + return false; + } + else + { + return !this->flagWriteError; + } + } + else if constexpr (__detail::AuHasSerialize2>::type::value) + { + in.Serialize(*this); + return !this->flagReadError; + } + else if constexpr (AuIsTuple_v>) { this->WriteTagged(AuTupleCountOf_v>);