test_stl.cpp (12391:ceeca8b41e4b) | test_stl.cpp (14299:2fbea9df56d2) |
---|---|
1/* 2 tests/test_stl.cpp -- STL type casters 3 4 Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch> 5 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 10#include "pybind11_tests.h" | 1/* 2 tests/test_stl.cpp -- STL type casters 3 4 Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch> 5 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 10#include "pybind11_tests.h" |
11#include "constructor_stats.h" |
|
11#include <pybind11/stl.h> 12 | 12#include <pybind11/stl.h> 13 |
14#include <vector> 15#include <string> 16 |
|
13// Test with `std::variant` in C++17 mode, or with `boost::variant` in C++11/14 14#if PYBIND11_HAS_VARIANT 15using std::variant; 16#elif defined(PYBIND11_TEST_BOOST) && (!defined(_MSC_VER) || _MSC_VER >= 1910) 17# include <boost/variant.hpp> 18# define PYBIND11_HAS_VARIANT 1 19using boost::variant; 20 --- 6 unchanged lines hidden (view full) --- 27 template <typename... Args> 28 static auto call(Args &&...args) -> decltype(boost::apply_visitor(args...)) { 29 return boost::apply_visitor(args...); 30 } 31}; 32}} // namespace pybind11::detail 33#endif 34 | 17// Test with `std::variant` in C++17 mode, or with `boost::variant` in C++11/14 18#if PYBIND11_HAS_VARIANT 19using std::variant; 20#elif defined(PYBIND11_TEST_BOOST) && (!defined(_MSC_VER) || _MSC_VER >= 1910) 21# include <boost/variant.hpp> 22# define PYBIND11_HAS_VARIANT 1 23using boost::variant; 24 --- 6 unchanged lines hidden (view full) --- 31 template <typename... Args> 32 static auto call(Args &&...args) -> decltype(boost::apply_visitor(args...)) { 33 return boost::apply_visitor(args...); 34 } 35}; 36}} // namespace pybind11::detail 37#endif 38 |
39PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>); 40 |
|
35/// Issue #528: templated constructor 36struct TplCtorClass { 37 template <typename T> TplCtorClass(const T &) { } 38 bool operator==(const TplCtorClass &) const { return true; } 39}; 40 41namespace std { 42 template <> --- 9 unchanged lines hidden (view full) --- 52 m.def("cast_bool_vector", []() { return std::vector<bool>{true, false}; }); 53 m.def("load_bool_vector", [](const std::vector<bool> &v) { 54 return v.at(0) == true && v.at(1) == false; 55 }); 56 // Unnumbered regression (caused by #936): pointers to stl containers aren't castable 57 static std::vector<RValueCaster> lvv{2}; 58 m.def("cast_ptr_vector", []() { return &lvv; }); 59 | 41/// Issue #528: templated constructor 42struct TplCtorClass { 43 template <typename T> TplCtorClass(const T &) { } 44 bool operator==(const TplCtorClass &) const { return true; } 45}; 46 47namespace std { 48 template <> --- 9 unchanged lines hidden (view full) --- 58 m.def("cast_bool_vector", []() { return std::vector<bool>{true, false}; }); 59 m.def("load_bool_vector", [](const std::vector<bool> &v) { 60 return v.at(0) == true && v.at(1) == false; 61 }); 62 // Unnumbered regression (caused by #936): pointers to stl containers aren't castable 63 static std::vector<RValueCaster> lvv{2}; 64 m.def("cast_ptr_vector", []() { return &lvv; }); 65 |
66 // test_deque 67 m.def("cast_deque", []() { return std::deque<int>{1}; }); 68 m.def("load_deque", [](const std::deque<int> &v) { return v.at(0) == 1 && v.at(1) == 2; }); 69 |
|
60 // test_array 61 m.def("cast_array", []() { return std::array<int, 2> {{1 , 2}}; }); 62 m.def("load_array", [](const std::array<int, 2> &a) { return a[0] == 1 && a[1] == 2; }); 63 64 // test_valarray 65 m.def("cast_valarray", []() { return std::valarray<int>{1, 4, 9}; }); 66 m.def("load_valarray", [](const std::valarray<int>& v) { 67 return v.size() == 3 && v[0] == 1 && v[1] == 4 && v[2] == 9; --- 162 unchanged lines hidden (view full) --- 230 static UserType p1{1}, p2{2}, p3{3}; 231 return std::vector<std::reference_wrapper<UserType>> { 232 std::ref(p1), std::ref(p2), std::ref(p3), p4 233 }; 234 }); 235 236 // test_stl_pass_by_pointer 237 m.def("stl_pass_by_pointer", [](std::vector<int>* v) { return *v; }, "v"_a=nullptr); | 70 // test_array 71 m.def("cast_array", []() { return std::array<int, 2> {{1 , 2}}; }); 72 m.def("load_array", [](const std::array<int, 2> &a) { return a[0] == 1 && a[1] == 2; }); 73 74 // test_valarray 75 m.def("cast_valarray", []() { return std::valarray<int>{1, 4, 9}; }); 76 m.def("load_valarray", [](const std::valarray<int>& v) { 77 return v.size() == 3 && v[0] == 1 && v[1] == 4 && v[2] == 9; --- 162 unchanged lines hidden (view full) --- 240 static UserType p1{1}, p2{2}, p3{3}; 241 return std::vector<std::reference_wrapper<UserType>> { 242 std::ref(p1), std::ref(p2), std::ref(p3), p4 243 }; 244 }); 245 246 // test_stl_pass_by_pointer 247 m.def("stl_pass_by_pointer", [](std::vector<int>* v) { return *v; }, "v"_a=nullptr); |
248 249 // #1258: pybind11/stl.h converts string to vector<string> 250 m.def("func_with_string_or_vector_string_arg_overload", [](std::vector<std::string>) { return 1; }); 251 m.def("func_with_string_or_vector_string_arg_overload", [](std::list<std::string>) { return 2; }); 252 m.def("func_with_string_or_vector_string_arg_overload", [](std::string) { return 3; }); 253 254 class Placeholder { 255 public: 256 Placeholder() { print_created(this); } 257 Placeholder(const Placeholder &) = delete; 258 ~Placeholder() { print_destroyed(this); } 259 }; 260 py::class_<Placeholder>(m, "Placeholder"); 261 262 /// test_stl_vector_ownership 263 m.def("test_stl_ownership", 264 []() { 265 std::vector<Placeholder *> result; 266 result.push_back(new Placeholder()); 267 return result; 268 }, 269 py::return_value_policy::take_ownership); 270 271 m.def("array_cast_sequence", [](std::array<int, 3> x) { return x; }); 272 273 /// test_issue_1561 274 struct Issue1561Inner { std::string data; }; 275 struct Issue1561Outer { std::vector<Issue1561Inner> list; }; 276 277 py::class_<Issue1561Inner>(m, "Issue1561Inner") 278 .def(py::init<std::string>()) 279 .def_readwrite("data", &Issue1561Inner::data); 280 281 py::class_<Issue1561Outer>(m, "Issue1561Outer") 282 .def(py::init<>()) 283 .def_readwrite("list", &Issue1561Outer::list); |
|
238} | 284} |