From aaeca44fb056a9473e983d60989e6da3df5a8fce Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Tue, 8 Aug 2023 00:00:16 +0100 Subject: [PATCH] [+] ByteBuffer::WriteTagged [+] ByteBuffer::ReadTagged --- Include/Aurora/Memory/ByteBuffer.hpp | 6 ++ .../Memory/ByteBuffer_TaggedReadWrite.inl | 13 --- .../Memory/ByteBuffer_TypedReadWrite.inl | 101 ++++++++++++++++-- Include/Aurora/Memory/_ByteBuffer.hpp | 1 - 4 files changed, 100 insertions(+), 21 deletions(-) delete mode 100644 Include/Aurora/Memory/ByteBuffer_TaggedReadWrite.inl diff --git a/Include/Aurora/Memory/ByteBuffer.hpp b/Include/Aurora/Memory/ByteBuffer.hpp index a45f7408..13f40738 100644 --- a/Include/Aurora/Memory/ByteBuffer.hpp +++ b/Include/Aurora/Memory/ByteBuffer.hpp @@ -521,6 +521,12 @@ namespace Aurora::Memory template bool Read(T &out); + + template + bool WriteTagged(const T &in); + + template + bool ReadTagged(T &out); }; static ByteBuffer NewResizableBuffer(AuUInt32 length = 0) diff --git a/Include/Aurora/Memory/ByteBuffer_TaggedReadWrite.inl b/Include/Aurora/Memory/ByteBuffer_TaggedReadWrite.inl deleted file mode 100644 index 2e10ea61..00000000 --- a/Include/Aurora/Memory/ByteBuffer_TaggedReadWrite.inl +++ /dev/null @@ -1,13 +0,0 @@ -/*** - Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved. - - File: ByteBuffer_TaggedReadWrite.inl - Date: 2022-1-18 - Author: Reece -***/ -#pragma once - -namespace Aurora::Memory -{ - -} diff --git a/Include/Aurora/Memory/ByteBuffer_TypedReadWrite.inl b/Include/Aurora/Memory/ByteBuffer_TypedReadWrite.inl index 3394d7ac..fdcc4186 100644 --- a/Include/Aurora/Memory/ByteBuffer_TypedReadWrite.inl +++ b/Include/Aurora/Memory/ByteBuffer_TypedReadWrite.inl @@ -15,6 +15,74 @@ namespace Aurora::Memory static const auto kMaxSaneElementsForAuMemory = AURORA_RUNTIME_BUFFER_MAX_ELEMS; + namespace __detail + { + template + static constexpr AuUInt8 TypeID_v = (AuUInt8)Data::EDataType::kTypeEND; + + template <> + static constexpr AuUInt8 TypeID_v = (AuUInt8)Data::EDataType::kTypeStructUInt8; + + template <> + static constexpr AuUInt8 TypeID_v = (AuUInt8)Data::EDataType::kTypeStructInt8; + + template <> + static constexpr AuUInt8 TypeID_v = (AuUInt8)Data::EDataType::kTypeStructUInt16; + + template <> + static constexpr AuUInt8 TypeID_v = (AuUInt8)Data::EDataType::kTypeStructInt16; + + template <> + static constexpr AuUInt8 TypeID_v = (AuUInt8)Data::EDataType::kTypeStructUInt32; + + template <> + static constexpr AuUInt8 TypeID_v = (AuUInt8)Data::EDataType::kTypeStructInt32; + + template <> + static constexpr AuUInt8 TypeID_v = (AuUInt8)Data::EDataType::kTypeStructUInt64; + + template <> + static constexpr AuUInt8 TypeID_v = (AuUInt8)Data::EDataType::kTypeStructInt64; + + template <> + static constexpr AuUInt8 TypeID_v = (AuUInt8)Data::EDataType::kTypeStructDouble; + + template <> + static constexpr AuUInt8 TypeID_v = (AuUInt8)Data::EDataType::kTypeStructFloat; + + template + static constexpr AuUInt8 TypeToID() + { + if constexpr (AuIsTuple_v>) + { + return (AuUInt8)Data::EDataType::kTypeSpecialReserved4; + } + else if constexpr (AuIsOptional_v>) + { + return (AuUInt8)Data::EDataType::kTypeSpecialReserved3; + } + else if constexpr (AuIsBaseOfTemplate>::value) + { + return (AuUInt8)Data::EDataType::kTypeSpecialArray; + } + else if constexpr (AuIsBST_v> || + AuIsHashMap_v>) + + { + return (AuUInt8)Data::EDataType::kTypeSpecialObject; + } + else if constexpr (AuIsSame_v, AuString> || + AuIsSame_v, std::string_view>) + { + return (AuUInt8)Data::EDataType::kTypeString; + } + else + { + return TypeID_v; + } + } + } + template bool ByteBuffer::Read(T &out) { @@ -53,7 +121,7 @@ namespace Aurora::Memory } else if constexpr (AuIsBaseOfTemplate>::value) { - if (Read() != sizeof(typename T::value_type)) + if (Read() != __detail::TypeToID()) { this->flagReadError = true; return false; @@ -83,13 +151,13 @@ namespace Aurora::Memory else if constexpr (AuIsBST_v> || AuIsHashMap_v>) { - if (Read() != sizeof(typename T::key_type)) + if (Read() != __detail::TypeToID()) { this->flagReadError = true; return false; } - if (Read() != sizeof(typename T::mapped_type)) + if (Read() != __detail::TypeToID()) { this->flagReadError = true; return false; @@ -192,7 +260,7 @@ namespace Aurora::Memory } else if constexpr (AuIsBaseOfTemplate>::value) { - Write(sizeof(typename T::value_type)); + Write(__detail::TypeToID()); Write(AuUInt32(in.size())); for (const auto &item : in) @@ -206,11 +274,11 @@ namespace Aurora::Memory AuIsHashMap_v>) { - Write(sizeof(typename T::key_type)); - Write(sizeof(typename T::mapped_type)); + Write(__detail::TypeToID()); + Write(__detail::TypeToID()); Write(AuUInt32(in.size())); - for (const auto &[key, value]: in) + for (const auto &[key, value] : in) { Write(key); Write(value); @@ -240,4 +308,23 @@ namespace Aurora::Memory } return true; } + + template + bool ByteBuffer::WriteTagged(const T &in) + { + Write(__detail::TypeToID()); + return Write(in); + } + + template + bool ByteBuffer::ReadTagged(T &out) + { + if (__detail::TypeToID() != Read()) + { + this->flagReadError = true; + return false; + } + + return Read(out); + } } diff --git a/Include/Aurora/Memory/_ByteBuffer.hpp b/Include/Aurora/Memory/_ByteBuffer.hpp index 80ca5e47..ebd68f66 100644 --- a/Include/Aurora/Memory/_ByteBuffer.hpp +++ b/Include/Aurora/Memory/_ByteBuffer.hpp @@ -1,5 +1,4 @@ #include "ByteBuffer_Utils.inl" -#include "ByteBuffer_TaggedReadWrite.inl" #include "ByteBuffer_TypedReadWrite.inl" #include "ByteBuffer_ReadWrite.inl" #include "ByteBuffer_Memory.inl"