opaque type redesign
diff --git a/example/example14.cpp b/example/example14.cpp
index 97b4d1f..754424e 100644
--- a/example/example14.cpp
+++ b/example/example14.cpp
@@ -18,22 +18,26 @@
StringList stringList;
};
+/* IMPORTANT: Disable internal pybind11 translation mechanisms for STL data structures */
+PYBIND11_MAKE_OPAQUE(StringList);
+
void init_ex14(py::module &m) {
- py::class_<py::opaque<StringList>>(m, "StringList")
+ py::class_<StringList>(m, "StringList")
.def(py::init<>())
- .def("push_back", [](py::opaque<StringList> &l, const std::string &str) { l->push_back(str); })
- .def("pop_back", [](py::opaque<StringList> &l) { l->pop_back(); })
- .def("back", [](py::opaque<StringList> &l) { return l->back(); });
+ .def("pop_back", &StringList::pop_back)
+ /* There are multiple versions of push_back(), etc. Select the right ones. */
+ .def("push_back", (void (StringList::*)(const std::string &)) &StringList::push_back)
+ .def("back", (std::string &(StringList::*)()) &StringList::back)
+ .def("__len__", [](const StringList &v) { return v.size(); })
+ .def("__iter__", [](StringList &v) {
+ return py::make_iterator(v.begin(), v.end());
+ }, py::keep_alive<0, 1>());
py::class_<ClassWithSTLVecProperty>(m, "ClassWithSTLVecProperty")
.def(py::init<>())
- /* Need to cast properties to opaque types to avoid pybind11-internal
- STL conversion code from becoming active */
- .def_readwrite("stringList", (py::opaque<StringList> ClassWithSTLVecProperty:: *)
- &ClassWithSTLVecProperty::stringList);
+ .def_readwrite("stringList", &ClassWithSTLVecProperty::stringList);
- m.def("print_opaque_list", [](py::opaque<StringList> &_l) {
- StringList &l = _l;
+ m.def("print_opaque_list", [](const StringList &l) {
std::cout << "Opaque list: [";
bool first = true;
for (auto entry : l) {
diff --git a/example/example14.py b/example/example14.py
index 95d014d..6bca45b 100644
--- a/example/example14.py
+++ b/example/example14.py
@@ -16,6 +16,8 @@
l.push_back("Element 2")
print_opaque_list(l)
print("Back element is %s" % l.back())
+for i, k in enumerate(l):
+ print("%i/%i : %s" % (i + 1, len(l), k))
l.pop_back()
print_opaque_list(l)
@@ -36,4 +38,6 @@
#####
-print(return_unique_ptr())
+ptr = return_unique_ptr()
+print(ptr)
+print_opaque_list(ptr)
diff --git a/example/example14.ref b/example/example14.ref
index 63c46b7..7c67b38 100644
--- a/example/example14.ref
+++ b/example/example14.ref
@@ -1,9 +1,12 @@
Opaque list: [Element 1, Element 2]
Back element is Element 2
+1/2 : Element 1
+2/2 : Element 2
Opaque list: [Element 1]
Opaque list: []
Opaque list: [Element 1, Element 3]
Got void ptr : 1234
None
Got null str : 0
-[u'some value']
+<example.StringList object at 0x104a47500>
+Opaque list: [some value]