test_operator_overloading.cpp revision 12391:ceeca8b41e4b
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 Vector2 &v) const { return Vector2(x + v.x, y + v.y); }
27    Vector2 operator-(const Vector2 &v) const { return Vector2(x - v.x, y - v.y); }
28    Vector2 operator-(float value) const { return Vector2(x - value, y - value); }
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*(const Vector2 &v) const { return Vector2(x * v.x, y * v.y); }
33    Vector2 operator/(const Vector2 &v) const { return Vector2(x / v.x, y / v.y); }
34    Vector2& operator+=(const Vector2 &v) { x += v.x; y += v.y; return *this; }
35    Vector2& operator-=(const Vector2 &v) { x -= v.x; y -= v.y; return *this; }
36    Vector2& operator*=(float v) { x *= v; y *= v; return *this; }
37    Vector2& operator/=(float v) { x /= v; y /= v; return *this; }
38    Vector2& operator*=(const Vector2 &v) { x *= v.x; y *= v.y; return *this; }
39    Vector2& operator/=(const Vector2 &v) { x /= v.x; y /= v.y; return *this; }
40
41    friend Vector2 operator+(float f, const Vector2 &v) { return Vector2(f + v.x, f + v.y); }
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); }
45private:
46    float x, y;
47};
48
49class C1 { };
50class C2 { };
51
52int operator+(const C1 &, const C1 &) { return 11; }
53int operator+(const C2 &, const C2 &) { return 22; }
54int operator+(const C2 &, const C1 &) { return 21; }
55int operator+(const C1 &, const C2 &) { return 12; }
56
57namespace std {
58    template<>
59    struct hash<Vector2> {
60        // Not a good hash function, but easy to test
61        size_t operator()(const Vector2 &) { return 4; }
62    };
63}
64
65TEST_SUBMODULE(operators, m) {
66
67    // test_operator_overloading
68    py::class_<Vector2>(m, "Vector2")
69        .def(py::init<float, float>())
70        .def(py::self + py::self)
71        .def(py::self + float())
72        .def(py::self - py::self)
73        .def(py::self - float())
74        .def(py::self * float())
75        .def(py::self / float())
76        .def(py::self * py::self)
77        .def(py::self / py::self)
78        .def(py::self += py::self)
79        .def(py::self -= py::self)
80        .def(py::self *= float())
81        .def(py::self /= float())
82        .def(py::self *= py::self)
83        .def(py::self /= py::self)
84        .def(float() + py::self)
85        .def(float() - py::self)
86        .def(float() * py::self)
87        .def(float() / py::self)
88        .def("__str__", &Vector2::toString)
89        .def(hash(py::self))
90        ;
91
92    m.attr("Vector") = m.attr("Vector2");
93
94    // test_operators_notimplemented
95    // #393: need to return NotSupported to ensure correct arithmetic operator behavior
96    py::class_<C1>(m, "C1")
97        .def(py::init<>())
98        .def(py::self + py::self);
99
100    py::class_<C2>(m, "C2")
101        .def(py::init<>())
102        .def(py::self + py::self)
103        .def("__add__", [](const C2& c2, const C1& c1) { return c2 + c1; })
104        .def("__radd__", [](const C2& c2, const C1& c1) { return c1 + c2; });
105
106    // test_nested
107    // #328: first member in a class can't be used in operators
108    struct NestABase { int value = -2; };
109    py::class_<NestABase>(m, "NestABase")
110        .def(py::init<>())
111        .def_readwrite("value", &NestABase::value);
112
113    struct NestA : NestABase {
114        int value = 3;
115        NestA& operator+=(int i) { value += i; return *this; }
116    };
117    py::class_<NestA>(m, "NestA")
118        .def(py::init<>())
119        .def(py::self += int())
120        .def("as_base", [](NestA &a) -> NestABase& {
121            return (NestABase&) a;
122        }, py::return_value_policy::reference_internal);
123    m.def("get_NestA", [](const NestA &a) { return a.value; });
124
125    struct NestB {
126        NestA a;
127        int value = 4;
128        NestB& operator-=(int i) { value -= i; return *this; }
129    };
130    py::class_<NestB>(m, "NestB")
131        .def(py::init<>())
132        .def(py::self -= int())
133        .def_readwrite("a", &NestB::a);
134    m.def("get_NestB", [](const NestB &b) { return b.value; });
135
136    struct NestC {
137        NestB b;
138        int value = 5;
139        NestC& operator*=(int i) { value *= i; return *this; }
140    };
141    py::class_<NestC>(m, "NestC")
142        .def(py::init<>())
143        .def(py::self *= int())
144        .def_readwrite("b", &NestC::b);
145    m.def("get_NestC", [](const NestC &c) { return c.value; });
146}
147