You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
4.1 KiB

  1. /*
  2. example/example1.cpp -- constructors, deconstructors, attribute access,
  3. __str__, argument and return value conventions
  4. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  5. All rights reserved. Use of this source code is governed by a
  6. BSD-style license that can be found in the LICENSE file.
  7. */
  8. #include "example.h"
  9. class Example1 {
  10. public:
  11. Example1() {
  12. cout << "Called Example1 default constructor.." << endl;
  13. }
  14. Example1(int value) : value(value) {
  15. cout << "Called Example1 constructor with value " << value << ".." << endl;
  16. }
  17. Example1(const Example1 &e) : value(e.value) {
  18. cout << "Called Example1 copy constructor with value " << value << ".." << endl;
  19. }
  20. Example1(Example1 &&e) : value(e.value) {
  21. cout << "Called Example1 move constructor with value " << value << ".." << endl;
  22. e.value = 0;
  23. }
  24. ~Example1() {
  25. cout << "Called Example1 destructor (" << value << ")" << endl;
  26. }
  27. std::string toString() {
  28. return "Example1[value=" + std::to_string(value) + "]";
  29. }
  30. void operator=(const Example1 &e) { cout << "Assignment operator" << endl; value = e.value; }
  31. void operator=(Example1 &&e) { cout << "Move assignment operator" << endl; value = e.value; e.value = 0;}
  32. void add1(Example1 other) { value += other.value; } // passing by value
  33. void add2(Example1 &other) { value += other.value; } // passing by reference
  34. void add3(const Example1 &other) { value += other.value; } // passing by const reference
  35. void add4(Example1 *other) { value += other->value; } // passing by pointer
  36. void add5(const Example1 *other) { value += other->value; } // passing by const pointer
  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. Example1 self1() { return *this; } // return by value
  43. Example1 &self2() { return *this; } // return by reference
  44. const Example1 &self3() { return *this; } // return by const reference
  45. Example1 *self4() { return this; } // return by pointer
  46. const Example1 *self5() { return this; } // return by const pointer
  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. int value = 0;
  53. };
  54. void init_ex1(py::module &m) {
  55. py::class_<Example1>(m, "Example1")
  56. .def(py::init<>())
  57. .def(py::init<int>())
  58. .def(py::init<const Example1&>())
  59. .def("add1", &Example1::add1)
  60. .def("add2", &Example1::add2)
  61. .def("add3", &Example1::add3)
  62. .def("add4", &Example1::add4)
  63. .def("add5", &Example1::add5)
  64. .def("add6", &Example1::add6)
  65. .def("add7", &Example1::add7)
  66. .def("add8", &Example1::add8)
  67. .def("add9", &Example1::add9)
  68. .def("add10", &Example1::add10)
  69. .def("self1", &Example1::self1)
  70. .def("self2", &Example1::self2)
  71. .def("self3", &Example1::self3)
  72. .def("self4", &Example1::self4)
  73. .def("self5", &Example1::self5)
  74. .def("internal1", &Example1::internal1)
  75. .def("internal2", &Example1::internal2)
  76. .def("internal3", &Example1::internal3)
  77. .def("internal4", &Example1::internal4)
  78. .def("internal5", &Example1::internal5)
  79. .def("__str__", &Example1::toString)
  80. .def_readwrite("value", &Example1::value);
  81. }