From 317d5c0e3cb071e42bf942ac9d07d6bf4a8d6eb5 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Sat, 26 Aug 2023 21:23:25 +0200 Subject: [PATCH] Introduce Version class --- openVulkanoCpp/Base/IGraphicsApp.hpp | 4 +- openVulkanoCpp/Base/Version.cpp | 180 ++++++++++++++++++ openVulkanoCpp/Base/Version.hpp | 69 +++++++ .../ExampleApps/CubesExampleApp.cpp | 3 +- openVulkanoCpp/Host/GraphicsAppManager.cpp | 2 +- openVulkanoCpp/Vulkan/Context.cpp | 2 +- 6 files changed, 254 insertions(+), 6 deletions(-) create mode 100644 openVulkanoCpp/Base/Version.cpp create mode 100644 openVulkanoCpp/Base/Version.hpp diff --git a/openVulkanoCpp/Base/IGraphicsApp.hpp b/openVulkanoCpp/Base/IGraphicsApp.hpp index e9d8b27..d533476 100644 --- a/openVulkanoCpp/Base/IGraphicsApp.hpp +++ b/openVulkanoCpp/Base/IGraphicsApp.hpp @@ -8,6 +8,7 @@ #include "ITickable.hpp" #include "ICloseable.hpp" +#include "Version.hpp" #include namespace openVulkanoCpp @@ -26,7 +27,6 @@ namespace openVulkanoCpp IGraphicsAppManager* GetGraphicsAppManager() const { return manager; } void SetGraphicsAppManager(IGraphicsAppManager* manager) { this->manager = manager; } virtual std::string GetAppName() = 0; - virtual std::string GetAppVersion() = 0; - virtual int GetAppVersionAsInt() = 0; + virtual Version GetAppVersion() = 0; }; } diff --git a/openVulkanoCpp/Base/Version.cpp b/openVulkanoCpp/Base/Version.cpp new file mode 100644 index 0000000..8f02ab0 --- /dev/null +++ b/openVulkanoCpp/Base/Version.cpp @@ -0,0 +1,180 @@ +/* + * 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 + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include "Version.hpp" +#include +#include +#include + +#if (__cplusplus >= 202002L) +#include +#endif + +namespace openVulkanoCpp +{ + namespace + { + struct ComponentDecodeHolder + { + std::vector versionComponents; + std::vector tagComponents; + std::vector appleBuildComponents; + + ComponentDecodeHolder(const std::string_view& versionStr) + { + if (versionStr.empty()) return; + int offset = 0; + if (versionStr[0] == 'v' || versionStr[0] == 'V') + { + offset++; + if (versionStr.size() == 1) return; + } + ReadVersionComponents(versionComponents, versionStr, offset); + if (versionStr[offset] == '-') + { + offset++; + ReadTagComponents(tagComponents, versionStr, offset); + } + if (versionStr.size() - 3 > offset && versionStr[offset] == ' ' && versionStr[offset + 1] == '(' && versionStr.back() == ')') + { + ReadVersionComponents(appleBuildComponents, versionStr, offset); + if (versionStr[offset] != ')') appleBuildComponents.clear(); + } + } + + private: + static int ToNumber(std::string_view numberStr) + { + if (numberStr.empty()) return 0; + int tmp = 0; + std::from_chars(numberStr.begin(), numberStr.end(), tmp); + return tmp; + } + + static void ReadVersionComponents(std::vector& comps, const std::string_view& versionStr, int& offset) + { + do + { + int blockStart = offset; + while (versionStr.size() > offset && versionStr[offset] >= '0' && versionStr[offset] <= '9') offset++; + comps.push_back(ToNumber(versionStr.substr(blockStart, offset - blockStart))); + if (versionStr[offset] != '.') break; + offset++; + } while(versionStr.size() > offset); + } + + static void ReadTagComponents(std::vector& tags, const std::string_view& versionStr, int& offset) + { + do + { + int blockStart = offset; + while (versionStr.size() > offset && versionStr[offset] != '-' && versionStr[offset] != ' ' && versionStr[offset] != '\0') offset++; + tags.emplace_back(versionStr.data() + blockStart, offset - blockStart); + if (versionStr[offset] == ' ' || versionStr[offset] == '\0') break; + offset++; + } while (versionStr.size() > offset); + } + }; + + std::pair ReadTagValue(const std::string& str) + { + std::pair p; + std::istringstream iss(str); + iss >> p.first >> p.second; + return p; + } + } + + Version::Version(uint32_t major, uint32_t minor, uint32_t patch, uint32_t build) + : m_versionComponents(build ? std::initializer_list{major, minor, patch, build} : std::initializer_list{major, minor, patch}) + , m_buildNumber(build) +#if (__cplusplus >= 202002L) + , m_versionString(std::format(build ? "v{}.{}.{}.{}" : "v{}.{}.{}", major, minor, patch, build)) +#else + , m_versionString("v" + std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(patch) + (build ? "." + std::to_string(build) : "")) +#endif + {} + + Version::Version(std::string_view versionString, bool ignoreTags) + : m_versionString(versionString) + { + ComponentDecodeHolder helper(versionString); + if (!helper.appleBuildComponents.empty()) + while (helper.versionComponents.size() < 3) helper.versionComponents.push_back(0); + if (helper.versionComponents.size() == 4) m_buildNumber = helper.versionComponents[3]; + m_versionComponents = std::move(helper.versionComponents); + for (auto val : helper.appleBuildComponents) { m_versionComponents.push_back(val); } + if (!ignoreTags) + { + m_tagComponents = std::move(helper.tagComponents); + const auto tagRegex = std::regex("[bBtT][0-9]+"); + for (const std::string& tag : m_tagComponents) + { + if (std::regex_match(tag, tagRegex)) + { + auto t = ReadTagValue(tag); + if (t.first == 't' || t.first == 'T') + m_timestamp = t.second; + else + m_buildNumber = t.second; + } + } + } + if (m_tagComponents.empty() && !helper.appleBuildComponents.empty()) + { + if (helper.appleBuildComponents.size() == 3) + { + m_timestamp = helper.appleBuildComponents[1] * 1000000uLL + helper.appleBuildComponents[2]; + } + m_buildNumber = helper.appleBuildComponents[0]; + } + } + + //region Compare functions + int Version::CompareBuildNr(const openVulkanoCpp::Version& other) const + { + if (m_buildNumber == 0 || other.m_buildNumber == 0) return 0; + if (m_buildNumber > other.m_buildNumber) return 1; + if (m_buildNumber < other.m_buildNumber) return -1; + return 0; + } + + int Version::CompareTimestamp(const Version& other) const + { + if (m_timestamp == 0 || other.m_timestamp == 0) return 0; + if (m_timestamp > other.m_timestamp) return 1; + if (m_timestamp < other.m_timestamp) return -1; + return 0; + } + + int Version::CompareComponents(const Version& other) const + { + size_t minCount = std::min(m_versionComponents.size(), other.m_versionComponents.size()); + for(size_t i = 0; i < minCount; i++) + { + if (m_versionComponents[i] > other.m_versionComponents[i]) return 1; + if (m_versionComponents[i] < other.m_versionComponents[i]) return -1; + } + if (m_versionComponents.size() != other.m_versionComponents.size()) + { + for(uint32_t i = minCount; i < std::max(m_versionComponents.size(), other.m_versionComponents.size()); i++) + { + if (GetComponent(i) > other.GetComponent(i)) return 1; + if (GetComponent(i) < other.GetComponent(i)) return -1; + } + } + return 0; + } + + int Version::Compare(const openVulkanoCpp::Version& other) + { + int comp; + if ((comp = CompareComponents(other)) != 0) return comp; + if ((comp = CompareBuildNr(other)) != 0) return comp; + return CompareTimestamp(other); + } + //endregion +} \ No newline at end of file diff --git a/openVulkanoCpp/Base/Version.hpp b/openVulkanoCpp/Base/Version.hpp new file mode 100644 index 0000000..0df4894 --- /dev/null +++ b/openVulkanoCpp/Base/Version.hpp @@ -0,0 +1,69 @@ +/* + * 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 + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include +#include +#include + +namespace openVulkanoCpp +{ + class Version + { + std::vector m_versionComponents; + std::vector m_tagComponents; + uint64_t m_timestamp = 0, m_buildNumber = 0; + std::string m_versionString; + bool m_preRelease = false; + + public: + Version(uint32_t major, uint32_t minor = 0, uint32_t patch = 0, uint32_t build = 0); + + Version(std::string_view versionString, bool ignoreTags = false); + + [[nodiscard]] uint32_t GetComponent(uint32_t compId) const + { + if (m_versionComponents.size() < compId + 1) return 0u; + return m_versionComponents[compId]; + } + + [[nodiscard]] uint32_t Major() const { return GetComponent(0); } + + [[nodiscard]] uint32_t Minor() const { return GetComponent(1); } + + [[nodiscard]] uint32_t Patch() const { return GetComponent(2); } + + [[nodiscard]] uint32_t Build() const { return m_buildNumber; } + + [[nodiscard]] bool IsPreRelease() const { return m_preRelease; } + + [[nodiscard]] const std::vector& GetTags() const { return m_tagComponents; } + + [[nodiscard]] const std::vector& GetVersionComponents() const { return m_versionComponents; } + + //region Conversion operators + [[nodiscard]] explicit operator uint32_t() const { return (Major() << 20) | ((Minor() & 0x3FF) << 10) | (Patch() & 0x3FF); } + + [[nodiscard]] operator const std::string&() const { return m_versionString; } + //endregion + + //region Comparison operators + [[nodiscard]] bool operator ==(const Version& rhs) { return Compare(rhs) == 0; } + [[nodiscard]] bool operator !=(const Version& rhs) { return Compare(rhs); } + [[nodiscard]] bool operator < (const Version& rhs) { return Compare(rhs) < 0; } + [[nodiscard]] bool operator <=(const Version& rhs) { return Compare(rhs) < 1; } + [[nodiscard]] bool operator > (const Version& rhs) { return Compare(rhs) > 0; } + [[nodiscard]] bool operator >=(const Version& rhs) { return Compare(rhs) > -1; } + //endregion + + private: + [[nodiscard]] int Compare(const Version& other); + [[nodiscard]] int CompareBuildNr(const Version& other) const; + [[nodiscard]] int CompareTimestamp(const Version& other) const; + [[nodiscard]] int CompareComponents(const Version& other) const; + }; +} \ No newline at end of file diff --git a/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp b/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp index b90309b..273cd1b 100644 --- a/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp +++ b/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp @@ -40,8 +40,7 @@ class CubesExampleAppImpl final : public CubesExampleApp public: std::string GetAppName() override { return "ExampleApp"; } - std::string GetAppVersion() override { return "v1.0"; } - int GetAppVersionAsInt() override { return 1; } + openVulkanoCpp::Version GetAppVersion() override { return {"v1.0"}; } void Init() override { diff --git a/openVulkanoCpp/Host/GraphicsAppManager.cpp b/openVulkanoCpp/Host/GraphicsAppManager.cpp index 504b4bd..c325ed5 100644 --- a/openVulkanoCpp/Host/GraphicsAppManager.cpp +++ b/openVulkanoCpp/Host/GraphicsAppManager.cpp @@ -96,7 +96,7 @@ namespace openVulkanoCpp window->Init(renderApi); //TODO restore window settings if there are any set renderer->Init(static_cast(this), window); - windowTitleFormat = app->GetAppName() + " " + app->GetAppVersion() + " - " + renderer->GetMainRenderDeviceName() + " - {:.1f} fps ({:.1f} ms)"; + windowTitleFormat = app->GetAppName() + " " + static_cast(app->GetAppVersion()) + " - " + renderer->GetMainRenderDeviceName() + " - {:.1f} fps ({:.1f} ms)"; Logger::MANAGER->info("Initialized"); } catch (std::exception& e) diff --git a/openVulkanoCpp/Vulkan/Context.cpp b/openVulkanoCpp/Vulkan/Context.cpp index 35aa90a..8853368 100644 --- a/openVulkanoCpp/Vulkan/Context.cpp +++ b/openVulkanoCpp/Vulkan/Context.cpp @@ -61,7 +61,7 @@ namespace openVulkanoCpp::Vulkan void Context::CreateInstance() { if (enableValidationLayer) RequireExtension(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); - vk::ApplicationInfo appInfo(graphicsAppManager->GetGraphicsApp()->GetAppName().c_str(), graphicsAppManager->GetGraphicsApp()->GetAppVersionAsInt(), ENGINE_NAME, ENGINE_VERSION.intVersion, VK_MAKE_VERSION(1, 1, 0)); + vk::ApplicationInfo appInfo(graphicsAppManager->GetGraphicsApp()->GetAppName().c_str(), static_cast(graphicsAppManager->GetGraphicsApp()->GetAppVersion()), ENGINE_NAME, ENGINE_VERSION.intVersion, VK_MAKE_VERSION(1, 1, 0)); std::vector extensions = Utils::toCString(requiredExtensions), layers = Debug::GetValidationLayers(); const vk::InstanceCreateInfo createInfo(vk::InstanceCreateFlags(), &appInfo, enableValidationLayer ? layers.size() : 0,