Skip to content

Commit d99cdd7

Browse files
authored
Add nb::is_flag() annotation to Counter::Flags (#1870)
This saves us the definition of `__or__`, because we can just use the one from `enum.IntFlag`.
1 parent 4e3f2d8 commit d99cdd7

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

bindings/python/google_benchmark/benchmark.cc

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ NB_MODULE(_benchmark, m) {
118118
using benchmark::Counter;
119119
nb::class_<Counter> py_counter(m, "Counter");
120120

121-
nb::enum_<Counter::Flags>(py_counter, "Flags", nb::is_arithmetic())
121+
nb::enum_<Counter::Flags>(py_counter, "Flags", nb::is_arithmetic(), nb::is_flag())
122122
.value("kDefaults", Counter::Flags::kDefaults)
123123
.value("kIsRate", Counter::Flags::kIsRate)
124124
.value("kAvgThreads", Counter::Flags::kAvgThreads)
@@ -129,24 +129,17 @@ NB_MODULE(_benchmark, m) {
129129
.value("kAvgIterations", Counter::Flags::kAvgIterations)
130130
.value("kAvgIterationsRate", Counter::Flags::kAvgIterationsRate)
131131
.value("kInvert", Counter::Flags::kInvert)
132-
.export_values()
133-
.def("__or__", [](Counter::Flags a, Counter::Flags b) {
134-
return static_cast<int>(a) | static_cast<int>(b);
135-
});
132+
.export_values();
136133

137134
nb::enum_<Counter::OneK>(py_counter, "OneK")
138135
.value("kIs1000", Counter::OneK::kIs1000)
139136
.value("kIs1024", Counter::OneK::kIs1024)
140137
.export_values();
141138

142139
py_counter
143-
.def(
144-
"__init__",
145-
[](Counter* c, double value, int flags, Counter::OneK oneK) {
146-
new (c) Counter(value, static_cast<Counter::Flags>(flags), oneK);
147-
},
148-
nb::arg("value") = 0., nb::arg("flags") = Counter::kDefaults,
149-
nb::arg("k") = Counter::kIs1000)
140+
.def(nb::init<double, Counter::Flags, Counter::OneK>(),
141+
nb::arg("value") = 0., nb::arg("flags") = Counter::kDefaults,
142+
nb::arg("k") = Counter::kIs1000)
150143
.def("__init__",
151144
([](Counter* c, double value) { new (c) Counter(value); }))
152145
.def_rw("value", &Counter::value)

0 commit comments

Comments
 (0)