2015-07-05 18:05:44 +00:00
|
|
|
/*
|
2015-07-29 15:51:54 +00:00
|
|
|
example/example2.cpp2 -- singleton design pattern, static functions and
|
|
|
|
variables, passing and interacting with Python types
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2016-04-17 18:21:41 +00:00
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
2015-07-05 18:05:44 +00:00
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "example.h"
|
2015-10-15 16:13:33 +00:00
|
|
|
#include <pybind11/stl.h>
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2016-03-31 12:22:37 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <io.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
class Example2 {
|
|
|
|
public:
|
|
|
|
static Example2 *new_instance() {
|
|
|
|
return new Example2();
|
|
|
|
}
|
|
|
|
~Example2() {
|
|
|
|
std::cout << "Destructing Example2" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create and return a Python dictionary */
|
|
|
|
py::dict get_dict() {
|
|
|
|
py::dict dict;
|
|
|
|
dict[py::str("key")] = py::str("value");
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:04:49 +00:00
|
|
|
/* Create and return a Python set */
|
|
|
|
py::set get_set() {
|
|
|
|
py::set set;
|
2015-11-15 12:03:07 +00:00
|
|
|
set.add(py::str("key1"));
|
|
|
|
set.add(py::str("key2"));
|
2015-11-14 18:04:49 +00:00
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
/* Create and return a C++ dictionary */
|
|
|
|
std::map<std::string, std::string> get_dict_2() {
|
|
|
|
std::map<std::string, std::string> result;
|
|
|
|
result["key"] = "value";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:04:49 +00:00
|
|
|
/* Create and return a C++ set */
|
|
|
|
std::set<std::string> get_set_2() {
|
|
|
|
std::set<std::string> result;
|
|
|
|
result.insert("key1");
|
|
|
|
result.insert("key2");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
/* Create, manipulate, and return a Python list */
|
|
|
|
py::list get_list() {
|
|
|
|
py::list list;
|
|
|
|
list.append(py::str("value"));
|
|
|
|
cout << "Entry at positon 0: " << py::object(list[0]) << endl;
|
|
|
|
list[0] = py::str("overwritten");
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* C++ STL data types are automatically casted */
|
2016-03-08 18:40:32 +00:00
|
|
|
std::vector<std::wstring> get_list_2() {
|
|
|
|
std::vector<std::wstring> list;
|
|
|
|
list.push_back(L"value");
|
2015-07-05 18:05:44 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2016-02-07 15:36:26 +00:00
|
|
|
/* C++ STL data types are automatically casted */
|
|
|
|
std::array<std::string, 2> get_array() {
|
|
|
|
return std::array<std::string, 2> {{ "array entry 1" , "array entry 2"}};
|
|
|
|
}
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
/* Easily iterate over a dictionary using a C++11 range-based for loop */
|
|
|
|
void print_dict(py::dict dict) {
|
|
|
|
for (auto item : dict)
|
|
|
|
std::cout << "key: " << item.first << ", value=" << item.second << std::endl;
|
|
|
|
}
|
|
|
|
|
2016-01-17 21:36:37 +00:00
|
|
|
/* Easily iterate over a set using a C++11 range-based for loop */
|
2015-11-14 18:04:49 +00:00
|
|
|
void print_set(py::set set) {
|
|
|
|
for (auto item : set)
|
|
|
|
std::cout << "key: " << item << std::endl;
|
|
|
|
}
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
/* Easily iterate over a list using a C++11 range-based for loop */
|
|
|
|
void print_list(py::list list) {
|
|
|
|
int index = 0;
|
|
|
|
for (auto item : list)
|
|
|
|
std::cout << "list item " << index++ << ": " << item << std::endl;
|
|
|
|
}
|
|
|
|
|
2016-01-17 21:36:37 +00:00
|
|
|
/* STL data types (such as maps) are automatically casted from Python */
|
|
|
|
void print_dict_2(const std::map<std::string, std::string> &dict) {
|
|
|
|
for (auto item : dict)
|
|
|
|
std::cout << "key: " << item.first << ", value=" << item.second << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* STL data types (such as sets) are automatically casted from Python */
|
|
|
|
void print_set_2(const std::set<std::string> &set) {
|
|
|
|
for (auto item : set)
|
|
|
|
std::cout << "key: " << item << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* STL data types (such as vectors) are automatically casted from Python */
|
2016-03-08 18:40:32 +00:00
|
|
|
void print_list_2(std::vector<std::wstring> &list) {
|
2016-03-31 12:22:37 +00:00
|
|
|
#ifdef _WIN32 /* Can't easily mix cout and wcout on Windows */
|
|
|
|
_setmode(_fileno(stdout), _O_TEXT);
|
|
|
|
#endif
|
2015-07-05 18:05:44 +00:00
|
|
|
int index = 0;
|
|
|
|
for (auto item : list)
|
2016-03-08 18:40:32 +00:00
|
|
|
std::wcout << L"list item " << index++ << L": " << item << std::endl;
|
2015-07-05 18:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* pybind automatically translates between C++11 and Python tuples */
|
|
|
|
std::pair<std::string, bool> pair_passthrough(std::pair<bool, std::string> input) {
|
|
|
|
return std::make_pair(input.second, input.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pybind automatically translates between C++11 and Python tuples */
|
|
|
|
std::tuple<int, std::string, bool> tuple_passthrough(std::tuple<bool, std::string, int> input) {
|
|
|
|
return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input));
|
|
|
|
}
|
|
|
|
|
2016-02-07 15:36:26 +00:00
|
|
|
/* STL data types (such as arrays) are automatically casted from Python */
|
|
|
|
void print_array(std::array<std::string, 2> &array) {
|
|
|
|
int index = 0;
|
|
|
|
for (auto item : array)
|
|
|
|
std::cout << "array item " << index++ << ": " << item << std::endl;
|
|
|
|
}
|
|
|
|
|
2015-07-05 18:05:44 +00:00
|
|
|
void throw_exception() {
|
|
|
|
throw std::runtime_error("This exception was intentionally thrown.");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int value;
|
|
|
|
static const int value2;
|
|
|
|
};
|
|
|
|
|
|
|
|
int Example2::value = 0;
|
|
|
|
const int Example2::value2 = 5;
|
|
|
|
|
|
|
|
void init_ex2(py::module &m) {
|
|
|
|
/* No constructor is explicitly defined below. An exception is raised when
|
|
|
|
trying to construct it directly from Python */
|
2015-09-05 00:09:17 +00:00
|
|
|
py::class_<Example2>(m, "Example2", "Example 2 documentation")
|
2015-07-05 18:05:44 +00:00
|
|
|
.def("get_dict", &Example2::get_dict, "Return a Python dictionary")
|
|
|
|
.def("get_dict_2", &Example2::get_dict_2, "Return a C++ dictionary")
|
|
|
|
.def("get_list", &Example2::get_list, "Return a Python list")
|
|
|
|
.def("get_list_2", &Example2::get_list_2, "Return a C++ list")
|
2015-11-14 18:04:49 +00:00
|
|
|
.def("get_set", &Example2::get_set, "Return a Python set")
|
|
|
|
.def("get_set2", &Example2::get_set, "Return a C++ set")
|
2016-02-07 15:36:26 +00:00
|
|
|
.def("get_array", &Example2::get_array, "Return a C++ array")
|
2015-07-05 18:05:44 +00:00
|
|
|
.def("print_dict", &Example2::print_dict, "Print entries of a Python dictionary")
|
|
|
|
.def("print_dict_2", &Example2::print_dict_2, "Print entries of a C++ dictionary")
|
2015-11-14 18:04:49 +00:00
|
|
|
.def("print_set", &Example2::print_set, "Print entries of a Python set")
|
|
|
|
.def("print_set_2", &Example2::print_set_2, "Print entries of a C++ set")
|
2015-07-05 18:05:44 +00:00
|
|
|
.def("print_list", &Example2::print_list, "Print entries of a Python list")
|
|
|
|
.def("print_list_2", &Example2::print_list_2, "Print entries of a C++ list")
|
2016-02-07 15:36:26 +00:00
|
|
|
.def("print_array", &Example2::print_array, "Print entries of a C++ array")
|
2015-07-05 18:05:44 +00:00
|
|
|
.def("pair_passthrough", &Example2::pair_passthrough, "Return a pair in reversed order")
|
|
|
|
.def("tuple_passthrough", &Example2::tuple_passthrough, "Return a triple in reversed order")
|
|
|
|
.def("throw_exception", &Example2::throw_exception, "Throw an exception")
|
|
|
|
.def_static("new_instance", &Example2::new_instance, "Return an instance")
|
|
|
|
.def_readwrite_static("value", &Example2::value, "Static value member")
|
|
|
|
.def_readonly_static("value2", &Example2::value2, "Static value member (readonly)");
|
|
|
|
}
|