Page MenuHomec4science

lm_type_caster.hh
No OneTemporary

File Metadata

Created
Mon, Jul 22, 15:54

lm_type_caster.hh

#ifndef __LIBMULTISCALE_TYPE_CASTER_HH__
#define __LIBMULTISCALE_TYPE_CASTER_HH__
/* -------------------------------------------------------------------------- */
#include "quantity.hh"
#include <map>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <vector>
/* -------------------------------------------------------------------------- */
namespace pybind11 {
namespace detail {
template <libmultiscale::UInt Dim, typename T, typename Cast>
std::enable_if_t<Dim == 1, bool> vector_load(Cast &value, PyObject *source) {
value = cast<T>(source);
return !PyErr_Occurred();
}
template <libmultiscale::UInt Dim, typename T, typename Cast>
std::enable_if_t<Dim != 1, bool> vector_load(Cast &value, PyObject *source) {
auto tmp = cast<list>(source);
for (unsigned int i = 0; i < tmp.size(); ++i) {
value[i] = cast<T>(tmp[i]);
}
return !PyErr_Occurred();
}
template <libmultiscale::PhysicalQuantity q, libmultiscale::UInt Dim,
typename T>
struct type_caster<libmultiscale::Quantity<q, Dim, T>> {
using type = libmultiscale::Quantity<q, Dim, T>;
PYBIND11_TYPE_CASTER(type, _("quantity"));
bool load(handle src, bool) {
return vector_load<Dim, T>(this->value, src.ptr());
}
static handle cast(type src, return_value_policy, handle) {
return PyFloat_FromDouble(src);
}
};
template <libmultiscale::UInt Dim, typename T>
struct type_caster<libmultiscale::Vector<Dim, T>> {
using type = libmultiscale::Vector<Dim, T>;
PYBIND11_TYPE_CASTER(type, _("vector"));
bool load(handle src, bool) {
return vector_load<Dim, T>(this->value, src.ptr());
}
static handle cast(type src, return_value_policy, handle) {
return PyFloat_FromDouble(src);
}
};
} // namespace detail
} // namespace pybind11
/* -------------------------------------------------------------------------- */
#endif /* __LIBMULTISCALE_TYPE_CASTER_HH__ */

Event Timeline