aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/tests/libsample/stdcomplex.h
blob: b39b80612bcd994a7c6e44b03ca051e70771a9eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#ifndef STDCOMPLEX_H
#define STDCOMPLEX_H

#include "libsamplemacros.h"

#include <complex>
#include <iosfwd>

// A complex number based on std::complex for exercising esoteric number
// protocols (Py_nb_). For standard number protocols, see Point.

class LIBSAMPLE_API StdComplex
{
    using Impl = std::complex<double>;

public:
    StdComplex() noexcept;
    explicit StdComplex(double re, double img) noexcept;

    double real() const { return m_impl.real(); }
    double imag() const { return m_impl.imag(); }

    double abs_value() const { return std::abs(m_impl); } // abs() is reserved Python word

    StdComplex pow(const StdComplex &exp) const;

    operator double() const { return abs_value(); }
    operator int() const;

    friend inline bool operator==(const StdComplex &c1, const StdComplex &c2) noexcept
    { return c1.m_impl == c2.m_impl; }
    friend inline bool operator!=(const StdComplex &c1, const StdComplex &c2) noexcept
    { return c1.m_impl != c2.m_impl; }

    friend inline StdComplex operator+(const StdComplex &c1, const StdComplex &c2) noexcept
    { return StdComplex(c1.m_impl + c2.m_impl); }
    friend inline StdComplex operator-(const StdComplex &c1, const StdComplex &c2) noexcept
    { return StdComplex(c1.m_impl - c2.m_impl); }
    friend inline StdComplex operator*(const StdComplex &c1, const StdComplex &c2) noexcept
    { return StdComplex(c1.m_impl * c2.m_impl); }
    friend inline StdComplex operator/(const StdComplex &c1, const StdComplex &c2) noexcept
    { return StdComplex(c1.m_impl / c2.m_impl); }

private:
    explicit StdComplex(const Impl &impl) noexcept;

    Impl m_impl;
};

std::ostream &operator<<(std::ostream &str, const StdComplex &c);

#endif // STDCOMPLEX_H