blob: 800078cf5dc4f6e6894f9aa6ca413859244f19a8 [file] [log] [blame]
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -04001/*
2 example/example-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 "example.h"
12
13class ExampleMandA {
14public:
15 ExampleMandA() {
16 cout << "Called ExampleMandA default constructor.." << endl;
17 }
18 ExampleMandA(int value) : value(value) {
19 cout << "Called ExampleMandA constructor with value " << value << ".." << endl;
20 }
21 ExampleMandA(const ExampleMandA &e) : value(e.value) {
22 cout << "Called ExampleMandA copy constructor with value " << value << ".." << endl;
23 }
24 ExampleMandA(ExampleMandA &&e) : value(e.value) {
25 cout << "Called ExampleMandA move constructor with value " << value << ".." << endl;
26 e.value = 0;
27 }
28 ~ExampleMandA() {
29 cout << "Called ExampleMandA destructor (" << value << ")" << endl;
30 }
31 std::string toString() {
32 return "ExampleMandA[value=" + std::to_string(value) + "]";
33 }
34
35 void operator=(const ExampleMandA &e) { cout << "Assignment operator" << endl; value = e.value; }
36 void operator=(ExampleMandA &&e) { cout << "Move assignment operator" << endl; value = e.value; e.value = 0;}
37
38 void add1(ExampleMandA other) { value += other.value; } // passing by value
39 void add2(ExampleMandA &other) { value += other.value; } // passing by reference
40 void add3(const ExampleMandA &other) { value += other.value; } // passing by const reference
41 void add4(ExampleMandA *other) { value += other->value; } // passing by pointer
42 void add5(const ExampleMandA *other) { value += other->value; } // passing by const pointer
43
44 void add6(int other) { value += other; } // passing by 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 by pointer
48 void add10(const int *other) { value += *other; } // passing by const pointer
49
50 ExampleMandA self1() { return *this; } // return by value
51 ExampleMandA &self2() { return *this; } // return by reference
52 const ExampleMandA &self3() { return *this; } // return by const reference
53 ExampleMandA *self4() { return this; } // return by pointer
54 const ExampleMandA *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
65void init_ex_methods_and_attributes(py::module &m) {
66 py::class_<ExampleMandA>(m, "ExampleMandA")
67 .def(py::init<>())
68 .def(py::init<int>())
69 .def(py::init<const ExampleMandA&>())
70 .def("add1", &ExampleMandA::add1)
71 .def("add2", &ExampleMandA::add2)
72 .def("add3", &ExampleMandA::add3)
73 .def("add4", &ExampleMandA::add4)
74 .def("add5", &ExampleMandA::add5)
75 .def("add6", &ExampleMandA::add6)
76 .def("add7", &ExampleMandA::add7)
77 .def("add8", &ExampleMandA::add8)
78 .def("add9", &ExampleMandA::add9)
79 .def("add10", &ExampleMandA::add10)
80 .def("self1", &ExampleMandA::self1)
81 .def("self2", &ExampleMandA::self2)
82 .def("self3", &ExampleMandA::self3)
83 .def("self4", &ExampleMandA::self4)
84 .def("self5", &ExampleMandA::self5)
85 .def("internal1", &ExampleMandA::internal1)
86 .def("internal2", &ExampleMandA::internal2)
87 .def("internal3", &ExampleMandA::internal3)
88 .def("internal4", &ExampleMandA::internal4)
89 .def("internal5", &ExampleMandA::internal5)
90 .def("__str__", &ExampleMandA::toString)
91 .def_readwrite("value", &ExampleMandA::value);
92}