[keel] Move vald and repair funcs to their own file, make conversion to validation

This commit is contained in:
Gary Talent 2024-09-14 14:16:16 -05:00
parent b7990ed25b
commit 3b8eaef36b
4 changed files with 50 additions and 34 deletions

View File

@ -14,6 +14,8 @@
#include <ox/std/hashmap.hpp> #include <ox/std/hashmap.hpp>
#include <ox/std/utility.hpp> #include <ox/std/utility.hpp>
#include "validation.hpp"
namespace keel { namespace keel {
class AssetManager; class AssetManager;
@ -21,17 +23,6 @@ class AssetManager;
template<typename T> template<typename T>
class AssetRef; 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 #ifndef OX_BARE_METAL
template<typename T> template<typename T>
class AssetContainer { class AssetContainer {
@ -226,9 +217,7 @@ class AssetManager {
ox::Result<AssetRef<T>> loadAsset(ox::StringView const assetId) noexcept { ox::Result<AssetRef<T>> loadAsset(ox::StringView const assetId) noexcept {
auto &p = m_cache[assetId]; auto &p = m_cache[assetId];
oxRequireM(obj, m_loader(assetId)); oxRequireM(obj, m_loader(assetId));
if (!valid(obj) && repair(obj)) { oxReturnError(ensureValid(obj));
return OxError(1, "asset is invalid state and could not be repaired");
}
if (!p) { if (!p) {
p = ox::make_unique<AssetContainer<T>>(std::move(obj)); p = ox::make_unique<AssetContainer<T>>(std::move(obj));
} else { } else {

View File

@ -51,27 +51,27 @@ constexpr T &wrapCast(Wrap &ptr) noexcept {
class BaseConverter { class BaseConverter {
public: public:
virtual ~BaseConverter() noexcept = default; constexpr virtual ~BaseConverter() noexcept = default;
[[nodiscard]] [[nodiscard]]
virtual ox::StringView srcTypeName() const noexcept = 0; constexpr virtual ox::StringView srcTypeName() const noexcept = 0;
[[nodiscard]] [[nodiscard]]
virtual int srcTypeVersion() const noexcept = 0; constexpr virtual int srcTypeVersion() const noexcept = 0;
[[nodiscard]] [[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]] [[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<ox::UPtr<Wrap>> convertPtrToPtr(keel::Context &ctx, Wrap &src) const noexcept = 0; virtual ox::Result<ox::UPtr<Wrap>> convertPtrToPtr(keel::Context &ctx, Wrap &src) const noexcept = 0;
virtual ox::Result<ox::UPtr<Wrap>> convertBuffToPtr( virtual ox::Result<ox::UPtr<Wrap>> convertBuffToPtr(
keel::Context &ctx, ox::Buffer const&srcBuff) const noexcept = 0; keel::Context &ctx, ox::BufferView const&srcBuff) const noexcept = 0;
[[nodiscard]] [[nodiscard]]
inline bool matches( constexpr bool matches(
ox::CRStringView srcTypeName, int srcTypeVersion, ox::CRStringView srcTypeName, int srcTypeVersion,
ox::CRStringView dstTypeName, int dstTypeVersion) const noexcept { ox::CRStringView dstTypeName, int dstTypeVersion) const noexcept {
return srcMatches(srcTypeName, srcTypeVersion) return srcMatches(srcTypeName, srcTypeVersion)
@ -84,17 +84,17 @@ template<typename SrcType, typename DstType>
class Converter: public BaseConverter { class Converter: public BaseConverter {
public: public:
[[nodiscard]] [[nodiscard]]
ox::StringView srcTypeName() const noexcept final { constexpr ox::StringView srcTypeName() const noexcept final {
return ox::ModelTypeName_v<SrcType>; return ox::ModelTypeName_v<SrcType>;
} }
[[nodiscard]] [[nodiscard]]
int srcTypeVersion() const noexcept final { constexpr int srcTypeVersion() const noexcept final {
return ox::ModelTypeVersion_v<SrcType>; return ox::ModelTypeVersion_v<SrcType>;
} }
[[nodiscard]] [[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<SrcType>(); constexpr auto SrcTypeName = ox::requireModelTypeName<SrcType>();
constexpr auto SrcTypeVersion = ox::requireModelTypeVersion<SrcType>(); constexpr auto SrcTypeVersion = ox::requireModelTypeVersion<SrcType>();
return pSrcTypeName == SrcTypeName return pSrcTypeName == SrcTypeName
@ -102,8 +102,8 @@ class Converter: public BaseConverter {
} }
[[nodiscard]] [[nodiscard]]
bool dstMatches(ox::CRStringView dstTypeName, int dstTypeVersion) const noexcept final { constexpr bool dstMatches(ox::CRStringView dstTypeName, int dstTypeVersion) const noexcept final {
constexpr auto DstTypeName = ox::StringView(ox::requireModelTypeName<DstType>()); constexpr auto DstTypeName = ox::StringView{ox::requireModelTypeName<DstType>()};
constexpr auto DstTypeVersion = ox::requireModelTypeVersion<DstType>(); constexpr auto DstTypeVersion = ox::requireModelTypeVersion<DstType>();
return dstTypeName == DstTypeName return dstTypeName == DstTypeName
&& dstTypeVersion == DstTypeVersion; && dstTypeVersion == DstTypeVersion;
@ -117,8 +117,9 @@ class Converter: public BaseConverter {
} }
ox::Result<ox::UPtr<Wrap>> convertBuffToPtr( ox::Result<ox::UPtr<Wrap>> convertBuffToPtr(
keel::Context &ctx, ox::Buffer const&srcBuff) const noexcept final { keel::Context &ctx, ox::BufferView const&srcBuff) const noexcept final {
oxRequireM(src, readAsset<SrcType>(srcBuff)); oxRequireM(src, readAsset<SrcType>(srcBuff));
oxReturnError(ensureValid(src));
auto dst = makeWrap<DstType>(); auto dst = makeWrap<DstType>();
oxReturnError(convert(ctx, src, wrapCast<DstType>(*dst))); oxReturnError(convert(ctx, src, wrapCast<DstType>(*dst)));
return {std::move(dst)}; return {std::move(dst)};
@ -131,12 +132,12 @@ class Converter: public BaseConverter {
ox::Result<ox::UPtr<Wrap>> convert( ox::Result<ox::UPtr<Wrap>> convert(
keel::Context &ctx, keel::Context &ctx,
ox::Buffer const&srcBuffer, ox::BufferView const&srcBuffer,
ox::CRStringView dstTypeName, ox::CRStringView dstTypeName,
int dstTypeVersion) noexcept; int dstTypeVersion) noexcept;
template<typename DstType> template<typename DstType>
ox::Result<DstType> convert(keel::Context &ctx, ox::Buffer const&srcBuffer) noexcept { ox::Result<DstType> convert(keel::Context &ctx, ox::BufferView const&srcBuffer) noexcept {
static constexpr auto DstTypeName = ox::requireModelTypeName<DstType>(); static constexpr auto DstTypeName = ox::requireModelTypeName<DstType>();
static constexpr auto DstTypeVersion = ox::requireModelTypeVersion<DstType>(); static constexpr auto DstTypeVersion = ox::requireModelTypeVersion<DstType>();
oxRequire(out, convert(ctx, srcBuffer, DstTypeName, DstTypeVersion)); oxRequire(out, convert(ctx, srcBuffer, DstTypeName, DstTypeVersion));
@ -144,7 +145,7 @@ ox::Result<DstType> convert(keel::Context &ctx, ox::Buffer const&srcBuffer) noex
} }
template<typename DstType> template<typename DstType>
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<DstType>(); static constexpr auto DstTypeName = ox::requireModelTypeName<DstType>();
static constexpr auto DstTypeVersion = ox::requireModelTypeVersion<DstType>(); static constexpr auto DstTypeVersion = ox::requireModelTypeVersion<DstType>();
oxRequire(outPtr, convert(ctx, buff, DstTypeName, DstTypeVersion)); 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<typename DstType> template<typename DstType>
ox::Result<ox::Buffer> convertBuffToBuff( ox::Result<ox::Buffer> 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<DstType>(); static constexpr auto DstTypeName = ox::requireModelTypeName<DstType>();
static constexpr auto DstTypeVersion = ox::requireModelTypeVersion<DstType>(); static constexpr auto DstTypeVersion = ox::requireModelTypeVersion<DstType>();
oxRequire(out, convert(ctx, srcBuffer, DstTypeName, DstTypeVersion)); oxRequire(out, convert(ctx, srcBuffer, DstTypeName, DstTypeVersion));

View File

@ -0,0 +1,27 @@
/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <ox/std/error.hpp>
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 {};
}
}

View File

@ -9,7 +9,6 @@
namespace keel { namespace keel {
[[nodiscard]]
static ox::Result<BaseConverter const*> findConverter( static ox::Result<BaseConverter const*> findConverter(
ox::SpanView<BaseConverter const*> const&converters, ox::SpanView<BaseConverter const*> const&converters,
ox::CRStringView srcTypeName, ox::CRStringView srcTypeName,
@ -27,7 +26,7 @@ static ox::Result<BaseConverter const*> findConverter(
static ox::Result<ox::UPtr<Wrap>> convert( static ox::Result<ox::UPtr<Wrap>> convert(
keel::Context &ctx, keel::Context &ctx,
ox::SpanView<BaseConverter const*> const&converters, ox::SpanView<BaseConverter const*> const&converters,
ox::Buffer const&srcBuffer, ox::BufferView const&srcBuffer,
ox::CRStringView srcTypeName, ox::CRStringView srcTypeName,
int srcTypeVersion, int srcTypeVersion,
ox::CRStringView dstTypeName, ox::CRStringView dstTypeName,
@ -55,7 +54,7 @@ static ox::Result<ox::UPtr<Wrap>> convert(
ox::Result<ox::UPtr<Wrap>> convert( ox::Result<ox::UPtr<Wrap>> convert(
keel::Context &ctx, keel::Context &ctx,
ox::Buffer const&srcBuffer, ox::BufferView const&srcBuffer,
ox::CRStringView dstTypeName, ox::CRStringView dstTypeName,
int dstTypeVersion) noexcept { int dstTypeVersion) noexcept {
oxRequire(hdr, readAssetHeader(srcBuffer)); oxRequire(hdr, readAssetHeader(srcBuffer));