[ox/model] Add type name and version functions overload option

This commit is contained in:
Gary Talent 2023-01-30 23:13:07 -06:00
parent 838187797a
commit 17cb40c0ec
3 changed files with 58 additions and 22 deletions

View File

@ -8,4 +8,6 @@ target_link_libraries(
OxModel OxModel
) )
add_test("[ox/model] ModelTest Writer" ModelTest ModelValue) add_test("[ox/model] ModelTest ModelValue" ModelTest ModelValue)
add_test("[ox/model] ModelTest getModelTypeName" ModelTest getModelTypeName)
add_test("[ox/model] ModelTest getModelTypeVersion" ModelTest getModelTypeVersion)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2015 - 2022 gary@drinkingtea.net * Copyright 2015 - 2023 gary@drinkingtea.net
* *
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
@ -12,6 +12,26 @@
#include <ox/model/model.hpp> #include <ox/model/model.hpp>
#include <ox/std/std.hpp> #include <ox/std/std.hpp>
struct TestType {
static constexpr auto TypeName = "net.drinkingtea.model.test.TestType";
static constexpr auto TypeVersion = 1;
};
oxModelBegin(TestType)
oxModelEnd()
struct TestType2 {
};
template<typename Str = ox::StringView>
constexpr auto getModelTypeName(TestType2*) noexcept {
return "net.drinkingtea.model.test.TestType2";
}
constexpr auto getModelTypeVersion(TestType2*) noexcept {
return 2;
}
std::map<ox::String, ox::Error(*)()> tests = { std::map<ox::String, ox::Error(*)()> tests = {
{ {
{ {
@ -27,6 +47,22 @@ std::map<ox::String, ox::Error(*)()> tests = {
return ox::Error{}; return ox::Error{};
} }
}, },
{
"getModelTypeName",
[] {
oxAssert(ox::getModelTypeName<TestType>() == TestType::TypeName, "getModelTypeName call failed");
oxAssert(ox::getModelTypeName<TestType2>() == ox::StringView("net.drinkingtea.model.test.TestType2"), "getModelTypeName call failed");
return ox::Error{};
}
},
{
"getModelTypeVersion",
[] {
oxAssert(ox::getModelTypeVersion<TestType>() == TestType::TypeVersion, "getModelTypeVersion call failed");
oxAssert(ox::getModelTypeVersion<TestType2>() == 2, "getModelTypeVersion call failed");
return ox::Error{};
}
},
} }
}; };

View File

@ -85,16 +85,6 @@ struct TypeInfoCatcher {
}; };
template<typename T>
constexpr int getModelTypeVersion() noexcept {
std::allocator<T> a;
auto t = a.allocate(1);
TypeInfoCatcher nc;
oxIgnoreError(model(&nc, t));
a.deallocate(t, 1);
return nc.version;
}
template<typename T> template<typename T>
constexpr int getModelTypeVersion(T *val) noexcept { constexpr int getModelTypeVersion(T *val) noexcept {
TypeInfoCatcher nc; TypeInfoCatcher nc;
@ -102,6 +92,15 @@ constexpr int getModelTypeVersion(T *val) noexcept {
return nc.version; return nc.version;
} }
template<typename T>
constexpr int getModelTypeVersion() noexcept {
std::allocator<T> a;
const auto t = a.allocate(1);
const auto out = getModelTypeVersion(t);
a.deallocate(t, 1);
return out;
}
template<typename T> template<typename T>
consteval int requireModelTypeVersion() noexcept { consteval int requireModelTypeVersion() noexcept {
constexpr auto version = getModelTypeVersion<T>(); constexpr auto version = getModelTypeVersion<T>();
@ -109,16 +108,6 @@ consteval int requireModelTypeVersion() noexcept {
return version; return version;
} }
template<typename T, typename Str = const char*>
constexpr Str getModelTypeName() noexcept {
std::allocator<T> a;
auto t = a.allocate(1);
TypeNameCatcher nc;
oxIgnoreError(model(&nc, t));
a.deallocate(t, 1);
return nc.name;
}
template<typename T, typename Str = const char*> template<typename T, typename Str = const char*>
constexpr Str getModelTypeName(T *val) noexcept { constexpr Str getModelTypeName(T *val) noexcept {
TypeNameCatcher nc; TypeNameCatcher nc;
@ -126,6 +115,15 @@ constexpr Str getModelTypeName(T *val) noexcept {
return nc.name; return nc.name;
} }
template<typename T, typename Str = const char*>
constexpr Str getModelTypeName() noexcept {
std::allocator<T> a;
auto t = a.allocate(1);
auto out = getModelTypeName(t);
a.deallocate(t, 1);
return out;
}
template<typename T> template<typename T>
consteval auto requireModelTypeName() noexcept { consteval auto requireModelTypeName() noexcept {
constexpr auto name = getModelTypeName<T>(); constexpr auto name = getModelTypeName<T>();