blob: e390376dcfba45b5bb96a428eb649b9b84e569eb [file] [log] [blame]
Sergey Lyskoveae77442016-05-07 00:26:19 -04001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_stl_binders.cpp -- Usage of stl_binders functions
Sergey Lyskoveae77442016-05-07 00:26:19 -04003
Wenzel Jakob25c03ce2016-05-15 20:50:38 +02004 Copyright (c) 2016 Sergey Lyskov
Sergey Lyskoveae77442016-05-07 00:26:19 -04005
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/
9
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020010#include "pybind11_tests.h"
Sergey Lyskoveae77442016-05-07 00:26:19 -040011
Wenzel Jakob25c03ce2016-05-15 20:50:38 +020012#include <pybind11/stl_bind.h>
Wenzel Jakobfe342412016-09-06 13:02:29 +090013#include <map>
14#include <unordered_map>
Sergey Lyskoveae77442016-05-07 00:26:19 -040015
Wenzel Jakob25c03ce2016-05-15 20:50:38 +020016class El {
Sergey Lyskoveae77442016-05-07 00:26:19 -040017public:
Jason Rhinelander540ae612016-08-28 13:46:25 -040018 El() = delete;
19 El(int v) : a(v) { }
Sergey Lyskova95bde12016-05-08 19:31:55 -040020
Jason Rhinelander540ae612016-08-28 13:46:25 -040021 int a;
Sergey Lyskoveae77442016-05-07 00:26:19 -040022};
23
Wenzel Jakob25c03ce2016-05-15 20:50:38 +020024std::ostream & operator<<(std::ostream &s, El const&v) {
Jason Rhinelander540ae612016-08-28 13:46:25 -040025 s << "El{" << v.a << '}';
26 return s;
Sergey Lyskova95bde12016-05-08 19:31:55 -040027}
Sergey Lyskoveae77442016-05-07 00:26:19 -040028
Jason Rhinelander52f4be82016-09-03 14:54:22 -040029test_initializer stl_binder_vector([](py::module &m) {
Jason Rhinelander540ae612016-08-28 13:46:25 -040030 py::class_<El>(m, "El")
31 .def(py::init<int>());
Sergey Lyskova95bde12016-05-08 19:31:55 -040032
Wenzel Jakobfe342412016-09-06 13:02:29 +090033 py::bind_vector<std::vector<unsigned int>>(m, "VectorInt");
34 py::bind_vector<std::vector<bool>>(m, "VectorBool");
Sergey Lyskova95bde12016-05-08 19:31:55 -040035
Wenzel Jakobfe342412016-09-06 13:02:29 +090036 py::bind_vector<std::vector<El>>(m, "VectorEl");
Wenzel Jakob25c03ce2016-05-15 20:50:38 +020037
Wenzel Jakobfe342412016-09-06 13:02:29 +090038 py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
Sergey Lyskov75204182016-08-29 22:50:38 -040039});
40
41test_initializer stl_binder_map([](py::module &m) {
Wenzel Jakobfe342412016-09-06 13:02:29 +090042 py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
43 py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
Sergey Lyskov75204182016-08-29 22:50:38 -040044
Wenzel Jakobfe342412016-09-06 13:02:29 +090045 py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
46 py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");
Jason Rhinelander52f4be82016-09-03 14:54:22 -040047});