130 lines
6.1 KiB
C++
130 lines
6.1 KiB
C++
#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
|