[+] ByteBuffer::WriteTagged<T>

[+] ByteBuffer::ReadTagged<T>
This commit is contained in:
Reece Wilson 2023-08-08 00:00:16 +01:00
parent 1cd56ab161
commit aaeca44fb0
4 changed files with 100 additions and 21 deletions

View File

@ -521,6 +521,12 @@ namespace Aurora::Memory
template<typename T>
bool Read(T &out);
template<typename T>
bool WriteTagged(const T &in);
template<typename T>
bool ReadTagged(T &out);
};
static ByteBuffer NewResizableBuffer(AuUInt32 length = 0)

View File

@ -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
{
}

View File

@ -15,6 +15,74 @@ namespace Aurora::Memory
static const auto kMaxSaneElementsForAuMemory = AURORA_RUNTIME_BUFFER_MAX_ELEMS;
namespace __detail
{
template <class>
static constexpr AuUInt8 TypeID_v = (AuUInt8)Data::EDataType::kTypeEND;
template <>
static constexpr AuUInt8 TypeID_v<AuUInt8> = (AuUInt8)Data::EDataType::kTypeStructUInt8;
template <>
static constexpr AuUInt8 TypeID_v<AuInt8> = (AuUInt8)Data::EDataType::kTypeStructInt8;
template <>
static constexpr AuUInt8 TypeID_v<AuUInt16> = (AuUInt8)Data::EDataType::kTypeStructUInt16;
template <>
static constexpr AuUInt8 TypeID_v<AuInt16> = (AuUInt8)Data::EDataType::kTypeStructInt16;
template <>
static constexpr AuUInt8 TypeID_v<AuUInt32> = (AuUInt8)Data::EDataType::kTypeStructUInt32;
template <>
static constexpr AuUInt8 TypeID_v<AuInt32> = (AuUInt8)Data::EDataType::kTypeStructInt32;
template <>
static constexpr AuUInt8 TypeID_v<AuUInt64> = (AuUInt8)Data::EDataType::kTypeStructUInt64;
template <>
static constexpr AuUInt8 TypeID_v<AuInt64> = (AuUInt8)Data::EDataType::kTypeStructInt64;
template <>
static constexpr AuUInt8 TypeID_v<double> = (AuUInt8)Data::EDataType::kTypeStructDouble;
template <>
static constexpr AuUInt8 TypeID_v<float> = (AuUInt8)Data::EDataType::kTypeStructFloat;
template <typename T>
static constexpr AuUInt8 TypeToID()
{
if constexpr (AuIsTuple_v<AuRemoveReference_t<T>>)
{
return (AuUInt8)Data::EDataType::kTypeSpecialReserved4;
}
else if constexpr (AuIsOptional_v<AuRemoveReference_t<T>>)
{
return (AuUInt8)Data::EDataType::kTypeSpecialReserved3;
}
else if constexpr (AuIsBaseOfTemplate<AURORA_RUNTIME_AU_LIST, AuRemoveReference_t<T>>::value)
{
return (AuUInt8)Data::EDataType::kTypeSpecialArray;
}
else if constexpr (AuIsBST_v<AuRemoveReference_t<T>> ||
AuIsHashMap_v<AuRemoveReference_t<T>>)
{
return (AuUInt8)Data::EDataType::kTypeSpecialObject;
}
else if constexpr (AuIsSame_v<AuRemoveReference_t<T>, AuString> ||
AuIsSame_v<AuRemoveReference_t<T>, std::string_view>)
{
return (AuUInt8)Data::EDataType::kTypeString;
}
else
{
return TypeID_v<T>;
}
}
}
template<typename T>
bool ByteBuffer::Read(T &out)
{
@ -53,7 +121,7 @@ namespace Aurora::Memory
}
else if constexpr (AuIsBaseOfTemplate<AURORA_RUNTIME_AU_LIST, AuRemoveReference_t<T>>::value)
{
if (Read<AuUInt32>() != sizeof(typename T::value_type))
if (Read<AuUInt8>() != __detail::TypeToID<typename T::value_type>())
{
this->flagReadError = true;
return false;
@ -83,13 +151,13 @@ namespace Aurora::Memory
else if constexpr (AuIsBST_v<AuRemoveReference_t<T>> ||
AuIsHashMap_v<AuRemoveReference_t<T>>)
{
if (Read<AuUInt32>() != sizeof(typename T::key_type))
if (Read<AuUInt8>() != __detail::TypeToID<typename T::key_type>())
{
this->flagReadError = true;
return false;
}
if (Read<AuUInt32>() != sizeof(typename T::mapped_type))
if (Read<AuUInt8>() != __detail::TypeToID<typename T::mapped_type>())
{
this->flagReadError = true;
return false;
@ -192,7 +260,7 @@ namespace Aurora::Memory
}
else if constexpr (AuIsBaseOfTemplate<AURORA_RUNTIME_AU_LIST, AuRemoveReference_t<T>>::value)
{
Write<AuUInt32>(sizeof(typename T::value_type));
Write<AuUInt8>(__detail::TypeToID<typename T::value_type>());
Write<AuUInt32>(AuUInt32(in.size()));
for (const auto &item : in)
@ -206,11 +274,11 @@ namespace Aurora::Memory
AuIsHashMap_v<AuRemoveReference_t<T>>)
{
Write<AuUInt32>(sizeof(typename T::key_type));
Write<AuUInt32>(sizeof(typename T::mapped_type));
Write<AuUInt8>(__detail::TypeToID<typename T::key_type>());
Write<AuUInt8>(__detail::TypeToID<typename T::mapped_type>());
Write<AuUInt32>(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<typename T>
bool ByteBuffer::WriteTagged(const T &in)
{
Write(__detail::TypeToID<T>());
return Write(in);
}
template<typename T>
bool ByteBuffer::ReadTagged(T &out)
{
if (__detail::TypeToID<T>() != Read<AuUInt8>())
{
this->flagReadError = true;
return false;
}
return Read(out);
}
}

View File

@ -1,5 +1,4 @@
#include "ByteBuffer_Utils.inl"
#include "ByteBuffer_TaggedReadWrite.inl"
#include "ByteBuffer_TypedReadWrite.inl"
#include "ByteBuffer_ReadWrite.inl"
#include "ByteBuffer_Memory.inl"