Adds type_caster support for std::deque. (#1609)

* Adds std::deque to the types supported by list_caster in stl.h.
* Adds a new test_deque test in test_stl.{py,cpp}.
* Updates the documentation to include std::deque as a default
  supported type.
This commit is contained in:
voxmea 2018-11-16 00:45:19 -05:00 committed by Wenzel Jakob
parent 8f5b7fce84
commit 17983e7425
4 changed files with 18 additions and 1 deletions

View File

@ -5,7 +5,7 @@ Automatic conversion
==================== ====================
When including the additional header file :file:`pybind11/stl.h`, conversions When including the additional header file :file:`pybind11/stl.h`, conversions
between ``std::vector<>``/``std::list<>``/``std::array<>``, between ``std::vector<>``/``std::deque<>``/``std::list<>``/``std::array<>``,
``std::set<>``/``std::unordered_set<>``, and ``std::set<>``/``std::unordered_set<>``, and
``std::map<>``/``std::unordered_map<>`` and the Python ``list``, ``set`` and ``std::map<>``/``std::unordered_map<>`` and the Python ``list``, ``set`` and
``dict`` data structures are automatically enabled. The types ``std::pair<>`` ``dict`` data structures are automatically enabled. The types ``std::pair<>``

View File

@ -16,6 +16,7 @@
#include <unordered_map> #include <unordered_map>
#include <iostream> #include <iostream>
#include <list> #include <list>
#include <deque>
#include <valarray> #include <valarray>
#if defined(_MSC_VER) #if defined(_MSC_VER)
@ -185,6 +186,9 @@ public:
template <typename Type, typename Alloc> struct type_caster<std::vector<Type, Alloc>> template <typename Type, typename Alloc> struct type_caster<std::vector<Type, Alloc>>
: list_caster<std::vector<Type, Alloc>, Type> { }; : list_caster<std::vector<Type, Alloc>, Type> { };
template <typename Type, typename Alloc> struct type_caster<std::deque<Type, Alloc>>
: list_caster<std::deque<Type, Alloc>, Type> { };
template <typename Type, typename Alloc> struct type_caster<std::list<Type, Alloc>> template <typename Type, typename Alloc> struct type_caster<std::list<Type, Alloc>>
: list_caster<std::list<Type, Alloc>, Type> { }; : list_caster<std::list<Type, Alloc>, Type> { };

View File

@ -63,6 +63,10 @@ TEST_SUBMODULE(stl, m) {
static std::vector<RValueCaster> lvv{2}; static std::vector<RValueCaster> lvv{2};
m.def("cast_ptr_vector", []() { return &lvv; }); m.def("cast_ptr_vector", []() { return &lvv; });
// test_deque
m.def("cast_deque", []() { return std::deque<int>{1}; });
m.def("load_deque", [](const std::deque<int> &v) { return v.at(0) == 1 && v.at(1) == 2; });
// test_array // test_array
m.def("cast_array", []() { return std::array<int, 2> {{1 , 2}}; }); m.def("cast_array", []() { return std::array<int, 2> {{1 , 2}}; });
m.def("load_array", [](const std::array<int, 2> &a) { return a[0] == 1 && a[1] == 2; }); m.def("load_array", [](const std::array<int, 2> &a) { return a[0] == 1 && a[1] == 2; });

View File

@ -23,6 +23,15 @@ def test_vector(doc):
assert m.cast_ptr_vector() == ["lvalue", "lvalue"] assert m.cast_ptr_vector() == ["lvalue", "lvalue"]
def test_deque(doc):
"""std::deque <-> list"""
lst = m.cast_deque()
assert lst == [1]
lst.append(2)
assert m.load_deque(lst)
assert m.load_deque(tuple(lst))
def test_array(doc): def test_array(doc):
"""std::array <-> list""" """std::array <-> list"""
lst = m.cast_array() lst = m.cast_array()