1 /*
2     Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
3 
4     All rights reserved. Use of this source code is governed by a
5     BSD-style license that can be found in the LICENSE file.
6 */
7 
8 
9 
10 
11 #pragma once
12 
13 #if !defined(NAMESPACE_BEGIN)
14 #  define NAMESPACE_BEGIN(name) namespace name {
15 #endif
16 #if !defined(NAMESPACE_END)
17 #  define NAMESPACE_END(name) }
18 #endif
19 
20 
21 
22 
23 #if !defined(PYBIND11_NAMESPACE)
24 #  ifdef __GNUG__
25 #    define PYBIND11_NAMESPACE pybind11 __attribute__((visibility("hidden")))
26 #  else
27 #    define PYBIND11_NAMESPACE pybind11
28 #  endif
29 #endif
30 
31 #if !(defined(_MSC_VER) && __cplusplus == 199711L) && !defined(__INTEL_COMPILER)
32 #  if __cplusplus >= 201402L
33 #    define PYBIND11_CPP14
34 #    if __cplusplus >= 201703L
35 #      define PYBIND11_CPP17
36 #    endif
37 #  endif
38 #elif defined(_MSC_VER) && __cplusplus == 199711L
39 
40 
41 #  if _MSVC_LANG >= 201402L
42 #    define PYBIND11_CPP14
43 #    if _MSVC_LANG > 201402L && _MSC_VER >= 1910
44 #      define PYBIND11_CPP17
45 #    endif
46 #  endif
47 #endif
48 
49 
50 #if defined(__INTEL_COMPILER)
51 #  if __INTEL_COMPILER < 1700
52 #    error pybind11 requires Intel C++ compiler v17 or newer
53 #  endif
54 #elif defined(__clang__) && !defined(__apple_build_version__)
55 #  if __clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ < 3)
56 #    error pybind11 requires clang 3.3 or newer
57 #  endif
58 #elif defined(__clang__)
59 
60 
61 #  if __clang_major__ < 5
62 #    error pybind11 requires Xcode/clang 5.0 or newer
63 #  endif
64 #elif defined(__GNUG__)
65 #  if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)
66 #    error pybind11 requires gcc 4.8 or newer
67 #  endif
68 #elif defined(_MSC_VER)
69 
70 
71 #  if _MSC_FULL_VER < 190024210
72 #    error pybind11 requires MSVC 2015 update 3 or newer
73 #  endif
74 #endif
75 
76 #if !defined(PYBIND11_EXPORT)
77 #  if defined(WIN32) || defined(_WIN32)
78 #    define PYBIND11_EXPORT __declspec(dllexport)
79 #  else
80 #    define PYBIND11_EXPORT __attribute__ ((visibility("default")))
81 #  endif
82 #endif
83 
84 #if defined(_MSC_VER)
85 #  define PYBIND11_NOINLINE __declspec(noinline)
86 #else
87 #  define PYBIND11_NOINLINE __attribute__ ((noinline))
88 #endif
89 
90 #if defined(PYBIND11_CPP14)
91 #  define PYBIND11_DEPRECATED(reason) [[deprecated(reason)]]
92 #else
93 #  define PYBIND11_DEPRECATED(reason) __attribute__((deprecated(reason)))
94 #endif
95 
96 #define PYBIND11_VERSION_MAJOR 2
97 #define PYBIND11_VERSION_MINOR 3
98 #define PYBIND11_VERSION_PATCH dev0
99 
100 
101 #if defined(_MSC_VER)
102 #  if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 4)
103 #    define HAVE_ROUND 1
104 #  endif
105 #  pragma warning(push)
106 #  pragma warning(disable: 4510 4610 4512 4005)
107 #  if defined(_DEBUG)
108 #    define PYBIND11_DEBUG_MARKER
109 #    undef _DEBUG
110 #  endif
111 #endif
112 
113 #include <Python.h>
114 #include <frameobject.h>
115 #include <pythread.h>
116 
117 #if defined(_WIN32) && (defined(min) || defined(max))
118 #  error Macro clash with min and max -- define NOMINMAX when compiling your program on Windows
119 #endif
120 
121 #if defined(isalnum)
122 #  undef isalnum
123 #  undef isalpha
124 #  undef islower
125 #  undef isspace
126 #  undef isupper
127 #  undef tolower
128 #  undef toupper
129 #endif
130 
131 #if defined(_MSC_VER)
132 #  if defined(PYBIND11_DEBUG_MARKER)
133 #    define _DEBUG
134 #    undef PYBIND11_DEBUG_MARKER
135 #  endif
136 #  pragma warning(pop)
137 #endif
138 
139 #include <cstddef>
140 #include <cstring>
141 #include <forward_list>
142 #include <vector>
143 #include <string>
144 #include <stdexcept>
145 #include <unordered_set>
146 #include <unordered_map>
147 #include <memory>
148 #include <typeindex>
149 #include <type_traits>
150 
151 #if PY_MAJOR_VERSION >= 3
152 #define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_) PyInstanceMethod_New(ptr)
153 #define PYBIND11_INSTANCE_METHOD_CHECK PyInstanceMethod_Check
154 #define PYBIND11_INSTANCE_METHOD_GET_FUNCTION PyInstanceMethod_GET_FUNCTION
155 #define PYBIND11_BYTES_CHECK PyBytes_Check
156 #define PYBIND11_BYTES_FROM_STRING PyBytes_FromString
157 #define PYBIND11_BYTES_FROM_STRING_AND_SIZE PyBytes_FromStringAndSize
158 #define PYBIND11_BYTES_AS_STRING_AND_SIZE PyBytes_AsStringAndSize
159 #define PYBIND11_BYTES_AS_STRING PyBytes_AsString
160 #define PYBIND11_BYTES_SIZE PyBytes_Size
161 #define PYBIND11_LONG_CHECK(o) PyLong_Check(o)
162 #define PYBIND11_LONG_AS_LONGLONG(o) PyLong_AsLongLong(o)
163 #define PYBIND11_LONG_FROM_SIGNED(o) PyLong_FromSsize_t((ssize_t) o)
164 #define PYBIND11_LONG_FROM_UNSIGNED(o) PyLong_FromSize_t((size_t) o)
165 #define PYBIND11_BYTES_NAME "bytes"
166 #define PYBIND11_STRING_NAME "str"
167 #define PYBIND11_SLICE_OBJECT PyObject
168 #define PYBIND11_FROM_STRING PyUnicode_FromString
169 #define PYBIND11_STR_TYPE ::pybind11::str
170 #define PYBIND11_BOOL_ATTR "__bool__"
171 #define PYBIND11_NB_BOOL(ptr) ((ptr)->nb_bool)
172 #define PYBIND11_PLUGIN_IMPL(name) \
173     extern "C" PYBIND11_EXPORT PyObject *PyInit_##name()
174 
175 #else
176 #define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_) PyMethod_New(ptr, nullptr, class_)
177 #define PYBIND11_INSTANCE_METHOD_CHECK PyMethod_Check
178 #define PYBIND11_INSTANCE_METHOD_GET_FUNCTION PyMethod_GET_FUNCTION
179 #define PYBIND11_BYTES_CHECK PyString_Check
180 #define PYBIND11_BYTES_FROM_STRING PyString_FromString
181 #define PYBIND11_BYTES_FROM_STRING_AND_SIZE PyString_FromStringAndSize
182 #define PYBIND11_BYTES_AS_STRING_AND_SIZE PyString_AsStringAndSize
183 #define PYBIND11_BYTES_AS_STRING PyString_AsString
184 #define PYBIND11_BYTES_SIZE PyString_Size
185 #define PYBIND11_LONG_CHECK(o) (PyInt_Check(o) || PyLong_Check(o))
186 #define PYBIND11_LONG_AS_LONGLONG(o) (PyInt_Check(o) ? (long long) PyLong_AsLong(o) : PyLong_AsLongLong(o))
187 #define PYBIND11_LONG_FROM_SIGNED(o) PyInt_FromSsize_t((ssize_t) o)
188 #define PYBIND11_LONG_FROM_UNSIGNED(o) PyInt_FromSize_t((size_t) o)
189 #define PYBIND11_BYTES_NAME "str"
190 #define PYBIND11_STRING_NAME "unicode"
191 #define PYBIND11_SLICE_OBJECT PySliceObject
192 #define PYBIND11_FROM_STRING PyString_FromString
193 #define PYBIND11_STR_TYPE ::pybind11::bytes
194 #define PYBIND11_BOOL_ATTR "__nonzero__"
195 #define PYBIND11_NB_BOOL(ptr) ((ptr)->nb_nonzero)
196 #define PYBIND11_PLUGIN_IMPL(name) \
197     static PyObject *pybind11_init_wrapper();               \
198     extern "C" PYBIND11_EXPORT void init##name() {          \
199         (void)pybind11_init_wrapper();                      \
200     }                                                       \
201     PyObject *pybind11_init_wrapper()
202 #endif
203 
204 #if PY_VERSION_HEX >= 0x03050000 && PY_VERSION_HEX < 0x03050200
205 extern "C" {
206     struct _Py_atomic_address { void *value; };
207     PyAPI_DATA(_Py_atomic_address) _PyThreadState_Current;
208 }
209 #endif
210 
211 #define PYBIND11_TRY_NEXT_OVERLOAD ((PyObject *) 1)
212 #define PYBIND11_STRINGIFY(x) #x
213 #define PYBIND11_TOSTRING(x) PYBIND11_STRINGIFY(x)
214 #define PYBIND11_CONCAT(first, second) first##second
215 
216 #define PYBIND11_CHECK_PYTHON_VERSION \
217     {                                                                          \
218         const char *compiled_ver = PYBIND11_TOSTRING(PY_MAJOR_VERSION)         \
219             "." PYBIND11_TOSTRING(PY_MINOR_VERSION);                           \
220         const char *runtime_ver = Py_GetVersion();                             \
221         size_t len = std::strlen(compiled_ver);                                \
222         if (std::strncmp(runtime_ver, compiled_ver, len) != 0                  \
223                 || (runtime_ver[len] >= '0' && runtime_ver[len] <= '9')) {     \
224             PyErr_Format(PyExc_ImportError,                                    \
225                 "Python version mismatch: module was compiled for Python %s, " \
226                 "but the interpreter version is incompatible: %s.",            \
227                 compiled_ver, runtime_ver);                                    \
228             return nullptr;                                                    \
229         }                                                                      \
230     }
231 
232 #define PYBIND11_CATCH_INIT_EXCEPTIONS \
233         catch (pybind11::error_already_set &e) {                               \
234             PyErr_SetString(PyExc_ImportError, e.what());                      \
235             return nullptr;                                                    \
236         } catch (const std::exception &e) {                                    \
237             PyErr_SetString(PyExc_ImportError, e.what());                      \
238             return nullptr;                                                    \
239         }                                                                      \
240 
241 
242 #define PYBIND11_PLUGIN(name)                                                  \
243     PYBIND11_DEPRECATED("PYBIND11_PLUGIN is deprecated, use PYBIND11_MODULE")  \
244     static PyObject *pybind11_init();                                          \
245     PYBIND11_PLUGIN_IMPL(name) {                                               \
246         PYBIND11_CHECK_PYTHON_VERSION                                          \
247         try {                                                                  \
248             return pybind11_init();                                            \
249         } PYBIND11_CATCH_INIT_EXCEPTIONS                                       \
250     }                                                                          \
251     PyObject *pybind11_init()
252 
253 
254 #define PYBIND11_MODULE(name, variable)                                        \
255     static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &);     \
256     PYBIND11_PLUGIN_IMPL(name) {                                               \
257         PYBIND11_CHECK_PYTHON_VERSION                                          \
258         auto m = pybind11::module(PYBIND11_TOSTRING(name));                    \
259         try {                                                                  \
260             PYBIND11_CONCAT(pybind11_init_, name)(m);                          \
261             return m.ptr();                                                    \
262         } PYBIND11_CATCH_INIT_EXCEPTIONS                                       \
263     }                                                                          \
264     void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &variable)
265 
266 
267 NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
268 
269 using ssize_t = Py_ssize_t;
270 using size_t  = std::size_t;
271 
272 
273 enum class return_value_policy : uint8_t {
274 
275     automatic = 0,
276 
277 
278     automatic_reference,
279 
280 
281     take_ownership,
282 
283 
284     copy,
285 
286 
287     move,
288 
289 
290     reference,
291 
292 
293     reference_internal
294 };
295 
NAMESPACE_BEGIN(detail)296 NAMESPACE_BEGIN(detail)
297 
298 inline static constexpr int log2(size_t n, int k = 0) { return (n <= 1) ? k : log2(n >> 1, k + 1); }
299 
300 
size_in_ptrs(size_t s)301 inline static constexpr size_t size_in_ptrs(size_t s) { return 1 + ((s - 1) >> log2(sizeof(void *))); }
302 
303 
instance_simple_holder_in_ptrs()304 constexpr size_t instance_simple_holder_in_ptrs() {
305     static_assert(sizeof(std::shared_ptr<int>) >= sizeof(std::unique_ptr<int>),
306             "pybind assumes std::shared_ptrs are at least as big as std::unique_ptrs");
307     return size_in_ptrs(sizeof(std::shared_ptr<int>));
308 }
309 
310 
311 struct type_info;
312 struct value_and_holder;
313 
314 struct nonsimple_values_and_holders {
315     void **values_and_holders;
316     uint8_t *status;
317 };
318 
319 
320 struct instance {
321     PyObject_HEAD
322 
323     union {
324         void *simple_value_holder[1 + instance_simple_holder_in_ptrs()];
325         nonsimple_values_and_holders nonsimple;
326     };
327 
328     PyObject *weakrefs;
329 
330     bool owned : 1;
331 
332     bool simple_layout : 1;
333 
334     bool simple_holder_constructed : 1;
335 
336     bool simple_instance_registered : 1;
337 
338     bool has_patients : 1;
339 
340 
341     void allocate_layout();
342 
343 
344     void deallocate_layout();
345 
346 
347 
348 
349     value_and_holder get_value_and_holder(const type_info *find_type = nullptr, bool throw_if_missing = true);
350 
351 
352     static constexpr uint8_t status_holder_constructed  = 1;
353     static constexpr uint8_t status_instance_registered = 2;
354 };
355 
356 static_assert(std::is_standard_layout<instance>::value, "Internal error: `pybind11::detail::instance` is not standard layout!");
357 
358 
359 #if defined(PYBIND11_CPP14) && (!defined(_MSC_VER) || _MSC_VER >= 1910)
360 using std::enable_if_t;
361 using std::conditional_t;
362 using std::remove_cv_t;
363 using std::remove_reference_t;
364 #else
365 template <bool B, typename T = void> using enable_if_t = typename std::enable_if<B, T>::type;
366 template <bool B, typename T, typename F> using conditional_t = typename std::conditional<B, T, F>::type;
367 template <typename T> using remove_cv_t = typename std::remove_cv<T>::type;
368 template <typename T> using remove_reference_t = typename std::remove_reference<T>::type;
369 #endif
370 
371 
372 #if defined(PYBIND11_CPP14)
373 using std::index_sequence;
374 using std::make_index_sequence;
375 #else
376 template<size_t ...> struct index_sequence  { };
377 template<size_t N, size_t ...S> struct make_index_sequence_impl : make_index_sequence_impl <N - 1, N - 1, S...> { };
378 template<size_t ...S> struct make_index_sequence_impl <0, S...> { typedef index_sequence<S...> type; };
379 template<size_t N> using make_index_sequence = typename make_index_sequence_impl<N>::type;
380 #endif
381 
382 
383 template <typename ISeq, size_t, bool...> struct select_indices_impl { using type = ISeq; };
384 template <size_t... IPrev, size_t I, bool B, bool... Bs> struct select_indices_impl<index_sequence<IPrev...>, I, B, Bs...>
385     : select_indices_impl<conditional_t<B, index_sequence<IPrev..., I>, index_sequence<IPrev...>>, I + 1, Bs...> {};
386 template <bool... Bs> using select_indices = typename select_indices_impl<index_sequence<>, 0, Bs...>::type;
387 
388 
389 template <bool B> using bool_constant = std::integral_constant<bool, B>;
390 template <typename T> struct negation : bool_constant<!T::value> { };
391 
392 template <typename...> struct void_t_impl { using type = void; };
393 template <typename... Ts> using void_t = typename void_t_impl<Ts...>::type;
394 
395 
396 #if defined(__cpp_fold_expressions) && !(defined(_MSC_VER) && (_MSC_VER < 1916))
397 template <class... Ts> using all_of = bool_constant<(Ts::value && ...)>;
398 template <class... Ts> using any_of = bool_constant<(Ts::value || ...)>;
399 #elif !defined(_MSC_VER)
400 template <bool...> struct bools {};
401 template <class... Ts> using all_of = std::is_same<
402     bools<Ts::value..., true>,
403     bools<true, Ts::value...>>;
404 template <class... Ts> using any_of = negation<all_of<negation<Ts>...>>;
405 #else
406 
407 
408 template <class... Ts> using all_of = std::conjunction<Ts...>;
409 template <class... Ts> using any_of = std::disjunction<Ts...>;
410 #endif
411 template <class... Ts> using none_of = negation<any_of<Ts...>>;
412 
413 template <class T, template<class> class... Predicates> using satisfies_all_of = all_of<Predicates<T>...>;
414 template <class T, template<class> class... Predicates> using satisfies_any_of = any_of<Predicates<T>...>;
415 template <class T, template<class> class... Predicates> using satisfies_none_of = none_of<Predicates<T>...>;
416 
417 
418 template <typename T> struct remove_class { };
419 template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...)> { typedef R type(A...); };
420 template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...) const> { typedef R type(A...); };
421 
422 
423 template <typename T> struct intrinsic_type                       { typedef T type; };
424 template <typename T> struct intrinsic_type<const T>              { typedef typename intrinsic_type<T>::type type; };
425 template <typename T> struct intrinsic_type<T*>                   { typedef typename intrinsic_type<T>::type type; };
426 template <typename T> struct intrinsic_type<T&>                   { typedef typename intrinsic_type<T>::type type; };
427 template <typename T> struct intrinsic_type<T&&>                  { typedef typename intrinsic_type<T>::type type; };
428 template <typename T, size_t N> struct intrinsic_type<const T[N]> { typedef typename intrinsic_type<T>::type type; };
429 template <typename T, size_t N> struct intrinsic_type<T[N]>       { typedef typename intrinsic_type<T>::type type; };
430 template <typename T> using intrinsic_t = typename intrinsic_type<T>::type;
431 
432 
433 struct void_type { };
434 
435 
436 template <typename...> struct type_list { };
437 
438 
439 #ifdef __cpp_fold_expressions
440 template <typename... Ts> constexpr size_t constexpr_sum(Ts... ns) { return (0 + ... + size_t{ns}); }
441 #else
442 constexpr size_t constexpr_sum() { return 0; }
443 template <typename T, typename... Ts>
444 constexpr size_t constexpr_sum(T n, Ts... ns) { return size_t{n} + constexpr_sum(ns...); }
445 #endif
446 
447 NAMESPACE_BEGIN(constexpr_impl)
448 
449 constexpr int first(int i) { return i; }
450 template <typename T, typename... Ts>
451 constexpr int first(int i, T v, Ts... vs) { return v ? i : first(i + 1, vs...); }
452 
453 constexpr int last(int  , int result) { return result; }
454 template <typename T, typename... Ts>
455 constexpr int last(int i, int result, T v, Ts... vs) { return last(i + 1, v ? i : result, vs...); }
456 NAMESPACE_END(constexpr_impl)
457 
458 
459 
460 template <template<typename> class Predicate, typename... Ts>
461 constexpr int constexpr_first() { return constexpr_impl::first(0, Predicate<Ts>::value...); }
462 
463 
464 template <template<typename> class Predicate, typename... Ts>
465 constexpr int constexpr_last() { return constexpr_impl::last(0, -1, Predicate<Ts>::value...); }
466 
467 
468 template <size_t N, typename T, typename... Ts>
469 struct pack_element { using type = typename pack_element<N - 1, Ts...>::type; };
470 template <typename T, typename... Ts>
471 struct pack_element<0, T, Ts...> { using type = T; };
472 
473 
474 
475 template <template<typename> class Predicate, typename Default, typename... Ts>
476 struct exactly_one {
477     static constexpr auto found = constexpr_sum(Predicate<Ts>::value...);
478     static_assert(found <= 1, "Found more than one type matching the predicate");
479 
480     static constexpr auto index = found ? constexpr_first<Predicate, Ts...>() : 0;
481     using type = conditional_t<found, typename pack_element<index, Ts...>::type, Default>;
482 };
483 template <template<typename> class P, typename Default>
484 struct exactly_one<P, Default> { using type = Default; };
485 
486 template <template<typename> class Predicate, typename Default, typename... Ts>
487 using exactly_one_t = typename exactly_one<Predicate, Default, Ts...>::type;
488 
489 
490 template <typename T, typename...  > struct deferred_type { using type = T; };
491 template <typename T, typename... Us> using deferred_t = typename deferred_type<T, Us...>::type;
492 
493 
494 
495 template <typename Base, typename Derived> using is_strict_base_of = bool_constant<
496     std::is_base_of<Base, Derived>::value && !std::is_same<Base, Derived>::value>;
497 
498 
499 
500 template <typename Base, typename Derived> using is_accessible_base_of = bool_constant<
501     std::is_base_of<Base, Derived>::value && std::is_convertible<Derived *, Base *>::value>;
502 
503 template <template<typename...> class Base>
504 struct is_template_base_of_impl {
505     template <typename... Us> static std::true_type check(Base<Us...> *);
506     static std::false_type check(...);
507 };
508 
509 
510 
511 template <template<typename...> class Base, typename T>
512 #if !defined(_MSC_VER)
513 using is_template_base_of = decltype(is_template_base_of_impl<Base>::check((intrinsic_t<T>*)nullptr));
514 #else
515 struct is_template_base_of : decltype(is_template_base_of_impl<Base>::check((intrinsic_t<T>*)nullptr)) { };
516 #endif
517 
518 
519 
520 template <template<typename...> class Class, typename T>
521 struct is_instantiation : std::false_type { };
522 template <template<typename...> class Class, typename... Us>
523 struct is_instantiation<Class, Class<Us...>> : std::true_type { };
524 
525 
526 template <typename T> using is_shared_ptr = is_instantiation<std::shared_ptr, T>;
527 
528 
529 template <typename T, typename = void> struct is_input_iterator : std::false_type {};
530 template <typename T>
531 struct is_input_iterator<T, void_t<decltype(*std::declval<T &>()), decltype(++std::declval<T &>())>>
532     : std::true_type {};
533 
534 template <typename T> using is_function_pointer = bool_constant<
535     std::is_pointer<T>::value && std::is_function<typename std::remove_pointer<T>::type>::value>;
536 
537 template <typename F> struct strip_function_object {
538     using type = typename remove_class<decltype(&F::operator())>::type;
539 };
540 
541 
542 template <typename Function, typename F = remove_reference_t<Function>>
543 using function_signature_t = conditional_t<
544     std::is_function<F>::value,
545     F,
546     typename conditional_t<
547         std::is_pointer<F>::value || std::is_member_pointer<F>::value,
548         std::remove_pointer<F>,
549         strip_function_object<F>
550     >::type
551 >;
552 
553 
554 
555 
556 template <typename T> using is_lambda = satisfies_none_of<remove_reference_t<T>,
557         std::is_function, std::is_pointer, std::is_member_pointer>;
558 
559 
560 inline void ignore_unused(const int *) { }
561 
562 
563 #ifdef __cpp_fold_expressions
564 #define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN) (((PATTERN), void()), ...)
565 #else
566 using expand_side_effects = bool[];
567 #define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN) pybind11::detail::expand_side_effects{ ((PATTERN), void(), false)..., false }
568 #endif
569 
570 NAMESPACE_END(detail)
571 
572 
573 class builtin_exception : public std::runtime_error {
574 public:
575     using std::runtime_error::runtime_error;
576 
577     virtual void set_error() const = 0;
578 };
579 
580 #define PYBIND11_RUNTIME_EXCEPTION(name, type) \
581     class name : public builtin_exception { public: \
582         using builtin_exception::builtin_exception; \
583         name() : name("") { } \
584         void set_error() const override { PyErr_SetString(type, what()); } \
585     };
586 
587 PYBIND11_RUNTIME_EXCEPTION(stop_iteration, PyExc_StopIteration)
588 PYBIND11_RUNTIME_EXCEPTION(index_error, PyExc_IndexError)
589 PYBIND11_RUNTIME_EXCEPTION(key_error, PyExc_KeyError)
590 PYBIND11_RUNTIME_EXCEPTION(value_error, PyExc_ValueError)
591 PYBIND11_RUNTIME_EXCEPTION(type_error, PyExc_TypeError)
592 PYBIND11_RUNTIME_EXCEPTION(cast_error, PyExc_RuntimeError)
593 PYBIND11_RUNTIME_EXCEPTION(reference_cast_error, PyExc_RuntimeError)
594 
595 [[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const char *reason) { throw std::runtime_error(reason); }
596 [[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const std::string &reason) { throw std::runtime_error(reason); }
597 
598 template <typename T, typename SFINAE = void> struct format_descriptor { };
599 
600 NAMESPACE_BEGIN(detail)
601 
602 
603 
604 
605 
606 template <typename T, typename SFINAE = void> struct is_fmt_numeric { static constexpr bool value = false; };
607 template <typename T> struct is_fmt_numeric<T, enable_if_t<std::is_arithmetic<T>::value>> {
608     static constexpr bool value = true;
609     static constexpr int index = std::is_same<T, bool>::value ? 0 : 1 + (
610         std::is_integral<T>::value ? detail::log2(sizeof(T))*2 + std::is_unsigned<T>::value : 8 + (
611         std::is_same<T, double>::value ? 1 : std::is_same<T, long double>::value ? 2 : 0));
612 };
613 NAMESPACE_END(detail)
614 
615 template <typename T> struct format_descriptor<T, detail::enable_if_t<std::is_arithmetic<T>::value>> {
616     static constexpr const char c = "?bBhHiIqQfdg"[detail::is_fmt_numeric<T>::index];
617     static constexpr const char value[2] = { c, '\0' };
618     static std::string format() { return std::string(1, c); }
619 };
620 
621 #if !defined(PYBIND11_CPP17)
622 
623 template <typename T> constexpr const char format_descriptor<
624     T, detail::enable_if_t<std::is_arithmetic<T>::value>>::value[2];
625 
626 #endif
627 
628 
629 struct error_scope {
630     PyObject *type, *value, *trace;
631     error_scope() { PyErr_Fetch(&type, &value, &trace); }
632     ~error_scope() { PyErr_Restore(type, value, trace); }
633 };
634 
635 
636 struct nodelete { template <typename T> void operator()(T*) { } };
637 
638 
639 #if defined(PYBIND11_CPP14)
640 #define PYBIND11_OVERLOAD_CAST 1
641 
642 NAMESPACE_BEGIN(detail)
643 template <typename... Args>
644 struct overload_cast_impl {
645     constexpr overload_cast_impl() {}
646 
647     template <typename Return>
648     constexpr auto operator()(Return (*pf)(Args...)) const noexcept
649                               -> decltype(pf) { return pf; }
650 
651     template <typename Return, typename Class>
652     constexpr auto operator()(Return (Class::*pmf)(Args...), std::false_type = {}) const noexcept
653                               -> decltype(pmf) { return pmf; }
654 
655     template <typename Return, typename Class>
656     constexpr auto operator()(Return (Class::*pmf)(Args...) const, std::true_type) const noexcept
657                               -> decltype(pmf) { return pmf; }
658 };
659 NAMESPACE_END(detail)
660 
661 
662 
663 
664 template <typename... Args>
665 static constexpr detail::overload_cast_impl<Args...> overload_cast = {};
666 
667 
668 
669 
670 
671 static constexpr auto const_ = std::true_type{};
672 
673 #else
674 template <typename... Args> struct overload_cast {
675     static_assert(detail::deferred_t<std::false_type, Args...>::value,
676                   "pybind11::overload_cast<...> requires compiling in C++14 mode");
677 };
678 #endif
679 
680 NAMESPACE_BEGIN(detail)
681 
682 
683 
684 
685 template <typename T>
686 class any_container {
687     std::vector<T> v;
688 public:
689     any_container() = default;
690 
691 
692     template <typename It, typename = enable_if_t<is_input_iterator<It>::value>>
693     any_container(It first, It last) : v(first, last) { }
694 
695 
696     template <typename Container, typename = enable_if_t<std::is_convertible<decltype(*std::begin(std::declval<const Container &>())), T>::value>>
697     any_container(const Container &c) : any_container(std::begin(c), std::end(c)) { }
698 
699 
700 
701     template <typename TIn, typename = enable_if_t<std::is_convertible<TIn, T>::value>>
702     any_container(const std::initializer_list<TIn> &c) : any_container(c.begin(), c.end()) { }
703 
704 
705     any_container(std::vector<T> &&v) : v(std::move(v)) { }
706 
707 
708     operator std::vector<T> &&() && { return std::move(v); }
709 
710 
711     std::vector<T> &operator*() { return v; }
712     const std::vector<T> &operator*() const { return v; }
713 
714 
715     std::vector<T> *operator->() { return &v; }
716     const std::vector<T> *operator->() const { return &v; }
717 };
718 
719 NAMESPACE_END(detail)
720 
721 
722 
723 NAMESPACE_END(PYBIND11_NAMESPACE)
724