blob: 647427b83260a9057b9e9437006184dbe34482b3 [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
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 Jakob333e8892015-11-14 19:04:49 +010081 /* 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 Jakob38bd7112015-07-05 20:05:44 +020093 /* 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
125int Example2::value = 0;
126const int Example2::value2 = 5;
127
128void 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 Jakob5116b022015-09-05 02:09:17 +0200131 py::class_<Example2>(m, "Example2", "Example 2 documentation")
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200132 .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 Jakob333e8892015-11-14 19:04:49 +0100136 .def("get_set", &Example2::get_set, "Return a Python set")
137 .def("get_set2", &Example2::get_set, "Return a C++ set")
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200138 .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 Jakob333e8892015-11-14 19:04:49 +0100140 .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 Jakob38bd7112015-07-05 20:05:44 +0200142 .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}