/* pybind11/stl.h: Transparent conversion for STL data types Copyright (c) 2016 Wenzel Jakob All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. */ #pragma once #include "pybind11.h" #include "detail/common.h" #include #include #include #include #include #include #include #include // See `detail/common.h` for implementation of these guards. #if defined(PYBIND11_HAS_OPTIONAL) # include #elif defined(PYBIND11_HAS_EXP_OPTIONAL) # include #endif #if defined(PYBIND11_HAS_VARIANT) # include #endif PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) /// Extracts an const lvalue reference or rvalue reference for U based on the type of T (e.g. for /// forwarding a container element). Typically used indirect via forwarded_type(), below. template using forwarded_type = conditional_t::value, remove_reference_t &, remove_reference_t &&>; /// Forwards a value U as rvalue or lvalue according to whether T is rvalue or lvalue; typically /// used for forwarding a container's elements. template forwarded_type forward_like(U &&u) { return std::forward>(std::forward(u)); } template struct set_caster { using type = Type; using key_conv = make_caster; bool load(handle src, bool convert) { if (!isinstance(src)) { return false; } auto s = reinterpret_borrow(src); value.clear(); for (auto entry : s) { key_conv conv; if (!conv.load(entry, convert)) { return false; } value.insert(cast_op(std::move(conv))); } return true; } template static handle cast(T &&src, return_value_policy policy, handle parent) { if (!std::is_lvalue_reference::value) { policy = return_value_policy_override::policy(policy); } pybind11::set s; for (auto &&value : src) { auto value_ = reinterpret_steal( key_conv::cast(forward_like(value), policy, parent)); if (!value_ || !s.add(value_)) { return handle(); } } return s.release(); } PYBIND11_TYPE_CASTER(type, const_name("Set[") + key_conv::name + const_name("]")); }; template struct map_caster { using key_conv = make_caster; using value_conv = make_caster; bool load(handle src, bool convert) { if (!isinstance(src)) { return false; } auto d = reinterpret_borrow(src); value.clear(); for (auto it : d) { key_conv kconv; value_conv vconv; if (!kconv.load(it.first.ptr(), convert) || !vconv.load(it.second.ptr(), convert)) { return false; } value.emplace(cast_op(std::move(kconv)), cast_op(std::move(vconv))); } return true; } template static handle cast(T &&src, return_value_policy policy, handle parent) { dict d; return_value_policy policy_key = policy; return_value_policy policy_value = policy; if (!std::is_lvalue_reference::value) { policy_key = return_value_policy_override::policy(policy_key); policy_value = return_value_policy_override::policy(policy_value); } for (auto &&kv : src) { auto key = reinterpret_steal( key_conv::cast(forward_like(kv.first), policy_key, parent)); auto value = reinterpret_steal( value_conv::cast(forward_like(kv.second), policy_value, parent)); if (!key || !value) { return handle(); } d[key] = value; } return d.release(); } PYBIND11_TYPE_CASTER(Type, const_name("Dict[") + key_conv::name + const_name(", ") + value_conv::name + const_name("]")); }; template struct list_caster { using value_conv = make_caster; bool load(handle src, bool convert) { if (!isinstance(src) || isinstance(src) || isinstance(src)) { return false; } auto s = reinterpret_borrow(src); value.clear(); reserve_maybe(s, &value); for (auto it : s) { value_conv conv; if (!conv.load(it, convert)) { return false; } value.push_back(cast_op(std::move(conv))); } return true; } private: template < typename T = Type, enable_if_t().reserve(0)), void>::value, int> = 0> void reserve_maybe(const sequence &s, Type *) { value.reserve(s.size()); } void reserve_maybe(const sequence &, void *) {} public: template static handle cast(T &&src, return_value_policy policy, handle parent) { if (!std::is_lvalue_reference::value) { policy = return_value_policy_override::policy(policy); } list l(src.size()); ssize_t index = 0; for (auto &&value : src) { auto value_ = reinterpret_steal( value_conv::cast(forward_like(value), policy, parent)); if (!value_) { return handle(); } PyList_SET_ITEM(l.ptr(), index++, value_.release().ptr()); // steals a reference } return l.release(); } PYBIND11_TYPE_CASTER(Type, const_name("List[") + value_conv::name + const_name("]")); }; template struct type_caster> : list_caster, Type> {}; template struct type_caster> : list_caster, Type> {}; template struct type_caster> : list_caster, Type> {}; template struct array_caster { using value_conv = make_caster; private: template bool require_size(enable_if_t size) { if (value.size() != size) { value.resize(size); } return true; } template bool require_size(enable_if_t size) { return size == Size; } public: bool load(handle src, bool convert) { if (!isinstance(src)) { return false; } auto l = reinterpret_borrow(src); if (!require_size(l.size())) { return false; } size_t ctr = 0; for (auto it : l) { value_conv conv; if (!conv.load(it, convert)) { return false; } value[ctr++] = cast_op(std::move(conv)); } return true; } template static handle cast(T &&src, return_value_policy policy, handle parent) { list l(src.size()); ssize_t index = 0; for (auto &&value : src) { auto value_ = reinterpret_steal( value_conv::cast(forward_like(value), policy, parent)); if (!value_) { return handle(); } PyList_SET_ITEM(l.ptr(), index++, value_.release().ptr()); // steals a reference } return l.release(); } PYBIND11_TYPE_CASTER(ArrayType, const_name("List[") + value_conv::name + const_name(const_name(""), const_name("[") + const_name() + const_name("]")) + const_name("]")); }; template struct type_caster> : array_caster, Type, false, Size> {}; template struct type_caster> : array_caster, Type, true> {}; template struct type_caster> : set_caster, Key> {}; template struct type_caster> : set_caster, Key> {}; template struct type_caster> : map_caster, Key, Value> {}; template struct type_caster> : map_caster, Key, Value> {}; // This type caster is intended to be used for std::optional and std::experimental::optional template struct optional_caster { using value_conv = make_caster; template static handle cast(T &&src, return_value_policy policy, handle parent) { if (!src) { return none().inc_ref(); } if (!std::is_lvalue_reference::value) { policy = return_value_policy_override::policy(policy); } return value_conv::cast(*std::forward(src), policy, parent); } bool load(handle src, bool convert) { if (!src) { return false; } if (src.is_none()) { return true; // default-constructed value is already empty } value_conv inner_caster; if (!inner_caster.load(src, convert)) { return false; } value.emplace(cast_op(std::move(inner_caster))); return true; } PYBIND11_TYPE_CASTER(Type, const_name("Optional[") + value_conv::name + const_name("]")); }; #if defined(PYBIND11_HAS_OPTIONAL) template struct type_caster> : public optional_caster> {}; template <> struct type_caster : public void_caster {}; #endif #if defined(PYBIND11_HAS_EXP_OPTIONAL) template struct type_caster> : public optional_caster> {}; template <> struct type_caster : public void_caster {}; #endif /// Visit a variant and cast any found type to Python struct variant_caster_visitor { return_value_policy policy; handle parent; using result_type = handle; // required by boost::variant in C++11 template result_type operator()(T &&src) const { return make_caster::cast(std::forward(src), policy, parent); } }; /// Helper class which abstracts away variant's `visit` function. `std::variant` and similar /// `namespace::variant` types which provide a `namespace::visit()` function are handled here /// automatically using argument-dependent lookup. Users can provide specializations for other /// variant-like classes, e.g. `boost::variant` and `boost::apply_visitor`. template