From 17cb40c0ec5b6d60eee3aa844af46ce906125d06 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Mon, 30 Jan 2023 23:13:07 -0600 Subject: [PATCH] [ox/model] Add type name and version functions overload option --- deps/ox/src/ox/model/test/CMakeLists.txt | 4 ++- deps/ox/src/ox/model/test/tests.cpp | 38 +++++++++++++++++++++++- deps/ox/src/ox/model/typenamecatcher.hpp | 38 +++++++++++------------- 3 files changed, 58 insertions(+), 22 deletions(-) diff --git a/deps/ox/src/ox/model/test/CMakeLists.txt b/deps/ox/src/ox/model/test/CMakeLists.txt index e0edb440..e74fde05 100644 --- a/deps/ox/src/ox/model/test/CMakeLists.txt +++ b/deps/ox/src/ox/model/test/CMakeLists.txt @@ -8,4 +8,6 @@ target_link_libraries( OxModel ) -add_test("[ox/model] ModelTest Writer" ModelTest ModelValue) \ No newline at end of file +add_test("[ox/model] ModelTest ModelValue" ModelTest ModelValue) +add_test("[ox/model] ModelTest getModelTypeName" ModelTest getModelTypeName) +add_test("[ox/model] ModelTest getModelTypeVersion" ModelTest getModelTypeVersion) diff --git a/deps/ox/src/ox/model/test/tests.cpp b/deps/ox/src/ox/model/test/tests.cpp index 5e21982f..96506b2b 100644 --- a/deps/ox/src/ox/model/test/tests.cpp +++ b/deps/ox/src/ox/model/test/tests.cpp @@ -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 * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -12,6 +12,26 @@ #include #include +struct TestType { + static constexpr auto TypeName = "net.drinkingtea.model.test.TestType"; + static constexpr auto TypeVersion = 1; +}; + +oxModelBegin(TestType) +oxModelEnd() + +struct TestType2 { +}; + +template +constexpr auto getModelTypeName(TestType2*) noexcept { + return "net.drinkingtea.model.test.TestType2"; +} + +constexpr auto getModelTypeVersion(TestType2*) noexcept { + return 2; +} + std::map tests = { { { @@ -27,6 +47,22 @@ std::map tests = { return ox::Error{}; } }, + { + "getModelTypeName", + [] { + oxAssert(ox::getModelTypeName() == TestType::TypeName, "getModelTypeName call failed"); + oxAssert(ox::getModelTypeName() == ox::StringView("net.drinkingtea.model.test.TestType2"), "getModelTypeName call failed"); + return ox::Error{}; + } + }, + { + "getModelTypeVersion", + [] { + oxAssert(ox::getModelTypeVersion() == TestType::TypeVersion, "getModelTypeVersion call failed"); + oxAssert(ox::getModelTypeVersion() == 2, "getModelTypeVersion call failed"); + return ox::Error{}; + } + }, } }; diff --git a/deps/ox/src/ox/model/typenamecatcher.hpp b/deps/ox/src/ox/model/typenamecatcher.hpp index 553ad1ec..a545c697 100644 --- a/deps/ox/src/ox/model/typenamecatcher.hpp +++ b/deps/ox/src/ox/model/typenamecatcher.hpp @@ -85,16 +85,6 @@ struct TypeInfoCatcher { }; -template -constexpr int getModelTypeVersion() noexcept { - std::allocator a; - auto t = a.allocate(1); - TypeInfoCatcher nc; - oxIgnoreError(model(&nc, t)); - a.deallocate(t, 1); - return nc.version; -} - template constexpr int getModelTypeVersion(T *val) noexcept { TypeInfoCatcher nc; @@ -102,6 +92,15 @@ constexpr int getModelTypeVersion(T *val) noexcept { return nc.version; } +template +constexpr int getModelTypeVersion() noexcept { + std::allocator a; + const auto t = a.allocate(1); + const auto out = getModelTypeVersion(t); + a.deallocate(t, 1); + return out; +} + template consteval int requireModelTypeVersion() noexcept { constexpr auto version = getModelTypeVersion(); @@ -109,16 +108,6 @@ consteval int requireModelTypeVersion() noexcept { return version; } -template -constexpr Str getModelTypeName() noexcept { - std::allocator a; - auto t = a.allocate(1); - TypeNameCatcher nc; - oxIgnoreError(model(&nc, t)); - a.deallocate(t, 1); - return nc.name; -} - template constexpr Str getModelTypeName(T *val) noexcept { TypeNameCatcher nc; @@ -126,6 +115,15 @@ constexpr Str getModelTypeName(T *val) noexcept { return nc.name; } +template +constexpr Str getModelTypeName() noexcept { + std::allocator a; + auto t = a.allocate(1); + auto out = getModelTypeName(t); + a.deallocate(t, 1); + return out; +} + template consteval auto requireModelTypeName() noexcept { constexpr auto name = getModelTypeName();