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