[ox] Make model TypeVersion mandatory so Claw can do version checking

This commit is contained in:
Gary Talent 2022-02-17 04:09:44 -06:00
parent 3389656ecf
commit 21d12fee94
5 changed files with 52 additions and 9 deletions

View File

@ -45,6 +45,7 @@ Error readClaw(const char *buff, std::size_t buffLen, T *val) {
return OxError(Error_ClawTypeMismatch, "Claw Read: Type mismatch"); return OxError(Error_ClawTypeMismatch, "Claw Read: Type mismatch");
} }
if (header.typeVersion != getModelTypeVersion<T>()) { if (header.typeVersion != getModelTypeVersion<T>()) {
oxDebugf("version: {}, {}", header.typeVersion, getModelTypeVersion<T>());
return OxError(Error_ClawTypeVersionMismatch, "Claw Read: Type Version mismatch"); return OxError(Error_ClawTypeVersionMismatch, "Claw Read: Type Version mismatch");
} }
switch (header.fmt) { switch (header.fmt) {

View File

@ -22,7 +22,7 @@
union TestUnion { union TestUnion {
static constexpr auto TypeName = "TestUnion"; static constexpr auto TypeName = "TestUnion";
static constexpr auto Fields = 3; static constexpr auto TypeVersion = 1;
bool Bool; bool Bool;
uint32_t Int = 5; uint32_t Int = 5;
char String[32]; char String[32];
@ -30,7 +30,7 @@ union TestUnion {
struct TestStructNest { struct TestStructNest {
static constexpr auto TypeName = "TestStructNest"; static constexpr auto TypeName = "TestStructNest";
static constexpr auto Fields = 3; static constexpr auto TypeVersion = 1;
bool Bool = false; bool Bool = false;
uint32_t Int = 0; uint32_t Int = 0;
ox::BString<32> String = ""; ox::BString<32> String = "";
@ -38,7 +38,7 @@ struct TestStructNest {
struct TestStruct { struct TestStruct {
static constexpr auto TypeName = "TestStruct"; static constexpr auto TypeName = "TestStruct";
static constexpr auto Fields = 16; static constexpr auto TypeVersion = 1;
bool Bool = false; bool Bool = false;
int32_t Int = 0; int32_t Int = 0;
int32_t Int1 = 0; int32_t Int1 = 0;

View File

@ -26,7 +26,7 @@ struct TypeInfoCatcher {
const char *name = nullptr; const char *name = nullptr;
template<typename T = void> template<typename T = void>
constexpr void setTypeInfo(const char *name = T::TypeName, int = T::Fields) noexcept { constexpr void setTypeInfo(const char *name = T::TypeName, int = T::TypeVersion) noexcept {
this->name = name; this->name = name;
} }

View File

@ -19,7 +19,6 @@
#include "desctypes.hpp" #include "desctypes.hpp"
#include "fieldcounter.hpp" #include "fieldcounter.hpp"
#include "optype.hpp" #include "optype.hpp"
#include "typenamecatcher.hpp"
#include "types.hpp" #include "types.hpp"
namespace ox::detail { namespace ox::detail {
@ -37,6 +36,10 @@ struct preloadable<T, BoolWrapper<T::Preloadable>> {
static constexpr bool value = T::Preloadable; static constexpr bool value = T::Preloadable;
}; };
template<int>
struct IntWrapper {
};
// cannot be done until C++20 // cannot be done until C++20
//struct PseudoString { //struct PseudoString {
// constexpr PseudoString(const char* = "") noexcept {} // constexpr PseudoString(const char* = "") noexcept {}

View File

@ -40,6 +40,45 @@ struct TypeNameCatcher {
return OxError(0); return OxError(0);
} }
template<typename T>
constexpr Error fieldCString(const char*, T) noexcept {
return OxError(0);
}
static constexpr auto opType() noexcept {
return OpType::WriteDefinition;
}
};
struct TypeInfoCatcher {
const char *name = "";
int version = 0;
constexpr TypeInfoCatcher() noexcept = default;
template<typename T = std::nullptr_t>
constexpr void setTypeInfo(const char *n = T::TypeName, int v = T::TypeVersion) noexcept {
this->name = n;
this->version = v;
}
template<typename T>
constexpr Error field(const char*, T*, std::size_t) noexcept {
return OxError(0);
}
template<typename T>
constexpr Error field(const char*, T) noexcept {
return OxError(0);
}
template<typename T>
constexpr Error fieldCString(const char*, T) noexcept {
return OxError(0);
}
static constexpr auto opType() noexcept { static constexpr auto opType() noexcept {
return OpType::WriteDefinition; return OpType::WriteDefinition;
} }
@ -50,7 +89,7 @@ template<typename T>
constexpr int getModelTypeVersion() noexcept { constexpr int getModelTypeVersion() noexcept {
auto a = std::allocator<T>(); auto a = std::allocator<T>();
auto t = a.allocate(1); auto t = a.allocate(1);
TypeNameCatcher nc; TypeInfoCatcher nc;
oxIgnoreError(model(&nc, t)); oxIgnoreError(model(&nc, t));
a.deallocate(t, 1); a.deallocate(t, 1);
return nc.version; return nc.version;
@ -58,7 +97,7 @@ constexpr int getModelTypeVersion() noexcept {
template<typename T> template<typename T>
constexpr int getModelTypeVersion(T *val) noexcept { constexpr int getModelTypeVersion(T *val) noexcept {
TypeNameCatcher nc; TypeInfoCatcher nc;
oxIgnoreError(model(&nc, val)); oxIgnoreError(model(&nc, val));
return nc.version; return nc.version;
} }
@ -66,7 +105,7 @@ constexpr int getModelTypeVersion(T *val) noexcept {
template<typename T> template<typename T>
consteval int requireModelTypeVersion() noexcept { consteval int requireModelTypeVersion() noexcept {
constexpr auto version = getModelTypeVersion<T>(); constexpr auto version = getModelTypeVersion<T>();
static_assert(version != 0, "TypeName is required"); static_assert(version != 0, "TypeVersion is required");
return version; return version;
} }