Skip to content

Commit eeeb012

Browse files
committed
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 <[email protected]>
1 parent 3ed23ca commit eeeb012

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/common/bitset_set.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
#pragma once
1616
#include <cstdint>
17+
#include <fmt/ranges.h>
1718

19+
#include "common/fmt_common.h"
1820
#include "include/buffer.h"
1921

2022
template<typename KeyT, typename IntT>
@@ -409,6 +411,20 @@ class bitset_set {
409411
return lhs;
410412
}
411413

414+
std::string fmt_print() const
415+
requires has_formatter<KeyT> {
416+
std::string s = "{";
417+
int c = (int)size();
418+
for (auto k : *this) {
419+
s += fmt::format("{}", k);
420+
if (--c > 0) {
421+
s += ",";
422+
}
423+
}
424+
s += "}";
425+
return s;
426+
}
427+
412428
/** returns a bitset_set with the elements from lhs which are not found in rhs
413429
*
414430
* Useful to replace calls to std::difference which looked at the complete
@@ -447,3 +463,8 @@ class bitset_set {
447463
return std::strong_ordering::equal;
448464
}
449465
};
466+
467+
// make sure fmt::range would not try (and fail) to treat bitset_set as a range
468+
template<size_t NumBitsV, typename KeyT>
469+
struct fmt::is_range<bitset_set<NumBitsV, KeyT>, char> : std::false_type {};
470+

src/common/mini_flat_map.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33

44
#pragma once
55

6-
#include <common/bitset_set.h>
7-
#include <include/ceph_assert.h>
86
#include <cstddef>
7+
#include <fmt/ranges.h>
98
#include <memory>
109
#include <vector>
1110

11+
#include "common/bitset_set.h"
12+
#include "common/fmt_common.h"
13+
#include "include/ceph_assert.h"
14+
1215

1316
/* This class struct provides an API similar to std::map, but with the
1417
* restriction that "Key" must cast to/from IntT without ambiguity. For
@@ -490,4 +493,23 @@ class mini_flat_map {
490493
lhs << "}";
491494
return lhs;
492495
}
496+
497+
std::string fmt_print() const
498+
requires has_formatter<KeyT> && has_formatter<ValueT> {
499+
int c = (int)_size;
500+
std::string s = "{";
501+
for (auto&& [k, v] : *this) {
502+
s += fmt::format("{}:{}", k, v);
503+
if (--c > 0) {
504+
s += ",";
505+
}
506+
}
507+
s += "}";
508+
return s;
509+
}
493510
};
511+
512+
// make sure fmt::range does not apply to mini_flat_map
513+
template<typename KeyT, typename ValueT>
514+
struct fmt::is_range<mini_flat_map<KeyT, ValueT>, char> : std::false_type {};
515+

0 commit comments

Comments
 (0)