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