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