mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 02:37:36 +02:00
Simplify ostream opt-in API
This commit is contained in:
@ -78,14 +78,12 @@ void format_value(buffer<Char>& buf, const T& value,
|
|||||||
output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
|
output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
|
||||||
buf.try_resize(buf.size());
|
buf.try_resize(buf.size());
|
||||||
}
|
}
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
// Formats an object of type T that has an overloaded ostream operator<<.
|
// Formats an object of type T that has an overloaded ostream operator<<.
|
||||||
template <typename T, typename Char>
|
template <typename Char>
|
||||||
struct fallback_formatter<T, Char, enable_if_t<is_streamable<T, Char>::value>>
|
struct basic_ostream_formatter : formatter<basic_string_view<Char>, Char> {
|
||||||
: private formatter<basic_string_view<Char>, Char> {
|
template <typename T, typename OutputIt>
|
||||||
using formatter<basic_string_view<Char>, Char>::parse;
|
|
||||||
|
|
||||||
template <typename OutputIt>
|
|
||||||
auto format(const T& value, basic_format_context<OutputIt, Char>& ctx) const
|
auto format(const T& value, basic_format_context<OutputIt, Char>& ctx) const
|
||||||
-> OutputIt {
|
-> OutputIt {
|
||||||
auto buffer = basic_memory_buffer<Char>();
|
auto buffer = basic_memory_buffer<Char>();
|
||||||
@ -93,7 +91,17 @@ struct fallback_formatter<T, Char, enable_if_t<is_streamable<T, Char>::value>>
|
|||||||
return formatter<basic_string_view<Char>, Char>::format(
|
return formatter<basic_string_view<Char>, Char>::format(
|
||||||
{buffer.data(), buffer.size()}, ctx);
|
{buffer.data(), buffer.size()}, ctx);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
using ostream_formatter = basic_ostream_formatter<char>;
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
// Formats an object of type T that has an overloaded ostream operator<<.
|
||||||
|
template <typename T, typename Char>
|
||||||
|
struct fallback_formatter<T, Char, enable_if_t<is_streamable<T, Char>::value>>
|
||||||
|
: basic_ostream_formatter<Char> {
|
||||||
|
using basic_ostream_formatter<Char>::format;
|
||||||
// DEPRECATED!
|
// DEPRECATED!
|
||||||
template <typename OutputIt>
|
template <typename OutputIt>
|
||||||
auto format(const T& value, basic_printf_context<OutputIt, Char>& ctx) const
|
auto format(const T& value, basic_printf_context<OutputIt, Char>& ctx) const
|
||||||
@ -105,9 +113,6 @@ struct fallback_formatter<T, Char, enable_if_t<is_streamable<T, Char>::value>>
|
|||||||
};
|
};
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
template <typename T, typename Char = char>
|
|
||||||
using ostream_formatter = detail::fallback_formatter<T, Char>;
|
|
||||||
|
|
||||||
FMT_MODULE_EXPORT
|
FMT_MODULE_EXPORT
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
void vprint(std::basic_ostream<Char>& os, basic_string_view<Char> format_str,
|
void vprint(std::basic_ostream<Char>& os, basic_string_view<Char> format_str,
|
||||||
|
@ -57,11 +57,10 @@ struct empty_test {};
|
|||||||
std::ostream& operator<<(std::ostream& os, empty_test) { return os << ""; }
|
std::ostream& operator<<(std::ostream& os, empty_test) { return os << ""; }
|
||||||
|
|
||||||
namespace fmt {
|
namespace fmt {
|
||||||
template <> struct formatter<test_string> : ostream_formatter<test_string> {};
|
template <> struct formatter<test_string> : ostream_formatter {};
|
||||||
template <> struct formatter<date> : ostream_formatter<date> {};
|
template <> struct formatter<date> : ostream_formatter {};
|
||||||
template <>
|
template <> struct formatter<streamable_enum> : ostream_formatter {};
|
||||||
struct formatter<streamable_enum> : ostream_formatter<streamable_enum> {};
|
template <> struct formatter<empty_test> : ostream_formatter {};
|
||||||
template <> struct formatter<empty_test> : ostream_formatter<empty_test> {};
|
|
||||||
} // namespace fmt
|
} // namespace fmt
|
||||||
|
|
||||||
TEST(ostream_test, enum) {
|
TEST(ostream_test, enum) {
|
||||||
@ -193,8 +192,7 @@ template <typename T> struct formatter<test_template<T>> : formatter<int> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <> struct formatter<fmt_test::abc> : ostream_formatter {};
|
||||||
struct formatter<fmt_test::abc> : ostream_formatter<fmt_test::abc> {};
|
|
||||||
} // namespace fmt
|
} // namespace fmt
|
||||||
|
|
||||||
TEST(ostream_test, template) {
|
TEST(ostream_test, template) {
|
||||||
@ -262,7 +260,7 @@ std::ostream& operator<<(std::ostream& os, copyfmt_test) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
namespace fmt {
|
namespace fmt {
|
||||||
template <> struct formatter<copyfmt_test> : ostream_formatter<copyfmt_test> {};
|
template <> struct formatter<copyfmt_test> : ostream_formatter {};
|
||||||
} // namespace fmt
|
} // namespace fmt
|
||||||
|
|
||||||
TEST(ostream_test, copyfmt) {
|
TEST(ostream_test, copyfmt) {
|
||||||
@ -287,7 +285,7 @@ struct abstract {
|
|||||||
};
|
};
|
||||||
|
|
||||||
namespace fmt {
|
namespace fmt {
|
||||||
template <> struct formatter<abstract> : ostream_formatter<abstract> {};
|
template <> struct formatter<abstract> : ostream_formatter {};
|
||||||
} // namespace fmt
|
} // namespace fmt
|
||||||
|
|
||||||
void format_abstract_compiles(const abstract& a) {
|
void format_abstract_compiles(const abstract& a) {
|
||||||
|
@ -222,8 +222,8 @@ std::wostream& operator<<(std::wostream& os, streamable_enum) {
|
|||||||
|
|
||||||
namespace fmt {
|
namespace fmt {
|
||||||
template <>
|
template <>
|
||||||
struct formatter<streamable_enum, wchar_t>
|
struct formatter<streamable_enum, wchar_t> : basic_ostream_formatter<wchar_t> {
|
||||||
: ostream_formatter<streamable_enum, wchar_t> {};
|
};
|
||||||
} // namespace fmt
|
} // namespace fmt
|
||||||
|
|
||||||
enum unstreamable_enum {};
|
enum unstreamable_enum {};
|
||||||
|
Reference in New Issue
Block a user