stl.h revision 12391:ceeca8b41e4b
1/*
2    pybind11/stl.h: Transparent conversion for STL data types
3
4    Copyright (c) 2016 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#pragma once
11
12#include "pybind11.h"
13#include <set>
14#include <unordered_set>
15#include <map>
16#include <unordered_map>
17#include <iostream>
18#include <list>
19#include <valarray>
20
21#if defined(_MSC_VER)
22#pragma warning(push)
23#pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
24#endif
25
26#ifdef __has_include
27// std::optional (but including it in c++14 mode isn't allowed)
28#  if defined(PYBIND11_CPP17) && __has_include(<optional>)
29#    include <optional>
30#    define PYBIND11_HAS_OPTIONAL 1
31#  endif
32// std::experimental::optional (but not allowed in c++11 mode)
33#  if defined(PYBIND11_CPP14) && __has_include(<experimental/optional>)
34#    include <experimental/optional>
35#    define PYBIND11_HAS_EXP_OPTIONAL 1
36#  endif
37// std::variant
38#  if defined(PYBIND11_CPP17) && __has_include(<variant>)
39#    include <variant>
40#    define PYBIND11_HAS_VARIANT 1
41#  endif
42#elif defined(_MSC_VER) && defined(PYBIND11_CPP17)
43#  include <optional>
44#  include <variant>
45#  define PYBIND11_HAS_OPTIONAL 1
46#  define PYBIND11_HAS_VARIANT 1
47#endif
48
49NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
50NAMESPACE_BEGIN(detail)
51
52/// Extracts an const lvalue reference or rvalue reference for U based on the type of T (e.g. for
53/// forwarding a container element).  Typically used indirect via forwarded_type(), below.
54template <typename T, typename U>
55using forwarded_type = conditional_t<
56    std::is_lvalue_reference<T>::value, remove_reference_t<U> &, remove_reference_t<U> &&>;
57
58/// Forwards a value U as rvalue or lvalue according to whether T is rvalue or lvalue; typically
59/// used for forwarding a container's elements.
60template <typename T, typename U>
61forwarded_type<T, U> forward_like(U &&u) {
62    return std::forward<detail::forwarded_type<T, U>>(std::forward<U>(u));
63}
64
65template <typename Type, typename Key> struct set_caster {
66    using type = Type;
67    using key_conv = make_caster<Key>;
68
69    bool load(handle src, bool convert) {
70        if (!isinstance<pybind11::set>(src))
71            return false;
72        auto s = reinterpret_borrow<pybind11::set>(src);
73        value.clear();
74        for (auto entry : s) {
75            key_conv conv;
76            if (!conv.load(entry, convert))
77                return false;
78            value.insert(cast_op<Key &&>(std::move(conv)));
79        }
80        return true;
81    }
82
83    template <typename T>
84    static handle cast(T &&src, return_value_policy policy, handle parent) {
85        pybind11::set s;
86        for (auto &&value : src) {
87            auto value_ = reinterpret_steal<object>(key_conv::cast(forward_like<T>(value), policy, parent));
88            if (!value_ || !s.add(value_))
89                return handle();
90        }
91        return s.release();
92    }
93
94    PYBIND11_TYPE_CASTER(type, _("Set[") + key_conv::name() + _("]"));
95};
96
97template <typename Type, typename Key, typename Value> struct map_caster {
98    using key_conv   = make_caster<Key>;
99    using value_conv = make_caster<Value>;
100
101    bool load(handle src, bool convert) {
102        if (!isinstance<dict>(src))
103            return false;
104        auto d = reinterpret_borrow<dict>(src);
105        value.clear();
106        for (auto it : d) {
107            key_conv kconv;
108            value_conv vconv;
109            if (!kconv.load(it.first.ptr(), convert) ||
110                !vconv.load(it.second.ptr(), convert))
111                return false;
112            value.emplace(cast_op<Key &&>(std::move(kconv)), cast_op<Value &&>(std::move(vconv)));
113        }
114        return true;
115    }
116
117    template <typename T>
118    static handle cast(T &&src, return_value_policy policy, handle parent) {
119        dict d;
120        for (auto &&kv : src) {
121            auto key = reinterpret_steal<object>(key_conv::cast(forward_like<T>(kv.first), policy, parent));
122            auto value = reinterpret_steal<object>(value_conv::cast(forward_like<T>(kv.second), policy, parent));
123            if (!key || !value)
124                return handle();
125            d[key] = value;
126        }
127        return d.release();
128    }
129
130    PYBIND11_TYPE_CASTER(Type, _("Dict[") + key_conv::name() + _(", ") + value_conv::name() + _("]"));
131};
132
133template <typename Type, typename Value> struct list_caster {
134    using value_conv = make_caster<Value>;
135
136    bool load(handle src, bool convert) {
137        if (!isinstance<sequence>(src))
138            return false;
139        auto s = reinterpret_borrow<sequence>(src);
140        value.clear();
141        reserve_maybe(s, &value);
142        for (auto it : s) {
143            value_conv conv;
144            if (!conv.load(it, convert))
145                return false;
146            value.push_back(cast_op<Value &&>(std::move(conv)));
147        }
148        return true;
149    }
150
151private:
152    template <typename T = Type,
153              enable_if_t<std::is_same<decltype(std::declval<T>().reserve(0)), void>::value, int> = 0>
154    void reserve_maybe(sequence s, Type *) { value.reserve(s.size()); }
155    void reserve_maybe(sequence, void *) { }
156
157public:
158    template <typename T>
159    static handle cast(T &&src, return_value_policy policy, handle parent) {
160        list l(src.size());
161        size_t index = 0;
162        for (auto &&value : src) {
163            auto value_ = reinterpret_steal<object>(value_conv::cast(forward_like<T>(value), policy, parent));
164            if (!value_)
165                return handle();
166            PyList_SET_ITEM(l.ptr(), (ssize_t) index++, value_.release().ptr()); // steals a reference
167        }
168        return l.release();
169    }
170
171    PYBIND11_TYPE_CASTER(Type, _("List[") + value_conv::name() + _("]"));
172};
173
174template <typename Type, typename Alloc> struct type_caster<std::vector<Type, Alloc>>
175 : list_caster<std::vector<Type, Alloc>, Type> { };
176
177template <typename Type, typename Alloc> struct type_caster<std::list<Type, Alloc>>
178 : list_caster<std::list<Type, Alloc>, Type> { };
179
180template <typename ArrayType, typename Value, bool Resizable, size_t Size = 0> struct array_caster {
181    using value_conv = make_caster<Value>;
182
183private:
184    template <bool R = Resizable>
185    bool require_size(enable_if_t<R, size_t> size) {
186        if (value.size() != size)
187            value.resize(size);
188        return true;
189    }
190    template <bool R = Resizable>
191    bool require_size(enable_if_t<!R, size_t> size) {
192        return size == Size;
193    }
194
195public:
196    bool load(handle src, bool convert) {
197        if (!isinstance<list>(src))
198            return false;
199        auto l = reinterpret_borrow<list>(src);
200        if (!require_size(l.size()))
201            return false;
202        size_t ctr = 0;
203        for (auto it : l) {
204            value_conv conv;
205            if (!conv.load(it, convert))
206                return false;
207            value[ctr++] = cast_op<Value &&>(std::move(conv));
208        }
209        return true;
210    }
211
212    template <typename T>
213    static handle cast(T &&src, return_value_policy policy, handle parent) {
214        list l(src.size());
215        size_t index = 0;
216        for (auto &&value : src) {
217            auto value_ = reinterpret_steal<object>(value_conv::cast(forward_like<T>(value), policy, parent));
218            if (!value_)
219                return handle();
220            PyList_SET_ITEM(l.ptr(), (ssize_t) index++, value_.release().ptr()); // steals a reference
221        }
222        return l.release();
223    }
224
225    PYBIND11_TYPE_CASTER(ArrayType, _("List[") + value_conv::name() + _<Resizable>(_(""), _("[") + _<Size>() + _("]")) + _("]"));
226};
227
228template <typename Type, size_t Size> struct type_caster<std::array<Type, Size>>
229 : array_caster<std::array<Type, Size>, Type, false, Size> { };
230
231template <typename Type> struct type_caster<std::valarray<Type>>
232 : array_caster<std::valarray<Type>, Type, true> { };
233
234template <typename Key, typename Compare, typename Alloc> struct type_caster<std::set<Key, Compare, Alloc>>
235  : set_caster<std::set<Key, Compare, Alloc>, Key> { };
236
237template <typename Key, typename Hash, typename Equal, typename Alloc> struct type_caster<std::unordered_set<Key, Hash, Equal, Alloc>>
238  : set_caster<std::unordered_set<Key, Hash, Equal, Alloc>, Key> { };
239
240template <typename Key, typename Value, typename Compare, typename Alloc> struct type_caster<std::map<Key, Value, Compare, Alloc>>
241  : map_caster<std::map<Key, Value, Compare, Alloc>, Key, Value> { };
242
243template <typename Key, typename Value, typename Hash, typename Equal, typename Alloc> struct type_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>>
244  : map_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>, Key, Value> { };
245
246// This type caster is intended to be used for std::optional and std::experimental::optional
247template<typename T> struct optional_caster {
248    using value_conv = make_caster<typename T::value_type>;
249
250    template <typename T_>
251    static handle cast(T_ &&src, return_value_policy policy, handle parent) {
252        if (!src)
253            return none().inc_ref();
254        return value_conv::cast(*std::forward<T_>(src), policy, parent);
255    }
256
257    bool load(handle src, bool convert) {
258        if (!src) {
259            return false;
260        } else if (src.is_none()) {
261            return true;  // default-constructed value is already empty
262        }
263        value_conv inner_caster;
264        if (!inner_caster.load(src, convert))
265            return false;
266
267        value.emplace(cast_op<typename T::value_type &&>(std::move(inner_caster)));
268        return true;
269    }
270
271    PYBIND11_TYPE_CASTER(T, _("Optional[") + value_conv::name() + _("]"));
272};
273
274#ifdef PYBIND11_HAS_OPTIONAL
275template<typename T> struct type_caster<std::optional<T>>
276    : public optional_caster<std::optional<T>> {};
277
278template<> struct type_caster<std::nullopt_t>
279    : public void_caster<std::nullopt_t> {};
280#endif
281
282#ifdef PYBIND11_HAS_EXP_OPTIONAL
283template<typename T> struct type_caster<std::experimental::optional<T>>
284    : public optional_caster<std::experimental::optional<T>> {};
285
286template<> struct type_caster<std::experimental::nullopt_t>
287    : public void_caster<std::experimental::nullopt_t> {};
288#endif
289
290/// Visit a variant and cast any found type to Python
291struct variant_caster_visitor {
292    return_value_policy policy;
293    handle parent;
294
295    using result_type = handle; // required by boost::variant in C++11
296
297    template <typename T>
298    result_type operator()(T &&src) const {
299        return make_caster<T>::cast(std::forward<T>(src), policy, parent);
300    }
301};
302
303/// Helper class which abstracts away variant's `visit` function. `std::variant` and similar
304/// `namespace::variant` types which provide a `namespace::visit()` function are handled here
305/// automatically using argument-dependent lookup. Users can provide specializations for other
306/// variant-like classes, e.g. `boost::variant` and `boost::apply_visitor`.
307template <template<typename...> class Variant>
308struct visit_helper {
309    template <typename... Args>
310    static auto call(Args &&...args) -> decltype(visit(std::forward<Args>(args)...)) {
311        return visit(std::forward<Args>(args)...);
312    }
313};
314
315/// Generic variant caster
316template <typename Variant> struct variant_caster;
317
318template <template<typename...> class V, typename... Ts>
319struct variant_caster<V<Ts...>> {
320    static_assert(sizeof...(Ts) > 0, "Variant must consist of at least one alternative.");
321
322    template <typename U, typename... Us>
323    bool load_alternative(handle src, bool convert, type_list<U, Us...>) {
324        auto caster = make_caster<U>();
325        if (caster.load(src, convert)) {
326            value = cast_op<U>(caster);
327            return true;
328        }
329        return load_alternative(src, convert, type_list<Us...>{});
330    }
331
332    bool load_alternative(handle, bool, type_list<>) { return false; }
333
334    bool load(handle src, bool convert) {
335        // Do a first pass without conversions to improve constructor resolution.
336        // E.g. `py::int_(1).cast<variant<double, int>>()` needs to fill the `int`
337        // slot of the variant. Without two-pass loading `double` would be filled
338        // because it appears first and a conversion is possible.
339        if (convert && load_alternative(src, false, type_list<Ts...>{}))
340            return true;
341        return load_alternative(src, convert, type_list<Ts...>{});
342    }
343
344    template <typename Variant>
345    static handle cast(Variant &&src, return_value_policy policy, handle parent) {
346        return visit_helper<V>::call(variant_caster_visitor{policy, parent},
347                                     std::forward<Variant>(src));
348    }
349
350    using Type = V<Ts...>;
351    PYBIND11_TYPE_CASTER(Type, _("Union[") + detail::concat(make_caster<Ts>::name()...) + _("]"));
352};
353
354#ifdef PYBIND11_HAS_VARIANT
355template <typename... Ts>
356struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> { };
357#endif
358NAMESPACE_END(detail)
359
360inline std::ostream &operator<<(std::ostream &os, const handle &obj) {
361    os << (std::string) str(obj);
362    return os;
363}
364
365NAMESPACE_END(PYBIND11_NAMESPACE)
366
367#if defined(_MSC_VER)
368#pragma warning(pop)
369#endif
370