mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
Generalize format_as
This commit is contained in:
@ -329,6 +329,12 @@ struct monostate {
|
|||||||
# define FMT_ENABLE_IF(...) fmt::enable_if_t<(__VA_ARGS__), int> = 0
|
# define FMT_ENABLE_IF(...) fmt::enable_if_t<(__VA_ARGS__), int> = 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cpp_lib_byte
|
||||||
|
inline auto format_as(std::byte b) -> unsigned char {
|
||||||
|
return static_cast<unsigned char>(b);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
FMT_BEGIN_DETAIL_NAMESPACE
|
FMT_BEGIN_DETAIL_NAMESPACE
|
||||||
|
|
||||||
// Suppresses "unused variable" warnings with the method described in
|
// Suppresses "unused variable" warnings with the method described in
|
||||||
@ -1359,12 +1365,6 @@ enum { long_short = sizeof(long) == sizeof(int) };
|
|||||||
using long_type = conditional_t<long_short, int, long long>;
|
using long_type = conditional_t<long_short, int, long long>;
|
||||||
using ulong_type = conditional_t<long_short, unsigned, unsigned long long>;
|
using ulong_type = conditional_t<long_short, unsigned, unsigned long long>;
|
||||||
|
|
||||||
#ifdef __cpp_lib_byte
|
|
||||||
inline auto format_as(std::byte b) -> unsigned char {
|
|
||||||
return static_cast<unsigned char>(b);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
template <typename T> struct format_as_result {
|
template <typename T> struct format_as_result {
|
||||||
template <typename U,
|
template <typename U,
|
||||||
FMT_ENABLE_IF(std::is_enum<U>::value || std::is_class<U>::value)>
|
FMT_ENABLE_IF(std::is_enum<U>::value || std::is_class<U>::value)>
|
||||||
|
@ -4218,6 +4218,18 @@ formatter<T, Char,
|
|||||||
return detail::write<Char>(ctx.out(), val, specs_, ctx.locale());
|
return detail::write<Char>(ctx.out(), val, specs_, ctx.locale());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T, typename Char>
|
||||||
|
struct formatter<T, Char, enable_if_t<detail::has_format_as<T>::value>>
|
||||||
|
: private formatter<detail::format_as_t<T>> {
|
||||||
|
using base = formatter<detail::format_as_t<T>>;
|
||||||
|
using base::parse;
|
||||||
|
|
||||||
|
template <typename FormatContext>
|
||||||
|
auto format(const T& value, FormatContext& ctx) const -> decltype(ctx.out()) {
|
||||||
|
return base::format(format_as(value), ctx);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
struct formatter<void*, Char> : formatter<const void*, Char> {
|
struct formatter<void*, Char> : formatter<const void*, Char> {
|
||||||
template <typename FormatContext>
|
template <typename FormatContext>
|
||||||
@ -4370,23 +4382,9 @@ struct formatter<join_view<It, Sentinel, Char>, Char> {
|
|||||||
#else
|
#else
|
||||||
typename std::iterator_traits<It>::value_type;
|
typename std::iterator_traits<It>::value_type;
|
||||||
#endif
|
#endif
|
||||||
using context = buffer_context<Char>;
|
|
||||||
using mapper = detail::arg_mapper<context>;
|
|
||||||
|
|
||||||
template <typename T, FMT_ENABLE_IF(has_formatter<T, context>::value)>
|
|
||||||
static auto map(const T& value) -> const T& {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
template <typename T, FMT_ENABLE_IF(!has_formatter<T, context>::value)>
|
|
||||||
static auto map(const T& value) -> decltype(mapper().map(value)) {
|
|
||||||
return mapper().map(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
using formatter_type =
|
using formatter_type =
|
||||||
conditional_t<is_formattable<value_type, Char>::value,
|
conditional_t<is_formattable<value_type, Char>::value,
|
||||||
formatter<remove_cvref_t<decltype(map(
|
formatter<remove_cvref_t<value_type>, Char>,
|
||||||
std::declval<const value_type&>()))>,
|
|
||||||
Char>,
|
|
||||||
detail::fallback_formatter<value_type, Char>>;
|
detail::fallback_formatter<value_type, Char>>;
|
||||||
|
|
||||||
formatter_type value_formatter_;
|
formatter_type value_formatter_;
|
||||||
@ -4403,12 +4401,12 @@ struct formatter<join_view<It, Sentinel, Char>, Char> {
|
|||||||
auto it = value.begin;
|
auto it = value.begin;
|
||||||
auto out = ctx.out();
|
auto out = ctx.out();
|
||||||
if (it != value.end) {
|
if (it != value.end) {
|
||||||
out = value_formatter_.format(map(*it), ctx);
|
out = value_formatter_.format(*it, ctx);
|
||||||
++it;
|
++it;
|
||||||
while (it != value.end) {
|
while (it != value.end) {
|
||||||
out = detail::copy_str<Char>(value.sep.begin(), value.sep.end(), out);
|
out = detail::copy_str<Char>(value.sep.begin(), value.sep.end(), out);
|
||||||
ctx.advance_to(out);
|
ctx.advance_to(out);
|
||||||
out = value_formatter_.format(map(*it), ctx);
|
out = value_formatter_.format(*it, ctx);
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -673,24 +673,6 @@ FMT_END_NAMESPACE
|
|||||||
|
|
||||||
enum class unformattable_scoped_enum {};
|
enum class unformattable_scoped_enum {};
|
||||||
|
|
||||||
namespace test {
|
|
||||||
enum class scoped_enum_as_int {};
|
|
||||||
auto format_as(scoped_enum_as_int) -> int { return 42; }
|
|
||||||
|
|
||||||
enum class scoped_enum_as_string_view {};
|
|
||||||
auto format_as(scoped_enum_as_string_view) -> fmt::string_view { return "foo"; }
|
|
||||||
|
|
||||||
enum class scoped_enum_as_string {};
|
|
||||||
auto format_as(scoped_enum_as_string) -> std::string { return "foo"; }
|
|
||||||
|
|
||||||
struct struct_as_int {};
|
|
||||||
auto format_as(struct_as_int) -> int { return 42; }
|
|
||||||
|
|
||||||
struct convertible_to_enum {
|
|
||||||
operator scoped_enum_as_int() const { return {}; }
|
|
||||||
};
|
|
||||||
} // namespace test
|
|
||||||
|
|
||||||
TEST(core_test, is_formattable) {
|
TEST(core_test, is_formattable) {
|
||||||
static_assert(!fmt::is_formattable<wchar_t>::value, "");
|
static_assert(!fmt::is_formattable<wchar_t>::value, "");
|
||||||
#ifdef __cpp_char8_t
|
#ifdef __cpp_char8_t
|
||||||
@ -729,7 +711,6 @@ TEST(core_test, is_formattable) {
|
|||||||
static_assert(!fmt::is_formattable<int(s::*)>::value, "");
|
static_assert(!fmt::is_formattable<int(s::*)>::value, "");
|
||||||
static_assert(!fmt::is_formattable<int (s::*)()>::value, "");
|
static_assert(!fmt::is_formattable<int (s::*)()>::value, "");
|
||||||
static_assert(!fmt::is_formattable<unformattable_scoped_enum>::value, "");
|
static_assert(!fmt::is_formattable<unformattable_scoped_enum>::value, "");
|
||||||
static_assert(fmt::is_formattable<test::scoped_enum_as_int>::value, "");
|
|
||||||
static_assert(!fmt::is_formattable<unformattable_scoped_enum>::value, "");
|
static_assert(!fmt::is_formattable<unformattable_scoped_enum>::value, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -741,13 +722,6 @@ TEST(core_test, format_to) {
|
|||||||
EXPECT_EQ(s, "42");
|
EXPECT_EQ(s, "42");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(core_test, format_as) {
|
|
||||||
EXPECT_EQ(fmt::format("{}", test::scoped_enum_as_int()), "42");
|
|
||||||
// EXPECT_EQ(fmt::format("{}", test::scoped_enum_as_string_view()), "foo");
|
|
||||||
// EXPECT_EQ(fmt::format("{}", test::scoped_enum_as_string()), "foo");
|
|
||||||
EXPECT_EQ(fmt::format("{}", test::struct_as_int()), "42");
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef __cpp_lib_byte
|
#ifdef __cpp_lib_byte
|
||||||
TEST(core_test, format_byte) {
|
TEST(core_test, format_byte) {
|
||||||
EXPECT_EQ(fmt::format("{}", std::byte(42)), "42");
|
EXPECT_EQ(fmt::format("{}", std::byte(42)), "42");
|
||||||
|
@ -2145,6 +2145,27 @@ TEST(format_test, back_insert_slicing) {
|
|||||||
EXPECT_EQ(fmt::format("{}", check_back_appender{}), "y");
|
EXPECT_EQ(fmt::format("{}", check_back_appender{}), "y");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace test {
|
||||||
|
enum class scoped_enum_as_int {};
|
||||||
|
auto format_as(scoped_enum_as_int) -> int { return 42; }
|
||||||
|
|
||||||
|
enum class scoped_enum_as_string_view {};
|
||||||
|
auto format_as(scoped_enum_as_string_view) -> fmt::string_view { return "foo"; }
|
||||||
|
|
||||||
|
enum class scoped_enum_as_string {};
|
||||||
|
auto format_as(scoped_enum_as_string) -> std::string { return "foo"; }
|
||||||
|
|
||||||
|
struct struct_as_int {};
|
||||||
|
auto format_as(struct_as_int) -> int { return 42; }
|
||||||
|
} // namespace test
|
||||||
|
|
||||||
|
TEST(format_test, format_as) {
|
||||||
|
EXPECT_EQ(fmt::format("{}", test::scoped_enum_as_int()), "42");
|
||||||
|
EXPECT_EQ(fmt::format("{}", test::scoped_enum_as_string_view()), "foo");
|
||||||
|
EXPECT_EQ(fmt::format("{}", test::scoped_enum_as_string()), "foo");
|
||||||
|
EXPECT_EQ(fmt::format("{}", test::struct_as_int()), "42");
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Char, typename T> bool check_enabled_formatter() {
|
template <typename Char, typename T> bool check_enabled_formatter() {
|
||||||
static_assert(std::is_default_constructible<fmt::formatter<T, Char>>::value,
|
static_assert(std::is_default_constructible<fmt::formatter<T, Char>>::value,
|
||||||
"");
|
"");
|
||||||
|
Reference in New Issue
Block a user