[ox/model] Cleanup TypeNameCatcher and TypeInfoCatcher
This commit is contained in:
+17
-59
@@ -17,76 +17,34 @@
|
|||||||
|
|
||||||
namespace ox {
|
namespace ox {
|
||||||
|
|
||||||
struct TypeNameCatcher {
|
|
||||||
|
|
||||||
const char *name = "";
|
|
||||||
int version = 0;
|
|
||||||
|
|
||||||
constexpr TypeNameCatcher() noexcept = default;
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
constexpr ox::Error setTypeInfo(
|
|
||||||
const char *n = T::TypeName,
|
|
||||||
int v = T::TypeVersion,
|
|
||||||
const Vector<String>& = {},
|
|
||||||
std::size_t = ModelFieldCount_v<T>) noexcept {
|
|
||||||
this->name = n;
|
|
||||||
this->version = v;
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
constexpr Error field(const char*, T*, std::size_t) noexcept {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
constexpr Error field(const char*, T) noexcept {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename ...Args>
|
|
||||||
constexpr Error fieldCString(Args&&...) noexcept {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
static constexpr auto opType() noexcept {
|
|
||||||
return OpType::Reflect;
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
struct TypeInfoCatcher {
|
struct TypeInfoCatcher {
|
||||||
|
|
||||||
const char *name = "";
|
CString name = "";
|
||||||
int version = 0;
|
int version = 0;
|
||||||
|
|
||||||
constexpr TypeInfoCatcher() noexcept = default;
|
|
||||||
|
|
||||||
template<typename T = std::nullptr_t>
|
template<typename T = std::nullptr_t>
|
||||||
constexpr ox::Error setTypeInfo(
|
constexpr Error setTypeInfo(
|
||||||
const char *n = T::TypeName,
|
CString const n = T::TypeName,
|
||||||
int v = T::TypeVersion,
|
int const v = T::TypeVersion,
|
||||||
const Vector<String>& = {},
|
Vector<String> const& = {},
|
||||||
std::size_t = 0) noexcept {
|
size_t = 0) noexcept {
|
||||||
this->name = n;
|
this->name = n;
|
||||||
this->version = v;
|
this->version = v;
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr Error field(const char*, T*, std::size_t) noexcept {
|
constexpr Error field(CString, T*, size_t) noexcept {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr Error field(const char*, T) noexcept {
|
constexpr Error field(CString, T const&) noexcept {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr Error fieldCString(const char*, T) noexcept {
|
constexpr Error fieldCString(CString, T const&) noexcept {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,14 +83,14 @@ consteval int requireModelTypeVersion() noexcept {
|
|||||||
template<typename T, typename Str = const char*>
|
template<typename T, typename Str = const char*>
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr Str getModelTypeName(T *val) noexcept {
|
constexpr Str getModelTypeName(T *val) noexcept {
|
||||||
TypeNameCatcher nc;
|
TypeInfoCatcher nc;
|
||||||
std::ignore = model(&nc, val);
|
std::ignore = model(&nc, val);
|
||||||
return nc.name;
|
return nc.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename Str = const char*>
|
template<typename T, typename Str = const char*>
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr Str getModelTypeName() noexcept {
|
consteval Str getModelTypeName() noexcept {
|
||||||
std::allocator<T> a;
|
std::allocator<T> a;
|
||||||
auto t = a.allocate(1);
|
auto t = a.allocate(1);
|
||||||
auto out = getModelTypeName(t);
|
auto out = getModelTypeName(t);
|
||||||
@@ -140,15 +98,15 @@ constexpr Str getModelTypeName() noexcept {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename Str = const char*>
|
template<typename T, typename Str = StringLiteral>
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
consteval auto requireModelTypeName() noexcept {
|
consteval auto requireModelTypeName() noexcept {
|
||||||
constexpr auto name = getModelTypeName<T, Str>();
|
constexpr auto name = getModelTypeName<T, Str>();
|
||||||
static_assert(ox::StringView{name}.size(), "Type lacks required TypeName");
|
static_assert(StringView{name}.size(), "Type lacks required TypeName");
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename Str = const char*>
|
template<typename T, typename Str = StringLiteral>
|
||||||
constexpr auto ModelTypeName_v = requireModelTypeName<T, Str>();
|
constexpr auto ModelTypeName_v = requireModelTypeName<T, Str>();
|
||||||
|
|
||||||
template<typename T, typename Str = const char*>
|
template<typename T, typename Str = const char*>
|
||||||
@@ -156,10 +114,10 @@ constexpr auto ModelTypeVersion_v = requireModelTypeVersion<T>();
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr auto ModelTypeId_v = [] {
|
constexpr auto ModelTypeId_v = [] {
|
||||||
constexpr auto name = ModelTypeName_v<T, ox::StringView>;
|
constexpr auto name = ModelTypeName_v<T, StringView>;
|
||||||
constexpr auto version = ModelTypeVersion_v<T>;
|
constexpr auto version = ModelTypeVersion_v<T>;
|
||||||
constexpr auto versionStr = ox::sfmt<ox::IString<19>>("{}", version);
|
constexpr auto versionStr = ox::sfmt<IString<19>>("{}", version);
|
||||||
return ox::sfmt<ox::IString<name.size() + versionStr.size() + 1>>("{};{}", name, versionStr);
|
return ox::sfmt<IString<name.size() + versionStr.size() + 1>>("{};{}", name, versionStr);
|
||||||
}();
|
}();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user