Improved STL support, support for std::set
diff --git a/example/example2.cpp b/example/example2.cpp
index f7972e0..2e20e8b 100644
--- a/example/example2.cpp
+++ b/example/example2.cpp
@@ -27,6 +27,14 @@
return dict;
}
+ /* Create and return a Python set */
+ py::set get_set() {
+ py::set set;
+ set.insert(py::str("key1"));
+ set.insert(py::str("key2"));
+ return set;
+ }
+
/* Create and return a C++ dictionary */
std::map<std::string, std::string> get_dict_2() {
std::map<std::string, std::string> result;
@@ -34,6 +42,14 @@
return result;
}
+ /* 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;
+ }
+
/* Create, manipulate, and return a Python list */
py::list get_list() {
py::list list;
@@ -62,6 +78,18 @@
std::cout << "key: " << item.first << ", value=" << item.second << std::endl;
}
+ /* Easily iterate over a setionary using a C++11 range-based for loop */
+ void print_set(py::set set) {
+ for (auto item : set)
+ std::cout << "key: " << item << std::endl;
+ }
+
+ /* STL data types 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;
+ }
+
/* Easily iterate over a list using a C++11 range-based for loop */
void print_list(py::list list) {
int index = 0;
@@ -105,8 +133,12 @@
.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")
+ .def("get_set", &Example2::get_set, "Return a Python set")
+ .def("get_set2", &Example2::get_set, "Return a C++ set")
.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("pair_passthrough", &Example2::pair_passthrough, "Return a pair in reversed order")
diff --git a/example/example2.py b/example/example2.py
index 2782da5..f42ee49 100755
--- a/example/example2.py
+++ b/example/example2.py
@@ -29,6 +29,14 @@
dict_result['key2'] = 'value2'
instance.print_dict_2(dict_result)
+set_result = instance.get_set()
+set_result.add(u'key3')
+instance.print_set(set_result)
+
+set_result = instance.get_set2()
+set_result.add(u'key3')
+instance.print_set_2(set_result)
+
list_result = instance.get_list()
list_result.append('value2')
instance.print_list(list_result)
diff --git a/example/example2.ref b/example/example2.ref
index 341fcb2..64d2afa 100644
--- a/example/example2.ref
+++ b/example/example2.ref
@@ -6,6 +6,12 @@
key: key, value=value
key: key, value=value
key: key2, value=value2
+key: key3
+key: key2
+key: key1
+key: key1
+key: key2
+key: key3
Entry at positon 0: value
list item 0: overwritten
list item 1: value2
@@ -44,6 +50,16 @@
|
| Return a C++ list
|
+ | ggeett__sseett(...)
+ | Signature : (Example2) -> set
+ |
+ | Return a Python set
+ |
+ | ggeett__sseett22(...)
+ | Signature : (Example2) -> set
+ |
+ | Return a C++ set
+ |
| ppaaiirr__ppaasssstthhrroouugghh(...)
| Signature : (Example2, (bool, str)) -> (str, bool)
|
@@ -69,6 +85,16 @@
|
| Print entries of a C++ list
|
+ | pprriinntt__sseett(...)
+ | Signature : (Example2, set) -> None
+ |
+ | Print entries of a Python set
+ |
+ | pprriinntt__sseett__22(...)
+ | Signature : (Example2, set<str>) -> None
+ |
+ | Print entries of a C++ set
+ |
| tthhrrooww__eexxcceeppttiioonn(...)
| Signature : (Example2) -> None
|
@@ -85,7 +111,7 @@
| ____nneeww____ = <built-in method __new__ of Example2_meta object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
|
- | ____ppyybbiinndd____ = <capsule object NULL>
+ | ____ppyybbiinndd1111____ = <capsule object NULL>
|
| nneeww__iinnssttaannccee = <built-in method new_instance of PyCapsule object>
| Signature : () -> Example2