From d99cdd7356de97b3056684d6b511189778d8a247 Mon Sep 17 00:00:00 2001 From: Nicholas Junge Date: Mon, 28 Oct 2024 19:18:40 +0100 Subject: [PATCH] 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`. --- bindings/python/google_benchmark/benchmark.cc | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/bindings/python/google_benchmark/benchmark.cc b/bindings/python/google_benchmark/benchmark.cc index 64ffb92b..a9358225 100644 --- a/bindings/python/google_benchmark/benchmark.cc +++ b/bindings/python/google_benchmark/benchmark.cc @@ -118,7 +118,7 @@ NB_MODULE(_benchmark, m) { using benchmark::Counter; nb::class_ py_counter(m, "Counter"); - nb::enum_(py_counter, "Flags", nb::is_arithmetic()) + nb::enum_(py_counter, "Flags", nb::is_arithmetic(), nb::is_flag()) .value("kDefaults", Counter::Flags::kDefaults) .value("kIsRate", Counter::Flags::kIsRate) .value("kAvgThreads", Counter::Flags::kAvgThreads) @@ -129,10 +129,7 @@ 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(a) | static_cast(b); - }); + .export_values(); nb::enum_(py_counter, "OneK") .value("kIs1000", Counter::OneK::kIs1000) @@ -140,13 +137,9 @@ NB_MODULE(_benchmark, m) { .export_values(); py_counter - .def( - "__init__", - [](Counter* c, double value, int flags, Counter::OneK oneK) { - new (c) Counter(value, static_cast(flags), oneK); - }, - nb::arg("value") = 0., nb::arg("flags") = Counter::kDefaults, - nb::arg("k") = Counter::kIs1000) + .def(nb::init(), + 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)