blob: 3282a85dded4dd0dc7762361ea1b02e4ced1a541 [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
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 Jakob8f4eb002015-10-15 18:13:33 +020012#include <pybind11/stl.h>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020013
14class Example2 {
15public:
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 Jakob333e8892015-11-14 19:04:49 +010030 /* Create and return a Python set */
31 py::set get_set() {
32 py::set set;
Wenzel Jakob3ee91b22015-11-15 13:03:07 +010033 set.add(py::str("key1"));
34 set.add(py::str("key2"));
Wenzel Jakob333e8892015-11-14 19:04:49 +010035 return set;
36 }
37
Wenzel Jakob38bd7112015-07-05 20:05:44 +020038 /* 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 Jakob333e8892015-11-14 19:04:49 +010045 /* 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 Jakob38bd7112015-07-05 20:05:44 +020053 /* 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
Wenzel Jakob10c74c62016-02-07 16:36:26 +010069 /* C++ STL data types are automatically casted */
70 std::array<std::string, 2> get_array() {
71 return std::array<std::string, 2> {{ "array entry 1" , "array entry 2"}};
72 }
73
Wenzel Jakob38bd7112015-07-05 20:05:44 +020074 /* Easily iterate over a dictionary using a C++11 range-based for loop */
75 void print_dict(py::dict dict) {
76 for (auto item : dict)
77 std::cout << "key: " << item.first << ", value=" << item.second << std::endl;
78 }
79
Wenzel Jakob27e8e102016-01-17 22:36:37 +010080 /* Easily iterate over a set using a C++11 range-based for loop */
Wenzel Jakob333e8892015-11-14 19:04:49 +010081 void print_set(py::set set) {
82 for (auto item : set)
83 std::cout << "key: " << item << std::endl;
84 }
85
Wenzel Jakob38bd7112015-07-05 20:05:44 +020086 /* Easily iterate over a list using a C++11 range-based for loop */
87 void print_list(py::list list) {
88 int index = 0;
89 for (auto item : list)
90 std::cout << "list item " << index++ << ": " << item << std::endl;
91 }
92
Wenzel Jakob27e8e102016-01-17 22:36:37 +010093 /* STL data types (such as maps) are automatically casted from Python */
94 void print_dict_2(const std::map<std::string, std::string> &dict) {
95 for (auto item : dict)
96 std::cout << "key: " << item.first << ", value=" << item.second << std::endl;
97 }
98
99 /* STL data types (such as sets) are automatically casted from Python */
100 void print_set_2(const std::set<std::string> &set) {
101 for (auto item : set)
102 std::cout << "key: " << item << std::endl;
103 }
104
105 /* STL data types (such as vectors) are automatically casted from Python */
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200106 void print_list_2(std::vector<std::string> &list) {
107 int index = 0;
108 for (auto item : list)
109 std::cout << "list item " << index++ << ": " << item << std::endl;
110 }
111
112 /* pybind automatically translates between C++11 and Python tuples */
113 std::pair<std::string, bool> pair_passthrough(std::pair<bool, std::string> input) {
114 return std::make_pair(input.second, input.first);
115 }
116
117 /* pybind automatically translates between C++11 and Python tuples */
118 std::tuple<int, std::string, bool> tuple_passthrough(std::tuple<bool, std::string, int> input) {
119 return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input));
120 }
121
Wenzel Jakob10c74c62016-02-07 16:36:26 +0100122 /* STL data types (such as arrays) are automatically casted from Python */
123 void print_array(std::array<std::string, 2> &array) {
124 int index = 0;
125 for (auto item : array)
126 std::cout << "array item " << index++ << ": " << item << std::endl;
127 }
128
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200129 void throw_exception() {
130 throw std::runtime_error("This exception was intentionally thrown.");
131 }
132
133 static int value;
134 static const int value2;
135};
136
137int Example2::value = 0;
138const int Example2::value2 = 5;
139
140void init_ex2(py::module &m) {
141 /* No constructor is explicitly defined below. An exception is raised when
142 trying to construct it directly from Python */
Wenzel Jakob5116b022015-09-05 02:09:17 +0200143 py::class_<Example2>(m, "Example2", "Example 2 documentation")
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200144 .def("get_dict", &Example2::get_dict, "Return a Python dictionary")
145 .def("get_dict_2", &Example2::get_dict_2, "Return a C++ dictionary")
146 .def("get_list", &Example2::get_list, "Return a Python list")
147 .def("get_list_2", &Example2::get_list_2, "Return a C++ list")
Wenzel Jakob333e8892015-11-14 19:04:49 +0100148 .def("get_set", &Example2::get_set, "Return a Python set")
149 .def("get_set2", &Example2::get_set, "Return a C++ set")
Wenzel Jakob10c74c62016-02-07 16:36:26 +0100150 .def("get_array", &Example2::get_array, "Return a C++ array")
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200151 .def("print_dict", &Example2::print_dict, "Print entries of a Python dictionary")
152 .def("print_dict_2", &Example2::print_dict_2, "Print entries of a C++ dictionary")
Wenzel Jakob333e8892015-11-14 19:04:49 +0100153 .def("print_set", &Example2::print_set, "Print entries of a Python set")
154 .def("print_set_2", &Example2::print_set_2, "Print entries of a C++ set")
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200155 .def("print_list", &Example2::print_list, "Print entries of a Python list")
156 .def("print_list_2", &Example2::print_list_2, "Print entries of a C++ list")
Wenzel Jakob10c74c62016-02-07 16:36:26 +0100157 .def("print_array", &Example2::print_array, "Print entries of a C++ array")
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200158 .def("pair_passthrough", &Example2::pair_passthrough, "Return a pair in reversed order")
159 .def("tuple_passthrough", &Example2::tuple_passthrough, "Return a triple in reversed order")
160 .def("throw_exception", &Example2::throw_exception, "Throw an exception")
161 .def_static("new_instance", &Example2::new_instance, "Return an instance")
162 .def_readwrite_static("value", &Example2::value, "Static value member")
163 .def_readonly_static("value2", &Example2::value2, "Static value member (readonly)");
164}