From cbf51cd5ab13a63647c47a61b01ca28c10bf9bc1 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Thu, 17 Aug 2023 11:55:27 +0900 Subject: [PATCH] `DrawStringFormatArg`: Use `std::variant` --- Source/engine/render/text_render.cpp | 5 ++-- Source/engine/render/text_render.hpp | 34 ++++++++-------------------- Source/player.h | 2 +- 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/Source/engine/render/text_render.cpp b/Source/engine/render/text_render.cpp index 5bfdce5fd..8d6f9f95c 100644 --- a/Source/engine/render/text_render.cpp +++ b/Source/engine/render/text_render.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include @@ -262,7 +263,7 @@ std::size_t CountNewlines(std::string_view fmt, const DrawStringFormatArg *args, { std::size_t result = c_count(fmt, '\n'); for (std::size_t i = 0; i < argsLen; ++i) { - if (args[i].GetType() == DrawStringFormatArg::Type::StringView) + if (std::holds_alternative(args[i].value())) result += c_count(args[i].GetFormatted(), '\n'); } return result; @@ -312,7 +313,7 @@ public: } else { if (!args_[*result].HasFormatted()) { const auto fmtStr = positional ? "{}" : std::string_view(rest.data(), fmtLen); - args_[*result].SetFormatted(fmt::format(fmt::runtime(fmtStr), args_[*result].GetIntValue())); + args_[*result].SetFormatted(fmt::format(fmt::runtime(fmtStr), std::get(args_[*result].value()))); } rest.remove_prefix(fmtLen); } diff --git a/Source/engine/render/text_render.hpp b/Source/engine/render/text_render.hpp index 372cd43b5..0083854df 100644 --- a/Source/engine/render/text_render.hpp +++ b/Source/engine/render/text_render.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -57,34 +58,24 @@ enum text_color : uint8_t { */ class DrawStringFormatArg { public: - enum class Type : uint8_t { - StringView, - Int - }; + using Value = std::variant; DrawStringFormatArg(std::string_view value, UiFlags flags) - : type_(Type::StringView) - , string_view_value_(value) + : value_(value) , flags_(flags) { } DrawStringFormatArg(int value, UiFlags flags) - : type_(Type::Int) - , int_value_(value) + : value_(value) , flags_(flags) { } - Type GetType() const - { - return type_; - } - std::string_view GetFormatted() const { - if (type_ == Type::StringView) - return string_view_value_; + if (std::holds_alternative(value_)) + return std::get(value_); return formatted_; } @@ -95,12 +86,12 @@ public: bool HasFormatted() const { - return type_ == Type::StringView || !formatted_.empty(); + return std::holds_alternative(value_) || !formatted_.empty(); } - int GetIntValue() const + const Value &value() const { - return int_value_; + return value_; } UiFlags GetFlags() const @@ -109,12 +100,7 @@ public: } private: - Type type_; - union { - std::string_view string_view_value_; - int int_value_; - }; - + Value value_; UiFlags flags_; std::string formatted_; }; diff --git a/Source/player.h b/Source/player.h index a466f2a91..6e39cebb1 100644 --- a/Source/player.h +++ b/Source/player.h @@ -172,7 +172,7 @@ enum class DeathReason { }; /** Maps from armor animation to letter used in graphic files. */ -constexpr std::array ArmourChar = { +constexpr std::array ArmourChar = { 'l', // light 'm', // medium 'h', // heavy