From 3b8eaef36b74aa026741725a16353f49d2106594 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 14 Sep 2024 14:16:16 -0500 Subject: [PATCH] [keel] Move vald and repair funcs to their own file, make conversion to validation --- .../keel/include/keel/assetmanager.hpp | 17 ++------- src/olympic/keel/include/keel/typeconv.hpp | 35 ++++++++++--------- src/olympic/keel/include/keel/validation.hpp | 27 ++++++++++++++ src/olympic/keel/src/typeconv.cpp | 5 ++- 4 files changed, 50 insertions(+), 34 deletions(-) create mode 100644 src/olympic/keel/include/keel/validation.hpp diff --git a/src/olympic/keel/include/keel/assetmanager.hpp b/src/olympic/keel/include/keel/assetmanager.hpp index 152be9d4..ca6e6cf8 100644 --- a/src/olympic/keel/include/keel/assetmanager.hpp +++ b/src/olympic/keel/include/keel/assetmanager.hpp @@ -14,6 +14,8 @@ #include #include +#include "validation.hpp" + namespace keel { class AssetManager; @@ -21,17 +23,6 @@ class AssetManager; template class AssetRef; - -[[nodiscard]] -constexpr bool valid(auto const&) noexcept { - return true; -} - -[[nodiscard]] -constexpr ox::Error repair(auto const&) noexcept { - return OxError(1, "No repair function for this type"); -} - #ifndef OX_BARE_METAL template class AssetContainer { @@ -226,9 +217,7 @@ class AssetManager { ox::Result> loadAsset(ox::StringView const assetId) noexcept { auto &p = m_cache[assetId]; oxRequireM(obj, m_loader(assetId)); - if (!valid(obj) && repair(obj)) { - return OxError(1, "asset is invalid state and could not be repaired"); - } + oxReturnError(ensureValid(obj)); if (!p) { p = ox::make_unique>(std::move(obj)); } else { diff --git a/src/olympic/keel/include/keel/typeconv.hpp b/src/olympic/keel/include/keel/typeconv.hpp index b2c51a65..a204103c 100644 --- a/src/olympic/keel/include/keel/typeconv.hpp +++ b/src/olympic/keel/include/keel/typeconv.hpp @@ -51,27 +51,27 @@ constexpr T &wrapCast(Wrap &ptr) noexcept { class BaseConverter { public: - virtual ~BaseConverter() noexcept = default; + constexpr virtual ~BaseConverter() noexcept = default; [[nodiscard]] - virtual ox::StringView srcTypeName() const noexcept = 0; + constexpr virtual ox::StringView srcTypeName() const noexcept = 0; [[nodiscard]] - virtual int srcTypeVersion() const noexcept = 0; + constexpr virtual int srcTypeVersion() const noexcept = 0; [[nodiscard]] - virtual bool srcMatches(ox::CRStringView pSrcTypeName, int pSrcTypeVersion) const noexcept = 0; + constexpr virtual bool srcMatches(ox::CRStringView pSrcTypeName, int pSrcTypeVersion) const noexcept = 0; [[nodiscard]] - virtual bool dstMatches(ox::CRStringView dstTypeName, int dstTypeVersion) const noexcept = 0; + constexpr virtual bool dstMatches(ox::CRStringView dstTypeName, int dstTypeVersion) const noexcept = 0; virtual ox::Result> convertPtrToPtr(keel::Context &ctx, Wrap &src) const noexcept = 0; virtual ox::Result> convertBuffToPtr( - keel::Context &ctx, ox::Buffer const&srcBuff) const noexcept = 0; + keel::Context &ctx, ox::BufferView const&srcBuff) const noexcept = 0; [[nodiscard]] - inline bool matches( + constexpr bool matches( ox::CRStringView srcTypeName, int srcTypeVersion, ox::CRStringView dstTypeName, int dstTypeVersion) const noexcept { return srcMatches(srcTypeName, srcTypeVersion) @@ -84,17 +84,17 @@ template class Converter: public BaseConverter { public: [[nodiscard]] - ox::StringView srcTypeName() const noexcept final { + constexpr ox::StringView srcTypeName() const noexcept final { return ox::ModelTypeName_v; } [[nodiscard]] - int srcTypeVersion() const noexcept final { + constexpr int srcTypeVersion() const noexcept final { return ox::ModelTypeVersion_v; } [[nodiscard]] - bool srcMatches(ox::CRStringView pSrcTypeName, int pSrcTypeVersion) const noexcept final { + constexpr bool srcMatches(ox::CRStringView pSrcTypeName, int pSrcTypeVersion) const noexcept final { constexpr auto SrcTypeName = ox::requireModelTypeName(); constexpr auto SrcTypeVersion = ox::requireModelTypeVersion(); return pSrcTypeName == SrcTypeName @@ -102,8 +102,8 @@ class Converter: public BaseConverter { } [[nodiscard]] - bool dstMatches(ox::CRStringView dstTypeName, int dstTypeVersion) const noexcept final { - constexpr auto DstTypeName = ox::StringView(ox::requireModelTypeName()); + constexpr bool dstMatches(ox::CRStringView dstTypeName, int dstTypeVersion) const noexcept final { + constexpr auto DstTypeName = ox::StringView{ox::requireModelTypeName()}; constexpr auto DstTypeVersion = ox::requireModelTypeVersion(); return dstTypeName == DstTypeName && dstTypeVersion == DstTypeVersion; @@ -117,8 +117,9 @@ class Converter: public BaseConverter { } ox::Result> convertBuffToPtr( - keel::Context &ctx, ox::Buffer const&srcBuff) const noexcept final { + keel::Context &ctx, ox::BufferView const&srcBuff) const noexcept final { oxRequireM(src, readAsset(srcBuff)); + oxReturnError(ensureValid(src)); auto dst = makeWrap(); oxReturnError(convert(ctx, src, wrapCast(*dst))); return {std::move(dst)}; @@ -131,12 +132,12 @@ class Converter: public BaseConverter { ox::Result> convert( keel::Context &ctx, - ox::Buffer const&srcBuffer, + ox::BufferView const&srcBuffer, ox::CRStringView dstTypeName, int dstTypeVersion) noexcept; template -ox::Result convert(keel::Context &ctx, ox::Buffer const&srcBuffer) noexcept { +ox::Result convert(keel::Context &ctx, ox::BufferView const&srcBuffer) noexcept { static constexpr auto DstTypeName = ox::requireModelTypeName(); static constexpr auto DstTypeVersion = ox::requireModelTypeVersion(); oxRequire(out, convert(ctx, srcBuffer, DstTypeName, DstTypeVersion)); @@ -144,7 +145,7 @@ ox::Result convert(keel::Context &ctx, ox::Buffer const&srcBuffer) noex } template -ox::Error convert(keel::Context &ctx, ox::Buffer const&buff, DstType *outObj) noexcept { +ox::Error convert(keel::Context &ctx, ox::BufferView const&buff, DstType *outObj) noexcept { static constexpr auto DstTypeName = ox::requireModelTypeName(); static constexpr auto DstTypeVersion = ox::requireModelTypeVersion(); oxRequire(outPtr, convert(ctx, buff, DstTypeName, DstTypeVersion)); @@ -154,7 +155,7 @@ ox::Error convert(keel::Context &ctx, ox::Buffer const&buff, DstType *outObj) no template ox::Result convertBuffToBuff( - keel::Context &ctx, ox::Buffer const&srcBuffer, ox::ClawFormat fmt) noexcept { + keel::Context &ctx, ox::BufferView const&srcBuffer, ox::ClawFormat fmt) noexcept { static constexpr auto DstTypeName = ox::requireModelTypeName(); static constexpr auto DstTypeVersion = ox::requireModelTypeVersion(); oxRequire(out, convert(ctx, srcBuffer, DstTypeName, DstTypeVersion)); diff --git a/src/olympic/keel/include/keel/validation.hpp b/src/olympic/keel/include/keel/validation.hpp new file mode 100644 index 00000000..7f86b0f0 --- /dev/null +++ b/src/olympic/keel/include/keel/validation.hpp @@ -0,0 +1,27 @@ +/* + * Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved. + */ + +#pragma once + +#include + +namespace keel { + +[[nodiscard]] +constexpr bool valid(auto const&) noexcept { + return true; +} + +constexpr ox::Error repair(auto const&) noexcept { + return OxError(1, "No repair function for this type"); +} + +constexpr ox::Error ensureValid(auto &o) noexcept { + if (!valid(o)) { + return repair(o); + } + return {}; +} + +} diff --git a/src/olympic/keel/src/typeconv.cpp b/src/olympic/keel/src/typeconv.cpp index bb821797..b8bef6af 100644 --- a/src/olympic/keel/src/typeconv.cpp +++ b/src/olympic/keel/src/typeconv.cpp @@ -9,7 +9,6 @@ namespace keel { -[[nodiscard]] static ox::Result findConverter( ox::SpanView const&converters, ox::CRStringView srcTypeName, @@ -27,7 +26,7 @@ static ox::Result findConverter( static ox::Result> convert( keel::Context &ctx, ox::SpanView const&converters, - ox::Buffer const&srcBuffer, + ox::BufferView const&srcBuffer, ox::CRStringView srcTypeName, int srcTypeVersion, ox::CRStringView dstTypeName, @@ -55,7 +54,7 @@ static ox::Result> convert( ox::Result> convert( keel::Context &ctx, - ox::Buffer const&srcBuffer, + ox::BufferView const&srcBuffer, ox::CRStringView dstTypeName, int dstTypeVersion) noexcept { oxRequire(hdr, readAssetHeader(srcBuffer));