transparent std::array conversion (fixes #97)
diff --git a/example/example2.cpp b/example/example2.cpp
index c4e0d7c..3282a85 100644
--- a/example/example2.cpp
+++ b/example/example2.cpp
@@ -66,6 +66,11 @@
return list;
}
+ /* 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"}};
+ }
+
/* Easily iterate over a dictionary using a C++11 range-based for loop */
void print_dict(py::dict dict) {
for (auto item : dict)
@@ -114,6 +119,13 @@
return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input));
}
+ /* 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;
+ }
+
void throw_exception() {
throw std::runtime_error("This exception was intentionally thrown.");
}
@@ -135,12 +147,14 @@
.def("get_list_2", &Example2::get_list_2, "Return a C++ list")
.def("get_set", &Example2::get_set, "Return a Python set")
.def("get_set2", &Example2::get_set, "Return a C++ set")
+ .def("get_array", &Example2::get_array, "Return a C++ array")
.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")
.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")
.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")
+ .def("print_array", &Example2::print_array, "Print entries of a C++ array")
.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")
diff --git a/example/example2.py b/example/example2.py
index f8bda4d..d335acc 100755
--- a/example/example2.py
+++ b/example/example2.py
@@ -46,6 +46,10 @@
list_result.append('value2')
instance.print_list_2(list_result)
+array_result = instance.get_array()
+print(array_result)
+instance.print_array(array_result)
+
try:
instance.throw_exception()
except Exception as e:
diff --git a/example/example2.ref b/example/example2.ref
index 08fb7cc..fd6c83d 100644
--- a/example/example2.ref
+++ b/example/example2.ref
@@ -17,6 +17,9 @@
list item 1: value2
list item 0: value
list item 1: value2
+[u'array entry 1', u'array entry 2']
+array item 0: array entry 1
+array item 1: array entry 2
This exception was intentionally thrown.
(u'test', True)
(5L, u'test', True)
@@ -30,6 +33,11 @@
| ____iinniitt____(...)
| x.__init__(...) initializes x; see help(type(x)) for signature
|
+ | ggeett__aarrrraayy(...)
+ | Signature : (example.Example2) -> list<unicode>[2]
+ |
+ | Return a C++ array
+ |
| ggeett__ddiicctt(...)
| Signature : (example.Example2) -> dict
|
@@ -65,6 +73,11 @@
|
| Return a pair in reversed order
|
+ | pprriinntt__aarrrraayy(...)
+ | Signature : (example.Example2, list<unicode>[2]) -> NoneType
+ |
+ | Print entries of a C++ array
+ |
| pprriinntt__ddiicctt(...)
| Signature : (example.Example2, dict) -> NoneType
|