complex.h revision 12391:ceeca8b41e4b
1/*
2    pybind11/complex.h: Complex number support
3
4    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5
6    All rights reserved. Use of this source code is governed by a
7    BSD-style license that can be found in the LICENSE file.
8*/
9
10#pragma once
11
12#include "pybind11.h"
13#include <complex>
14
15/// glibc defines I as a macro which breaks things, e.g., boost template names
16#ifdef I
17#  undef I
18#endif
19
20NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
21
22template <typename T> struct format_descriptor<std::complex<T>, detail::enable_if_t<std::is_floating_point<T>::value>> {
23    static constexpr const char c = format_descriptor<T>::c;
24    static constexpr const char value[3] = { 'Z', c, '\0' };
25    static std::string format() { return std::string(value); }
26};
27
28template <typename T> constexpr const char format_descriptor<
29    std::complex<T>, detail::enable_if_t<std::is_floating_point<T>::value>>::value[3];
30
31NAMESPACE_BEGIN(detail)
32
33template <typename T> struct is_fmt_numeric<std::complex<T>, detail::enable_if_t<std::is_floating_point<T>::value>> {
34    static constexpr bool value = true;
35    static constexpr int index = is_fmt_numeric<T>::index + 3;
36};
37
38template <typename T> class type_caster<std::complex<T>> {
39public:
40    bool load(handle src, bool convert) {
41        if (!src)
42            return false;
43        if (!convert && !PyComplex_Check(src.ptr()))
44            return false;
45        Py_complex result = PyComplex_AsCComplex(src.ptr());
46        if (result.real == -1.0 && PyErr_Occurred()) {
47            PyErr_Clear();
48            return false;
49        }
50        value = std::complex<T>((T) result.real, (T) result.imag);
51        return true;
52    }
53
54    static handle cast(const std::complex<T> &src, return_value_policy /* policy */, handle /* parent */) {
55        return PyComplex_FromDoubles((double) src.real(), (double) src.imag());
56    }
57
58    PYBIND11_TYPE_CASTER(std::complex<T>, _("complex"));
59};
60NAMESPACE_END(detail)
61NAMESPACE_END(PYBIND11_NAMESPACE)
62