blob: 08fdd06a7cf434954fab312639da41a2bc73ca9e [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"
12
13class Example2 {
14public:
15 static Example2 *new_instance() {
16 return new Example2();
17 }
18 ~Example2() {
19 std::cout << "Destructing Example2" << std::endl;
20 }
21
22 /* Create and return a Python dictionary */
23 py::dict get_dict() {
24 py::dict dict;
25 dict[py::str("key")] = py::str("value");
26 return dict;
27 }
28
29 /* Create and return a C++ dictionary */
30 std::map<std::string, std::string> get_dict_2() {
31 std::map<std::string, std::string> result;
32 result["key"] = "value";
33 return result;
34 }
35
36 /* Create, manipulate, and return a Python list */
37 py::list get_list() {
38 py::list list;
39 list.append(py::str("value"));
40 cout << "Entry at positon 0: " << py::object(list[0]) << endl;
41 list[0] = py::str("overwritten");
42 return list;
43 }
44
45 /* C++ STL data types are automatically casted */
46 std::vector<std::string> get_list_2() {
47 std::vector<std::string> list;
48 list.push_back("value");
49 return list;
50 }
51
52 /* Easily iterate over a dictionary using a C++11 range-based for loop */
53 void print_dict(py::dict dict) {
54 for (auto item : dict)
55 std::cout << "key: " << item.first << ", value=" << item.second << std::endl;
56 }
57
58 /* STL data types are automatically casted from Python */
59 void print_dict_2(const std::map<std::string, std::string> &dict) {
60 for (auto item : dict)
61 std::cout << "key: " << item.first << ", value=" << item.second << std::endl;
62 }
63
64 /* Easily iterate over a list using a C++11 range-based for loop */
65 void print_list(py::list list) {
66 int index = 0;
67 for (auto item : list)
68 std::cout << "list item " << index++ << ": " << item << std::endl;
69 }
70
71 /* STL data types are automatically casted from Python */
72 void print_list_2(std::vector<std::string> &list) {
73 int index = 0;
74 for (auto item : list)
75 std::cout << "list item " << index++ << ": " << item << std::endl;
76 }
77
78 /* pybind automatically translates between C++11 and Python tuples */
79 std::pair<std::string, bool> pair_passthrough(std::pair<bool, std::string> input) {
80 return std::make_pair(input.second, input.first);
81 }
82
83 /* pybind automatically translates between C++11 and Python tuples */
84 std::tuple<int, std::string, bool> tuple_passthrough(std::tuple<bool, std::string, int> input) {
85 return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input));
86 }
87
88 void throw_exception() {
89 throw std::runtime_error("This exception was intentionally thrown.");
90 }
91
92 static int value;
93 static const int value2;
94};
95
96int Example2::value = 0;
97const int Example2::value2 = 5;
98
99void init_ex2(py::module &m) {
100 /* No constructor is explicitly defined below. An exception is raised when
101 trying to construct it directly from Python */
102 py::class_<Example2>(m, "Example2")
103 .def("get_dict", &Example2::get_dict, "Return a Python dictionary")
104 .def("get_dict_2", &Example2::get_dict_2, "Return a C++ dictionary")
105 .def("get_list", &Example2::get_list, "Return a Python list")
106 .def("get_list_2", &Example2::get_list_2, "Return a C++ list")
107 .def("print_dict", &Example2::print_dict, "Print entries of a Python dictionary")
108 .def("print_dict_2", &Example2::print_dict_2, "Print entries of a C++ dictionary")
109 .def("print_list", &Example2::print_list, "Print entries of a Python list")
110 .def("print_list_2", &Example2::print_list_2, "Print entries of a C++ list")
111 .def("pair_passthrough", &Example2::pair_passthrough, "Return a pair in reversed order")
112 .def("tuple_passthrough", &Example2::tuple_passthrough, "Return a triple in reversed order")
113 .def("throw_exception", &Example2::throw_exception, "Throw an exception")
114 .def_static("new_instance", &Example2::new_instance, "Return an instance")
115 .def_readwrite_static("value", &Example2::value, "Static value member")
116 .def_readonly_static("value2", &Example2::value2, "Static value member (readonly)");
117}