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.

169 lines
7.8 KiB

  1. /*
  2. tests/test_methods_and_attributes.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 "pybind11_tests.h"
  9. #include "constructor_stats.h"
  10. class ExampleMandA {
  11. public:
  12. ExampleMandA() { print_default_created(this); }
  13. ExampleMandA(int value) : value(value) { print_created(this, value); }
  14. ExampleMandA(const ExampleMandA &e) : value(e.value) { print_copy_created(this); }
  15. ExampleMandA(ExampleMandA &&e) : value(e.value) { print_move_created(this); }
  16. ~ExampleMandA() { print_destroyed(this); }
  17. std::string toString() {
  18. return "ExampleMandA[value=" + std::to_string(value) + "]";
  19. }
  20. void operator=(const ExampleMandA &e) { print_copy_assigned(this); value = e.value; }
  21. void operator=(ExampleMandA &&e) { print_move_assigned(this); value = e.value; }
  22. void add1(ExampleMandA other) { value += other.value; } // passing by value
  23. void add2(ExampleMandA &other) { value += other.value; } // passing by reference
  24. void add3(const ExampleMandA &other) { value += other.value; } // passing by const reference
  25. void add4(ExampleMandA *other) { value += other->value; } // passing by pointer
  26. void add5(const ExampleMandA *other) { value += other->value; } // passing by const pointer
  27. void add6(int other) { value += other; } // passing by value
  28. void add7(int &other) { value += other; } // passing by reference
  29. void add8(const int &other) { value += other; } // passing by const reference
  30. void add9(int *other) { value += *other; } // passing by pointer
  31. void add10(const int *other) { value += *other; } // passing by const pointer
  32. ExampleMandA self1() { return *this; } // return by value
  33. ExampleMandA &self2() { return *this; } // return by reference
  34. const ExampleMandA &self3() { return *this; } // return by const reference
  35. ExampleMandA *self4() { return this; } // return by pointer
  36. const ExampleMandA *self5() { return this; } // return by const pointer
  37. int internal1() { return value; } // return by value
  38. int &internal2() { return value; } // return by reference
  39. const int &internal3() { return value; } // return by const reference
  40. int *internal4() { return &value; } // return by pointer
  41. const int *internal5() { return &value; } // return by const pointer
  42. int value = 0;
  43. };
  44. struct TestProperties {
  45. int value = 1;
  46. static int static_value;
  47. int get() const { return value; }
  48. void set(int v) { value = v; }
  49. static int static_get() { return static_value; }
  50. static void static_set(int v) { static_value = v; }
  51. };
  52. int TestProperties::static_value = 1;
  53. struct SimpleValue { int value = 1; };
  54. struct TestPropRVP {
  55. SimpleValue v1;
  56. SimpleValue v2;
  57. static SimpleValue sv1;
  58. static SimpleValue sv2;
  59. const SimpleValue &get1() const { return v1; }
  60. const SimpleValue &get2() const { return v2; }
  61. SimpleValue get_rvalue() const { return v2; }
  62. void set1(int v) { v1.value = v; }
  63. void set2(int v) { v2.value = v; }
  64. };
  65. SimpleValue TestPropRVP::sv1{};
  66. SimpleValue TestPropRVP::sv2{};
  67. class DynamicClass {
  68. public:
  69. DynamicClass() { print_default_created(this); }
  70. ~DynamicClass() { print_destroyed(this); }
  71. };
  72. class CppDerivedDynamicClass : public DynamicClass { };
  73. test_initializer methods_and_attributes([](py::module &m) {
  74. py::class_<ExampleMandA>(m, "ExampleMandA")
  75. .def(py::init<>())
  76. .def(py::init<int>())
  77. .def(py::init<const ExampleMandA&>())
  78. .def("add1", &ExampleMandA::add1)
  79. .def("add2", &ExampleMandA::add2)
  80. .def("add3", &ExampleMandA::add3)
  81. .def("add4", &ExampleMandA::add4)
  82. .def("add5", &ExampleMandA::add5)
  83. .def("add6", &ExampleMandA::add6)
  84. .def("add7", &ExampleMandA::add7)
  85. .def("add8", &ExampleMandA::add8)
  86. .def("add9", &ExampleMandA::add9)
  87. .def("add10", &ExampleMandA::add10)
  88. .def("self1", &ExampleMandA::self1)
  89. .def("self2", &ExampleMandA::self2)
  90. .def("self3", &ExampleMandA::self3)
  91. .def("self4", &ExampleMandA::self4)
  92. .def("self5", &ExampleMandA::self5)
  93. .def("internal1", &ExampleMandA::internal1)
  94. .def("internal2", &ExampleMandA::internal2)
  95. .def("internal3", &ExampleMandA::internal3)
  96. .def("internal4", &ExampleMandA::internal4)
  97. .def("internal5", &ExampleMandA::internal5)
  98. .def("__str__", &ExampleMandA::toString)
  99. .def_readwrite("value", &ExampleMandA::value)
  100. ;
  101. py::class_<TestProperties>(m, "TestProperties")
  102. .def(py::init<>())
  103. .def_readonly("def_readonly", &TestProperties::value)
  104. .def_readwrite("def_readwrite", &TestProperties::value)
  105. .def_property_readonly("def_property_readonly", &TestProperties::get)
  106. .def_property("def_property", &TestProperties::get, &TestProperties::set)
  107. .def_readonly_static("def_readonly_static", &TestProperties::static_value)
  108. .def_readwrite_static("def_readwrite_static", &TestProperties::static_value)
  109. .def_property_readonly_static("def_property_readonly_static",
  110. [](py::object) { return TestProperties::static_get(); })
  111. .def_property_static("def_property_static",
  112. [](py::object) { return TestProperties::static_get(); },
  113. [](py::object, int v) { return TestProperties::static_set(v); });
  114. py::class_<SimpleValue>(m, "SimpleValue")
  115. .def_readwrite("value", &SimpleValue::value);
  116. auto static_get1 = [](py::object) -> const SimpleValue & { return TestPropRVP::sv1; };
  117. auto static_get2 = [](py::object) -> const SimpleValue & { return TestPropRVP::sv2; };
  118. auto static_set1 = [](py::object, int v) { TestPropRVP::sv1.value = v; };
  119. auto static_set2 = [](py::object, int v) { TestPropRVP::sv2.value = v; };
  120. auto rvp_copy = py::return_value_policy::copy;
  121. py::class_<TestPropRVP>(m, "TestPropRVP")
  122. .def(py::init<>())
  123. .def_property_readonly("ro_ref", &TestPropRVP::get1)
  124. .def_property_readonly("ro_copy", &TestPropRVP::get2, rvp_copy)
  125. .def_property_readonly("ro_func", py::cpp_function(&TestPropRVP::get2, rvp_copy))
  126. .def_property("rw_ref", &TestPropRVP::get1, &TestPropRVP::set1)
  127. .def_property("rw_copy", &TestPropRVP::get2, &TestPropRVP::set2, rvp_copy)
  128. .def_property("rw_func", py::cpp_function(&TestPropRVP::get2, rvp_copy), &TestPropRVP::set2)
  129. .def_property_readonly_static("static_ro_ref", static_get1)
  130. .def_property_readonly_static("static_ro_copy", static_get2, rvp_copy)
  131. .def_property_readonly_static("static_ro_func", py::cpp_function(static_get2, rvp_copy))
  132. .def_property_static("static_rw_ref", static_get1, static_set1)
  133. .def_property_static("static_rw_copy", static_get2, static_set2, rvp_copy)
  134. .def_property_static("static_rw_func", py::cpp_function(static_get2, rvp_copy), static_set2)
  135. .def_property_readonly("rvalue", &TestPropRVP::get_rvalue)
  136. .def_property_readonly_static("static_rvalue", [](py::object) { return SimpleValue(); });
  137. py::class_<DynamicClass>(m, "DynamicClass", py::dynamic_attr())
  138. .def(py::init());
  139. py::class_<CppDerivedDynamicClass, DynamicClass>(m, "CppDerivedDynamicClass")
  140. .def(py::init());
  141. });