blob: edf519aa9b72aa38e7feb6b44de0e2e058b96e54 [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
Jason Rhinelander3f589372016-08-07 13:05:26 -040011#include <unordered_map>
12#include <list>
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040013#include "example.h"
Jason Rhinelander3f589372016-08-07 13:05:26 -040014#include "constructor-stats.h"
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040015
16class ExampleMandA {
17public:
Jason Rhinelander3f589372016-08-07 13:05:26 -040018 ExampleMandA() { print_default_created(this); }
19 ExampleMandA(int value) : value(value) { print_created(this, value); }
20 ExampleMandA(const ExampleMandA &e) : value(e.value) { print_copy_created(this); }
21 ExampleMandA(ExampleMandA &&e) : value(e.value) { print_move_created(this); }
22 ~ExampleMandA() { print_destroyed(this); }
23
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040024 std::string toString() {
25 return "ExampleMandA[value=" + std::to_string(value) + "]";
26 }
27
Jason Rhinelander3f589372016-08-07 13:05:26 -040028 void operator=(const ExampleMandA &e) { print_copy_assigned(this); value = e.value; }
29 void operator=(ExampleMandA &&e) { print_move_assigned(this); value = e.value; }
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040030
31 void add1(ExampleMandA other) { value += other.value; } // passing by value
32 void add2(ExampleMandA &other) { value += other.value; } // passing by reference
33 void add3(const ExampleMandA &other) { value += other.value; } // passing by const reference
34 void add4(ExampleMandA *other) { value += other->value; } // passing by pointer
35 void add5(const ExampleMandA *other) { value += other->value; } // passing by const pointer
36
37 void add6(int other) { value += other; } // passing by value
38 void add7(int &other) { value += other; } // passing by reference
39 void add8(const int &other) { value += other; } // passing by const reference
40 void add9(int *other) { value += *other; } // passing by pointer
41 void add10(const int *other) { value += *other; } // passing by const pointer
42
43 ExampleMandA self1() { return *this; } // return by value
44 ExampleMandA &self2() { return *this; } // return by reference
45 const ExampleMandA &self3() { return *this; } // return by const reference
46 ExampleMandA *self4() { return this; } // return by pointer
47 const ExampleMandA *self5() { return this; } // return by const pointer
48
49 int internal1() { return value; } // return by value
50 int &internal2() { return value; } // return by reference
51 const int &internal3() { return value; } // return by const reference
52 int *internal4() { return &value; } // return by pointer
53 const int *internal5() { return &value; } // return by const pointer
54
55 int value = 0;
56};
57
58void init_ex_methods_and_attributes(py::module &m) {
59 py::class_<ExampleMandA>(m, "ExampleMandA")
60 .def(py::init<>())
61 .def(py::init<int>())
62 .def(py::init<const ExampleMandA&>())
63 .def("add1", &ExampleMandA::add1)
64 .def("add2", &ExampleMandA::add2)
65 .def("add3", &ExampleMandA::add3)
66 .def("add4", &ExampleMandA::add4)
67 .def("add5", &ExampleMandA::add5)
68 .def("add6", &ExampleMandA::add6)
69 .def("add7", &ExampleMandA::add7)
70 .def("add8", &ExampleMandA::add8)
71 .def("add9", &ExampleMandA::add9)
72 .def("add10", &ExampleMandA::add10)
73 .def("self1", &ExampleMandA::self1)
74 .def("self2", &ExampleMandA::self2)
75 .def("self3", &ExampleMandA::self3)
76 .def("self4", &ExampleMandA::self4)
77 .def("self5", &ExampleMandA::self5)
78 .def("internal1", &ExampleMandA::internal1)
79 .def("internal2", &ExampleMandA::internal2)
80 .def("internal3", &ExampleMandA::internal3)
81 .def("internal4", &ExampleMandA::internal4)
82 .def("internal5", &ExampleMandA::internal5)
83 .def("__str__", &ExampleMandA::toString)
Jason Rhinelander3f589372016-08-07 13:05:26 -040084 .def_readwrite("value", &ExampleMandA::value)
85 ;
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040086}