[keel] Move vald and repair funcs to their own file, make conversion to validation
This commit is contained in:
parent
b7990ed25b
commit
3b8eaef36b
@ -14,6 +14,8 @@
|
||||
#include <ox/std/hashmap.hpp>
|
||||
#include <ox/std/utility.hpp>
|
||||
|
||||
#include "validation.hpp"
|
||||
|
||||
namespace keel {
|
||||
|
||||
class AssetManager;
|
||||
@ -21,17 +23,6 @@ class AssetManager;
|
||||
template<typename T>
|
||||
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<typename T>
|
||||
class AssetContainer {
|
||||
@ -226,9 +217,7 @@ class AssetManager {
|
||||
ox::Result<AssetRef<T>> 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<AssetContainer<T>>(std::move(obj));
|
||||
} else {
|
||||
|
@ -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<ox::UPtr<Wrap>> convertPtrToPtr(keel::Context &ctx, Wrap &src) const noexcept = 0;
|
||||
|
||||
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]]
|
||||
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<typename SrcType, typename DstType>
|
||||
class Converter: public BaseConverter {
|
||||
public:
|
||||
[[nodiscard]]
|
||||
ox::StringView srcTypeName() const noexcept final {
|
||||
constexpr ox::StringView srcTypeName() const noexcept final {
|
||||
return ox::ModelTypeName_v<SrcType>;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
int srcTypeVersion() const noexcept final {
|
||||
constexpr int srcTypeVersion() const noexcept final {
|
||||
return ox::ModelTypeVersion_v<SrcType>;
|
||||
}
|
||||
|
||||
[[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 SrcTypeVersion = ox::requireModelTypeVersion<SrcType>();
|
||||
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<DstType>());
|
||||
constexpr bool dstMatches(ox::CRStringView dstTypeName, int dstTypeVersion) const noexcept final {
|
||||
constexpr auto DstTypeName = ox::StringView{ox::requireModelTypeName<DstType>()};
|
||||
constexpr auto DstTypeVersion = ox::requireModelTypeVersion<DstType>();
|
||||
return dstTypeName == DstTypeName
|
||||
&& dstTypeVersion == DstTypeVersion;
|
||||
@ -117,8 +117,9 @@ class Converter: public BaseConverter {
|
||||
}
|
||||
|
||||
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));
|
||||
oxReturnError(ensureValid(src));
|
||||
auto dst = makeWrap<DstType>();
|
||||
oxReturnError(convert(ctx, src, wrapCast<DstType>(*dst)));
|
||||
return {std::move(dst)};
|
||||
@ -131,12 +132,12 @@ class Converter: public BaseConverter {
|
||||
|
||||
ox::Result<ox::UPtr<Wrap>> convert(
|
||||
keel::Context &ctx,
|
||||
ox::Buffer const&srcBuffer,
|
||||
ox::BufferView const&srcBuffer,
|
||||
ox::CRStringView dstTypeName,
|
||||
int dstTypeVersion) noexcept;
|
||||
|
||||
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 DstTypeVersion = ox::requireModelTypeVersion<DstType>();
|
||||
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>
|
||||
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 DstTypeVersion = ox::requireModelTypeVersion<DstType>();
|
||||
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>
|
||||
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 DstTypeVersion = ox::requireModelTypeVersion<DstType>();
|
||||
oxRequire(out, convert(ctx, srcBuffer, DstTypeName, DstTypeVersion));
|
||||
|
27
src/olympic/keel/include/keel/validation.hpp
Normal file
27
src/olympic/keel/include/keel/validation.hpp
Normal 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 {};
|
||||
}
|
||||
|
||||
}
|
@ -9,7 +9,6 @@
|
||||
|
||||
namespace keel {
|
||||
|
||||
[[nodiscard]]
|
||||
static ox::Result<BaseConverter const*> findConverter(
|
||||
ox::SpanView<BaseConverter const*> const&converters,
|
||||
ox::CRStringView srcTypeName,
|
||||
@ -27,7 +26,7 @@ static ox::Result<BaseConverter const*> findConverter(
|
||||
static ox::Result<ox::UPtr<Wrap>> convert(
|
||||
keel::Context &ctx,
|
||||
ox::SpanView<BaseConverter const*> 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<ox::UPtr<Wrap>> convert(
|
||||
|
||||
ox::Result<ox::UPtr<Wrap>> convert(
|
||||
keel::Context &ctx,
|
||||
ox::Buffer const&srcBuffer,
|
||||
ox::BufferView const&srcBuffer,
|
||||
ox::CRStringView dstTypeName,
|
||||
int dstTypeVersion) noexcept {
|
||||
oxRequire(hdr, readAssetHeader(srcBuffer));
|
||||
|
Loading…
Reference in New Issue
Block a user