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.

172 lines
6.4 KiB

  1. /*
  2. example/example2.cpp2 -- singleton design pattern, static functions and
  3. variables, passing and interacting with Python types
  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. #include <pybind11/stl.h>
  10. #ifdef _WIN32
  11. # include <io.h>
  12. # include <fcntl.h>
  13. #endif
  14. class Example2 {
  15. public:
  16. static Example2 *new_instance() {
  17. return new Example2();
  18. }
  19. ~Example2() {
  20. std::cout << "Destructing Example2" << std::endl;
  21. }
  22. /* Create and return a Python dictionary */
  23. py::dict get_dict() {
  24. py::dict dict;
  25. dict[py::str("key")] = py::str("value");
  26. return dict;
  27. }
  28. /* Create and return a Python set */
  29. py::set get_set() {
  30. py::set set;
  31. set.add(py::str("key1"));
  32. set.add(py::str("key2"));
  33. return set;
  34. }
  35. /* Create and return a C++ dictionary */
  36. std::map<std::string, std::string> get_dict_2() {
  37. std::map<std::string, std::string> result;
  38. result["key"] = "value";
  39. return result;
  40. }
  41. /* Create and return a C++ set */
  42. std::set<std::string> get_set_2() {
  43. std::set<std::string> result;
  44. result.insert("key1");
  45. result.insert("key2");
  46. return result;
  47. }
  48. /* Create, manipulate, and return a Python list */
  49. py::list get_list() {
  50. py::list list;
  51. list.append(py::str("value"));
  52. cout << "Entry at positon 0: " << py::object(list[0]) << endl;
  53. list[0] = py::str("overwritten");
  54. return list;
  55. }
  56. /* C++ STL data types are automatically casted */
  57. std::vector<std::wstring> get_list_2() {
  58. std::vector<std::wstring> list;
  59. list.push_back(L"value");
  60. return list;
  61. }
  62. /* C++ STL data types are automatically casted */
  63. std::array<std::string, 2> get_array() {
  64. return std::array<std::string, 2> {{ "array entry 1" , "array entry 2"}};
  65. }
  66. /* Easily iterate over a dictionary using a C++11 range-based for loop */
  67. void print_dict(py::dict dict) {
  68. for (auto item : dict)
  69. std::cout << "key: " << item.first << ", value=" << item.second << std::endl;
  70. }
  71. /* Easily iterate over a set using a C++11 range-based for loop */
  72. void print_set(py::set set) {
  73. for (auto item : set)
  74. std::cout << "key: " << item << std::endl;
  75. }
  76. /* Easily iterate over a list using a C++11 range-based for loop */
  77. void print_list(py::list list) {
  78. int index = 0;
  79. for (auto item : list)
  80. std::cout << "list item " << index++ << ": " << item << std::endl;
  81. }
  82. /* STL data types (such as maps) are automatically casted from Python */
  83. void print_dict_2(const std::map<std::string, std::string> &dict) {
  84. for (auto item : dict)
  85. std::cout << "key: " << item.first << ", value=" << item.second << std::endl;
  86. }
  87. /* STL data types (such as sets) are automatically casted from Python */
  88. void print_set_2(const std::set<std::string> &set) {
  89. for (auto item : set)
  90. std::cout << "key: " << item << std::endl;
  91. }
  92. /* STL data types (such as vectors) are automatically casted from Python */
  93. void print_list_2(std::vector<std::wstring> &list) {
  94. #ifdef _WIN32 /* Can't easily mix cout and wcout on Windows */
  95. _setmode(_fileno(stdout), _O_TEXT);
  96. #endif
  97. int index = 0;
  98. for (auto item : list)
  99. std::wcout << L"list item " << index++ << L": " << item << std::endl;
  100. }
  101. /* pybind automatically translates between C++11 and Python tuples */
  102. std::pair<std::string, bool> pair_passthrough(std::pair<bool, std::string> input) {
  103. return std::make_pair(input.second, input.first);
  104. }
  105. /* pybind automatically translates between C++11 and Python tuples */
  106. std::tuple<int, std::string, bool> tuple_passthrough(std::tuple<bool, std::string, int> input) {
  107. return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input));
  108. }
  109. /* STL data types (such as arrays) are automatically casted from Python */
  110. void print_array(std::array<std::string, 2> &array) {
  111. int index = 0;
  112. for (auto item : array)
  113. std::cout << "array item " << index++ << ": " << item << std::endl;
  114. }
  115. void throw_exception() {
  116. throw std::runtime_error("This exception was intentionally thrown.");
  117. }
  118. static int value;
  119. static const int value2;
  120. };
  121. int Example2::value = 0;
  122. const int Example2::value2 = 5;
  123. void init_ex2(py::module &m) {
  124. /* No constructor is explicitly defined below. An exception is raised when
  125. trying to construct it directly from Python */
  126. py::class_<Example2>(m, "Example2", "Example 2 documentation")
  127. .def("get_dict", &Example2::get_dict, "Return a Python dictionary")
  128. .def("get_dict_2", &Example2::get_dict_2, "Return a C++ dictionary")
  129. .def("get_list", &Example2::get_list, "Return a Python list")
  130. .def("get_list_2", &Example2::get_list_2, "Return a C++ list")
  131. .def("get_set", &Example2::get_set, "Return a Python set")
  132. .def("get_set2", &Example2::get_set_2, "Return a C++ set")
  133. .def("get_array", &Example2::get_array, "Return a C++ array")
  134. .def("print_dict", &Example2::print_dict, "Print entries of a Python dictionary")
  135. .def("print_dict_2", &Example2::print_dict_2, "Print entries of a C++ dictionary")
  136. .def("print_set", &Example2::print_set, "Print entries of a Python set")
  137. .def("print_set_2", &Example2::print_set_2, "Print entries of a C++ set")
  138. .def("print_list", &Example2::print_list, "Print entries of a Python list")
  139. .def("print_list_2", &Example2::print_list_2, "Print entries of a C++ list")
  140. .def("print_array", &Example2::print_array, "Print entries of a C++ array")
  141. .def("pair_passthrough", &Example2::pair_passthrough, "Return a pair in reversed order")
  142. .def("tuple_passthrough", &Example2::tuple_passthrough, "Return a triple in reversed order")
  143. .def("throw_exception", &Example2::throw_exception, "Throw an exception")
  144. .def_static("new_instance", &Example2::new_instance, "Return an instance")
  145. .def_readwrite_static("value", &Example2::value, "Static value member")
  146. .def_readonly_static("value2", &Example2::value2, "Static value member (readonly)");
  147. }