1/*
2    tests/test_operator_overloading.cpp -- operator overloading
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#include "pybind11_tests.h"
11#include "constructor_stats.h"
12#include <pybind11/operators.h>
13#include <functional>
14
15class Vector2 {
16public:
17    Vector2(float x, float y) : x(x), y(y) { print_created(this, toString()); }
18    Vector2(const Vector2 &v) : x(v.x), y(v.y) { print_copy_created(this); }
19    Vector2(Vector2 &&v) : x(v.x), y(v.y) { print_move_created(this); v.x = v.y = 0; }
20    Vector2 &operator=(const Vector2 &v) { x = v.x; y = v.y; print_copy_assigned(this); return *this; }
21    Vector2 &operator=(Vector2 &&v) { x = v.x; y = v.y; v.x = v.y = 0; print_move_assigned(this); return *this; }
22    ~Vector2() { print_destroyed(this); }
23
24    std::string toString() const { return "[" + std::to_string(x) + ", " + std::to_string(y) + "]"; }
25
26    Vector2 operator-() const { return Vector2(-x, -y); }
27    Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); }
28    Vector2 operator-(const Vector2 &v) const { return Vector2(x - v.x, y - v.y); }
29    Vector2 operator-(float value) const { return Vector2(x - value, y - value); }
30    Vector2 operator+(float value) const { return Vector2(x + value, y + value); }
31    Vector2 operator*(float value) const { return Vector2(x * value, y * value); }
32    Vector2 operator/(float value) const { return Vector2(x / value, y / value); }
33    Vector2 operator*(const Vector2 &v) const { return Vector2(x * v.x, y * v.y); }
34    Vector2 operator/(const Vector2 &v) const { return Vector2(x / v.x, y / v.y); }
35    Vector2& operator+=(const Vector2 &v) { x += v.x; y += v.y; return *this; }
36    Vector2& operator-=(const Vector2 &v) { x -= v.x; y -= v.y; return *this; }
37    Vector2& operator*=(float v) { x *= v; y *= v; return *this; }
38    Vector2& operator/=(float v) { x /= v; y /= v; return *this; }
39    Vector2& operator*=(const Vector2 &v) { x *= v.x; y *= v.y; return *this; }
40    Vector2& operator/=(const Vector2 &v) { x /= v.x; y /= v.y; return *this; }
41
42    friend Vector2 operator+(float f, const Vector2 &v) { return Vector2(f + v.x, f + v.y); }
43    friend Vector2 operator-(float f, const Vector2 &v) { return Vector2(f - v.x, f - v.y); }
44    friend Vector2 operator*(float f, const Vector2 &v) { return Vector2(f * v.x, f * v.y); }
45    friend Vector2 operator/(float f, const Vector2 &v) { return Vector2(f / v.x, f / v.y); }
46private:
47    float x, y;
48};
49
50class C1 { };
51class C2 { };
52
53int operator+(const C1 &, const C1 &) { return 11; }
54int operator+(const C2 &, const C2 &) { return 22; }
55int operator+(const C2 &, const C1 &) { return 21; }
56int operator+(const C1 &, const C2 &) { return 12; }
57
58namespace std {
59    template<>
60    struct hash<Vector2> {
61        // Not a good hash function, but easy to test
62        size_t operator()(const Vector2 &) { return 4; }
63    };
64}
65
66// MSVC warns about unknown pragmas, and warnings are errors.
67#ifndef _MSC_VER
68  #pragma GCC diagnostic push
69  // clang 7.0.0 and Apple LLVM 10.0.1 introduce `-Wself-assign-overloaded` to
70  // `-Wall`, which is used here for overloading (e.g. `py::self += py::self `).
71  // Here, we suppress the warning using `#pragma diagnostic`.
72  // Taken from: https://github.com/RobotLocomotion/drake/commit/aaf84b46
73  // TODO(eric): This could be resolved using a function / functor (e.g. `py::self()`).
74  #if (__APPLE__) && (__clang__)
75    #if (__clang_major__ >= 10) && (__clang_minor__ >= 0) && (__clang_patchlevel__ >= 1)
76      #pragma GCC diagnostic ignored "-Wself-assign-overloaded"
77    #endif
78  #elif (__clang__)
79    #if (__clang_major__ >= 7)
80      #pragma GCC diagnostic ignored "-Wself-assign-overloaded"
81    #endif
82  #endif
83#endif
84
85TEST_SUBMODULE(operators, m) {
86
87    // test_operator_overloading
88    py::class_<Vector2>(m, "Vector2")
89        .def(py::init<float, float>())
90        .def(py::self + py::self)
91        .def(py::self + float())
92        .def(py::self - py::self)
93        .def(py::self - float())
94        .def(py::self * float())
95        .def(py::self / float())
96        .def(py::self * py::self)
97        .def(py::self / py::self)
98        .def(py::self += py::self)
99        .def(py::self -= py::self)
100        .def(py::self *= float())
101        .def(py::self /= float())
102        .def(py::self *= py::self)
103        .def(py::self /= py::self)
104        .def(float() + py::self)
105        .def(float() - py::self)
106        .def(float() * py::self)
107        .def(float() / py::self)
108        .def(-py::self)
109        .def("__str__", &Vector2::toString)
110        .def(hash(py::self))
111        ;
112
113    m.attr("Vector") = m.attr("Vector2");
114
115    // test_operators_notimplemented
116    // #393: need to return NotSupported to ensure correct arithmetic operator behavior
117    py::class_<C1>(m, "C1")
118        .def(py::init<>())
119        .def(py::self + py::self);
120
121    py::class_<C2>(m, "C2")
122        .def(py::init<>())
123        .def(py::self + py::self)
124        .def("__add__", [](const C2& c2, const C1& c1) { return c2 + c1; })
125        .def("__radd__", [](const C2& c2, const C1& c1) { return c1 + c2; });
126
127    // test_nested
128    // #328: first member in a class can't be used in operators
129    struct NestABase { int value = -2; };
130    py::class_<NestABase>(m, "NestABase")
131        .def(py::init<>())
132        .def_readwrite("value", &NestABase::value);
133
134    struct NestA : NestABase {
135        int value = 3;
136        NestA& operator+=(int i) { value += i; return *this; }
137    };
138    py::class_<NestA>(m, "NestA")
139        .def(py::init<>())
140        .def(py::self += int())
141        .def("as_base", [](NestA &a) -> NestABase& {
142            return (NestABase&) a;
143        }, py::return_value_policy::reference_internal);
144    m.def("get_NestA", [](const NestA &a) { return a.value; });
145
146    struct NestB {
147        NestA a;
148        int value = 4;
149        NestB& operator-=(int i) { value -= i; return *this; }
150    };
151    py::class_<NestB>(m, "NestB")
152        .def(py::init<>())
153        .def(py::self -= int())
154        .def_readwrite("a", &NestB::a);
155    m.def("get_NestB", [](const NestB &b) { return b.value; });
156
157    struct NestC {
158        NestB b;
159        int value = 5;
160        NestC& operator*=(int i) { value *= i; return *this; }
161    };
162    py::class_<NestC>(m, "NestC")
163        .def(py::init<>())
164        .def(py::self *= int())
165        .def_readwrite("b", &NestC::b);
166    m.def("get_NestC", [](const NestC &c) { return c.value; });
167}
168
169#ifndef _MSC_VER
170  #pragma GCC diagnostic pop
171#endif
172