[keel] Add cleaner way to write type converters
This commit is contained in:
parent
c84b85102c
commit
849aceb86d
@ -174,6 +174,73 @@ class Converter: public BaseConverter {
|
||||
|
||||
};
|
||||
|
||||
|
||||
template<auto Func>
|
||||
class ConverterFunc: public BaseConverter {
|
||||
private:
|
||||
template<typename SrcType, typename DstType>
|
||||
struct ParamExtractor {
|
||||
using Src = SrcType;
|
||||
using Dst = DstType;
|
||||
};
|
||||
|
||||
template<typename S, typename D>
|
||||
static auto makeParamExtractor(ox::Error (*)(keel::Context&, S&, D&)) {
|
||||
return ParamExtractor<S, D>{};
|
||||
}
|
||||
|
||||
public:
|
||||
using SrcType = decltype(makeParamExtractor(Func))::Src;
|
||||
using DstType = decltype(makeParamExtractor(Func))::Dst;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr ox::StringView srcTypeName() const noexcept final {
|
||||
return ox::ModelTypeName_v<SrcType>;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr int srcTypeVersion() const noexcept final {
|
||||
return ox::ModelTypeVersion_v<SrcType>;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr bool srcMatches(ox::StringViewCR pSrcTypeName, int pSrcTypeVersion) const noexcept final {
|
||||
constexpr auto SrcTypeName = ox::requireModelTypeName<SrcType>();
|
||||
constexpr auto SrcTypeVersion = ox::requireModelTypeVersion<SrcType>();
|
||||
return pSrcTypeName == SrcTypeName
|
||||
&& pSrcTypeVersion == SrcTypeVersion;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr bool dstMatches(ox::StringViewCR 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;
|
||||
}
|
||||
|
||||
ox::Result<ox::UPtr<Wrap>> convertPtrToPtr(
|
||||
keel::Context &ctx, Wrap &src) const noexcept final {
|
||||
ox::Result<ox::UPtr<Wrap>> dst{makeWrap<DstType>()};
|
||||
OX_RETURN_ERROR(convert(ctx, wrapCast<SrcType>(src), wrapCast<DstType>(*dst.value)));
|
||||
return dst;
|
||||
}
|
||||
|
||||
ox::Result<ox::UPtr<Wrap>> convertBuffToPtr(
|
||||
keel::Context &ctx, ox::BufferView const&srcBuff) const noexcept final {
|
||||
OX_REQUIRE_M(src, readAsset<SrcType>(srcBuff));
|
||||
ox::Result<ox::UPtr<Wrap>> dst{makeWrap<DstType>()};
|
||||
OX_RETURN_ERROR(convert(ctx, src, wrapCast<DstType>(*dst.value)));
|
||||
return dst;
|
||||
}
|
||||
|
||||
protected:
|
||||
ox::Error convert(keel::Context &ctx, SrcType &src, DstType &dst) const noexcept {
|
||||
return Func(ctx, src, dst);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
ox::Result<ox::UPtr<Wrap>> convert(
|
||||
keel::Context &ctx,
|
||||
ox::BufferView const&srcBuffer,
|
||||
|
Loading…
x
Reference in New Issue
Block a user