[ox/claw] Fix Claw read to check for type/version compatibility
This commit is contained in:
parent
1d17b3df82
commit
0c6e47e0b3
9
deps/ox/src/ox/claw/read.hpp
vendored
9
deps/ox/src/ox/claw/read.hpp
vendored
@ -19,6 +19,9 @@
|
|||||||
|
|
||||||
namespace ox {
|
namespace ox {
|
||||||
|
|
||||||
|
constexpr auto Error_ClawTypeMismatch = 200;
|
||||||
|
constexpr auto Error_ClawTypeVersionMismatch = 200;
|
||||||
|
|
||||||
struct ClawHeader {
|
struct ClawHeader {
|
||||||
String typeName;
|
String typeName;
|
||||||
int typeVersion = -1;
|
int typeVersion = -1;
|
||||||
@ -38,6 +41,12 @@ Result<Buffer> stripClawHeader(const ox::Buffer &buff) noexcept;
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
Error readClaw(const char *buff, std::size_t buffLen, T *val) {
|
Error readClaw(const char *buff, std::size_t buffLen, T *val) {
|
||||||
oxRequire(header, readClawHeader(buff, buffLen));
|
oxRequire(header, readClawHeader(buff, buffLen));
|
||||||
|
if (header.typeName != getModelTypeName<T>()) {
|
||||||
|
return OxError(Error_ClawTypeMismatch, "Claw Read: Type mismatch");
|
||||||
|
}
|
||||||
|
if (header.typeVersion != getModelTypeVersion<T>()) {
|
||||||
|
return OxError(Error_ClawTypeVersionMismatch, "Claw Read: Type Verion mismatch");
|
||||||
|
}
|
||||||
switch (header.fmt) {
|
switch (header.fmt) {
|
||||||
case ClawFormat::Metal:
|
case ClawFormat::Metal:
|
||||||
{
|
{
|
||||||
|
28
deps/ox/src/ox/model/typenamecatcher.hpp
vendored
28
deps/ox/src/ox/model/typenamecatcher.hpp
vendored
@ -20,12 +20,14 @@ namespace ox {
|
|||||||
struct TypeNameCatcher {
|
struct TypeNameCatcher {
|
||||||
|
|
||||||
const char *name = "";
|
const char *name = "";
|
||||||
|
int version = 0;
|
||||||
|
|
||||||
constexpr TypeNameCatcher() noexcept = default;
|
constexpr TypeNameCatcher() noexcept = default;
|
||||||
|
|
||||||
template<typename T = std::nullptr_t>
|
template<typename T = std::nullptr_t>
|
||||||
constexpr void setTypeInfo(const char *n = T::TypeName, int = 0) noexcept {
|
constexpr void setTypeInfo(const char *n = T::TypeName, int v = 0) noexcept {
|
||||||
this->name = n;
|
this->name = n;
|
||||||
|
this->version = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -44,6 +46,30 @@ struct TypeNameCatcher {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr int getModelTypeVersion() noexcept {
|
||||||
|
auto a = std::allocator<T>();
|
||||||
|
auto t = a.allocate(1);
|
||||||
|
TypeNameCatcher nc;
|
||||||
|
oxIgnoreError(model(&nc, t));
|
||||||
|
a.deallocate(t, 1);
|
||||||
|
return nc.version;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr int getModelTypeVersion(T *val) noexcept {
|
||||||
|
TypeNameCatcher nc;
|
||||||
|
oxIgnoreError(model(&nc, val));
|
||||||
|
return nc.version;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
consteval int requireModelTypeVersion() noexcept {
|
||||||
|
constexpr auto version = getModelTypeVersion<T>();
|
||||||
|
static_assert(version != 0, "TypeName is required");
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T, typename Str = const char*>
|
template<typename T, typename Str = const char*>
|
||||||
constexpr Str getModelTypeName() noexcept {
|
constexpr Str getModelTypeName() noexcept {
|
||||||
auto a = std::allocator<T>();
|
auto a = std::allocator<T>();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user