/* pybind11/complex.h: Complex number support Copyright (c) 2016 Wenzel Jakob 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 /// glibc defines I as a macro which breaks things, e.g., boost template names #ifdef I # undef I #endif NAMESPACE_BEGIN(PYBIND11_NAMESPACE) template struct format_descriptor, detail::enable_if_t::value>> { static constexpr const char c = format_descriptor::c; static constexpr const char value[3] = { 'Z', c, '\0' }; static std::string format() { return std::string(value); } }; #ifndef PYBIND11_CPP17 template constexpr const char format_descriptor< std::complex, detail::enable_if_t::value>>::value[3]; #endif NAMESPACE_BEGIN(detail) template struct is_fmt_numeric, detail::enable_if_t::value>> { static constexpr bool value = true; static constexpr int index = is_fmt_numeric::index + 3; }; template class type_caster> { public: bool load(handle src, bool convert) { if (!src) return false; if (!convert && !PyComplex_Check(src.ptr())) return false; Py_complex result = PyComplex_AsCComplex(src.ptr()); if (result.real == -1.0 && PyErr_Occurred()) { PyErr_Clear(); return false; } value = std::complex((T) result.real, (T) result.imag); return true; } static handle cast(const std::complex &src, return_value_policy /* policy */, handle /* parent */) { return PyComplex_FromDoubles((double) src.real(), (double) src.imag()); } PYBIND11_TYPE_CASTER(std::complex, _("complex")); }; NAMESPACE_END(detail) NAMESPACE_END(PYBIND11_NAMESPACE)