From 9ad5771767bfc12a1694c9c37da472e0ef21ff14 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 25 May 2022 01:58:11 -0500 Subject: [PATCH] [nostalgia/core] Make type conv system access type info correctly --- src/nostalgia/core/typeconv.cpp | 15 +++--- src/nostalgia/core/typeconv.hpp | 81 ++++++++++++++++++++------------- 2 files changed, 58 insertions(+), 38 deletions(-) diff --git a/src/nostalgia/core/typeconv.cpp b/src/nostalgia/core/typeconv.cpp index 3226b974..2f89a167 100644 --- a/src/nostalgia/core/typeconv.cpp +++ b/src/nostalgia/core/typeconv.cpp @@ -4,6 +4,8 @@ #include +#include "gfx.hpp" + #include "typeconv.hpp" namespace nostalgia::core { @@ -57,9 +59,9 @@ static auto findConverter(const char *srcTypeName, int srcTypeVersion, return OxError(1, "Could not find converter"); }; -static ox::Result convert(const ox::Buffer &srcBuffer, - const char *srcTypeName, int srcTypeVersion, - const char *dstTypeName, int dstTypeVersion) noexcept { +static ox::Result> convert(const ox::Buffer &srcBuffer, + const char *srcTypeName, int srcTypeVersion, + const char *dstTypeName, int dstTypeVersion) noexcept { // look for direct converter auto [c, err] = findConverter(srcTypeName, srcTypeVersion, dstTypeName, dstTypeVersion); if (!err) { @@ -71,15 +73,16 @@ static ox::Result convert(const ox::Buffer &srcBuffer, continue; } const auto [intermediate, chainErr] = - convert(srcBuffer, subConverter->srcTypeName(), subConverter->srcTypeVersion()); + convert(srcBuffer, srcTypeName, srcTypeVersion, + subConverter->srcTypeName(), subConverter->srcTypeVersion()); if (!chainErr) { - return subConverter->convertPtrToPtr(intermediate); + return subConverter->convertPtrToPtr(intermediate.get()); } } return OxError(1, "Could not convert between types"); } -ox::Result convert(const ox::Buffer &srcBuffer, const char *dstTypeName, int dstTypeVersion) noexcept { +ox::Result> convert(const ox::Buffer &srcBuffer, const char *dstTypeName, int dstTypeVersion) noexcept { oxRequire(hdr, ox::readClawHeader(srcBuffer)); return convert(srcBuffer, hdr.typeName.c_str(), hdr.typeVersion, dstTypeName, dstTypeVersion); } diff --git a/src/nostalgia/core/typeconv.hpp b/src/nostalgia/core/typeconv.hpp index 9f773741..de247205 100644 --- a/src/nostalgia/core/typeconv.hpp +++ b/src/nostalgia/core/typeconv.hpp @@ -6,12 +6,11 @@ #include #include +#include #include - +#include #include -#include "gfx.hpp" - namespace nostalgia::core { class Wrap { @@ -20,12 +19,16 @@ class Wrap { }; template -class WrapTemplate: public Wrap { +class WrapInline: public Wrap { private: T m_obj; public: - constexpr WrapTemplate() = default; + constexpr WrapInline() = default; + + template + constexpr explicit WrapInline(Args &&...args): m_obj(ox::forward(args)...) { + } [[nodiscard]] constexpr auto obj() noexcept { @@ -34,11 +37,14 @@ class WrapTemplate: public Wrap { }; -using WrapPtr = ox::UniquePtr; +template +constexpr auto makeWrap(Args &&...args) noexcept { + return ox::make_unique>(ox::forward(args)...); +} template -constexpr auto wrapCast(const auto &ptr) noexcept { - return static_cast*>(ptr.get())->obj(); +constexpr auto wrapCast(auto ptr) noexcept { + return static_cast*>(ptr)->obj(); } struct BaseConverter { @@ -56,9 +62,9 @@ struct BaseConverter { [[nodiscard]] virtual bool dstMatches(const char *dstTypeName, int dstTypeVersion) const noexcept = 0; - virtual ox::Result convertPtrToPtr(const WrapPtr &src) noexcept = 0; + virtual ox::Result> convertPtrToPtr(Wrap *src) noexcept = 0; - virtual ox::Result convertBuffToPtr(const ox::Buffer &srcBuff) noexcept = 0; + virtual ox::Result> convertBuffToPtr(const ox::Buffer &srcBuff) noexcept = 0; [[nodiscard]] inline bool matches(const char *srcTypeName, int srcTypeVersion, @@ -76,60 +82,71 @@ struct Converter: public BaseConverter{ [[nodiscard]] const char *srcTypeName() noexcept final { - return SrcType::TypeName; + return ox::requireModelTypeName(); } [[nodiscard]] int srcTypeVersion() noexcept final { - return SrcType::TypeVersion; + return ox::requireModelTypeVersion(); } [[nodiscard]] bool srcMatches(const char *srcTypeName, int srcTypeVersion) const noexcept final { - return ox_strcmp(srcTypeName, SrcType::TypeName) == 0 - && srcTypeVersion == SrcType::TypeVersion; + static constexpr auto SrcTypeName = ox::requireModelTypeName(); + static constexpr auto SrcTypeVersion = ox::requireModelTypeVersion(); + return ox_strcmp(srcTypeName, SrcTypeName) == 0 + && srcTypeVersion == SrcTypeVersion; } [[nodiscard]] bool dstMatches(const char *dstTypeName, int dstTypeVersion) const noexcept final { - return ox_strcmp(dstTypeName, DstType::TypeName) == 0 - && dstTypeVersion == DstType::TypeVersion; + static constexpr auto DstTypeName = ox::requireModelTypeName(); + static constexpr auto DstTypeVersion = ox::requireModelTypeVersion(); + return ox_strcmp(dstTypeName, DstTypeName) == 0 + && dstTypeVersion == DstTypeVersion; } - ox::Result convertPtrToPtr(const WrapPtr &src) noexcept final { - auto dst = ox::make_unique>(); - oxReturnError(convert(wrapCast(src), wrapCast(dst))); - return ox::Result(std::move(dst)); + ox::Result> convertPtrToPtr(Wrap *src) noexcept final { + auto dst = makeWrap(); + oxReturnError(convert(wrapCast(src), wrapCast(dst.get()))); + return ox::Result>(std::move(dst)); } - ox::Result convertBuffToPtr(const ox::Buffer &srcBuff) noexcept final { + ox::Result> convertBuffToPtr(const ox::Buffer &srcBuff) noexcept final { oxRequireM(src, ox::readClaw(srcBuff)); - auto dst = ox::make_unique>(); - oxReturnError(convert(&src, wrapCast(dst))); - return ox::Result(std::move(dst)); + auto dst = makeWrap(); + oxReturnError(convert(&src, wrapCast(dst.get()))); + return ox::Result>(std::move(dst)); } }; -ox::Result convert(const ox::Buffer &srcBuffer, const char *dstTypeName, int dstTypeVersion) noexcept; +ox::Result> convert(const ox::Buffer &srcBuffer, + const char *dstTypeName, int dstTypeVersion) noexcept; template ox::Result convert(const ox::Buffer &srcBuffer) noexcept { - oxRequire(out, convert(srcBuffer, DstType::TypeName, DstType::TypeVersion)); + static constexpr auto DstTypeName = ox::requireModelTypeName(); + static constexpr auto DstTypeVersion = ox::requireModelTypeVersion(); + oxRequire(out, convert(srcBuffer, DstTypeName, DstTypeVersion)); return wrapCast(out); } -template -ox::Error convert(const ox::Buffer &buff, T *outObj) noexcept { - oxRequire(outPtr, convert(buff, T::TypeName, T::TypeVersion)); - *outObj = std::move(*wrapCast(outPtr)); +template +ox::Error convert(const ox::Buffer &buff, DstType *outObj) noexcept { + static constexpr auto DstTypeName = ox::requireModelTypeName(); + static constexpr auto DstTypeVersion = ox::requireModelTypeVersion(); + oxRequire(outPtr, convert(buff, DstTypeName, DstTypeVersion)); + *outObj = std::move(*wrapCast(outPtr.get())); return OxError(0); } template ox::Result convertBuffToBuff(const ox::Buffer &srcBuffer, ox::ClawFormat fmt) noexcept { - oxRequire(out, convert(srcBuffer, DstType::TypeName, DstType::TypeVersion)); - return ox::writeClaw(wrapCast(out), fmt); + static constexpr auto DstTypeName = ox::requireModelTypeName(); + static constexpr auto DstTypeVersion = ox::requireModelTypeVersion(); + oxRequire(out, convert(srcBuffer, DstTypeName, DstTypeVersion)); + return ox::writeClaw(wrapCast(out.get()), fmt); } }