Skip to content

Commit

Permalink
Add nb::is_flag() annotation to Counter::Flags (#1870)
Browse files Browse the repository at this point in the history
This saves us the definition of `__or__`, because we can just use the
one from `enum.IntFlag`.
  • Loading branch information
nicholasjng authored Oct 28, 2024
1 parent 4e3f2d8 commit d99cdd7
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions bindings/python/google_benchmark/benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ NB_MODULE(_benchmark, m) {
using benchmark::Counter;
nb::class_<Counter> py_counter(m, "Counter");

nb::enum_<Counter::Flags>(py_counter, "Flags", nb::is_arithmetic())
nb::enum_<Counter::Flags>(py_counter, "Flags", nb::is_arithmetic(), nb::is_flag())
.value("kDefaults", Counter::Flags::kDefaults)
.value("kIsRate", Counter::Flags::kIsRate)
.value("kAvgThreads", Counter::Flags::kAvgThreads)
Expand All @@ -129,24 +129,17 @@ NB_MODULE(_benchmark, m) {
.value("kAvgIterations", Counter::Flags::kAvgIterations)
.value("kAvgIterationsRate", Counter::Flags::kAvgIterationsRate)
.value("kInvert", Counter::Flags::kInvert)
.export_values()
.def("__or__", [](Counter::Flags a, Counter::Flags b) {
return static_cast<int>(a) | static_cast<int>(b);
});
.export_values();

nb::enum_<Counter::OneK>(py_counter, "OneK")
.value("kIs1000", Counter::OneK::kIs1000)
.value("kIs1024", Counter::OneK::kIs1024)
.export_values();

py_counter
.def(
"__init__",
[](Counter* c, double value, int flags, Counter::OneK oneK) {
new (c) Counter(value, static_cast<Counter::Flags>(flags), oneK);
},
nb::arg("value") = 0., nb::arg("flags") = Counter::kDefaults,
nb::arg("k") = Counter::kIs1000)
.def(nb::init<double, Counter::Flags, Counter::OneK>(),
nb::arg("value") = 0., nb::arg("flags") = Counter::kDefaults,
nb::arg("k") = Counter::kIs1000)
.def("__init__",
([](Counter* c, double value) { new (c) Counter(value); }))
.def_rw("value", &Counter::value)
Expand Down

0 comments on commit d99cdd7

Please sign in to comment.