From 856ba177aa3cbe0ff2a89b63e4ac5e70dcddc514 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 20 Oct 2024 14:09:50 +0000 Subject: [PATCH] refactor: Replace BoolName w/ FormattedBool --- src/lib_system/system/bool_name.h | 50 ---------- src/lib_system/system/formatted_bool.h | 129 +++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 50 deletions(-) delete mode 100644 src/lib_system/system/bool_name.h create mode 100644 src/lib_system/system/formatted_bool.h diff --git a/src/lib_system/system/bool_name.h b/src/lib_system/system/bool_name.h deleted file mode 100644 index e20aaf4ef..000000000 --- a/src/lib_system/system/bool_name.h +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once -#include - -namespace bts::core { - -class BoolName { - public: - explicit constexpr BoolName(bool value); - - [[nodiscard]] static constexpr const std::string &true_false(bool value); - [[nodiscard]] static constexpr const std::string &yes_no(bool value); - [[nodiscard]] static constexpr const std::string &y_n(bool value); - [[nodiscard]] static constexpr const std::string &on_off(bool value); - - private: - bool value_; - - struct Names { - static inline const std::string _TRUE = "true"; - static inline const std::string _FALSE = "false"; - static inline const std::string YES = "yes"; - static inline const std::string NO = "no"; - static inline const std::string Y = "y"; - static inline const std::string N = "n"; - static inline const std::string ON = "on"; - static inline const std::string OFF = "off"; - }; -}; - -////////////////////////////////////////////////// -// Definition -////////////////////////////////////////////////// - -constexpr const std::string &BoolName::true_false(bool value) { - return value ? Names::_TRUE : Names::_FALSE; -} - -constexpr const std::string &BoolName::yes_no(bool value) { - return value ? Names::YES : Names::NO; -} - -constexpr const std::string &BoolName::y_n(bool value) { - return value ? Names::Y : Names::N; -} - -constexpr const std::string &BoolName::on_off(bool value) { - return value ? Names::ON : Names::OFF; -} - -} // namespace bts::core diff --git a/src/lib_system/system/formatted_bool.h b/src/lib_system/system/formatted_bool.h new file mode 100644 index 000000000..fbcfd46eb --- /dev/null +++ b/src/lib_system/system/formatted_bool.h @@ -0,0 +1,129 @@ +#pragma once + +#include +#include +#include + +namespace bts::system { + +enum class FormattedBool_Format { + TRUE_FALSE, + TRUE_FALSE_UPPER, + YES_NO, + YES_NO_UPPER, + Y_N, + Y_N_UPPER, + ON_OFF, + ON_OFF_UPPER, +}; + +template +struct FormattedBool { + constexpr FormattedBool() = default; + constexpr FormattedBool(bool value) : value(value) {} + bool value = false; + + constexpr bool operator()() const noexcept; +}; + +typedef FormattedBool fbool; +typedef FormattedBool fbool_true_false; +typedef FormattedBool fbool_TRUE_FALSE; +typedef FormattedBool fbool_yes_no; +typedef FormattedBool fbool_YES_NO; +typedef FormattedBool fbool_y_n; +typedef FormattedBool fbool_Y_N; +typedef FormattedBool fbool_on_off; +typedef FormattedBool fbool_ON_OFF; + +template +std::ostream &operator<<(std::ostream &out, FormattedBool rhs) noexcept; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +template +constexpr bool FormattedBool::operator()() const noexcept { + return value; +} + +} // namespace bts::system + +template +inline std::ostream &operator<<( + std::ostream &out, bts::system::FormattedBool fb) noexcept { + out << fb.value ? "true" : "false"; + return out; +} + +#define FORMATTED_BOOL_OUTPUT_IMPL(enum_name, t, f) \ + template <> \ + inline std::ostream &operator<<( \ + std::ostream &out, \ + bts::system::FormattedBool< \ + bts::system::FormattedBool_Format::enum_name> fb) noexcept { \ + if (fb.value) { \ + out << t; \ + } else { \ + out << f; \ + } \ + return out; \ + } + +#define FORMATTED_BOOL_JSON_IMPL(enum_name, t, f) \ + inline void from_json( \ + const nlohmann::json &j, \ + bts::system::FormattedBool< \ + bts::system::FormattedBool_Format::enum_name> &fb) { \ + switch (j.type()) { \ + case nlohmann::detail::value_t::string: { \ + const auto &s = j.get(); \ + if (s == t) { \ + fb.value = true; \ + } else if (s == f) { \ + fb.value = false; \ + } \ + break; \ + } \ + case nlohmann::detail::value_t::boolean: \ + fb.value = j.get(); \ + break; \ + default: \ + break; \ + } \ + } + +#define FORMATTED_BOOL_FMT_IMPL(enum_name, t, f) \ + template <> \ + struct fmt::formatter> \ + : formatter { \ + template \ + auto format( \ + const bts::system::FormattedBool< \ + bts::system::FormattedBool_Format::enum_name> &fb, \ + FormatContext &ctx) const -> format_context::iterator { \ + string_view name = fb.value ? t : f; \ + return formatter::format(name, ctx); \ + } \ + } + +#define FORMATTED_BOOL_IMPL(enum_name, t, f) \ + FORMATTED_BOOL_OUTPUT_IMPL(enum_name, t, f) \ + FORMATTED_BOOL_JSON_IMPL(enum_name, t, f) \ + FORMATTED_BOOL_FMT_IMPL(enum_name, t, f) + +FORMATTED_BOOL_IMPL(TRUE_FALSE, "true", "false"); +FORMATTED_BOOL_IMPL(TRUE_FALSE_UPPER, "TRUE", "FALSE"); +FORMATTED_BOOL_IMPL(YES_NO, "yes", "no"); +FORMATTED_BOOL_IMPL(YES_NO_UPPER, "YES", "NO"); +FORMATTED_BOOL_IMPL(Y_N, "yes", "no"); +FORMATTED_BOOL_IMPL(Y_N_UPPER, "YES", "NO"); +FORMATTED_BOOL_IMPL(ON_OFF, "on", "off"); +FORMATTED_BOOL_IMPL(ON_OFF_UPPER, "ON", "OFF"); + +#undef FORMATTED_BOOL_OUTPUT_IMPL +#undef FORMATTED_BOOL_JSON_IMPL +#undef FORMATTED_BOOL_FMT_IMPL +#undef FORMATTED_BOOL_IMPL