test_methods_and_attributes.cpp revision 11986:c12e4625ab56
1/*
2    tests/test_methods_and_attributes.cpp -- constructors, deconstructors, attribute access,
3    __str__, argument and return value conventions
4
5    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
6
7    All rights reserved. Use of this source code is governed by a
8    BSD-style license that can be found in the LICENSE file.
9*/
10
11#include "pybind11_tests.h"
12#include "constructor_stats.h"
13
14class ExampleMandA {
15public:
16    ExampleMandA() { print_default_created(this); }
17    ExampleMandA(int value) : value(value) { print_created(this, value); }
18    ExampleMandA(const ExampleMandA &e) : value(e.value) { print_copy_created(this); }
19    ExampleMandA(ExampleMandA &&e) : value(e.value) { print_move_created(this); }
20    ~ExampleMandA() { print_destroyed(this); }
21
22    std::string toString() {
23        return "ExampleMandA[value=" + std::to_string(value) + "]";
24    }
25
26    void operator=(const ExampleMandA &e) { print_copy_assigned(this); value = e.value; }
27    void operator=(ExampleMandA &&e) { print_move_assigned(this); value = e.value; }
28
29    void add1(ExampleMandA other) { value += other.value; }           // passing by value
30    void add2(ExampleMandA &other) { value += other.value; }          // passing by reference
31    void add3(const ExampleMandA &other) { value += other.value; }    // passing by const reference
32    void add4(ExampleMandA *other) { value += other->value; }         // passing by pointer
33    void add5(const ExampleMandA *other) { value += other->value; }   // passing by const pointer
34
35    void add6(int other) { value += other; }                      // passing by value
36    void add7(int &other) { value += other; }                     // passing by reference
37    void add8(const int &other) { value += other; }               // passing by const reference
38    void add9(int *other) { value += *other; }                    // passing by pointer
39    void add10(const int *other) { value += *other; }             // passing by const pointer
40
41    ExampleMandA self1() { return *this; }                            // return by value
42    ExampleMandA &self2() { return *this; }                           // return by reference
43    const ExampleMandA &self3() { return *this; }                     // return by const reference
44    ExampleMandA *self4() { return this; }                            // return by pointer
45    const ExampleMandA *self5() { return this; }                      // return by const pointer
46
47    int internal1() { return value; }                             // return by value
48    int &internal2() { return value; }                            // return by reference
49    const int &internal3() { return value; }                      // return by const reference
50    int *internal4() { return &value; }                           // return by pointer
51    const int *internal5() { return &value; }                     // return by const pointer
52
53    py::str overloaded(int, float) { return "(int, float)"; }
54    py::str overloaded(float, int) { return "(float, int)"; }
55    py::str overloaded(int, float) const { return "(int, float) const"; }
56    py::str overloaded(float, int) const { return "(float, int) const"; }
57
58    int value = 0;
59};
60
61struct TestProperties {
62    int value = 1;
63    static int static_value;
64
65    int get() const { return value; }
66    void set(int v) { value = v; }
67
68    static int static_get() { return static_value; }
69    static void static_set(int v) { static_value = v; }
70};
71
72int TestProperties::static_value = 1;
73
74struct SimpleValue { int value = 1; };
75
76struct TestPropRVP {
77    SimpleValue v1;
78    SimpleValue v2;
79    static SimpleValue sv1;
80    static SimpleValue sv2;
81
82    const SimpleValue &get1() const { return v1; }
83    const SimpleValue &get2() const { return v2; }
84    SimpleValue get_rvalue() const { return v2; }
85    void set1(int v) { v1.value = v; }
86    void set2(int v) { v2.value = v; }
87};
88
89SimpleValue TestPropRVP::sv1{};
90SimpleValue TestPropRVP::sv2{};
91
92class DynamicClass {
93public:
94    DynamicClass() { print_default_created(this); }
95    ~DynamicClass() { print_destroyed(this); }
96};
97
98class CppDerivedDynamicClass : public DynamicClass { };
99
100test_initializer methods_and_attributes([](py::module &m) {
101    py::class_<ExampleMandA>(m, "ExampleMandA")
102        .def(py::init<>())
103        .def(py::init<int>())
104        .def(py::init<const ExampleMandA&>())
105        .def("add1", &ExampleMandA::add1)
106        .def("add2", &ExampleMandA::add2)
107        .def("add3", &ExampleMandA::add3)
108        .def("add4", &ExampleMandA::add4)
109        .def("add5", &ExampleMandA::add5)
110        .def("add6", &ExampleMandA::add6)
111        .def("add7", &ExampleMandA::add7)
112        .def("add8", &ExampleMandA::add8)
113        .def("add9", &ExampleMandA::add9)
114        .def("add10", &ExampleMandA::add10)
115        .def("self1", &ExampleMandA::self1)
116        .def("self2", &ExampleMandA::self2)
117        .def("self3", &ExampleMandA::self3)
118        .def("self4", &ExampleMandA::self4)
119        .def("self5", &ExampleMandA::self5)
120        .def("internal1", &ExampleMandA::internal1)
121        .def("internal2", &ExampleMandA::internal2)
122        .def("internal3", &ExampleMandA::internal3)
123        .def("internal4", &ExampleMandA::internal4)
124        .def("internal5", &ExampleMandA::internal5)
125#if defined(PYBIND11_OVERLOAD_CAST)
126        .def("overloaded", py::overload_cast<int, float>(&ExampleMandA::overloaded))
127        .def("overloaded", py::overload_cast<float, int>(&ExampleMandA::overloaded))
128        .def("overloaded_const", py::overload_cast<int, float>(&ExampleMandA::overloaded, py::const_))
129        .def("overloaded_const", py::overload_cast<float, int>(&ExampleMandA::overloaded, py::const_))
130#else
131        .def("overloaded", static_cast<py::str (ExampleMandA::*)(int, float)>(&ExampleMandA::overloaded))
132        .def("overloaded", static_cast<py::str (ExampleMandA::*)(float, int)>(&ExampleMandA::overloaded))
133        .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(int, float) const>(&ExampleMandA::overloaded))
134        .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(float, int) const>(&ExampleMandA::overloaded))
135#endif
136        .def("__str__", &ExampleMandA::toString)
137        .def_readwrite("value", &ExampleMandA::value)
138        ;
139
140    py::class_<TestProperties>(m, "TestProperties")
141        .def(py::init<>())
142        .def_readonly("def_readonly", &TestProperties::value)
143        .def_readwrite("def_readwrite", &TestProperties::value)
144        .def_property_readonly("def_property_readonly", &TestProperties::get)
145        .def_property("def_property", &TestProperties::get, &TestProperties::set)
146        .def_readonly_static("def_readonly_static", &TestProperties::static_value)
147        .def_readwrite_static("def_readwrite_static", &TestProperties::static_value)
148        .def_property_readonly_static("def_property_readonly_static",
149                                      [](py::object) { return TestProperties::static_get(); })
150        .def_property_static("def_property_static",
151                             [](py::object) { return TestProperties::static_get(); },
152                             [](py::object, int v) { return TestProperties::static_set(v); });
153
154    py::class_<SimpleValue>(m, "SimpleValue")
155        .def_readwrite("value", &SimpleValue::value);
156
157    auto static_get1 = [](py::object) -> const SimpleValue & { return TestPropRVP::sv1; };
158    auto static_get2 = [](py::object) -> const SimpleValue & { return TestPropRVP::sv2; };
159    auto static_set1 = [](py::object, int v) { TestPropRVP::sv1.value = v; };
160    auto static_set2 = [](py::object, int v) { TestPropRVP::sv2.value = v; };
161    auto rvp_copy = py::return_value_policy::copy;
162
163    py::class_<TestPropRVP>(m, "TestPropRVP")
164        .def(py::init<>())
165        .def_property_readonly("ro_ref", &TestPropRVP::get1)
166        .def_property_readonly("ro_copy", &TestPropRVP::get2, rvp_copy)
167        .def_property_readonly("ro_func", py::cpp_function(&TestPropRVP::get2, rvp_copy))
168        .def_property("rw_ref", &TestPropRVP::get1, &TestPropRVP::set1)
169        .def_property("rw_copy", &TestPropRVP::get2, &TestPropRVP::set2, rvp_copy)
170        .def_property("rw_func", py::cpp_function(&TestPropRVP::get2, rvp_copy), &TestPropRVP::set2)
171        .def_property_readonly_static("static_ro_ref", static_get1)
172        .def_property_readonly_static("static_ro_copy", static_get2, rvp_copy)
173        .def_property_readonly_static("static_ro_func", py::cpp_function(static_get2, rvp_copy))
174        .def_property_static("static_rw_ref", static_get1, static_set1)
175        .def_property_static("static_rw_copy", static_get2, static_set2, rvp_copy)
176        .def_property_static("static_rw_func", py::cpp_function(static_get2, rvp_copy), static_set2)
177        .def_property_readonly("rvalue", &TestPropRVP::get_rvalue)
178        .def_property_readonly_static("static_rvalue", [](py::object) { return SimpleValue(); });
179
180    py::class_<DynamicClass>(m, "DynamicClass", py::dynamic_attr())
181        .def(py::init());
182
183    py::class_<CppDerivedDynamicClass, DynamicClass>(m, "CppDerivedDynamicClass")
184        .def(py::init());
185});
186