LSSTApplications
20.0.0
LSSTDataManagementBasePackage
|
Go to the documentation of this file.
24 #ifndef LSST_UTILS_PYTHON_TEMPLATEINVOKER_H
25 #define LSST_UTILS_PYTHON_TEMPLATEINVOKER_H
27 #include "pybind11/pybind11.h"
28 #include "pybind11/numpy.h"
32 namespace lsst {
namespace utils {
namespace python {
110 template <
typename ...Types>
118 PyErr_Format(PyExc_TypeError,
"dtype '%R' not supported.", dtype.ptr());
119 throw pybind11::error_already_set();
155 template <
typename Function,
typename ...TypesToTry>
158 pybind11::dtype
const & dtype,
161 return _apply(
function, dtype, typesToTry);
166 template <
typename Function>
167 pybind11::object _apply(Function &
function, pybind11::dtype
const & dtype, Tag<>)
const {
168 return _onError(dtype);
171 template <
typename Function,
typename T,
typename ...A>
172 pybind11::object _apply(Function &
function, pybind11::dtype
const & dtype, Tag<T, A...>)
const {
173 if (pybind11::detail::npy_api::get().PyArray_EquivTypes_(dtype.ptr(),
174 pybind11::dtype::of<T>().ptr())) {
175 return pybind11::cast(
function(
static_cast<T
>(0)));
177 return _apply(
function, dtype, Tag<A...>());
A simple tag type used to pass one or more types as a function argument.
pybind11::object apply(Function function, pybind11::dtype const &dtype, Tag< TypesToTry... > typesToTry) const
Call and return function(static_cast<T>(0)) with the type T that matches a given NumPy dtype object.
A helper class for wrapping C++ template functions as Python functions with dtype arguments.
static pybind11::object handleErrorDefault(pybind11::dtype const &dtype)
Callback used for handling unmatched-type errors by default.
A base class for image defects.
TemplateInvoker()
Construct a TemplateInvoker that calls handleErrorDefault when no match is found.
std::function< pybind11::object(pybind11::dtype const &dtype)> OnErrorCallback
Callback type for handling unmatched-type errors.
TemplateInvoker(OnErrorCallback onError)
Construct a TemplateInvoker that calls the given object when no match is found.