From 7a942ac83c24855a1735b77415598f9d93502557 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 19 May 2022 22:23:49 -0500 Subject: [PATCH] [nostalgia/core] Remove unnecessary serialization and deserialization from type conversion --- src/nostalgia/core/typeconv.cpp | 10 ++-- src/nostalgia/core/typeconv.hpp | 84 ++++++++++++++++++++++--------- src/nostalgia/tools/pack/pack.cpp | 2 +- 3 files changed, 66 insertions(+), 30 deletions(-) diff --git a/src/nostalgia/core/typeconv.cpp b/src/nostalgia/core/typeconv.cpp index b99d27aa..dabe413a 100644 --- a/src/nostalgia/core/typeconv.cpp +++ b/src/nostalgia/core/typeconv.cpp @@ -55,22 +55,22 @@ static auto findConverter(const ox::String &srcTypeName, int srcTypeVersion, con return OxError(1, "Could not find converter"); }; -ox::Result convert(const ox::Buffer &srcBuffer, const ox::String &dstTypeName, int dstTypeVersion, ox::ClawFormat fmt) noexcept { +ox::Result convert(const ox::Buffer &srcBuffer, const ox::String &dstTypeName, int dstTypeVersion) noexcept { oxRequire(hdr, ox::readClawHeader(srcBuffer)); // look for direct converter auto [c, err] = findConverter(hdr.typeName, hdr.typeVersion, dstTypeName, dstTypeVersion); if (!err) { - return c->convertBuffToBuff(srcBuffer); + return c->convertBuffToPtr(srcBuffer); } // try to chain multiple converters for (const auto &subConverter : converters) { if (!subConverter->dstMatches(dstTypeName, dstTypeVersion)) { continue; } - const auto [intermediateBuff, chainErr] = - convert(srcBuffer, subConverter->srcTypeName(), subConverter->srcTypeVersion(), fmt); + const auto [intermediate, chainErr] = + convert(srcBuffer, subConverter->srcTypeName(), subConverter->srcTypeVersion()); if (!chainErr) { - return subConverter->convertBuffToBuff(intermediateBuff, fmt); + return subConverter->convertPtrToPtr(intermediate); } } return OxError(1, "Could not convert between types"); diff --git a/src/nostalgia/core/typeconv.hpp b/src/nostalgia/core/typeconv.hpp index 88a6acee..d2b497a9 100644 --- a/src/nostalgia/core/typeconv.hpp +++ b/src/nostalgia/core/typeconv.hpp @@ -14,22 +14,56 @@ namespace nostalgia::core { +class ModelContainer { + public: + virtual ~ModelContainer() = default; + virtual void *obj() noexcept = 0; + virtual const char *typeName() noexcept = 0; + virtual int typeVersion() noexcept = 0; +}; + +template +class ModelContainerTemplate: public ModelContainer { + private: + T m_obj; + + public: + constexpr ModelContainerTemplate() = default; + + void *obj() noexcept final { + return &m_obj; + } + + const char *typeName() noexcept final { + return T::TypeName; + } + + int typeVersion() noexcept final { + return T::TypeVersion; + } + +}; + +using ClawObjPtr = ox::UniquePtr; + struct BaseConverter { virtual ~BaseConverter() noexcept = default; + [[nodiscard]] virtual ox::String srcTypeName() noexcept = 0; + [[nodiscard]] virtual int srcTypeVersion() noexcept = 0; - virtual bool srcMatches(const ox::String &srcTypeName, int srcTypeVersion) noexcept = 0; - + [[nodiscard]] virtual bool dstMatches(const ox::String &dstTypeName, int dstTypeVersion) noexcept = 0; + [[nodiscard]] virtual bool matches(const ox::String &srcTypeName, int srcTypeVersion, const ox::String &dstTypeName, int dstTypeVersion) noexcept = 0; - virtual ox::Error convertRaw(const ox::Buffer &pV1Buff, void *pV2) noexcept = 0; + virtual ox::Result convertPtrToPtr(const ClawObjPtr &src) noexcept = 0; - virtual ox::Result convertBuffToBuff(const ox::Buffer &srcBuff, ox::ClawFormat dstFmt = ox::ClawFormat::Metal) noexcept = 0; + virtual ox::Result convertBuffToPtr(const ox::Buffer &srcBuff) noexcept = 0; }; template @@ -42,11 +76,6 @@ struct Converter: public BaseConverter{ return SrcType::TypeVersion; } - bool srcMatches(const ox::String &srcTypeName, int srcTypeVersion) noexcept final { - return srcTypeName == DstType::TypeName - && srcTypeVersion == DstType::TypeVersion; - } - bool dstMatches(const ox::String &dstTypeName, int dstTypeVersion) noexcept final { return dstTypeName == DstType::TypeName && dstTypeVersion == DstType::TypeVersion; @@ -63,31 +92,38 @@ struct Converter: public BaseConverter{ return OxError(1); } - ox::Error convertRaw(const ox::Buffer &pV1Buff, void *pV2) noexcept override { - oxRequireM(src, ox::readClaw(pV1Buff)); - auto dst = static_cast(pV2); - return convert(&src, dst); + ox::Result convertPtrToPtr(const ClawObjPtr &src) noexcept final { + auto dst = ox::make_unique>(); + oxReturnError(convert(reinterpret_cast(src->obj()), reinterpret_cast(dst->obj()))); + return ox::Result(std::move(dst)); } - ox::Result convertBuffToBuff(const ox::Buffer &srcBuff, ox::ClawFormat dstFmt) noexcept final { - DstType dst; - oxReturnError(convertRaw(srcBuff, &dst)); - oxRequireM(out, ox::writeClaw(&dst, dstFmt)); - return out; + ox::Result convertBuffToPtr(const ox::Buffer &srcBuff) noexcept final { + oxRequireM(src, ox::readClaw(srcBuff)); + auto dst = ox::make_unique>(); + oxReturnError(convert(&src, reinterpret_cast(dst->obj()))); + return ox::Result(std::move(dst)); } }; -ox::Result convert(const ox::Buffer &srcBuffer, const ox::String &dstTypeName, int dstTypeVersion, ox::ClawFormat fmt = ox::ClawFormat::Metal) noexcept; +ox::Result convert(const ox::Buffer &srcBuffer, const ox::String &dstTypeName, int dstTypeVersion) noexcept; -template -ox::Result convert(const ox::Buffer &srcBuffer, ox::ClawFormat fmt = ox::ClawFormat::Metal) noexcept { - return convert(srcBuffer, T::TypeName, T::TypeVersion, fmt); +template +ox::Result convert(const ox::Buffer &srcBuffer) noexcept { + return convert(srcBuffer, DstType::TypeName, DstType::TypeVersion); } template ox::Error convert(const ox::Buffer &buff, T *outObj) noexcept { - oxRequireM(outBuff, convert(buff)); - return ox::readClaw(outBuff).moveTo(outObj); + oxRequireM(outPtr, convert(buff, T::TypeName, T::TypeVersion)); + *outObj = std::move(*reinterpret_cast(outPtr->obj())); + return OxError(0); +} + +template +ox::Result convertBuffToBuff(const ox::Buffer &srcBuffer, ox::ClawFormat fmt) noexcept { + oxRequireM(out, convert(srcBuffer, DstType::TypeName, DstType::TypeVersion)); + return ox::writeClaw(reinterpret_cast(out->obj()), fmt); } } diff --git a/src/nostalgia/tools/pack/pack.cpp b/src/nostalgia/tools/pack/pack.cpp index a14dee3e..87ecb3df 100644 --- a/src/nostalgia/tools/pack/pack.cpp +++ b/src/nostalgia/tools/pack/pack.cpp @@ -36,7 +36,7 @@ static ox::Error doTransformations(ox::FileSystem *dest, const ox::String &fileP // load file oxRequireM(buff, dest->read(filePath.c_str())); if (filePath.endsWith(".ng")) { - oxReturnError(core::convert(buff).moveTo(&buff)); + oxReturnError(core::convertBuffToBuff(buff, ox::ClawFormat::Metal).moveTo(&buff)); } // do transformations oxReturnError(pathToInode(buff).moveTo(&buff));