diff --git a/python/src/typecaster.h b/python/src/typecaster.h index 6412afa72..d1a8a699c 100644 --- a/python/src/typecaster.h +++ b/python/src/typecaster.h @@ -1,11 +1,11 @@ // SPDX-License-Identifier: LGPL-3.0-or-other // Copyright (C) 2021 Contributors to the SLS Detector Package #pragma once -#include #include +#include -#include "sls/Result.h" #include "DurationWrapper.h" +#include "sls/Result.h" namespace py = pybind11; namespace pybind11 { @@ -14,84 +14,88 @@ template struct type_caster> : list_caster, Type> {}; - // Based on the typecaster in pybind11/chrono.h template <> struct type_caster { - public: - PYBIND11_TYPE_CASTER(std::chrono::nanoseconds, const_name("DurationWrapper")); + public: + PYBIND11_TYPE_CASTER(std::chrono::nanoseconds, + const_name("DurationWrapper")); - // signed 25 bits required by the standard. - using days = std::chrono::duration>; + // signed 25 bits required by the standard. + using days = std::chrono::duration>; - /** - * Conversion part 1 (Python->C++): convert a PyObject into std::chrono::nanoseconds - * try datetime.timedelta, floats and our DurationWrapper wrapper - */ + /** + * Conversion part 1 (Python->C++): convert a PyObject into + * std::chrono::nanoseconds try datetime.timedelta, floats and our + * DurationWrapper wrapper + */ - bool load(handle src, bool) { - using namespace std::chrono; + bool load(handle src, bool) { + using namespace std::chrono; - // Lazy initialise the PyDateTime import - if (!PyDateTimeAPI) { - PyDateTime_IMPORT; - } + // Lazy initialise the PyDateTime import + if (!PyDateTimeAPI) { + PyDateTime_IMPORT; + } - if (!src) { - return false; - } - // If invoked with datetime.delta object, same as in chrono.h - if (PyDelta_Check(src.ptr())) { - value = duration_cast( - days(PyDateTime_DELTA_GET_DAYS(src.ptr())) + - seconds(PyDateTime_DELTA_GET_SECONDS(src.ptr())) + - microseconds(PyDateTime_DELTA_GET_MICROSECONDS(src.ptr())) - - ); - return true; - } - // If invoked with a float we assume it is seconds and convert, same as in chrono.h - if (PyFloat_Check(src.ptr())) { - value = duration_cast(duration(PyFloat_AsDouble(src.ptr()))); - return true; - } - // If invoked with an int we assume it is nanoseconds and convert, same as in chrono.h - if (PyLong_Check(src.ptr())) { - value = duration_cast(duration(PyLong_AsLongLong(src.ptr()))); - return true; - } - - - // Lastly if we were actually called with a DurationWrapper object we get - // the number of nanoseconds and create a std::chrono::nanoseconds from it - py::object py_cls = py::module::import("slsdet._slsdet").attr("DurationWrapper"); - if (py::isinstance(src, py_cls)){ - sls::DurationWrapper *cls = src.cast(); - value = nanoseconds(cls->count()); - return true; - } - - + if (!src) { return false; + } + // If invoked with datetime.delta object, same as in chrono.h + if (PyDelta_Check(src.ptr())) { + value = duration_cast( + days(PyDateTime_DELTA_GET_DAYS(src.ptr())) + + seconds(PyDateTime_DELTA_GET_SECONDS(src.ptr())) + + microseconds(PyDateTime_DELTA_GET_MICROSECONDS(src.ptr())) + ); + return true; + } + // If invoked with a float we assume it is seconds and convert, same as + // in chrono.h + if (PyFloat_Check(src.ptr())) { + value = duration_cast( + duration(PyFloat_AsDouble(src.ptr()))); + return true; + } + // If invoked with an int we assume it is nanoseconds and convert, same + // as in chrono.h + if (PyLong_Check(src.ptr())) { + value = duration_cast( + duration(PyLong_AsLongLong(src.ptr()))); + return true; } - /** - * Conversion part 2 (C++ -> Python) - * import the module to get a handle to the wrapped class - * Default construct an object of (wrapped) DurationWrapper - * set the count from chrono::nanoseconds and return - */ - static handle cast(std::chrono::nanoseconds src, return_value_policy /* policy */, handle /* parent */) { - py::object py_cls = py::module::import("slsdet._slsdet").attr("DurationWrapper"); - py::object* obj = new py::object; - *obj = py_cls(); - sls::DurationWrapper *dur = obj->cast(); - dur->set_count(src.count()); - return *obj; + // Lastly if we were actually called with a DurationWrapper object we + // get the number of nanoseconds and create a std::chrono::nanoseconds + // from it + py::object py_cls = + py::module::import("slsdet._slsdet").attr("DurationWrapper"); + if (py::isinstance(src, py_cls)) { + sls::DurationWrapper *cls = src.cast(); + value = nanoseconds(cls->count()); + return true; } - }; + return false; + } + /** + * Conversion part 2 (C++ -> Python) + * import the module to get a handle to the wrapped class + * Default construct an object of (wrapped) DurationWrapper + * set the count from chrono::nanoseconds and return + */ + static handle cast(std::chrono::nanoseconds src, + return_value_policy /* policy */, handle /* parent */) { + py::object py_cls = + py::module::import("slsdet._slsdet").attr("DurationWrapper"); + py::object *obj = new py::object; + *obj = py_cls(); + sls::DurationWrapper *dur = obj->cast(); + dur->set_count(src.count()); + return *obj; + } +}; } // namespace detail } // namespace pybind11 \ No newline at end of file