Use pybind11 to make a minimal C++ Python extension.
[alexxy/gromacs.git] / python_packaging / src / external / pybind / include / pybind11 / stl.h
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                                  !__has_include(<optional>))
35 #    include <experimental/optional>
36 #    define PYBIND11_HAS_EXP_OPTIONAL 1
37 #  endif
38 // std::variant
39 #  if defined(PYBIND11_CPP17) && __has_include(<variant>)
40 #    include <variant>
41 #    define PYBIND11_HAS_VARIANT 1
42 #  endif
43 #elif defined(_MSC_VER) && defined(PYBIND11_CPP17)
44 #  include <optional>
45 #  include <variant>
46 #  define PYBIND11_HAS_OPTIONAL 1
47 #  define PYBIND11_HAS_VARIANT 1
48 #endif
49
50 NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
51 NAMESPACE_BEGIN(detail)
52
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> &&>;
58
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));
64 }
65
66 template <typename Type, typename Key> struct set_caster {
67     using type = Type;
68     using key_conv = make_caster<Key>;
69
70     bool load(handle src, bool convert) {
71         if (!isinstance<pybind11::set>(src))
72             return false;
73         auto s = reinterpret_borrow<pybind11::set>(src);
74         value.clear();
75         for (auto entry : s) {
76             key_conv conv;
77             if (!conv.load(entry, convert))
78                 return false;
79             value.insert(cast_op<Key &&>(std::move(conv)));
80         }
81         return true;
82     }
83
84     template <typename T>
85     static handle cast(T &&src, return_value_policy policy, handle parent) {
86         policy = return_value_policy_override<Key>::policy(policy);
87         pybind11::set s;
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_))
91                 return handle();
92         }
93         return s.release();
94     }
95
96     PYBIND11_TYPE_CASTER(type, _("Set[") + key_conv::name() + _("]"));
97 };
98
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>;
102
103     bool load(handle src, bool convert) {
104         if (!isinstance<dict>(src))
105             return false;
106         auto d = reinterpret_borrow<dict>(src);
107         value.clear();
108         for (auto it : d) {
109             key_conv kconv;
110             value_conv vconv;
111             if (!kconv.load(it.first.ptr(), convert) ||
112                 !vconv.load(it.second.ptr(), convert))
113                 return false;
114             value.emplace(cast_op<Key &&>(std::move(kconv)), cast_op<Value &&>(std::move(vconv)));
115         }
116         return true;
117     }
118
119     template <typename T>
120     static handle cast(T &&src, return_value_policy policy, handle parent) {
121         dict d;
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));
127             if (!key || !value)
128                 return handle();
129             d[key] = value;
130         }
131         return d.release();
132     }
133
134     PYBIND11_TYPE_CASTER(Type, _("Dict[") + key_conv::name() + _(", ") + value_conv::name() + _("]"));
135 };
136
137 template <typename Type, typename Value> struct list_caster {
138     using value_conv = make_caster<Value>;
139
140     bool load(handle src, bool convert) {
141         if (!isinstance<sequence>(src))
142             return false;
143         auto s = reinterpret_borrow<sequence>(src);
144         value.clear();
145         reserve_maybe(s, &value);
146         for (auto it : s) {
147             value_conv conv;
148             if (!conv.load(it, convert))
149                 return false;
150             value.push_back(cast_op<Value &&>(std::move(conv)));
151         }
152         return true;
153     }
154
155 private:
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 *) { }
160
161 public:
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);
165         list l(src.size());
166         size_t index = 0;
167         for (auto &&value : src) {
168             auto value_ = reinterpret_steal<object>(value_conv::cast(forward_like<T>(value), policy, parent));
169             if (!value_)
170                 return handle();
171             PyList_SET_ITEM(l.ptr(), (ssize_t) index++, value_.release().ptr()); // steals a reference
172         }
173         return l.release();
174     }
175
176     PYBIND11_TYPE_CASTER(Type, _("List[") + value_conv::name() + _("]"));
177 };
178
179 template <typename Type, typename Alloc> struct type_caster<std::vector<Type, Alloc>>
180  : list_caster<std::vector<Type, Alloc>, Type> { };
181
182 template <typename Type, typename Alloc> struct type_caster<std::list<Type, Alloc>>
183  : list_caster<std::list<Type, Alloc>, Type> { };
184
185 template <typename ArrayType, typename Value, bool Resizable, size_t Size = 0> struct array_caster {
186     using value_conv = make_caster<Value>;
187
188 private:
189     template <bool R = Resizable>
190     bool require_size(enable_if_t<R, size_t> size) {
191         if (value.size() != size)
192             value.resize(size);
193         return true;
194     }
195     template <bool R = Resizable>
196     bool require_size(enable_if_t<!R, size_t> size) {
197         return size == Size;
198     }
199
200 public:
201     bool load(handle src, bool convert) {
202         if (!isinstance<list>(src))
203             return false;
204         auto l = reinterpret_borrow<list>(src);
205         if (!require_size(l.size()))
206             return false;
207         size_t ctr = 0;
208         for (auto it : l) {
209             value_conv conv;
210             if (!conv.load(it, convert))
211                 return false;
212             value[ctr++] = cast_op<Value &&>(std::move(conv));
213         }
214         return true;
215     }
216
217     template <typename T>
218     static handle cast(T &&src, return_value_policy policy, handle parent) {
219         list l(src.size());
220         size_t index = 0;
221         for (auto &&value : src) {
222             auto value_ = reinterpret_steal<object>(value_conv::cast(forward_like<T>(value), policy, parent));
223             if (!value_)
224                 return handle();
225             PyList_SET_ITEM(l.ptr(), (ssize_t) index++, value_.release().ptr()); // steals a reference
226         }
227         return l.release();
228     }
229
230     PYBIND11_TYPE_CASTER(ArrayType, _("List[") + value_conv::name() + _<Resizable>(_(""), _("[") + _<Size>() + _("]")) + _("]"));
231 };
232
233 template <typename Type, size_t Size> struct type_caster<std::array<Type, Size>>
234  : array_caster<std::array<Type, Size>, Type, false, Size> { };
235
236 template <typename Type> struct type_caster<std::valarray<Type>>
237  : array_caster<std::valarray<Type>, Type, true> { };
238
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> { };
241
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> { };
244
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> { };
247
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> { };
250
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>;
254
255     template <typename T_>
256     static handle cast(T_ &&src, return_value_policy policy, handle parent) {
257         if (!src)
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);
261     }
262
263     bool load(handle src, bool convert) {
264         if (!src) {
265             return false;
266         } else if (src.is_none()) {
267             return true;  // default-constructed value is already empty
268         }
269         value_conv inner_caster;
270         if (!inner_caster.load(src, convert))
271             return false;
272
273         value.emplace(cast_op<typename T::value_type &&>(std::move(inner_caster)));
274         return true;
275     }
276
277     PYBIND11_TYPE_CASTER(T, _("Optional[") + value_conv::name() + _("]"));
278 };
279
280 #if PYBIND11_HAS_OPTIONAL
281 template<typename T> struct type_caster<std::optional<T>>
282     : public optional_caster<std::optional<T>> {};
283
284 template<> struct type_caster<std::nullopt_t>
285     : public void_caster<std::nullopt_t> {};
286 #endif
287
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>> {};
291
292 template<> struct type_caster<std::experimental::nullopt_t>
293     : public void_caster<std::experimental::nullopt_t> {};
294 #endif
295
296 /// Visit a variant and cast any found type to Python
297 struct variant_caster_visitor {
298     return_value_policy policy;
299     handle parent;
300
301     using result_type = handle; // required by boost::variant in C++11
302
303     template <typename T>
304     result_type operator()(T &&src) const {
305         return make_caster<T>::cast(std::forward<T>(src), policy, parent);
306     }
307 };
308
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)...);
318     }
319 };
320
321 /// Generic variant caster
322 template <typename Variant> struct variant_caster;
323
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.");
327
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);
333             return true;
334         }
335         return load_alternative(src, convert, type_list<Us...>{});
336     }
337
338     bool load_alternative(handle, bool, type_list<>) { return false; }
339
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...>{}))
346             return true;
347         return load_alternative(src, convert, type_list<Ts...>{});
348     }
349
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));
354     }
355
356     using Type = V<Ts...>;
357     PYBIND11_TYPE_CASTER(Type, _("Union[") + detail::concat(make_caster<Ts>::name()...) + _("]"));
358 };
359
360 #if PYBIND11_HAS_VARIANT
361 template <typename... Ts>
362 struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> { };
363 #endif
364
365 NAMESPACE_END(detail)
366
367 inline std::ostream &operator<<(std::ostream &os, const handle &obj) {
368     os << (std::string) str(obj);
369     return os;
370 }
371
372 NAMESPACE_END(PYBIND11_NAMESPACE)
373
374 #if defined(_MSC_VER)
375 #pragma warning(pop)
376 #endif