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 | |
| 5 | Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.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 | |
| 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 | |
| 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 | |
| 23 | /* Create and return a Python dictionary */ |
| 24 | py::dict get_dict() { |
| 25 | py::dict dict; |
| 26 | dict[py::str("key")] = py::str("value"); |
| 27 | return dict; |
| 28 | } |
| 29 | |
Wenzel Jakob | 333e889 | 2015-11-14 19:04:49 +0100 | [diff] [blame] | 30 | /* Create and return a Python set */ |
| 31 | py::set get_set() { |
| 32 | py::set set; |
Wenzel Jakob | 3ee91b2 | 2015-11-15 13:03:07 +0100 | [diff] [blame] | 33 | set.add(py::str("key1")); |
| 34 | set.add(py::str("key2")); |
Wenzel Jakob | 333e889 | 2015-11-14 19:04:49 +0100 | [diff] [blame] | 35 | return set; |
| 36 | } |
| 37 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 38 | /* Create and return a C++ dictionary */ |
| 39 | std::map<std::string, std::string> get_dict_2() { |
| 40 | std::map<std::string, std::string> result; |
| 41 | result["key"] = "value"; |
| 42 | return result; |
| 43 | } |
| 44 | |
Wenzel Jakob | 333e889 | 2015-11-14 19:04:49 +0100 | [diff] [blame] | 45 | /* Create and return a C++ set */ |
| 46 | std::set<std::string> get_set_2() { |
| 47 | std::set<std::string> result; |
| 48 | result.insert("key1"); |
| 49 | result.insert("key2"); |
| 50 | return result; |
| 51 | } |
| 52 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 53 | /* Create, manipulate, and return a Python list */ |
| 54 | py::list get_list() { |
| 55 | py::list list; |
| 56 | list.append(py::str("value")); |
| 57 | cout << "Entry at positon 0: " << py::object(list[0]) << endl; |
| 58 | list[0] = py::str("overwritten"); |
| 59 | return list; |
| 60 | } |
| 61 | |
| 62 | /* C++ STL data types are automatically casted */ |
| 63 | std::vector<std::string> get_list_2() { |
| 64 | std::vector<std::string> list; |
| 65 | list.push_back("value"); |
| 66 | return list; |
| 67 | } |
| 68 | |
| 69 | /* Easily iterate over a dictionary using a C++11 range-based for loop */ |
| 70 | void print_dict(py::dict dict) { |
| 71 | for (auto item : dict) |
| 72 | std::cout << "key: " << item.first << ", value=" << item.second << std::endl; |
| 73 | } |
| 74 | |
| 75 | /* STL data types are automatically casted from Python */ |
| 76 | void print_dict_2(const std::map<std::string, std::string> &dict) { |
| 77 | for (auto item : dict) |
| 78 | std::cout << "key: " << item.first << ", value=" << item.second << std::endl; |
| 79 | } |
| 80 | |
Wenzel Jakob | 333e889 | 2015-11-14 19:04:49 +0100 | [diff] [blame] | 81 | /* Easily iterate over a setionary using a C++11 range-based for loop */ |
| 82 | void print_set(py::set set) { |
| 83 | for (auto item : set) |
| 84 | std::cout << "key: " << item << std::endl; |
| 85 | } |
| 86 | |
| 87 | /* STL data types 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 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 93 | /* Easily iterate over a list using a C++11 range-based for loop */ |
| 94 | void print_list(py::list list) { |
| 95 | int index = 0; |
| 96 | for (auto item : list) |
| 97 | std::cout << "list item " << index++ << ": " << item << std::endl; |
| 98 | } |
| 99 | |
| 100 | /* STL data types are automatically casted from Python */ |
| 101 | void print_list_2(std::vector<std::string> &list) { |
| 102 | int index = 0; |
| 103 | for (auto item : list) |
| 104 | std::cout << "list item " << index++ << ": " << item << std::endl; |
| 105 | } |
| 106 | |
| 107 | /* pybind automatically translates between C++11 and Python tuples */ |
| 108 | std::pair<std::string, bool> pair_passthrough(std::pair<bool, std::string> input) { |
| 109 | return std::make_pair(input.second, input.first); |
| 110 | } |
| 111 | |
| 112 | /* pybind automatically translates between C++11 and Python tuples */ |
| 113 | std::tuple<int, std::string, bool> tuple_passthrough(std::tuple<bool, std::string, int> input) { |
| 114 | return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input)); |
| 115 | } |
| 116 | |
| 117 | void throw_exception() { |
| 118 | throw std::runtime_error("This exception was intentionally thrown."); |
| 119 | } |
| 120 | |
| 121 | static int value; |
| 122 | static const int value2; |
| 123 | }; |
| 124 | |
| 125 | int Example2::value = 0; |
| 126 | const int Example2::value2 = 5; |
| 127 | |
| 128 | void init_ex2(py::module &m) { |
| 129 | /* No constructor is explicitly defined below. An exception is raised when |
| 130 | trying to construct it directly from Python */ |
Wenzel Jakob | 5116b02 | 2015-09-05 02:09:17 +0200 | [diff] [blame] | 131 | py::class_<Example2>(m, "Example2", "Example 2 documentation") |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 132 | .def("get_dict", &Example2::get_dict, "Return a Python dictionary") |
| 133 | .def("get_dict_2", &Example2::get_dict_2, "Return a C++ dictionary") |
| 134 | .def("get_list", &Example2::get_list, "Return a Python list") |
| 135 | .def("get_list_2", &Example2::get_list_2, "Return a C++ list") |
Wenzel Jakob | 333e889 | 2015-11-14 19:04:49 +0100 | [diff] [blame] | 136 | .def("get_set", &Example2::get_set, "Return a Python set") |
| 137 | .def("get_set2", &Example2::get_set, "Return a C++ set") |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 138 | .def("print_dict", &Example2::print_dict, "Print entries of a Python dictionary") |
| 139 | .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] | 140 | .def("print_set", &Example2::print_set, "Print entries of a Python set") |
| 141 | .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] | 142 | .def("print_list", &Example2::print_list, "Print entries of a Python list") |
| 143 | .def("print_list_2", &Example2::print_list_2, "Print entries of a C++ list") |
| 144 | .def("pair_passthrough", &Example2::pair_passthrough, "Return a pair in reversed order") |
| 145 | .def("tuple_passthrough", &Example2::tuple_passthrough, "Return a triple in reversed order") |
| 146 | .def("throw_exception", &Example2::throw_exception, "Throw an exception") |
| 147 | .def_static("new_instance", &Example2::new_instance, "Return an instance") |
| 148 | .def_readwrite_static("value", &Example2::value, "Static value member") |
| 149 | .def_readonly_static("value2", &Example2::value2, "Static value member (readonly)"); |
| 150 | } |