Skip to content

Commit

Permalink
Move formatter<std::error_code> from fmt/os.h to fmt/std.h (#3125)
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Shchapov <[email protected]>

Signed-off-by: Vladislav Shchapov <[email protected]>
  • Loading branch information
phprus authored Oct 2, 2022
1 parent ad91cab commit 662adf4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
18 changes: 0 additions & 18 deletions include/fmt/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,6 @@ template <typename Char> class basic_cstring_view {
using cstring_view = basic_cstring_view<char>;
using wcstring_view = basic_cstring_view<wchar_t>;

template <typename Char> struct formatter<std::error_code, Char> {
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}

template <typename FormatContext>
FMT_CONSTEXPR auto format(const std::error_code& ec, FormatContext& ctx) const
-> decltype(ctx.out()) {
auto out = ctx.out();
out = detail::write_bytes(out, ec.category().name(),
basic_format_specs<Char>());
out = detail::write<Char>(out, Char(':'));
out = detail::write<Char>(out, ec.value());
return out;
}
};

#ifdef _WIN32
FMT_API const std::error_category& system_category() noexcept;

Expand Down
19 changes: 19 additions & 0 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,25 @@ FMT_END_NAMESPACE
#endif // __cpp_lib_variant

FMT_BEGIN_NAMESPACE

template <typename Char> struct formatter<std::error_code, Char> {
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}

template <typename FormatContext>
FMT_CONSTEXPR auto format(const std::error_code& ec, FormatContext& ctx) const
-> decltype(ctx.out()) {
auto out = ctx.out();
out = detail::write_bytes(out, ec.category().name(),
basic_format_specs<Char>());
out = detail::write<Char>(out, Char(':'));
out = detail::write<Char>(out, ec.value());
return out;
}
};

template <typename T, typename Char>
struct formatter<
T, Char,
Expand Down
12 changes: 0 additions & 12 deletions test/os-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,6 @@ TEST(util_test, utf16_to_utf8_convert) {
u.convert(wstring_view(L"foo", INT_MAX + 1u)));
}

TEST(os_test, format_std_error_code) {
EXPECT_EQ("generic:42",
fmt::format(FMT_STRING("{0}"),
std::error_code(42, std::generic_category())));
EXPECT_EQ("system:42",
fmt::format(FMT_STRING("{0}"),
std::error_code(42, fmt::system_category())));
EXPECT_EQ("system:-42",
fmt::format(FMT_STRING("{0}"),
std::error_code(-42, fmt::system_category())));
}

TEST(os_test, format_windows_error) {
LPWSTR message = nullptr;
auto result = FormatMessageW(
Expand Down
13 changes: 13 additions & 0 deletions test/std-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string>
#include <vector>

#include "fmt/os.h" // fmt::system_category
#include "fmt/ranges.h"
#include "gtest-extra.h" // StartsWith

Expand Down Expand Up @@ -83,6 +84,18 @@ TEST(std_test, variant) {
#endif
}

TEST(std_test, error_code) {
EXPECT_EQ("generic:42",
fmt::format(FMT_STRING("{0}"),
std::error_code(42, std::generic_category())));
EXPECT_EQ("system:42",
fmt::format(FMT_STRING("{0}"),
std::error_code(42, fmt::system_category())));
EXPECT_EQ("system:-42",
fmt::format(FMT_STRING("{0}"),
std::error_code(-42, fmt::system_category())));
}

template <typename Catch> void exception_test() {
try {
throw std::runtime_error("Test Exception");
Expand Down

0 comments on commit 662adf4

Please sign in to comment.