summaryrefslogtreecommitdiff
path: root/ext/pybind11/tests/test_stl_binders.cpp
diff options
context:
space:
mode:
authorAndreas Sandberg <andreas.sandberg@arm.com>2017-02-27 13:17:51 +0000
committerAndreas Sandberg <andreas.sandberg@arm.com>2017-05-02 12:37:32 +0000
commitc79706ff4ce591df2151db5504d3c224f3c9965f (patch)
treeb56cd2bfe704a40575a71075e78194a4c516c98d /ext/pybind11/tests/test_stl_binders.cpp
parent359cb08623324b62d7c34973ae54d5bc7f23f9fd (diff)
downloadgem5-c79706ff4ce591df2151db5504d3c224f3c9965f.tar.xz
ext: Add pybind rev f4b81b3
Change-Id: I52e4fc9ebf2f59da57d8cf8f3e37cc79598c2f5f Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com> Reviewed-by: Andreas Hansson <andreas.hansson@arm.com> Reviewed-by: Curtis Dunham <curtis.dunham@arm.com> Reviewed-on: https://gem5-review.googlesource.com/2229 Reviewed-by: Tony Gutierrez <anthony.gutierrez@amd.com> Reviewed-by: Jason Lowe-Power <jason@lowepower.com> Reviewed-by: Pierre-Yves PĂ©neau <pierre-yves.peneau@lirmm.fr>
Diffstat (limited to 'ext/pybind11/tests/test_stl_binders.cpp')
-rw-r--r--ext/pybind11/tests/test_stl_binders.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/ext/pybind11/tests/test_stl_binders.cpp b/ext/pybind11/tests/test_stl_binders.cpp
new file mode 100644
index 000000000..b9b56c15d
--- /dev/null
+++ b/ext/pybind11/tests/test_stl_binders.cpp
@@ -0,0 +1,95 @@
+/*
+ tests/test_stl_binders.cpp -- Usage of stl_binders functions
+
+ Copyright (c) 2016 Sergey Lyskov
+
+ All rights reserved. Use of this source code is governed by a
+ BSD-style license that can be found in the LICENSE file.
+*/
+
+#include "pybind11_tests.h"
+
+#include <pybind11/stl_bind.h>
+#include <map>
+#include <deque>
+#include <unordered_map>
+
+class El {
+public:
+ El() = delete;
+ El(int v) : a(v) { }
+
+ int a;
+};
+
+std::ostream & operator<<(std::ostream &s, El const&v) {
+ s << "El{" << v.a << '}';
+ return s;
+}
+
+/// Issue #487: binding std::vector<E> with E non-copyable
+class E_nc {
+public:
+ explicit E_nc(int i) : value{i} {}
+ E_nc(const E_nc &) = delete;
+ E_nc &operator=(const E_nc &) = delete;
+ E_nc(E_nc &&) = default;
+ E_nc &operator=(E_nc &&) = default;
+
+ int value;
+};
+
+template <class Container> Container *one_to_n(int n) {
+ auto v = new Container();
+ for (int i = 1; i <= n; i++)
+ v->emplace_back(i);
+ return v;
+}
+
+template <class Map> Map *times_ten(int n) {
+ auto m = new Map();
+ for (int i = 1; i <= n; i++)
+ m->emplace(int(i), E_nc(10*i));
+ return m;
+}
+
+test_initializer stl_binder_vector([](py::module &m) {
+ py::class_<El>(m, "El")
+ .def(py::init<int>());
+
+ py::bind_vector<std::vector<unsigned int>>(m, "VectorInt");
+ py::bind_vector<std::vector<bool>>(m, "VectorBool");
+
+ py::bind_vector<std::vector<El>>(m, "VectorEl");
+
+ py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
+
+});
+
+test_initializer stl_binder_map([](py::module &m) {
+ py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
+ py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
+
+ py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
+ py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");
+
+});
+
+test_initializer stl_binder_noncopyable([](py::module &m) {
+ py::class_<E_nc>(m, "ENC")
+ .def(py::init<int>())
+ .def_readwrite("value", &E_nc::value);
+
+ py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
+ m.def("get_vnc", &one_to_n<std::vector<E_nc>>, py::return_value_policy::reference);
+
+ py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
+ m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference);
+
+ py::bind_map<std::map<int, E_nc>>(m, "MapENC");
+ m.def("get_mnc", &times_ten<std::map<int, E_nc>>, py::return_value_policy::reference);
+
+ py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
+ m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference);
+});
+