blob: 9b91baaad8641b05ac2006e17fe41ed9ee2c69f4 [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Wenzel Jakoba576e6a2015-07-29 17:51:54 +02002 example/example2.cpp2 -- singleton design pattern, static functions and
3 variables, passing and interacting with Python types
Wenzel Jakob38bd7112015-07-05 20:05:44 +02004
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +02005 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob38bd7112015-07-05 20:05:44 +02006
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 Jakob8f4eb002015-10-15 18:13:33 +020012#include <pybind11/stl.h>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020013
Wenzel Jakob9e759052016-03-31 14:22:37 +020014#ifdef _WIN32
15# include <io.h>
16# include <fcntl.h>
17#endif
18
Wenzel Jakob38bd7112015-07-05 20:05:44 +020019class Example2 {
20public:
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 Jakob333e8892015-11-14 19:04:49 +010035 /* Create and return a Python set */
36 py::set get_set() {
37 py::set set;
Wenzel Jakob3ee91b22015-11-15 13:03:07 +010038 set.add(py::str("key1"));
39 set.add(py::str("key2"));
Wenzel Jakob333e8892015-11-14 19:04:49 +010040 return set;
41 }
42
Wenzel Jakob38bd7112015-07-05 20:05:44 +020043 /* 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 Jakob333e8892015-11-14 19:04:49 +010050 /* 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 Jakob38bd7112015-07-05 20:05:44 +020058 /* 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 Jakob81dfd2c2016-03-08 19:40:32 +010068 std::vector<std::wstring> get_list_2() {
69 std::vector<std::wstring> list;
70 list.push_back(L"value");
Wenzel Jakob38bd7112015-07-05 20:05:44 +020071 return list;
72 }
73
Wenzel Jakob10c74c62016-02-07 16:36:26 +010074 /* 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 Jakob38bd7112015-07-05 20:05:44 +020079 /* 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 Jakob27e8e102016-01-17 22:36:37 +010085 /* Easily iterate over a set using a C++11 range-based for loop */
Wenzel Jakob333e8892015-11-14 19:04:49 +010086 void print_set(py::set set) {
87 for (auto item : set)
88 std::cout << "key: " << item << std::endl;
89 }
90
Wenzel Jakob38bd7112015-07-05 20:05:44 +020091 /* 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 Jakob27e8e102016-01-17 22:36:37 +010098 /* 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 Jakob81dfd2c2016-03-08 19:40:32 +0100111 void print_list_2(std::vector<std::wstring> &list) {
Wenzel Jakob9e759052016-03-31 14:22:37 +0200112#ifdef _WIN32 /* Can't easily mix cout and wcout on Windows */
113 _setmode(_fileno(stdout), _O_TEXT);
114#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200115 int index = 0;
116 for (auto item : list)
Wenzel Jakob81dfd2c2016-03-08 19:40:32 +0100117 std::wcout << L"list item " << index++ << L": " << item << std::endl;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200118 }
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 Jakob10c74c62016-02-07 16:36:26 +0100130 /* 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 Jakob38bd7112015-07-05 20:05:44 +0200137 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
145int Example2::value = 0;
146const int Example2::value2 = 5;
147
148void 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 Jakob5116b022015-09-05 02:09:17 +0200151 py::class_<Example2>(m, "Example2", "Example 2 documentation")
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200152 .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 Jakob333e8892015-11-14 19:04:49 +0100156 .def("get_set", &Example2::get_set, "Return a Python set")
157 .def("get_set2", &Example2::get_set, "Return a C++ set")
Wenzel Jakob10c74c62016-02-07 16:36:26 +0100158 .def("get_array", &Example2::get_array, "Return a C++ array")
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200159 .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 Jakob333e8892015-11-14 19:04:49 +0100161 .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 Jakob38bd7112015-07-05 20:05:44 +0200163 .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 Jakob10c74c62016-02-07 16:36:26 +0100165 .def("print_array", &Example2::print_array, "Print entries of a C++ array")
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200166 .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}