Generalize format_as

This commit is contained in:
Victor Zverovich
2023-03-26 07:37:51 -07:00
parent f6276a2c2b
commit 41cfc739fe
4 changed files with 42 additions and 49 deletions
+6 -6
View File
@@ -329,6 +329,12 @@ struct monostate {
# define FMT_ENABLE_IF(...) fmt::enable_if_t<(__VA_ARGS__), int> = 0
#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
// 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 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 U,
FMT_ENABLE_IF(std::is_enum<U>::value || std::is_class<U>::value)>
+15 -17
View File
@@ -4218,6 +4218,18 @@ formatter<T, Char,
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>
struct formatter<void*, Char> : formatter<const void*, Char> {
template <typename FormatContext>
@@ -4370,23 +4382,9 @@ struct formatter<join_view<It, Sentinel, Char>, Char> {
#else
typename std::iterator_traits<It>::value_type;
#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 =
conditional_t<is_formattable<value_type, Char>::value,
formatter<remove_cvref_t<decltype(map(
std::declval<const value_type&>()))>,
Char>,
formatter<remove_cvref_t<value_type>, Char>,
detail::fallback_formatter<value_type, Char>>;
formatter_type value_formatter_;
@@ -4403,12 +4401,12 @@ struct formatter<join_view<It, Sentinel, Char>, Char> {
auto it = value.begin;
auto out = ctx.out();
if (it != value.end) {
out = value_formatter_.format(map(*it), ctx);
out = value_formatter_.format(*it, ctx);
++it;
while (it != value.end) {
out = detail::copy_str<Char>(value.sep.begin(), value.sep.end(), out);
ctx.advance_to(out);
out = value_formatter_.format(map(*it), ctx);
out = value_formatter_.format(*it, ctx);
++it;
}
}