diff --git a/docs/basics.rst b/docs/basics.rst index ac7062ce5..7c2b4ee44 100644 --- a/docs/basics.rst +++ b/docs/basics.rst @@ -251,6 +251,8 @@ as arguments and return values, refer to the section on binding :ref:`classes`. +------------------------+--------------------------+-----------------------+ | std::complex | Complex numbers | pybind11/complex.h | +------------------------+--------------------------+-----------------------+ +| std::array | STL static array | pybind11/stl.h | ++------------------------+--------------------------+-----------------------+ | std::vector | STL dynamic array | pybind11/stl.h | +------------------------+--------------------------+-----------------------+ | std::map | STL ordered map | pybind11/stl.h | diff --git a/example/example2.cpp b/example/example2.cpp index c4e0d7cd0..3282a85dd 100644 --- a/example/example2.cpp +++ b/example/example2.cpp @@ -66,6 +66,11 @@ public: return list; } + /* C++ STL data types are automatically casted */ + std::array get_array() { + return std::array {{ "array entry 1" , "array entry 2"}}; + } + /* Easily iterate over a dictionary using a C++11 range-based for loop */ void print_dict(py::dict dict) { for (auto item : dict) @@ -114,6 +119,13 @@ public: return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input)); } + /* STL data types (such as arrays) are automatically casted from Python */ + void print_array(std::array &array) { + int index = 0; + for (auto item : array) + std::cout << "array item " << index++ << ": " << item << std::endl; + } + void throw_exception() { throw std::runtime_error("This exception was intentionally thrown."); } @@ -135,12 +147,14 @@ void init_ex2(py::module &m) { .def("get_list_2", &Example2::get_list_2, "Return a C++ list") .def("get_set", &Example2::get_set, "Return a Python set") .def("get_set2", &Example2::get_set, "Return a C++ set") + .def("get_array", &Example2::get_array, "Return a C++ array") .def("print_dict", &Example2::print_dict, "Print entries of a Python dictionary") .def("print_dict_2", &Example2::print_dict_2, "Print entries of a C++ dictionary") .def("print_set", &Example2::print_set, "Print entries of a Python set") .def("print_set_2", &Example2::print_set_2, "Print entries of a C++ set") .def("print_list", &Example2::print_list, "Print entries of a Python list") .def("print_list_2", &Example2::print_list_2, "Print entries of a C++ list") + .def("print_array", &Example2::print_array, "Print entries of a C++ array") .def("pair_passthrough", &Example2::pair_passthrough, "Return a pair in reversed order") .def("tuple_passthrough", &Example2::tuple_passthrough, "Return a triple in reversed order") .def("throw_exception", &Example2::throw_exception, "Throw an exception") diff --git a/example/example2.py b/example/example2.py index f8bda4d58..d335acc3d 100755 --- a/example/example2.py +++ b/example/example2.py @@ -46,6 +46,10 @@ list_result = instance.get_list_2() list_result.append('value2') instance.print_list_2(list_result) +array_result = instance.get_array() +print(array_result) +instance.print_array(array_result) + try: instance.throw_exception() except Exception as e: diff --git a/example/example2.ref b/example/example2.ref index 08fb7ccc0..fd6c83dda 100644 --- a/example/example2.ref +++ b/example/example2.ref @@ -17,6 +17,9 @@ list item 0: overwritten list item 1: value2 list item 0: value list item 1: value2 +[u'array entry 1', u'array entry 2'] +array item 0: array entry 1 +array item 1: array entry 2 This exception was intentionally thrown. (u'test', True) (5L, u'test', True) @@ -30,6 +33,11 @@ class EExxaammppllee22(__builtin__.object) | ____iinniitt____(...) | x.__init__(...) initializes x; see help(type(x)) for signature | + | ggeett__aarrrraayy(...) + | Signature : (example.Example2) -> list[2] + | + | Return a C++ array + | | ggeett__ddiicctt(...) | Signature : (example.Example2) -> dict | @@ -65,6 +73,11 @@ class EExxaammppllee22(__builtin__.object) | | Return a pair in reversed order | + | pprriinntt__aarrrraayy(...) + | Signature : (example.Example2, list[2]) -> NoneType + | + | Print entries of a C++ array + | | pprriinntt__ddiicctt(...) | Signature : (example.Example2, dict) -> NoneType | diff --git a/include/pybind11/descr.h b/include/pybind11/descr.h index ed44b2626..b040014dc 100644 --- a/include/pybind11/descr.h +++ b/include/pybind11/descr.h @@ -78,6 +78,15 @@ template constexpr descr _(char const(&text)[Size]) { return descr(text, { nullptr }); } +template struct int_to_str : int_to_str { }; +template struct int_to_str<0, Digits...> { + static constexpr auto digits = descr({ ('0' + Digits)..., '\0' }, { nullptr }); +}; + +template auto constexpr _() { + return int_to_str::digits; +} + template constexpr descr<1, 1> _() { return descr<1, 1>({ '%', '\0' }, { &typeid(Type), nullptr }); } @@ -149,6 +158,11 @@ template PYBIND11_NOINLINE descr _() { return descr("%", types); } +template PYBIND11_NOINLINE descr _() { + const std::type_info *types[1] = { nullptr }; + return descr(std::to_string(Size).c_str(), types); +} + PYBIND11_NOINLINE inline descr concat() { return _(""); } PYBIND11_NOINLINE inline descr concat(descr &&d) { return d; } template PYBIND11_NOINLINE descr concat(descr &&d, Args&&... args) { return std::move(d) + _(", ") + concat(std::forward(args)...); } diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 231c80840..dcb96e046 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -22,9 +22,9 @@ NAMESPACE_BEGIN(pybind11) NAMESPACE_BEGIN(detail) -template struct type_caster> { - typedef std::vector type; - typedef type_caster value_conv; +template struct type_caster> { + typedef std::vector vector_type; + typedef type_caster value_conv; public: bool load(handle src, bool convert) { list l(src, true); @@ -36,12 +36,12 @@ public: for (auto it : l) { if (!conv.load(it, convert)) return false; - value.push_back((Value) conv); + value.push_back((Type) conv); } return true; } - static handle cast(const type &src, return_value_policy policy, handle parent) { + static handle cast(const vector_type &src, return_value_policy policy, handle parent) { list l(src.size()); size_t index = 0; for (auto const &value: src) { @@ -52,7 +52,41 @@ public: } return l.release(); } - PYBIND11_TYPE_CASTER(type, _("list<") + value_conv::name() + _(">")); + PYBIND11_TYPE_CASTER(vector_type, _("list<") + value_conv::name() + _(">")); +}; + +template struct type_caster> { + typedef std::array array_type; + typedef type_caster value_conv; +public: + bool load(handle src, bool convert) { + list l(src, true); + if (!l.check()) + return false; + if (l.size() != Size) + return false; + value_conv conv; + size_t ctr = 0; + for (auto it : l) { + if (!conv.load(it, convert)) + return false; + value[ctr++] = (Type) conv; + } + return true; + } + + static handle cast(const array_type &src, return_value_policy policy, handle parent) { + list l(Size); + size_t index = 0; + for (auto const &value: src) { + object value_ = object(value_conv::cast(value, policy, parent), false); + if (!value_) + return handle(); + PyList_SET_ITEM(l.ptr(), index++, value_.release().ptr()); // steals a reference + } + return l.release(); + } + PYBIND11_TYPE_CASTER(array_type, _("list<") + value_conv::name() + _(">") + _("[") + _() + _("]")); }; template struct type_caster> {