Skip to content

Commit

Permalink
util: add fmt::formatter for bool_class<Tag>
Browse files Browse the repository at this point in the history
this change addresses the formatting with fmtlib v10, which stopped
creating the default-generated formatter even if FMT_DEPRECATED_OSTREAM
preprocess macro is defined. so to enable Seastar applications to
print bool_class<Tag> using {fmt} without defining the formatter
by themselves, we provide the formatter for this class in Seastar.

the operator<< for bool_class<Tag> is preserved for backward
compatibility. please note, {fmt} format a `bool` variable as "true" or
"false" based on its value, so we don't have to put "true" or "false"
explicitly as we do in the implementation of operator<<.

Refs scylladb/scylladb#13245

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>

Closes #2144
  • Loading branch information
tchaikov authored and nyh committed Mar 18, 2024
1 parent a8aa79b commit c91f3a9
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/seastar/util/bool_class.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#pragma once

#include <ostream>
#include <fmt/core.h>

namespace seastar {

Expand Down Expand Up @@ -101,3 +102,12 @@ const bool_class<Tag> bool_class<Tag>::no { false };
/// @}

}

template<typename Tag>
struct fmt::formatter<seastar::bool_class<Tag>> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(seastar::bool_class<Tag> v, FormatContext& ctx) const{
return fmt::format_to(ctx.out(), "{}", bool(v));
}
};

0 comments on commit c91f3a9

Please sign in to comment.