From: Ronen Friedman Date: Mon, 10 Mar 2025 17:12:10 +0000 (-0500) Subject: common: fmt support for bitset_set & mini_flat_map X-Git-Tag: v20.3.0~388^2~1 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=eeeb0127ae7b437ad688df4b1e7b865aec3f1ff2;p=ceph.git common: fmt support for bitset_set & mini_flat_map Note: both classes have begin()/end() methods, which required an explicit opt-out of fmt/range default handling. Signed-off-by: Ronen Friedman --- diff --git a/src/common/bitset_set.h b/src/common/bitset_set.h index 0b19b0041e01d..f1411d0cdb909 100644 --- a/src/common/bitset_set.h +++ b/src/common/bitset_set.h @@ -14,7 +14,9 @@ #pragma once #include +#include +#include "common/fmt_common.h" #include "include/buffer.h" template @@ -409,6 +411,20 @@ class bitset_set { return lhs; } + std::string fmt_print() const + requires has_formatter { + std::string s = "{"; + int c = (int)size(); + for (auto k : *this) { + s += fmt::format("{}", k); + if (--c > 0) { + s += ","; + } + } + s += "}"; + return s; + } + /** returns a bitset_set with the elements from lhs which are not found in rhs * * Useful to replace calls to std::difference which looked at the complete @@ -447,3 +463,8 @@ class bitset_set { return std::strong_ordering::equal; } }; + +// make sure fmt::range would not try (and fail) to treat bitset_set as a range +template +struct fmt::is_range, char> : std::false_type {}; + diff --git a/src/common/mini_flat_map.h b/src/common/mini_flat_map.h index 931326f014821..5e880f50d70f2 100644 --- a/src/common/mini_flat_map.h +++ b/src/common/mini_flat_map.h @@ -3,12 +3,15 @@ #pragma once -#include -#include #include +#include #include #include +#include "common/bitset_set.h" +#include "common/fmt_common.h" +#include "include/ceph_assert.h" + /* This class struct provides an API similar to std::map, but with the * restriction that "Key" must cast to/from IntT without ambiguity. For @@ -490,4 +493,23 @@ class mini_flat_map { lhs << "}"; return lhs; } + + std::string fmt_print() const + requires has_formatter && has_formatter { + int c = (int)_size; + std::string s = "{"; + for (auto&& [k, v] : *this) { + s += fmt::format("{}:{}", k, v); + if (--c > 0) { + s += ","; + } + } + s += "}"; + return s; + } }; + +// make sure fmt::range does not apply to mini_flat_map +template +struct fmt::is_range, char> : std::false_type {}; +