Introduce Version class

This commit is contained in:
2023-08-26 21:23:25 +02:00
parent a683d2f476
commit 317d5c0e3c
6 changed files with 254 additions and 6 deletions

View File

@@ -8,6 +8,7 @@
#include "ITickable.hpp" #include "ITickable.hpp"
#include "ICloseable.hpp" #include "ICloseable.hpp"
#include "Version.hpp"
#include <string> #include <string>
namespace openVulkanoCpp namespace openVulkanoCpp
@@ -26,7 +27,6 @@ namespace openVulkanoCpp
IGraphicsAppManager* GetGraphicsAppManager() const { return manager; } IGraphicsAppManager* GetGraphicsAppManager() const { return manager; }
void SetGraphicsAppManager(IGraphicsAppManager* manager) { this->manager = manager; } void SetGraphicsAppManager(IGraphicsAppManager* manager) { this->manager = manager; }
virtual std::string GetAppName() = 0; virtual std::string GetAppName() = 0;
virtual std::string GetAppVersion() = 0; virtual Version GetAppVersion() = 0;
virtual int GetAppVersionAsInt() = 0;
}; };
} }

View File

@@ -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 <charconv>
#include <sstream>
#include <regex>
#if (__cplusplus >= 202002L)
#include <format>
#endif
namespace openVulkanoCpp
{
namespace
{
struct ComponentDecodeHolder
{
std::vector<uint32_t> versionComponents;
std::vector<std::string> tagComponents;
std::vector<uint32_t> 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<uint32_t>& 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<std::string>& 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<char, uint64_t> ReadTagValue(const std::string& str)
{
std::pair<char, uint64_t> 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<uint32_t>{major, minor, patch, build} : std::initializer_list<uint32_t>{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
}

View File

@@ -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 <cstdint>
#include <string>
#include <vector>
namespace openVulkanoCpp
{
class Version
{
std::vector<uint32_t> m_versionComponents;
std::vector<std::string> 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<std::string>& GetTags() const { return m_tagComponents; }
[[nodiscard]] const std::vector<uint32_t>& 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;
};
}

View File

@@ -40,8 +40,7 @@ class CubesExampleAppImpl final : public CubesExampleApp
public: public:
std::string GetAppName() override { return "ExampleApp"; } std::string GetAppName() override { return "ExampleApp"; }
std::string GetAppVersion() override { return "v1.0"; } openVulkanoCpp::Version GetAppVersion() override { return {"v1.0"}; }
int GetAppVersionAsInt() override { return 1; }
void Init() override void Init() override
{ {

View File

@@ -96,7 +96,7 @@ namespace openVulkanoCpp
window->Init(renderApi); window->Init(renderApi);
//TODO restore window settings if there are any set //TODO restore window settings if there are any set
renderer->Init(static_cast<IGraphicsAppManager*>(this), window); renderer->Init(static_cast<IGraphicsAppManager*>(this), window);
windowTitleFormat = app->GetAppName() + " " + app->GetAppVersion() + " - " + renderer->GetMainRenderDeviceName() + " - {:.1f} fps ({:.1f} ms)"; windowTitleFormat = app->GetAppName() + " " + static_cast<std::string>(app->GetAppVersion()) + " - " + renderer->GetMainRenderDeviceName() + " - {:.1f} fps ({:.1f} ms)";
Logger::MANAGER->info("Initialized"); Logger::MANAGER->info("Initialized");
} }
catch (std::exception& e) catch (std::exception& e)

View File

@@ -61,7 +61,7 @@ namespace openVulkanoCpp::Vulkan
void Context::CreateInstance() void Context::CreateInstance()
{ {
if (enableValidationLayer) RequireExtension(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); 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<uint32_t>(graphicsAppManager->GetGraphicsApp()->GetAppVersion()), ENGINE_NAME, ENGINE_VERSION.intVersion, VK_MAKE_VERSION(1, 1, 0));
std::vector<const char*> extensions = Utils::toCString(requiredExtensions), layers = Debug::GetValidationLayers(); std::vector<const char*> extensions = Utils::toCString(requiredExtensions), layers = Debug::GetValidationLayers();
const vk::InstanceCreateInfo createInfo(vk::InstanceCreateFlags(), &appInfo, enableValidationLayer ? layers.size() : 0, const vk::InstanceCreateInfo createInfo(vk::InstanceCreateFlags(), &appInfo, enableValidationLayer ? layers.size() : 0,