refactor: Replace BoolName w/ FormattedBool

This commit is contained in:
John Colvin committed 2024-11-26 18:40:34 +00:00
1 parent 54819d7d9b
commit 856ba177aa
2 files changed
+129 -50

No files matched your search

-50
View File
@@ -1,50 +0,0 @@
#pragma once
#include <string>
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
+129
View File
@@ -0,0 +1,129 @@
#pragma once
#include <fmt/format.h>
#include <nlohmann/json.hpp>
#include <ostream>
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 <FormattedBool_Format F = FormattedBool_Format::TRUE_FALSE>
struct FormattedBool {
constexpr FormattedBool() = default;
constexpr FormattedBool(bool value) : value(value) {}
bool value = false;
constexpr bool operator()() const noexcept;
};
typedef FormattedBool<FormattedBool_Format::TRUE_FALSE> fbool;
typedef FormattedBool<FormattedBool_Format::TRUE_FALSE> fbool_true_false;
typedef FormattedBool<FormattedBool_Format::TRUE_FALSE> fbool_TRUE_FALSE;
typedef FormattedBool<FormattedBool_Format::YES_NO> fbool_yes_no;
typedef FormattedBool<FormattedBool_Format::YES_NO_UPPER> fbool_YES_NO;
typedef FormattedBool<FormattedBool_Format::Y_N> fbool_y_n;
typedef FormattedBool<FormattedBool_Format::Y_N_UPPER> fbool_Y_N;
typedef FormattedBool<FormattedBool_Format::ON_OFF> fbool_on_off;
typedef FormattedBool<FormattedBool_Format::ON_OFF_UPPER> fbool_ON_OFF;
template <FormattedBool_Format F>
std::ostream &operator<<(std::ostream &out, FormattedBool<F> rhs) noexcept;
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
template <FormattedBool_Format F>
constexpr bool FormattedBool<F>::operator()() const noexcept {
return value;
}
} // namespace bts::system
template <bts::system::FormattedBool_Format F>
inline std::ostream &operator<<(
std::ostream &out, bts::system::FormattedBool<F> 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<std::string>(); \
if (s == t) { \
fb.value = true; \
} else if (s == f) { \
fb.value = false; \
} \
break; \
} \
case nlohmann::detail::value_t::boolean: \
fb.value = j.get<bool>(); \
break; \
default: \
break; \
} \
}
#define FORMATTED_BOOL_FMT_IMPL(enum_name, t, f) \
template <> \
struct fmt::formatter<bts::system::FormattedBool< \
bts::system::FormattedBool_Format::enum_name>> \
: formatter<string_view> { \
template <typename FormatContext> \
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<string_view>::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