Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F75815323
lm_type_caster.hh
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Aug 4, 11:15
Size
1 KB
Mime Type
text/x-c++
Expires
Tue, Aug 6, 11:15 (2 d)
Engine
blob
Format
Raw Data
Handle
19614682
Attached To
rLIBMULTISCALE LibMultiScale
lm_type_caster.hh
View Options
#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
Log In to Comment