Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1 | /* |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 2 | example/example1.cpp -- constructors, deconstructors, attribute access, |
| 3 | __str__, argument and return value conventions |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 4 | |
| 5 | Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.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 "example.h" |
| 12 | |
| 13 | class Example1 { |
| 14 | public: |
| 15 | Example1() { |
| 16 | cout << "Called Example1 default constructor.." << endl; |
| 17 | } |
| 18 | Example1(int value) : value(value) { |
| 19 | cout << "Called Example1 constructor with value " << value << ".." << endl; |
| 20 | } |
| 21 | Example1(const Example1 &e) : value(e.value) { |
| 22 | cout << "Called Example1 copy constructor with value " << value << ".." << endl; |
| 23 | } |
| 24 | Example1(Example1 &&e) : value(e.value) { |
| 25 | cout << "Called Example1 move constructor with value " << value << ".." << endl; |
| 26 | e.value = 0; |
| 27 | } |
| 28 | ~Example1() { |
| 29 | cout << "Called Example1 destructor (" << value << ")" << endl; |
| 30 | } |
| 31 | std::string toString() { |
| 32 | return "Example1[value=" + std::to_string(value) + "]"; |
| 33 | } |
| 34 | |
| 35 | void operator=(const Example1 &e) { cout << "Assignment operator" << endl; value = e.value; } |
| 36 | void operator=(Example1 &&e) { cout << "Move assignment operator" << endl; value = e.value; e.value = 0;} |
| 37 | |
| 38 | void add1(Example1 other) { value += other.value; } // passing py value |
| 39 | void add2(Example1 &other) { value += other.value; } // passing by reference |
| 40 | void add3(const Example1 &other) { value += other.value; } // passing by const reference |
| 41 | void add4(Example1 *other) { value += other->value; } // passing py pointer |
| 42 | void add5(const Example1 *other) { value += other->value; } // passing by const pointer |
| 43 | |
| 44 | void add6(int other) { value += other; } // passing py value |
| 45 | void add7(int &other) { value += other; } // passing by reference |
| 46 | void add8(const int &other) { value += other; } // passing by const reference |
| 47 | void add9(int *other) { value += *other; } // passing py pointer |
| 48 | void add10(const int *other) { value += *other; } // passing by const pointer |
| 49 | |
| 50 | Example1 self1() { return *this; } // return by value |
| 51 | Example1 &self2() { return *this; } // return by reference |
| 52 | const Example1 &self3() { return *this; } // return by const reference |
| 53 | Example1 *self4() { return this; } // return by pointer |
| 54 | const Example1 *self5() { return this; } // return by const pointer |
| 55 | |
| 56 | int internal1() { return value; } // return by value |
| 57 | int &internal2() { return value; } // return by reference |
| 58 | const int &internal3() { return value; } // return by const reference |
| 59 | int *internal4() { return &value; } // return by pointer |
| 60 | const int *internal5() { return &value; } // return by const pointer |
| 61 | |
| 62 | int value = 0; |
| 63 | }; |
| 64 | |
| 65 | void init_ex1(py::module &m) { |
| 66 | py::class_<Example1>(m, "Example1") |
| 67 | .def(py::init<>()) |
| 68 | .def(py::init<int>()) |
| 69 | .def(py::init<const Example1&>()) |
| 70 | .def("add1", &Example1::add1) |
| 71 | .def("add2", &Example1::add2) |
| 72 | .def("add3", &Example1::add3) |
| 73 | .def("add4", &Example1::add4) |
| 74 | .def("add5", &Example1::add5) |
| 75 | .def("add6", &Example1::add6) |
| 76 | .def("add7", &Example1::add7) |
| 77 | .def("add8", &Example1::add8) |
| 78 | .def("add9", &Example1::add9) |
| 79 | .def("add10", &Example1::add10) |
| 80 | .def("self1", &Example1::self1) |
| 81 | .def("self2", &Example1::self2) |
| 82 | .def("self3", &Example1::self3) |
| 83 | .def("self4", &Example1::self4) |
| 84 | .def("self5", &Example1::self5) |
| 85 | .def("internal1", &Example1::internal1) |
| 86 | .def("internal2", &Example1::internal2) |
| 87 | .def("internal3", &Example1::internal3) |
| 88 | .def("internal4", &Example1::internal4) |
| 89 | .def("internal5", &Example1::internal5) |
| 90 | .def("__str__", &Example1::toString) |
| 91 | .def_readwrite("value", &Example1::value); |
| 92 | } |