From 09195153e0fc5fc502caea9bab98c76e39f5e4bc Mon Sep 17 00:00:00 2001 From: Georg Hagen Date: Tue, 11 Feb 2025 22:02:25 +0100 Subject: [PATCH] Move some fmt related code and allow formatting of std::filesystem::path --- openVulkanoCpp/Base/Logger.hpp | 1 + openVulkanoCpp/Extensions/FmtFormatter.hpp | 45 ++++++++++ openVulkanoCpp/Math/ByteSize.hpp | 87 +++++++------------ .../Scene/UI/ImGuiExtensions/ImGuiTextFmt.hpp | 9 +- 4 files changed, 79 insertions(+), 63 deletions(-) create mode 100644 openVulkanoCpp/Extensions/FmtFormatter.hpp diff --git a/openVulkanoCpp/Base/Logger.hpp b/openVulkanoCpp/Base/Logger.hpp index 2490e20..27527a6 100644 --- a/openVulkanoCpp/Base/Logger.hpp +++ b/openVulkanoCpp/Base/Logger.hpp @@ -9,6 +9,7 @@ #define SPDLOG_DEBUG_ON #define SPDLOG_TRACE_ON +#include "Extensions/FmtFormatter.hpp" #include namespace OpenVulkano diff --git a/openVulkanoCpp/Extensions/FmtFormatter.hpp b/openVulkanoCpp/Extensions/FmtFormatter.hpp new file mode 100644 index 0000000..085a5bc --- /dev/null +++ b/openVulkanoCpp/Extensions/FmtFormatter.hpp @@ -0,0 +1,45 @@ +/* + * 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 + +#if __has_include("fmt/format.h") +#include +#elif __has_include("spdlog/fmt/fmt.h") +#include +#else +#error "Failed to find fmt include" +#endif +#include "Math/ByteSize.hpp" +#include + +template<> struct fmt::formatter +{ + template constexpr auto parse(ParseContext& ctx) + { + return ctx.begin(); + } + + template auto format(const OpenVulkano::ByteSize& bs, FormatContext& ctx) const + { + return fmt::format_to(ctx.out(), "{}", bs.Format()); + } +}; + +template<> struct fmt::formatter : fmt::formatter +{ + template + auto format(const std::filesystem::path& path, format_context& ctx) const requires std::is_same_v + { + return formatter::format(path.native(), ctx); + } + + template + auto format(const std::filesystem::path& path, format_context& ctx) const requires std::is_same_v + { + return formatter::format(path.string(), ctx); + } +}; \ No newline at end of file diff --git a/openVulkanoCpp/Math/ByteSize.hpp b/openVulkanoCpp/Math/ByteSize.hpp index 1e3bfef..830861d 100644 --- a/openVulkanoCpp/Math/ByteSize.hpp +++ b/openVulkanoCpp/Math/ByteSize.hpp @@ -11,22 +11,13 @@ #include #include #include -#include -#if __has_include("fmt/format.h") -#include -#elif __has_include("spdlog/fmt/fmt.h") -#include -#else -#warning "Failed to find fmt include" -#define OVKMBS_FMT_MISSING -#endif #include namespace OpenVulkano { namespace ByteSizeUnitHelper { - constexpr std::array BuildFactors() + consteval std::array BuildFactors() { std::array factors = { 1 }; for(uint64_t j = 1, i = 10; i < 70; i += 10, j++) @@ -40,7 +31,7 @@ namespace OpenVulkano return factors; } - constexpr std::array BuildDivisors() + consteval std::array BuildDivisors() { std::array factors = BuildFactors(); std::array divs{{1}}; @@ -59,7 +50,7 @@ namespace OpenVulkano public: enum Unit : int { B = 0, kiB, MiB, GiB, TiB, PiB, EiB, kB, MB, GB, TB, PB, EB }; - constexpr ByteSizeUnit(Unit unit) : unit(unit) {} + constexpr ByteSizeUnit(const Unit unit) : unit(unit) {} [[nodiscard]] constexpr uint64_t GetFactor() const { return FACTORS[unit]; } @@ -97,10 +88,10 @@ namespace OpenVulkano uint64_t bytes; public: - constexpr ByteSize(uint64_t size = 0) : bytes(size) {} + constexpr ByteSize(const uint64_t size = 0) : bytes(size) {} template - constexpr ByteSize(T size, ByteSizeUnit unit) + constexpr ByteSize(const T size, const ByteSizeUnit unit) : bytes(size * unit.GetFactor()) {} ByteSize(const std::string& str) @@ -114,7 +105,7 @@ namespace OpenVulkano std::ostream& operator<<(std::ostream& os) const { return FormatStream(os); } - std::ostream& FormatStream(std::ostream& oss, bool si = false) const + std::ostream& FormatStream(std::ostream& oss, const bool si = false) const { oss << std::setprecision(3); auto unit = ByteSizeUnit::GetClosestUnit(bytes, si); @@ -122,7 +113,7 @@ namespace OpenVulkano return oss; } - [[nodiscard]] std::string Format(bool si = false) const + [[nodiscard]] std::string Format(const bool si = false) const { std::ostringstream oss; FormatStream(oss, si); @@ -145,44 +136,30 @@ namespace OpenVulkano ByteSize& operator -=(const ByteSize other) { bytes -= other.bytes; return *this; } }; - inline constexpr ByteSize operator"" _kiB(long double num) { return { num, ByteSizeUnit::kiB }; } - inline constexpr ByteSize operator"" _MiB(long double num) { return { num, ByteSizeUnit::MiB }; } - inline constexpr ByteSize operator"" _GiB(long double num) { return { num, ByteSizeUnit::GiB }; } - inline constexpr ByteSize operator"" _TiB(long double num) { return { num, ByteSizeUnit::TiB }; } - inline constexpr ByteSize operator"" _PiB(long double num) { return { num, ByteSizeUnit::PiB }; } - inline constexpr ByteSize operator"" _EiB(long double num) { return { num, ByteSizeUnit::EiB }; } - inline constexpr ByteSize operator"" _kB(long double num) { return { num, ByteSizeUnit::kB }; } - inline constexpr ByteSize operator"" _MB(long double num) { return { num, ByteSizeUnit::MB }; } - inline constexpr ByteSize operator"" _GB(long double num) { return { num, ByteSizeUnit::GB }; } - inline constexpr ByteSize operator"" _TB(long double num) { return { num, ByteSizeUnit::TB }; } - inline constexpr ByteSize operator"" _PB(long double num) { return { num, ByteSizeUnit::PB }; } - inline constexpr ByteSize operator"" _EB(long double num) { return { num, ByteSizeUnit::EB }; } - inline constexpr ByteSize operator"" _B(unsigned long long int num) { return { num }; } - inline constexpr ByteSize operator"" _kiB(unsigned long long int num) { return { num << 10 }; } - inline constexpr ByteSize operator"" _MiB(unsigned long long int num) { return { num << 20 }; } - inline constexpr ByteSize operator"" _GiB(unsigned long long int num) { return { num << 30 }; } - inline constexpr ByteSize operator"" _TiB(unsigned long long int num) { return { num << 40 }; } - inline constexpr ByteSize operator"" _PiB(unsigned long long int num) { return { num << 50 }; } - inline constexpr ByteSize operator"" _EiB(unsigned long long int num) { return { num << 60 }; } - inline constexpr ByteSize operator"" _kB(unsigned long long int num) { return { num, ByteSizeUnit::kB }; } - inline constexpr ByteSize operator"" _MB(unsigned long long int num) { return { num, ByteSizeUnit::MB }; } - inline constexpr ByteSize operator"" _GB(unsigned long long int num) { return { num, ByteSizeUnit::GB }; } - inline constexpr ByteSize operator"" _TB(unsigned long long int num) { return { num, ByteSizeUnit::TB }; } - inline constexpr ByteSize operator"" _PB(unsigned long long int num) { return { num, ByteSizeUnit::PB }; } - inline constexpr ByteSize operator"" _EB(unsigned long long int num) { return { num, ByteSizeUnit::EB }; } + constexpr ByteSize operator"" _kiB(long double num) { return { num, ByteSizeUnit::kiB }; } + constexpr ByteSize operator"" _MiB(long double num) { return { num, ByteSizeUnit::MiB }; } + constexpr ByteSize operator"" _GiB(long double num) { return { num, ByteSizeUnit::GiB }; } + constexpr ByteSize operator"" _TiB(long double num) { return { num, ByteSizeUnit::TiB }; } + constexpr ByteSize operator"" _PiB(long double num) { return { num, ByteSizeUnit::PiB }; } + constexpr ByteSize operator"" _EiB(long double num) { return { num, ByteSizeUnit::EiB }; } + constexpr ByteSize operator"" _kB(long double num) { return { num, ByteSizeUnit::kB }; } + constexpr ByteSize operator"" _MB(long double num) { return { num, ByteSizeUnit::MB }; } + constexpr ByteSize operator"" _GB(long double num) { return { num, ByteSizeUnit::GB }; } + constexpr ByteSize operator"" _TB(long double num) { return { num, ByteSizeUnit::TB }; } + constexpr ByteSize operator"" _PB(long double num) { return { num, ByteSizeUnit::PB }; } + constexpr ByteSize operator"" _EB(long double num) { return { num, ByteSizeUnit::EB }; } + constexpr ByteSize operator"" _B(unsigned long long int num) { return { num }; } + constexpr ByteSize operator"" _kiB(unsigned long long int num) { return { num << 10 }; } + constexpr ByteSize operator"" _MiB(unsigned long long int num) { return { num << 20 }; } + constexpr ByteSize operator"" _GiB(unsigned long long int num) { return { num << 30 }; } + constexpr ByteSize operator"" _TiB(unsigned long long int num) { return { num << 40 }; } + constexpr ByteSize operator"" _PiB(unsigned long long int num) { return { num << 50 }; } + constexpr ByteSize operator"" _EiB(unsigned long long int num) { return { num << 60 }; } + constexpr ByteSize operator"" _kB(unsigned long long int num) { return { num, ByteSizeUnit::kB }; } + constexpr ByteSize operator"" _MB(unsigned long long int num) { return { num, ByteSizeUnit::MB }; } + constexpr ByteSize operator"" _GB(unsigned long long int num) { return { num, ByteSizeUnit::GB }; } + constexpr ByteSize operator"" _TB(unsigned long long int num) { return { num, ByteSizeUnit::TB }; } + constexpr ByteSize operator"" _PB(unsigned long long int num) { return { num, ByteSizeUnit::PB }; } + constexpr ByteSize operator"" _EB(unsigned long long int num) { return { num, ByteSizeUnit::EB }; } } -#ifndef OVKMBS_FMT_MISSING -template<> struct fmt::formatter -{ - template constexpr auto parse(ParseContext& ctx) - { - return ctx.begin(); - } - - template auto format(const OpenVulkano::ByteSize& bs, FormatContext& ctx) - { - return fmt::format_to(ctx.out(), "{}", bs.Format()); - } -}; -#endif diff --git a/openVulkanoCpp/Scene/UI/ImGuiExtensions/ImGuiTextFmt.hpp b/openVulkanoCpp/Scene/UI/ImGuiExtensions/ImGuiTextFmt.hpp index 558519d..965cce9 100644 --- a/openVulkanoCpp/Scene/UI/ImGuiExtensions/ImGuiTextFmt.hpp +++ b/openVulkanoCpp/Scene/UI/ImGuiExtensions/ImGuiTextFmt.hpp @@ -6,17 +6,10 @@ #pragma once +#include "Extensions/FmtFormatter.hpp" #include #include -#if __has_include("fmt/format.h") -#include -#elif __has_include("spdlog/fmt/fmt.h") -#include -#else -#error "Failed to find fmt include" -#endif - namespace ImGui { template