Commit 25c03cec by Wenzel Jakob

stl_bind redesign & cleanup pass

parent 26aca3d8
...@@ -108,6 +108,7 @@ set(PYBIND11_HEADERS ...@@ -108,6 +108,7 @@ set(PYBIND11_HEADERS
include/pybind11/pybind11.h include/pybind11/pybind11.h
include/pybind11/pytypes.h include/pybind11/pytypes.h
include/pybind11/stl.h include/pybind11/stl.h
include/pybind11/stl_bind.h
include/pybind11/typeid.h include/pybind11/typeid.h
) )
......
...@@ -99,6 +99,7 @@ Jonas Adler, ...@@ -99,6 +99,7 @@ Jonas Adler,
Sylvain Corlay, Sylvain Corlay,
Axel Huebl, Axel Huebl,
@hulucc, @hulucc,
Sergey Lyskov
Johan Mabille, Johan Mabille,
Tomasz Miąsko, and Tomasz Miąsko, and
Ben Pritchard. Ben Pritchard.
......
/* /*
example/example17.cpp -- Usage of stl_binders functions example/example17.cpp -- Usage of stl_binders functions
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> Copyright (c) 2016 Sergey Lyskov
All rights reserved. Use of this source code is governed by a All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file. BSD-style license that can be found in the LICENSE file.
...@@ -9,29 +9,28 @@ ...@@ -9,29 +9,28 @@
#include "example.h" #include "example.h"
#include <pybind11/stl_binders.h> #include <pybind11/stl_bind.h>
class El {
class A {
public: public:
A() = delete; El() = delete;
A(int v) :a(v) {} El(int v) : a(v) { }
int a; int a;
}; };
std::ostream & operator<<(std::ostream &s, El const&v) {
std::ostream & operator<<(std::ostream &s, A const&v) { s << "El{" << v.a << '}';
s << "A{" << v.a << '}';
return s; return s;
} }
void init_ex17(py::module &m) { void init_ex17(py::module &m) {
pybind11::class_<A>(m, "A") pybind11::class_<El>(m, "El")
.def(pybind11::init<int>()); .def(pybind11::init<int>());
pybind11::vector_binder<int>(m, "VectorInt"); pybind11::bind_vector<int>(m, "VectorInt");
pybind11::bind_vector<El>(m, "VectorEl");
pybind11::vector_binder<A>(m, "VectorA"); pybind11::bind_vector<std::vector<El>>(m, "VectorVectorEl");
} }
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function from __future__ import print_function
from example import VectorInt, VectorA, A from example import VectorInt, El, VectorEl, VectorVectorEl
v_int = VectorInt(2) v_int = VectorInt([0, 0])
print(len(v_int)) print(len(v_int))
print(bool(v_int)) print(bool(v_int))
v_int2 = VectorInt(2) v_int2 = VectorInt([0, 0])
print(v_int == v_int2) print(v_int == v_int2)
v_int2[1] = 1 v_int2[1] = 1
...@@ -24,8 +24,17 @@ print(v_int2) ...@@ -24,8 +24,17 @@ print(v_int2)
v_int.append(99) v_int.append(99)
v_int2[2:-2] = v_int v_int2[2:-2] = v_int
print(v_int2) print(v_int2)
del v_int2[1:3]
print(v_int2)
del v_int2[0]
print(v_int2)
v_a = VectorA() v_a = VectorEl()
v_a.append(A(1)) v_a.append(El(1))
v_a.append(A(2)) v_a.append(El(2))
print(v_a) print(v_a)
vv_a = VectorVectorEl()
vv_a.append(v_a)
vv_b = vv_a[0]
print(vv_b)
...@@ -4,8 +4,7 @@ True ...@@ -4,8 +4,7 @@ True
True True
VectorInt[3, 2, 1, 0, 1, 2, 3] VectorInt[3, 2, 1, 0, 1, 2, 3]
VectorInt[3, 2, 0, 0, 99, 2, 3] VectorInt[3, 2, 0, 0, 99, 2, 3]
A constructor VectorInt[3, 0, 99, 2, 3]
A destructor VectorInt[0, 99, 2, 3]
A constructor VectorEl[El{1}, El{2}]
A destructor VectorEl[El{1}, El{2}]
VectorA[A{1}, A{2}]
...@@ -212,7 +212,7 @@ protected: ...@@ -212,7 +212,7 @@ protected:
rec->is_constructor = !strcmp(rec->name, "__init__"); rec->is_constructor = !strcmp(rec->name, "__init__");
rec->has_args = false; rec->has_args = false;
rec->has_kwargs = false; rec->has_kwargs = false;
rec->nargs = args; rec->nargs = (uint16_t) args;
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
if (rec->sibling && PyMethod_Check(rec->sibling.ptr())) if (rec->sibling && PyMethod_Check(rec->sibling.ptr()))
......
...@@ -24,6 +24,7 @@ setup( ...@@ -24,6 +24,7 @@ setup(
'include/pybind11/numpy.h', 'include/pybind11/numpy.h',
'include/pybind11/pybind11.h', 'include/pybind11/pybind11.h',
'include/pybind11/stl.h', 'include/pybind11/stl.h',
'include/pybind11/stl_bind.h',
'include/pybind11/common.h', 'include/pybind11/common.h',
'include/pybind11/functional.h', 'include/pybind11/functional.h',
'include/pybind11/operators.h', 'include/pybind11/operators.h',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment