wrap/pybind11/include/pybind11/detail/common.h
Go to the documentation of this file.
1 /*
2  pybind11/detail/common.h -- Basic macros
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 #define PYBIND11_VERSION_MAJOR 2
13 #define PYBIND11_VERSION_MINOR 10
14 #define PYBIND11_VERSION_PATCH 0
15 
16 // Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html
17 // Additional convention: 0xD = dev
18 #define PYBIND11_VERSION_HEX 0x020A0000
19 
20 #define PYBIND11_NAMESPACE_BEGIN(name) namespace name {
21 #define PYBIND11_NAMESPACE_END(name) }
22 
23 // Robust support for some features and loading modules compiled against different pybind versions
24 // requires forcing hidden visibility on pybind code, so we enforce this by setting the attribute
25 // on the main `pybind11` namespace.
26 #if !defined(PYBIND11_NAMESPACE)
27 # ifdef __GNUG__
28 # define PYBIND11_NAMESPACE pybind11 __attribute__((visibility("hidden")))
29 # else
30 # define PYBIND11_NAMESPACE pybind11
31 # endif
32 #endif
33 
34 #if !(defined(_MSC_VER) && __cplusplus == 199711L)
35 # if __cplusplus >= 201402L
36 # define PYBIND11_CPP14
37 # if __cplusplus >= 201703L
38 # define PYBIND11_CPP17
39 # if __cplusplus >= 202002L
40 # define PYBIND11_CPP20
41 // Please update tests/pybind11_tests.cpp `cpp_std()` when adding a macro here.
42 # endif
43 # endif
44 # endif
45 #elif defined(_MSC_VER) && __cplusplus == 199711L
46 // MSVC sets _MSVC_LANG rather than __cplusplus (supposedly until the standard is fully
47 // implemented). Unless you use the /Zc:__cplusplus flag on Visual Studio 2017 15.7 Preview 3
48 // or newer.
49 # if _MSVC_LANG >= 201402L
50 # define PYBIND11_CPP14
51 # if _MSVC_LANG > 201402L
52 # define PYBIND11_CPP17
53 # if _MSVC_LANG >= 202002L
54 # define PYBIND11_CPP20
55 # endif
56 # endif
57 # endif
58 #endif
59 
60 // Compiler version assertions
61 #if defined(__INTEL_COMPILER)
62 # if __INTEL_COMPILER < 1800
63 # error pybind11 requires Intel C++ compiler v18 or newer
64 # elif __INTEL_COMPILER < 1900 && defined(PYBIND11_CPP14)
65 # error pybind11 supports only C++11 with Intel C++ compiler v18. Use v19 or newer for C++14.
66 # endif
67 /* The following pragma cannot be pop'ed:
68  https://community.intel.com/t5/Intel-C-Compiler/Inline-and-no-inline-warning/td-p/1216764 */
69 # pragma warning disable 2196 // warning #2196: routine is both "inline" and "noinline"
70 #elif defined(__clang__) && !defined(__apple_build_version__)
71 # if __clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ < 3)
72 # error pybind11 requires clang 3.3 or newer
73 # endif
74 #elif defined(__clang__)
75 // Apple changes clang version macros to its Xcode version; the first Xcode release based on
76 // (upstream) clang 3.3 was Xcode 5:
77 # if __clang_major__ < 5
78 # error pybind11 requires Xcode/clang 5.0 or newer
79 # endif
80 #elif defined(__GNUG__)
81 # if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)
82 # error pybind11 requires gcc 4.8 or newer
83 # endif
84 #elif defined(_MSC_VER)
85 # if _MSC_VER < 1910
86 # error pybind11 2.10+ requires MSVC 2017 or newer
87 # endif
88 #endif
89 
90 #if !defined(PYBIND11_EXPORT)
91 # if defined(WIN32) || defined(_WIN32)
92 # define PYBIND11_EXPORT __declspec(dllexport)
93 # else
94 # define PYBIND11_EXPORT __attribute__((visibility("default")))
95 # endif
96 #endif
97 
98 #if !defined(PYBIND11_EXPORT_EXCEPTION)
99 # ifdef __MINGW32__
100 // workaround for:
101 // error: 'dllexport' implies default visibility, but xxx has already been declared with a
102 // different visibility
103 # define PYBIND11_EXPORT_EXCEPTION
104 # else
105 # define PYBIND11_EXPORT_EXCEPTION PYBIND11_EXPORT
106 # endif
107 #endif
108 
109 // For CUDA, GCC7, GCC8:
110 // PYBIND11_NOINLINE_FORCED is incompatible with `-Wattributes -Werror`.
111 // When defining PYBIND11_NOINLINE_FORCED, it is best to also use `-Wno-attributes`.
112 // However, the measured shared-library size saving when using noinline are only
113 // 1.7% for CUDA, -0.2% for GCC7, and 0.0% for GCC8 (using -DCMAKE_BUILD_TYPE=MinSizeRel,
114 // the default under pybind11/tests).
115 #if !defined(PYBIND11_NOINLINE_FORCED) \
116  && (defined(__CUDACC__) || (defined(__GNUC__) && (__GNUC__ == 7 || __GNUC__ == 8)))
117 # define PYBIND11_NOINLINE_DISABLED
118 #endif
119 
120 // The PYBIND11_NOINLINE macro is for function DEFINITIONS.
121 // In contrast, FORWARD DECLARATIONS should never use this macro:
122 // https://stackoverflow.com/questions/9317473/forward-declaration-of-inline-functions
123 #if defined(PYBIND11_NOINLINE_DISABLED) // Option for maximum portability and experimentation.
124 # define PYBIND11_NOINLINE inline
125 #elif defined(_MSC_VER)
126 # define PYBIND11_NOINLINE __declspec(noinline) inline
127 #else
128 # define PYBIND11_NOINLINE __attribute__((noinline)) inline
129 #endif
130 
131 #if defined(__MINGW32__)
132 // For unknown reasons all PYBIND11_DEPRECATED member trigger a warning when declared
133 // whether it is used or not
134 # define PYBIND11_DEPRECATED(reason)
135 #elif defined(PYBIND11_CPP14)
136 # define PYBIND11_DEPRECATED(reason) [[deprecated(reason)]]
137 #else
138 # define PYBIND11_DEPRECATED(reason) __attribute__((deprecated(reason)))
139 #endif
140 
141 #if defined(PYBIND11_CPP17)
142 # define PYBIND11_MAYBE_UNUSED [[maybe_unused]]
143 #elif defined(_MSC_VER) && !defined(__clang__)
144 # define PYBIND11_MAYBE_UNUSED
145 #else
146 # define PYBIND11_MAYBE_UNUSED __attribute__((__unused__))
147 #endif
148 
149 /* Don't let Python.h #define (v)snprintf as macro because they are implemented
150  properly in Visual Studio since 2015. */
151 #if defined(_MSC_VER)
152 # define HAVE_SNPRINTF 1
153 #endif
154 
156 #if defined(_MSC_VER)
157 # pragma warning(push)
158 // C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only)
159 # pragma warning(disable : 4505)
160 # if defined(_DEBUG) && !defined(Py_DEBUG)
161 // Workaround for a VS 2022 issue.
162 // NOTE: This workaround knowingly violates the Python.h include order requirement:
163 // https://docs.python.org/3/c-api/intro.html#include-files
164 // See https://github.com/pybind/pybind11/pull/3497 for full context.
165 # include <yvals.h>
166 # if _MSVC_STL_VERSION >= 143
167 # include <crtdefs.h>
168 # endif
169 # define PYBIND11_DEBUG_MARKER
170 # undef _DEBUG
171 # endif
172 #endif
173 
174 // https://en.cppreference.com/w/c/chrono/localtime
175 #if defined(__STDC_LIB_EXT1__) && !defined(__STDC_WANT_LIB_EXT1__)
176 # define __STDC_WANT_LIB_EXT1__
177 #endif
178 
179 #ifdef __has_include
180 // std::optional (but including it in c++14 mode isn't allowed)
181 # if defined(PYBIND11_CPP17) && __has_include(<optional>)
182 # define PYBIND11_HAS_OPTIONAL 1
183 # endif
184 // std::experimental::optional (but not allowed in c++11 mode)
185 # if defined(PYBIND11_CPP14) && (__has_include(<experimental/optional>) && \
186  !__has_include(<optional>))
187 # define PYBIND11_HAS_EXP_OPTIONAL 1
188 # endif
189 // std::variant
190 # if defined(PYBIND11_CPP17) && __has_include(<variant>)
191 # define PYBIND11_HAS_VARIANT 1
192 # endif
193 #elif defined(_MSC_VER) && defined(PYBIND11_CPP17)
194 # define PYBIND11_HAS_OPTIONAL 1
195 # define PYBIND11_HAS_VARIANT 1
196 #endif
197 
198 #if defined(PYBIND11_CPP17)
199 # if defined(__has_include)
200 # if __has_include(<string_view>)
201 # define PYBIND11_HAS_STRING_VIEW
202 # endif
203 # elif defined(_MSC_VER)
204 # define PYBIND11_HAS_STRING_VIEW
205 # endif
206 #endif
207 
208 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
209 # define PYBIND11_HAS_U8STRING
210 #endif
211 
212 #include <Python.h>
213 #if PY_VERSION_HEX < 0x03060000
214 # error "PYTHON < 3.6 IS UNSUPPORTED. pybind11 v2.9 was the last to support Python 2 and 3.5."
215 #endif
216 #include <frameobject.h>
217 #include <pythread.h>
218 
219 /* Python #defines overrides on all sorts of core functions, which
220  tends to weak havok in C++ codebases that expect these to work
221  like regular functions (potentially with several overloads) */
222 #if defined(isalnum)
223 # undef isalnum
224 # undef isalpha
225 # undef islower
226 # undef isspace
227 # undef isupper
228 # undef tolower
229 # undef toupper
230 #endif
231 
232 #if defined(copysign)
233 # undef copysign
234 #endif
235 
236 #if defined(_MSC_VER)
237 # if defined(PYBIND11_DEBUG_MARKER)
238 # define _DEBUG
239 # undef PYBIND11_DEBUG_MARKER
240 # endif
241 # pragma warning(pop)
242 #endif
243 
244 #include <cstddef>
245 #include <cstring>
246 #include <exception>
247 #include <forward_list>
248 #include <memory>
249 #include <stdexcept>
250 #include <string>
251 #include <type_traits>
252 #include <typeindex>
253 #include <unordered_map>
254 #include <unordered_set>
255 #include <vector>
256 #if defined(__has_include)
257 # if __has_include(<version>)
258 # include <version>
259 # endif
260 #endif
261 
262 // #define PYBIND11_STR_LEGACY_PERMISSIVE
263 // If DEFINED, pybind11::str can hold PyUnicodeObject or PyBytesObject
264 // (probably surprising and never documented, but this was the
265 // legacy behavior until and including v2.6.x). As a side-effect,
266 // pybind11::isinstance<str>() is true for both pybind11::str and
267 // pybind11::bytes.
268 // If UNDEFINED, pybind11::str can only hold PyUnicodeObject, and
269 // pybind11::isinstance<str>() is true only for pybind11::str.
270 // However, for Python 2 only (!), the pybind11::str caster
271 // implicitly decoded bytes to PyUnicodeObject. This was to ease
272 // the transition from the legacy behavior to the non-permissive
273 // behavior.
274 
276 #define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_) PyInstanceMethod_New(ptr)
277 #define PYBIND11_INSTANCE_METHOD_CHECK PyInstanceMethod_Check
278 #define PYBIND11_INSTANCE_METHOD_GET_FUNCTION PyInstanceMethod_GET_FUNCTION
279 #define PYBIND11_BYTES_CHECK PyBytes_Check
280 #define PYBIND11_BYTES_FROM_STRING PyBytes_FromString
281 #define PYBIND11_BYTES_FROM_STRING_AND_SIZE PyBytes_FromStringAndSize
282 #define PYBIND11_BYTES_AS_STRING_AND_SIZE PyBytes_AsStringAndSize
283 #define PYBIND11_BYTES_AS_STRING PyBytes_AsString
284 #define PYBIND11_BYTES_SIZE PyBytes_Size
285 #define PYBIND11_LONG_CHECK(o) PyLong_Check(o)
286 #define PYBIND11_LONG_AS_LONGLONG(o) PyLong_AsLongLong(o)
287 #define PYBIND11_LONG_FROM_SIGNED(o) PyLong_FromSsize_t((ssize_t) (o))
288 #define PYBIND11_LONG_FROM_UNSIGNED(o) PyLong_FromSize_t((size_t) (o))
289 #define PYBIND11_BYTES_NAME "bytes"
290 #define PYBIND11_STRING_NAME "str"
291 #define PYBIND11_SLICE_OBJECT PyObject
292 #define PYBIND11_FROM_STRING PyUnicode_FromString
293 #define PYBIND11_STR_TYPE ::pybind11::str
294 #define PYBIND11_BOOL_ATTR "__bool__"
295 #define PYBIND11_NB_BOOL(ptr) ((ptr)->nb_bool)
296 #define PYBIND11_BUILTINS_MODULE "builtins"
297 // Providing a separate declaration to make Clang's -Wmissing-prototypes happy.
298 // See comment for PYBIND11_MODULE below for why this is marked "maybe unused".
299 #define PYBIND11_PLUGIN_IMPL(name) \
300  extern "C" PYBIND11_MAYBE_UNUSED PYBIND11_EXPORT PyObject *PyInit_##name(); \
301  extern "C" PYBIND11_EXPORT PyObject *PyInit_##name()
302 
303 #define PYBIND11_TRY_NEXT_OVERLOAD ((PyObject *) 1) // special failure return code
304 #define PYBIND11_STRINGIFY(x) #x
305 #define PYBIND11_TOSTRING(x) PYBIND11_STRINGIFY(x)
306 #define PYBIND11_CONCAT(first, second) first##second
307 #define PYBIND11_ENSURE_INTERNALS_READY pybind11::detail::get_internals();
308 
309 #define PYBIND11_CHECK_PYTHON_VERSION \
310  { \
311  const char *compiled_ver \
312  = PYBIND11_TOSTRING(PY_MAJOR_VERSION) "." PYBIND11_TOSTRING(PY_MINOR_VERSION); \
313  const char *runtime_ver = Py_GetVersion(); \
314  size_t len = std::strlen(compiled_ver); \
315  if (std::strncmp(runtime_ver, compiled_ver, len) != 0 \
316  || (runtime_ver[len] >= '0' && runtime_ver[len] <= '9')) { \
317  PyErr_Format(PyExc_ImportError, \
318  "Python version mismatch: module was compiled for Python %s, " \
319  "but the interpreter version is incompatible: %s.", \
320  compiled_ver, \
321  runtime_ver); \
322  return nullptr; \
323  } \
324  }
325 
326 #define PYBIND11_CATCH_INIT_EXCEPTIONS \
327  catch (pybind11::error_already_set & e) { \
328  pybind11::raise_from(e, PyExc_ImportError, "initialization failed"); \
329  return nullptr; \
330  } \
331  catch (const std::exception &e) { \
332  PyErr_SetString(PyExc_ImportError, e.what()); \
333  return nullptr; \
334  }
335 
348 
351 #define PYBIND11_PLUGIN(name) \
352  PYBIND11_DEPRECATED("PYBIND11_PLUGIN is deprecated, use PYBIND11_MODULE") \
353  static PyObject *pybind11_init(); \
354  PYBIND11_PLUGIN_IMPL(name) { \
355  PYBIND11_CHECK_PYTHON_VERSION \
356  PYBIND11_ENSURE_INTERNALS_READY \
357  try { \
358  return pybind11_init(); \
359  } \
360  PYBIND11_CATCH_INIT_EXCEPTIONS \
361  } \
362  PyObject *pybind11_init()
363 
385 #define PYBIND11_MODULE(name, variable) \
386  static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name) \
387  PYBIND11_MAYBE_UNUSED; \
388  PYBIND11_MAYBE_UNUSED \
389  static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \
390  PYBIND11_PLUGIN_IMPL(name) { \
391  PYBIND11_CHECK_PYTHON_VERSION \
392  PYBIND11_ENSURE_INTERNALS_READY \
393  auto m = ::pybind11::module_::create_extension_module( \
394  PYBIND11_TOSTRING(name), nullptr, &PYBIND11_CONCAT(pybind11_module_def_, name)); \
395  try { \
396  PYBIND11_CONCAT(pybind11_init_, name)(m); \
397  return m.ptr(); \
398  } \
399  PYBIND11_CATCH_INIT_EXCEPTIONS \
400  } \
401  void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ & (variable))
402 
404 
405 using ssize_t = Py_ssize_t;
406 using size_t = std::size_t;
407 
408 template <typename IntType>
409 inline ssize_t ssize_t_cast(const IntType &val) {
410  static_assert(sizeof(IntType) <= sizeof(ssize_t), "Implicit narrowing is not permitted.");
411  return static_cast<ssize_t>(val);
412 }
413 
421  automatic = 0,
422 
428 
434 
438  copy,
439 
444  move,
445 
451  reference,
452 
464 };
465 
467 
468 inline static constexpr int log2(size_t n, int k = 0) {
469  return (n <= 1) ? k : log2(n >> 1, k + 1);
470 }
471 
472 // Returns the size as a multiple of sizeof(void *), rounded up.
473 inline static constexpr size_t size_in_ptrs(size_t s) {
474  return 1 + ((s - 1) >> log2(sizeof(void *)));
475 }
476 
483 constexpr size_t instance_simple_holder_in_ptrs() {
484  static_assert(sizeof(std::shared_ptr<int>) >= sizeof(std::unique_ptr<int>),
485  "pybind assumes std::shared_ptrs are at least as big as std::unique_ptrs");
486  return size_in_ptrs(sizeof(std::shared_ptr<int>));
487 }
488 
489 // Forward declarations
490 struct type_info;
491 struct value_and_holder;
492 
496 };
497 
499 struct instance {
500  PyObject_HEAD
502  union {
503  void *simple_value_holder[1 + instance_simple_holder_in_ptrs()];
505  };
507  PyObject *weakrefs;
509  bool owned : 1;
533  bool simple_layout : 1;
539  bool has_patients : 1;
540 
543  void allocate_layout();
544 
546  void deallocate_layout();
547 
551  value_and_holder get_value_and_holder(const type_info *find_type = nullptr,
552  bool throw_if_missing = true);
553 
555  static constexpr uint8_t status_holder_constructed = 1;
556  static constexpr uint8_t status_instance_registered = 2;
557 };
558 
560  "Internal error: `pybind11::detail::instance` is not standard layout!");
561 
563 #if defined(PYBIND11_CPP14)
564 using std::conditional_t;
565 using std::enable_if_t;
566 using std::remove_cv_t;
568 #else
569 template <bool B, typename T = void>
571 template <bool B, typename T, typename F>
573 template <typename T>
575 template <typename T>
577 #endif
578 
579 #if defined(PYBIND11_CPP20)
580 using std::remove_cvref;
581 using std::remove_cvref_t;
582 #else
583 template <class T>
584 struct remove_cvref {
586 };
587 template <class T>
589 #endif
590 
592 #if defined(PYBIND11_CPP14)
593 using std::index_sequence;
595 #else
596 template <size_t...>
597 struct index_sequence {};
598 template <size_t N, size_t... S>
599 struct make_index_sequence_impl : make_index_sequence_impl<N - 1, N - 1, S...> {};
600 template <size_t... S>
602  using type = index_sequence<S...>;
603 };
604 template <size_t N>
606 #endif
607 
609 template <typename ISeq, size_t, bool...>
611  using type = ISeq;
612 };
613 template <size_t... IPrev, size_t I, bool B, bool... Bs>
614 struct select_indices_impl<index_sequence<IPrev...>, I, B, Bs...>
615  : select_indices_impl<conditional_t<B, index_sequence<IPrev..., I>, index_sequence<IPrev...>>,
616  I + 1,
617  Bs...> {};
618 template <bool... Bs>
620 
622 template <bool B>
623 using bool_constant = std::integral_constant<bool, B>;
624 template <typename T>
625 struct negation : bool_constant<!T::value> {};
626 
627 // PGI/Intel cannot detect operator delete with the "compatible" void_t impl, so
628 // using the new one (C++14 defect, so generally works on newer compilers, even
629 // if not in C++17 mode)
630 #if defined(__PGIC__) || defined(__INTEL_COMPILER)
631 template <typename...>
632 using void_t = void;
633 #else
634 template <typename...>
635 struct void_t_impl {
636  using type = void;
637 };
638 template <typename... Ts>
639 using void_t = typename void_t_impl<Ts...>::type;
640 #endif
641 
643 #if defined(__cpp_fold_expressions) && !(defined(_MSC_VER) && (_MSC_VER < 1916))
644 template <class... Ts>
645 using all_of = bool_constant<(Ts::value && ...)>;
646 template <class... Ts>
647 using any_of = bool_constant<(Ts::value || ...)>;
648 #elif !defined(_MSC_VER)
649 template <bool...>
650 struct bools {};
651 template <class... Ts>
652 using all_of = std::is_same<bools<Ts::value..., true>, bools<true, Ts::value...>>;
653 template <class... Ts>
655 #else
656 // MSVC has trouble with the above, but supports std::conjunction, which we can use instead (albeit
657 // at a slight loss of compilation efficiency).
658 template <class... Ts>
659 using all_of = std::conjunction<Ts...>;
660 template <class... Ts>
661 using any_of = std::disjunction<Ts...>;
662 #endif
663 template <class... Ts>
664 using none_of = negation<any_of<Ts...>>;
665 
666 template <class T, template <class> class... Predicates>
668 template <class T, template <class> class... Predicates>
670 template <class T, template <class> class... Predicates>
672 
674 template <typename T>
675 struct remove_class {};
676 template <typename C, typename R, typename... A>
677 struct remove_class<R (C::*)(A...)> {
678  using type = R(A...);
679 };
680 template <typename C, typename R, typename... A>
681 struct remove_class<R (C::*)(A...) const> {
682  using type = R(A...);
683 };
684 
686 template <typename T>
688  using type = T;
689 };
690 template <typename T>
691 struct intrinsic_type<const T> {
692  using type = typename intrinsic_type<T>::type;
693 };
694 template <typename T>
695 struct intrinsic_type<T *> {
696  using type = typename intrinsic_type<T>::type;
697 };
698 template <typename T>
699 struct intrinsic_type<T &> {
700  using type = typename intrinsic_type<T>::type;
701 };
702 template <typename T>
703 struct intrinsic_type<T &&> {
704  using type = typename intrinsic_type<T>::type;
705 };
706 template <typename T, size_t N>
707 struct intrinsic_type<const T[N]> {
708  using type = typename intrinsic_type<T>::type;
709 };
710 template <typename T, size_t N>
711 struct intrinsic_type<T[N]> {
712  using type = typename intrinsic_type<T>::type;
713 };
714 template <typename T>
716 
718 struct void_type {};
719 
721 template <typename...>
722 struct type_list {};
723 
725 #ifdef __cpp_fold_expressions
726 template <typename... Ts>
727 constexpr size_t constexpr_sum(Ts... ns) {
728  return (0 + ... + size_t{ns});
729 }
730 #else
731 constexpr size_t constexpr_sum() { return 0; }
732 template <typename T, typename... Ts>
733 constexpr size_t constexpr_sum(T n, Ts... ns) {
734  return size_t{n} + constexpr_sum(ns...);
735 }
736 #endif
737 
738 PYBIND11_NAMESPACE_BEGIN(constexpr_impl)
740 constexpr int first(int i) { return i; }
741 template <typename T, typename... Ts>
742 constexpr int first(int i, T v, Ts... vs) {
743  return v ? i : first(i + 1, vs...);
744 }
745 
746 constexpr int last(int /*i*/, int result) { return result; }
747 template <typename T, typename... Ts>
748 constexpr int last(int i, int result, T v, Ts... vs) {
749  return last(i + 1, v ? i : result, vs...);
750 }
751 PYBIND11_NAMESPACE_END(constexpr_impl)
752 
753 template <template <typename> class Predicate, typename... Ts>
756 constexpr int constexpr_first() {
758 }
759 
761 template <template <typename> class Predicate, typename... Ts>
762 constexpr int constexpr_last() {
763  return constexpr_impl::last(0, -1, Predicate<Ts>::value...);
764 }
765 
767 template <size_t N, typename T, typename... Ts>
768 struct pack_element {
769  using type = typename pack_element<N - 1, Ts...>::type;
770 };
771 template <typename T, typename... Ts>
772 struct pack_element<0, T, Ts...> {
773  using type = T;
774 };
775 
778 template <template <typename> class Predicate, typename Default, typename... Ts>
779 struct exactly_one {
780  static constexpr auto found = constexpr_sum(Predicate<Ts>::value...);
781  static_assert(found <= 1, "Found more than one type matching the predicate");
782 
783  static constexpr auto index = found ? constexpr_first<Predicate, Ts...>() : 0;
784  using type = conditional_t<found, typename pack_element<index, Ts...>::type, Default>;
785 };
786 template <template <typename> class P, typename Default>
788  using type = Default;
789 };
790 
791 template <template <typename> class Predicate, typename Default, typename... Ts>
792 using exactly_one_t = typename exactly_one<Predicate, Default, Ts...>::type;
793 
795 template <typename T, typename... /*Us*/>
797  using type = T;
798 };
799 template <typename T, typename... Us>
800 using deferred_t = typename deferred_type<T, Us...>::type;
801 
804 template <typename Base, typename Derived>
805 using is_strict_base_of
807 
811 template <typename Base, typename Derived>
815 
816 template <template <typename...> class Base>
818  template <typename... Us>
819  static std::true_type check(Base<Us...> *);
820  static std::false_type check(...);
821 };
822 
825 template <template <typename...> class Base, typename T>
826 // Sadly, all MSVC versions incl. 2022 need the workaround, even in C++20 mode.
827 // See also: https://github.com/pybind/pybind11/pull/3741
828 #if !defined(_MSC_VER)
831 #else
832 struct is_template_base_of
833  : decltype(is_template_base_of_impl<Base>::check((intrinsic_t<T> *) nullptr)) {
834 };
835 #endif
836 
839 template <template <typename...> class Class, typename T>
840 struct is_instantiation : std::false_type {};
841 template <template <typename...> class Class, typename... Us>
842 struct is_instantiation<Class, Class<Us...>> : std::true_type {};
843 
845 template <typename T>
847 
849 template <typename T, typename = void>
850 struct is_input_iterator : std::false_type {};
851 template <typename T>
853  void_t<decltype(*std::declval<T &>()), decltype(++std::declval<T &>())>>
854  : std::true_type {};
855 
856 template <typename T>
860 
861 template <typename F>
863  // If you are encountering an
864  // 'error: name followed by "::" must be a class or namespace name'
865  // with the Intel compiler and a noexcept function here,
866  // try to use noexcept(true) instead of plain noexcept.
868 };
869 
870 // Extracts the function signature from a function, function pointer or lambda.
871 template <typename Function, typename F = remove_reference_t<Function>>
874  F,
876  std::remove_pointer<F>,
878 
882 template <typename T>
884  std::is_function,
885  std::is_pointer,
886  std::is_member_pointer>;
887 
888 // [workaround(intel)] Internal error on fold expression
890 #if defined(__cpp_fold_expressions) && !defined(__INTEL_COMPILER)
891 // Intel compiler produces an internal error on this fold expression (tested with ICC 19.0.2)
892 # define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN) (((PATTERN), void()), ...)
893 #else
894 using expand_side_effects = bool[];
895 # define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN) \
896  (void) pybind11::detail::expand_side_effects { ((PATTERN), void(), false)..., false }
897 #endif
898 
900 
901 #if defined(_MSC_VER)
902 # pragma warning(push)
903 # pragma warning(disable : 4275)
904 // warning C4275: An exported class was derived from a class that wasn't exported.
905 // Can be ignored when derived from a STL class.
906 #endif
907 class PYBIND11_EXPORT_EXCEPTION builtin_exception : public std::runtime_error {
909 public:
910  using std::runtime_error::runtime_error;
912  virtual void set_error() const = 0;
913 };
914 #if defined(_MSC_VER)
915 # pragma warning(pop)
916 #endif
917 
918 #define PYBIND11_RUNTIME_EXCEPTION(name, type) \
919  class PYBIND11_EXPORT_EXCEPTION name : public builtin_exception { \
920  public: \
921  using builtin_exception::builtin_exception; \
922  name() : name("") {} \
923  void set_error() const override { PyErr_SetString(type, what()); } \
924  };
925 
926 PYBIND11_RUNTIME_EXCEPTION(stop_iteration, PyExc_StopIteration)
927 PYBIND11_RUNTIME_EXCEPTION(index_error, PyExc_IndexError)
928 PYBIND11_RUNTIME_EXCEPTION(key_error, PyExc_KeyError)
929 PYBIND11_RUNTIME_EXCEPTION(value_error, PyExc_ValueError)
930 PYBIND11_RUNTIME_EXCEPTION(type_error, PyExc_TypeError)
931 PYBIND11_RUNTIME_EXCEPTION(buffer_error, PyExc_BufferError)
932 PYBIND11_RUNTIME_EXCEPTION(import_error, PyExc_ImportError)
933 PYBIND11_RUNTIME_EXCEPTION(attribute_error, PyExc_AttributeError)
934 PYBIND11_RUNTIME_EXCEPTION(cast_error, PyExc_RuntimeError)
935 PYBIND11_RUNTIME_EXCEPTION(reference_cast_error, PyExc_RuntimeError)
938 
939 [[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason) {
940  assert(!PyErr_Occurred());
941  throw std::runtime_error(reason);
942 }
943 [[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const std::string &reason) {
944  assert(!PyErr_Occurred());
945  throw std::runtime_error(reason);
946 }
947 
948 template <typename T, typename SFINAE = void>
950 
952 // Returns the index of the given type in the type char array below, and in the list in numpy.h
953 // The order here is: bool; 8 ints ((signed,unsigned)x(8,16,32,64)bits); float,double,long double;
954 // complex float,double,long double. Note that the long double types only participate when long
955 // double is actually longer than double (it isn't under MSVC).
956 // NB: not only the string below but also complex.h and numpy.h rely on this order.
957 template <typename T, typename SFINAE = void>
959  static constexpr bool value = false;
960 };
961 template <typename T>
962 struct is_fmt_numeric<T, enable_if_t<std::is_arithmetic<T>::value>> {
963  static constexpr bool value = true;
964  static constexpr int index
966  ? 0
967  : 1
969  ? detail::log2(sizeof(T)) * 2 + std::is_unsigned<T>::value
970  : 8
973  : 0));
974 };
976 
977 template <typename T>
978 struct format_descriptor<T, detail::enable_if_t<std::is_arithmetic<T>::value>> {
979  static constexpr const char c = "?bBhHiIqQfdg"[detail::is_fmt_numeric<T>::index];
980  static constexpr const char value[2] = {c, '\0'};
981  static std::string format() { return std::string(1, c); }
982 };
983 
984 #if !defined(PYBIND11_CPP17)
985 
986 template <typename T>
987 constexpr const char
989 
990 #endif
991 
993 struct error_scope {
994  PyObject *type, *value, *trace;
995  error_scope() { PyErr_Fetch(&type, &value, &trace); }
996  error_scope(const error_scope &) = delete;
997  error_scope &operator=(const error_scope &) = delete;
998  ~error_scope() { PyErr_Restore(type, value, trace); }
999 };
1000 
1002 struct nodelete {
1003  template <typename T>
1004  void operator()(T *) {}
1005 };
1006 
1008 template <typename... Args>
1010  template <typename Return>
1011  constexpr auto operator()(Return (*pf)(Args...)) const noexcept -> decltype(pf) {
1012  return pf;
1013  }
1014 
1015  template <typename Return, typename Class>
1016  constexpr auto operator()(Return (Class::*pmf)(Args...), std::false_type = {}) const noexcept
1017  -> decltype(pmf) {
1018  return pmf;
1019  }
1020 
1021  template <typename Return, typename Class>
1022  constexpr auto operator()(Return (Class::*pmf)(Args...) const, std::true_type) const noexcept
1023  -> decltype(pmf) {
1024  return pmf;
1025  }
1026 };
1028 
1029 // overload_cast requires variable templates: C++14
1030 #if defined(PYBIND11_CPP14)
1031 # define PYBIND11_OVERLOAD_CAST 1
1032 template <typename... Args>
1036 # if (defined(_MSC_VER) && _MSC_VER < 1920) /* MSVC 2017 */ \
1037  || (defined(__clang__) && __clang_major__ == 5)
1038 static constexpr detail::overload_cast_impl<Args...> overload_cast = {};
1039 # else
1040 static constexpr detail::overload_cast_impl<Args...> overload_cast;
1041 # endif
1042 #endif
1043 
1047 static constexpr auto const_ = std::true_type{};
1048 
1049 #if !defined(PYBIND11_CPP14) // no overload_cast: providing something that static_assert-fails:
1050 template <typename... Args>
1053  "pybind11::overload_cast<...> requires compiling in C++14 mode");
1054 };
1055 #endif // overload_cast
1056 
1058 
1059 // Adaptor for converting arbitrary container arguments into a vector; implicitly convertible from
1060 // any standard container (or C-style array) supporting std::begin/std::end, any singleton
1061 // arithmetic type (if T is arithmetic), or explicitly constructible from an iterator pair.
1062 template <typename T>
1064  std::vector<T> v;
1065 
1066 public:
1067  any_container() = default;
1068 
1069  // Can construct from a pair of iterators
1071  any_container(It first, It last) : v(first, last) {}
1072 
1073  // Implicit conversion constructor from any arbitrary container type
1074  // with values convertible to T
1075  template <typename Container,
1076  typename = enable_if_t<
1077  std::is_convertible<decltype(*std::begin(std::declval<const Container &>())),
1078  T>::value>>
1079  // NOLINTNEXTLINE(google-explicit-constructor)
1080  any_container(const Container &c) : any_container(std::begin(c), std::end(c)) {}
1081 
1082  // initializer_list's aren't deducible, so don't get matched by the above template;
1083  // we need this to explicitly allow implicit conversion from one:
1085  any_container(const std::initializer_list<TIn> &c) : any_container(c.begin(), c.end()) {}
1086 
1087  // Avoid copying if given an rvalue vector of the correct type.
1088  // NOLINTNEXTLINE(google-explicit-constructor)
1089  any_container(std::vector<T> &&v) : v(std::move(v)) {}
1090 
1091  // Moves the vector out of an rvalue any_container
1092  // NOLINTNEXTLINE(google-explicit-constructor)
1093  operator std::vector<T> &&() && { return std::move(v); }
1094 
1095  // Dereferencing obtains a reference to the underlying vector
1096  std::vector<T> &operator*() { return v; }
1097  const std::vector<T> &operator*() const { return v; }
1098 
1099  // -> lets you call methods on the underlying vector
1100  std::vector<T> *operator->() { return &v; }
1101  const std::vector<T> *operator->() const { return &v; }
1102 };
1103 
1104 // Forward-declaration; see detail/class.h
1105 std::string get_fully_qualified_tp_name(PyTypeObject *);
1106 
1107 template <typename T>
1108 inline static std::shared_ptr<T>
1109 try_get_shared_from_this(std::enable_shared_from_this<T> *holder_value_ptr) {
1110 // Pre C++17, this code path exploits undefined behavior, but is known to work on many platforms.
1111 // Use at your own risk!
1112 // See also https://en.cppreference.com/w/cpp/memory/enable_shared_from_this, and in particular
1113 // the `std::shared_ptr<Good> gp1 = not_so_good.getptr();` and `try`-`catch` parts of the example.
1114 #if defined(__cpp_lib_enable_shared_from_this) && (!defined(_MSC_VER) || _MSC_VER >= 1912)
1115  return holder_value_ptr->weak_from_this().lock();
1116 #else
1117  try {
1118  return holder_value_ptr->shared_from_this();
1119  } catch (const std::bad_weak_ptr &) {
1120  return nullptr;
1121  }
1122 #endif
1123 }
1124 
1125 // For silencing "unused" compiler warnings in special situations.
1126 template <typename... Args>
1127 #if defined(_MSC_VER) && _MSC_VER < 1920 // MSVC 2017
1128 constexpr
1129 #endif
1130  inline void
1132 }
1133 
1134 // MSVC warning C4100: Unreferenced formal parameter
1135 #if defined(_MSC_VER) && _MSC_VER <= 1916
1136 # define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...) \
1137  detail::silence_unused_warnings(__VA_ARGS__)
1138 #else
1139 # define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...)
1140 #endif
1141 
1142 // GCC -Wunused-but-set-parameter All GCC versions (as of July 2021).
1143 #if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
1144 # define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...) \
1145  detail::silence_unused_warnings(__VA_ARGS__)
1146 #else
1147 # define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...)
1148 #endif
1149 
1150 #if defined(_MSC_VER) // All versions (as of July 2021).
1151 
1152 // warning C4127: Conditional expression is constant
1153 constexpr inline bool silence_msvc_c4127(bool cond) { return cond; }
1154 
1155 # define PYBIND11_SILENCE_MSVC_C4127(...) ::pybind11::detail::silence_msvc_c4127(__VA_ARGS__)
1156 
1157 #else
1158 # define PYBIND11_SILENCE_MSVC_C4127(...) __VA_ARGS__
1159 #endif
1160 
1161 // Pybind offers detailed error messages by default for all builts that are debug (through the
1162 // negation of ndebug). This can also be manually enabled by users, for any builds, through
1163 // defining PYBIND11_DETAILED_ERROR_MESSAGES.
1164 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES) && !defined(NDEBUG)
1165 # define PYBIND11_DETAILED_ERROR_MESSAGES
1166 #endif
1167 
typename std::conditional< B, T, F >::type conditional_t
bool simple_holder_constructed
For simple layout, tracks whether the holder has been constructed.
Helper template which holds a list of types.
unsigned char uint8_t
Definition: ms_stdint.h:83
bool has_patients
If true, get_internals().patients has an entry for this object.
PyObject * weakrefs
Weak references.
#define I
Definition: main.h:112
constexpr auto operator()(Return(*pf)(Args...)) const noexcept -> decltype(pf)
constexpr int last(int, int result)
Helper template to strip away type modifiers.
remove_cv_t< remove_reference_t< T > > type
Key F(std::uint64_t j)
any_container(const std::initializer_list< TIn > &c)
const std::vector< T > & operator*() const
void silence_unused_warnings(Args &&...)
bool[] expand_side_effects
Apply a function over each element of a parameter pack.
constexpr size_t instance_simple_holder_in_ptrs()
typename std::remove_cv< T >::type remove_cv_t
std::string get_fully_qualified_tp_name(PyTypeObject *)
Definition: class.h:28
typename remove_cvref< T >::type remove_cvref_t
bool_constant< std::is_pointer< T >::value &&std::is_function< typename std::remove_pointer< T >::type >::value > is_function_pointer
int n
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
Rot2 R(Rot2::fromAngle(0.1))
decltype(is_template_base_of_impl< Base >::check((intrinsic_t< T > *) nullptr)) is_template_base_of
Compile-time all/any/none of that check the boolean value of all template types.
Definition: BFloat16.h:88
static constexpr size_t size_in_ptrs(size_t s)
#define N
Definition: gksort.c:12
typename exactly_one< Predicate, Default, Ts... >::type exactly_one_t
DiscreteKey S(1, 2)
Strip the class from a method type.
The &#39;instance&#39; type which needs to be standard layout (need to be able to use &#39;offsetof&#39;) ...
constexpr size_t constexpr_sum()
Compile-time integer sum.
PyExc_RuntimeError [[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
conditional_t< std::is_function< F >::value, F, typename conditional_t< std::is_pointer< F >::value||std::is_member_pointer< F >::value, std::remove_pointer< F >, strip_function_object< F > >::type > function_signature_t
static constexpr auto const_
all_of< Predicates< T >... > satisfies_all_of
typename select_indices_impl< index_sequence<>, 0, Bs... >::type select_indices
ssize_t ssize_t_cast(const IntType &val)
constexpr int first(int i)
Implementation details for constexpr functions.
Values result
Array< int, Dynamic, 1 > v
std::integral_constant< bool, B > bool_constant
Backports of std::bool_constant and std::negation to accommodate older compilers. ...
Eigen::Triplet< double > T
RealScalar s
Make an index sequence of the indices of true arguments.
typename std::remove_reference< T >::type remove_reference_t
typename void_t_impl< Ts... >::type void_t
Defer the evaluation of type T until types Us are instantiated.
Helper type to replace &#39;void&#39; in some expressions.
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:50
constexpr int constexpr_last()
Return the index of the last type in Ts which satisfies Predicate<T>, or -1 if none match...
bool owned
If true, the pointer is owned which means we&#39;re free to manage it with a holder.
std::is_same< bools< Ts::value..., true >, bools< true, Ts::value... > > all_of
constexpr auto operator()(Return(Class::*pmf)(Args...) const, std::true_type) const noexcept -> decltype(pmf)
void check(bool b, bool ref)
Definition: fastmath.cpp:12
typename intrinsic_type< T >::type intrinsic_t
constexpr auto operator()(Return(Class::*pmf)(Args...), std::false_type={}) const noexcept -> decltype(pmf)
RAII wrapper that temporarily clears any Python error state.
typename deferred_type< T, Us... >::type deferred_t
bool simple_instance_registered
For simple layout, tracks whether the instance is registered in registered_instances ...
static EIGEN_DEPRECATED const end_t end
Dummy destructor wrapper that can be used to expose classes with a private destructor.
bool_constant< std::is_base_of< Base, Derived >::value &&!std::is_same< Base, Derived >::value > is_strict_base_of
static constexpr int log2(size_t n, int k=0)
static std::shared_ptr< T > try_get_shared_from_this(std::enable_shared_from_this< T > *holder_value_ptr)
C++ bindings of builtin Python exceptions.
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
#define PYBIND11_RUNTIME_EXCEPTION(name, type)
Check if T looks like an input iterator.
typename make_index_sequence_impl< N >::type make_index_sequence
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
nonsimple_values_and_holders nonsimple
#define PYBIND11_NAMESPACE_END(name)
const std::vector< T > * operator->() const
Definition: pytypes.h:1370
#define PYBIND11_NAMESPACE_BEGIN(name)
Return the Nth element from the parameter pack.
bool_constant<(std::is_same< Base, Derived >::value||std::is_base_of< Base, Derived >::value) &&std::is_convertible< Derived *, Base * >::value > is_accessible_base_of


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:02