diff options
Diffstat (limited to 'ext/pybind11/include/pybind11/complex.h')
-rw-r--r-- | ext/pybind11/include/pybind11/complex.h | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/ext/pybind11/include/pybind11/complex.h b/ext/pybind11/include/pybind11/complex.h new file mode 100644 index 000000000..f767f354c --- /dev/null +++ b/ext/pybind11/include/pybind11/complex.h @@ -0,0 +1,47 @@ +/* + pybind11/complex.h: Complex number support + + Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#pragma once + +#include "pybind11.h" +#include <complex> + +/// glibc defines I as a macro which breaks things, e.g., boost template names +#ifdef I +# undef I +#endif + +NAMESPACE_BEGIN(pybind11) + +PYBIND11_DECL_FMT(std::complex<float>, "Zf"); +PYBIND11_DECL_FMT(std::complex<double>, "Zd"); + +NAMESPACE_BEGIN(detail) +template <typename T> class type_caster<std::complex<T>> { +public: + bool load(handle src, bool) { + if (!src) + return false; + Py_complex result = PyComplex_AsCComplex(src.ptr()); + if (result.real == -1.0 && PyErr_Occurred()) { + PyErr_Clear(); + return false; + } + value = std::complex<T>((T) result.real, (T) result.imag); + return true; + } + + static handle cast(const std::complex<T> &src, return_value_policy /* policy */, handle /* parent */) { + return PyComplex_FromDoubles((double) src.real(), (double) src.imag()); + } + + PYBIND11_TYPE_CASTER(std::complex<T>, _("complex")); +}; +NAMESPACE_END(detail) +NAMESPACE_END(pybind11) |