2 pybind11/stl.h: Transparent conversion for STL data types
4 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
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.
14 #include <unordered_set>
16 #include <unordered_map>
23 #pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
27 // std::optional (but including it in c++14 mode isn't allowed)
28 # if defined(PYBIND11_CPP17) && __has_include(<optional>)
30 # define PYBIND11_HAS_OPTIONAL 1
32 // std::experimental::optional (but not allowed in c++11 mode)
33 # if defined(PYBIND11_CPP14) && (__has_include(<experimental/optional>) && \
34 !__has_include(<optional>))
35 # include <experimental/optional>
36 # define PYBIND11_HAS_EXP_OPTIONAL 1
39 # if defined(PYBIND11_CPP17) && __has_include(<variant>)
41 # define PYBIND11_HAS_VARIANT 1
43 #elif defined(_MSC_VER) && defined(PYBIND11_CPP17)
46 # define PYBIND11_HAS_OPTIONAL 1
47 # define PYBIND11_HAS_VARIANT 1
50 NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
51 NAMESPACE_BEGIN(detail)
53 /// Extracts an const lvalue reference or rvalue reference for U based on the type of T (e.g. for
54 /// forwarding a container element). Typically used indirect via forwarded_type(), below.
55 template <typename T, typename U>
56 using forwarded_type = conditional_t<
57 std::is_lvalue_reference<T>::value, remove_reference_t<U> &, remove_reference_t<U> &&>;
59 /// Forwards a value U as rvalue or lvalue according to whether T is rvalue or lvalue; typically
60 /// used for forwarding a container's elements.
61 template <typename T, typename U>
62 forwarded_type<T, U> forward_like(U &&u) {
63 return std::forward<detail::forwarded_type<T, U>>(std::forward<U>(u));
66 template <typename Type, typename Key> struct set_caster {
68 using key_conv = make_caster<Key>;
70 bool load(handle src, bool convert) {
71 if (!isinstance<pybind11::set>(src))
73 auto s = reinterpret_borrow<pybind11::set>(src);
75 for (auto entry : s) {
77 if (!conv.load(entry, convert))
79 value.insert(cast_op<Key &&>(std::move(conv)));
85 static handle cast(T &&src, return_value_policy policy, handle parent) {
86 policy = return_value_policy_override<Key>::policy(policy);
88 for (auto &&value : src) {
89 auto value_ = reinterpret_steal<object>(key_conv::cast(forward_like<T>(value), policy, parent));
90 if (!value_ || !s.add(value_))
96 PYBIND11_TYPE_CASTER(type, _("Set[") + key_conv::name() + _("]"));
99 template <typename Type, typename Key, typename Value> struct map_caster {
100 using key_conv = make_caster<Key>;
101 using value_conv = make_caster<Value>;
103 bool load(handle src, bool convert) {
104 if (!isinstance<dict>(src))
106 auto d = reinterpret_borrow<dict>(src);
111 if (!kconv.load(it.first.ptr(), convert) ||
112 !vconv.load(it.second.ptr(), convert))
114 value.emplace(cast_op<Key &&>(std::move(kconv)), cast_op<Value &&>(std::move(vconv)));
119 template <typename T>
120 static handle cast(T &&src, return_value_policy policy, handle parent) {
122 return_value_policy policy_key = return_value_policy_override<Key>::policy(policy);
123 return_value_policy policy_value = return_value_policy_override<Value>::policy(policy);
124 for (auto &&kv : src) {
125 auto key = reinterpret_steal<object>(key_conv::cast(forward_like<T>(kv.first), policy_key, parent));
126 auto value = reinterpret_steal<object>(value_conv::cast(forward_like<T>(kv.second), policy_value, parent));
134 PYBIND11_TYPE_CASTER(Type, _("Dict[") + key_conv::name() + _(", ") + value_conv::name() + _("]"));
137 template <typename Type, typename Value> struct list_caster {
138 using value_conv = make_caster<Value>;
140 bool load(handle src, bool convert) {
141 if (!isinstance<sequence>(src))
143 auto s = reinterpret_borrow<sequence>(src);
145 reserve_maybe(s, &value);
148 if (!conv.load(it, convert))
150 value.push_back(cast_op<Value &&>(std::move(conv)));
156 template <typename T = Type,
157 enable_if_t<std::is_same<decltype(std::declval<T>().reserve(0)), void>::value, int> = 0>
158 void reserve_maybe(sequence s, Type *) { value.reserve(s.size()); }
159 void reserve_maybe(sequence, void *) { }
162 template <typename T>
163 static handle cast(T &&src, return_value_policy policy, handle parent) {
164 policy = return_value_policy_override<Value>::policy(policy);
167 for (auto &&value : src) {
168 auto value_ = reinterpret_steal<object>(value_conv::cast(forward_like<T>(value), policy, parent));
171 PyList_SET_ITEM(l.ptr(), (ssize_t) index++, value_.release().ptr()); // steals a reference
176 PYBIND11_TYPE_CASTER(Type, _("List[") + value_conv::name() + _("]"));
179 template <typename Type, typename Alloc> struct type_caster<std::vector<Type, Alloc>>
180 : list_caster<std::vector<Type, Alloc>, Type> { };
182 template <typename Type, typename Alloc> struct type_caster<std::list<Type, Alloc>>
183 : list_caster<std::list<Type, Alloc>, Type> { };
185 template <typename ArrayType, typename Value, bool Resizable, size_t Size = 0> struct array_caster {
186 using value_conv = make_caster<Value>;
189 template <bool R = Resizable>
190 bool require_size(enable_if_t<R, size_t> size) {
191 if (value.size() != size)
195 template <bool R = Resizable>
196 bool require_size(enable_if_t<!R, size_t> size) {
201 bool load(handle src, bool convert) {
202 if (!isinstance<list>(src))
204 auto l = reinterpret_borrow<list>(src);
205 if (!require_size(l.size()))
210 if (!conv.load(it, convert))
212 value[ctr++] = cast_op<Value &&>(std::move(conv));
217 template <typename T>
218 static handle cast(T &&src, return_value_policy policy, handle parent) {
221 for (auto &&value : src) {
222 auto value_ = reinterpret_steal<object>(value_conv::cast(forward_like<T>(value), policy, parent));
225 PyList_SET_ITEM(l.ptr(), (ssize_t) index++, value_.release().ptr()); // steals a reference
230 PYBIND11_TYPE_CASTER(ArrayType, _("List[") + value_conv::name() + _<Resizable>(_(""), _("[") + _<Size>() + _("]")) + _("]"));
233 template <typename Type, size_t Size> struct type_caster<std::array<Type, Size>>
234 : array_caster<std::array<Type, Size>, Type, false, Size> { };
236 template <typename Type> struct type_caster<std::valarray<Type>>
237 : array_caster<std::valarray<Type>, Type, true> { };
239 template <typename Key, typename Compare, typename Alloc> struct type_caster<std::set<Key, Compare, Alloc>>
240 : set_caster<std::set<Key, Compare, Alloc>, Key> { };
242 template <typename Key, typename Hash, typename Equal, typename Alloc> struct type_caster<std::unordered_set<Key, Hash, Equal, Alloc>>
243 : set_caster<std::unordered_set<Key, Hash, Equal, Alloc>, Key> { };
245 template <typename Key, typename Value, typename Compare, typename Alloc> struct type_caster<std::map<Key, Value, Compare, Alloc>>
246 : map_caster<std::map<Key, Value, Compare, Alloc>, Key, Value> { };
248 template <typename Key, typename Value, typename Hash, typename Equal, typename Alloc> struct type_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>>
249 : map_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>, Key, Value> { };
251 // This type caster is intended to be used for std::optional and std::experimental::optional
252 template<typename T> struct optional_caster {
253 using value_conv = make_caster<typename T::value_type>;
255 template <typename T_>
256 static handle cast(T_ &&src, return_value_policy policy, handle parent) {
258 return none().inc_ref();
259 policy = return_value_policy_override<typename T::value_type>::policy(policy);
260 return value_conv::cast(*std::forward<T_>(src), policy, parent);
263 bool load(handle src, bool convert) {
266 } else if (src.is_none()) {
267 return true; // default-constructed value is already empty
269 value_conv inner_caster;
270 if (!inner_caster.load(src, convert))
273 value.emplace(cast_op<typename T::value_type &&>(std::move(inner_caster)));
277 PYBIND11_TYPE_CASTER(T, _("Optional[") + value_conv::name() + _("]"));
280 #if PYBIND11_HAS_OPTIONAL
281 template<typename T> struct type_caster<std::optional<T>>
282 : public optional_caster<std::optional<T>> {};
284 template<> struct type_caster<std::nullopt_t>
285 : public void_caster<std::nullopt_t> {};
288 #if PYBIND11_HAS_EXP_OPTIONAL
289 template<typename T> struct type_caster<std::experimental::optional<T>>
290 : public optional_caster<std::experimental::optional<T>> {};
292 template<> struct type_caster<std::experimental::nullopt_t>
293 : public void_caster<std::experimental::nullopt_t> {};
296 /// Visit a variant and cast any found type to Python
297 struct variant_caster_visitor {
298 return_value_policy policy;
301 using result_type = handle; // required by boost::variant in C++11
303 template <typename T>
304 result_type operator()(T &&src) const {
305 return make_caster<T>::cast(std::forward<T>(src), policy, parent);
309 /// Helper class which abstracts away variant's `visit` function. `std::variant` and similar
310 /// `namespace::variant` types which provide a `namespace::visit()` function are handled here
311 /// automatically using argument-dependent lookup. Users can provide specializations for other
312 /// variant-like classes, e.g. `boost::variant` and `boost::apply_visitor`.
313 template <template<typename...> class Variant>
314 struct visit_helper {
315 template <typename... Args>
316 static auto call(Args &&...args) -> decltype(visit(std::forward<Args>(args)...)) {
317 return visit(std::forward<Args>(args)...);
321 /// Generic variant caster
322 template <typename Variant> struct variant_caster;
324 template <template<typename...> class V, typename... Ts>
325 struct variant_caster<V<Ts...>> {
326 static_assert(sizeof...(Ts) > 0, "Variant must consist of at least one alternative.");
328 template <typename U, typename... Us>
329 bool load_alternative(handle src, bool convert, type_list<U, Us...>) {
330 auto caster = make_caster<U>();
331 if (caster.load(src, convert)) {
332 value = cast_op<U>(caster);
335 return load_alternative(src, convert, type_list<Us...>{});
338 bool load_alternative(handle, bool, type_list<>) { return false; }
340 bool load(handle src, bool convert) {
341 // Do a first pass without conversions to improve constructor resolution.
342 // E.g. `py::int_(1).cast<variant<double, int>>()` needs to fill the `int`
343 // slot of the variant. Without two-pass loading `double` would be filled
344 // because it appears first and a conversion is possible.
345 if (convert && load_alternative(src, false, type_list<Ts...>{}))
347 return load_alternative(src, convert, type_list<Ts...>{});
350 template <typename Variant>
351 static handle cast(Variant &&src, return_value_policy policy, handle parent) {
352 return visit_helper<V>::call(variant_caster_visitor{policy, parent},
353 std::forward<Variant>(src));
356 using Type = V<Ts...>;
357 PYBIND11_TYPE_CASTER(Type, _("Union[") + detail::concat(make_caster<Ts>::name()...) + _("]"));
360 #if PYBIND11_HAS_VARIANT
361 template <typename... Ts>
362 struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> { };
365 NAMESPACE_END(detail)
367 inline std::ostream &operator<<(std::ostream &os, const handle &obj) {
368 os << (std::string) str(obj);
372 NAMESPACE_END(PYBIND11_NAMESPACE)
374 #if defined(_MSC_VER)