12 #if !defined(NAMESPACE_BEGIN) 13 # define NAMESPACE_BEGIN(name) namespace name { 15 #if !defined(NAMESPACE_END) 16 # define NAMESPACE_END(name) } 22 #if !defined(PYBIND11_NAMESPACE) 24 # define PYBIND11_NAMESPACE pybind11 __attribute__((visibility("hidden"))) 26 # define PYBIND11_NAMESPACE pybind11 30 #if !(defined(_MSC_VER) && __cplusplus == 199711L) && !defined(__INTEL_COMPILER) 31 # if __cplusplus >= 201402L 32 # define PYBIND11_CPP14 33 # if __cplusplus >= 201703L 34 # define PYBIND11_CPP17 37 #elif defined(_MSC_VER) && __cplusplus == 199711L 40 # if _MSVC_LANG >= 201402L 41 # define PYBIND11_CPP14 42 # if _MSVC_LANG > 201402L && _MSC_VER >= 1910 43 # define PYBIND11_CPP17 49 #if defined(__INTEL_COMPILER) 50 # if __INTEL_COMPILER < 1700 51 # error pybind11 requires Intel C++ compiler v17 or newer 53 #elif defined(__clang__) && !defined(__apple_build_version__) 54 # if __clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ < 3) 55 # error pybind11 requires clang 3.3 or newer 57 #elif defined(__clang__) 60 # if __clang_major__ < 5 61 # error pybind11 requires Xcode/clang 5.0 or newer 63 #elif defined(__GNUG__) 64 # if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8) 65 # error pybind11 requires gcc 4.8 or newer 67 #elif defined(_MSC_VER) 70 # if _MSC_FULL_VER < 190024210 71 # error pybind11 requires MSVC 2015 update 3 or newer 75 #if !defined(PYBIND11_EXPORT) 76 # if defined(WIN32) || defined(_WIN32) 77 # define PYBIND11_EXPORT __declspec(dllexport) 79 # define PYBIND11_EXPORT __attribute__ ((visibility("default"))) 84 # define PYBIND11_NOINLINE __declspec(noinline) 86 # define PYBIND11_NOINLINE __attribute__ ((noinline)) 89 #if defined(PYBIND11_CPP14) 90 # define PYBIND11_DEPRECATED(reason) [[deprecated(reason)]] 92 # define PYBIND11_DEPRECATED(reason) __attribute__((deprecated(reason))) 95 #define PYBIND11_VERSION_MAJOR 2 96 #define PYBIND11_VERSION_MINOR 3 97 #define PYBIND11_VERSION_PATCH dev1 100 #if defined(_MSC_VER) 101 # if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 4) 102 # define HAVE_ROUND 1 104 # pragma warning(push) 105 # pragma warning(disable: 4510 4610 4512 4005) 107 # define PYBIND11_DEBUG_MARKER 113 #include <frameobject.h> 114 #include <pythread.h> 126 #if defined(_MSC_VER) 127 # if defined(PYBIND11_DEBUG_MARKER) 129 # undef PYBIND11_DEBUG_MARKER 131 # pragma warning(pop) 136 #include <forward_list> 140 #include <unordered_set> 141 #include <unordered_map> 144 #include <type_traits> 146 #if PY_MAJOR_VERSION >= 3 147 #define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_) PyInstanceMethod_New(ptr) 148 #define PYBIND11_INSTANCE_METHOD_CHECK PyInstanceMethod_Check 149 #define PYBIND11_INSTANCE_METHOD_GET_FUNCTION PyInstanceMethod_GET_FUNCTION 150 #define PYBIND11_BYTES_CHECK PyBytes_Check 151 #define PYBIND11_BYTES_FROM_STRING PyBytes_FromString 152 #define PYBIND11_BYTES_FROM_STRING_AND_SIZE PyBytes_FromStringAndSize 153 #define PYBIND11_BYTES_AS_STRING_AND_SIZE PyBytes_AsStringAndSize 154 #define PYBIND11_BYTES_AS_STRING PyBytes_AsString 155 #define PYBIND11_BYTES_SIZE PyBytes_Size 156 #define PYBIND11_LONG_CHECK(o) PyLong_Check(o) 157 #define PYBIND11_LONG_AS_LONGLONG(o) PyLong_AsLongLong(o) 158 #define PYBIND11_LONG_FROM_SIGNED(o) PyLong_FromSsize_t((ssize_t) o) 159 #define PYBIND11_LONG_FROM_UNSIGNED(o) PyLong_FromSize_t((size_t) o) 160 #define PYBIND11_BYTES_NAME "bytes" 161 #define PYBIND11_STRING_NAME "str" 162 #define PYBIND11_SLICE_OBJECT PyObject 163 #define PYBIND11_FROM_STRING PyUnicode_FromString 164 #define PYBIND11_STR_TYPE ::pybind11::str 165 #define PYBIND11_BOOL_ATTR "__bool__" 166 #define PYBIND11_NB_BOOL(ptr) ((ptr)->nb_bool) 168 #define PYBIND11_PLUGIN_IMPL(name) \ 169 extern "C" PYBIND11_EXPORT PyObject *PyInit_##name(); \ 170 extern "C" PYBIND11_EXPORT PyObject *PyInit_##name() 173 #define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_) PyMethod_New(ptr, nullptr, class_) 174 #define PYBIND11_INSTANCE_METHOD_CHECK PyMethod_Check 175 #define PYBIND11_INSTANCE_METHOD_GET_FUNCTION PyMethod_GET_FUNCTION 176 #define PYBIND11_BYTES_CHECK PyString_Check 177 #define PYBIND11_BYTES_FROM_STRING PyString_FromString 178 #define PYBIND11_BYTES_FROM_STRING_AND_SIZE PyString_FromStringAndSize 179 #define PYBIND11_BYTES_AS_STRING_AND_SIZE PyString_AsStringAndSize 180 #define PYBIND11_BYTES_AS_STRING PyString_AsString 181 #define PYBIND11_BYTES_SIZE PyString_Size 182 #define PYBIND11_LONG_CHECK(o) (PyInt_Check(o) || PyLong_Check(o)) 183 #define PYBIND11_LONG_AS_LONGLONG(o) (PyInt_Check(o) ? (long long) PyLong_AsLong(o) : PyLong_AsLongLong(o)) 184 #define PYBIND11_LONG_FROM_SIGNED(o) PyInt_FromSsize_t((ssize_t) o) // Returns long if needed. 185 #define PYBIND11_LONG_FROM_UNSIGNED(o) PyInt_FromSize_t((size_t) o) // Returns long if needed. 186 #define PYBIND11_BYTES_NAME "str" 187 #define PYBIND11_STRING_NAME "unicode" 188 #define PYBIND11_SLICE_OBJECT PySliceObject 189 #define PYBIND11_FROM_STRING PyString_FromString 190 #define PYBIND11_STR_TYPE ::pybind11::bytes 191 #define PYBIND11_BOOL_ATTR "__nonzero__" 192 #define PYBIND11_NB_BOOL(ptr) ((ptr)->nb_nonzero) 194 #define PYBIND11_PLUGIN_IMPL(name) \ 195 static PyObject *pybind11_init_wrapper(); \ 196 extern "C" PYBIND11_EXPORT void init##name(); \ 197 extern "C" PYBIND11_EXPORT void init##name() { \ 198 (void)pybind11_init_wrapper(); \ 200 PyObject *pybind11_init_wrapper() 203 #if PY_VERSION_HEX >= 0x03050000 && PY_VERSION_HEX < 0x03050200 205 struct _Py_atomic_address {
void *
value; };
206 PyAPI_DATA(_Py_atomic_address) _PyThreadState_Current;
210 #define PYBIND11_TRY_NEXT_OVERLOAD ((PyObject *) 1) // special failure return code 211 #define PYBIND11_STRINGIFY(x) #x 212 #define PYBIND11_TOSTRING(x) PYBIND11_STRINGIFY(x) 213 #define PYBIND11_CONCAT(first, second) first##second 215 #define PYBIND11_CHECK_PYTHON_VERSION \ 217 const char *compiled_ver = PYBIND11_TOSTRING(PY_MAJOR_VERSION) \ 218 "." PYBIND11_TOSTRING(PY_MINOR_VERSION); \ 219 const char *runtime_ver = Py_GetVersion(); \ 220 size_t len = std::strlen(compiled_ver); \ 221 if (std::strncmp(runtime_ver, compiled_ver, len) != 0 \ 222 || (runtime_ver[len] >= '0' && runtime_ver[len] <= '9')) { \ 223 PyErr_Format(PyExc_ImportError, \ 224 "Python version mismatch: module was compiled for Python %s, " \ 225 "but the interpreter version is incompatible: %s.", \ 226 compiled_ver, runtime_ver); \ 231 #define PYBIND11_CATCH_INIT_EXCEPTIONS \ 232 catch (pybind11::error_already_set &e) { \ 233 PyErr_SetString(PyExc_ImportError, e.what()); \ 235 } catch (const std::exception &e) { \ 236 PyErr_SetString(PyExc_ImportError, e.what()); \ 255 #define PYBIND11_PLUGIN(name) \ 256 PYBIND11_DEPRECATED("PYBIND11_PLUGIN is deprecated, use PYBIND11_MODULE") \ 257 static PyObject *pybind11_init(); \ 258 PYBIND11_PLUGIN_IMPL(name) { \ 259 PYBIND11_CHECK_PYTHON_VERSION \ 261 return pybind11_init(); \ 262 } PYBIND11_CATCH_INIT_EXCEPTIONS \ 264 PyObject *pybind11_init() 283 #define PYBIND11_MODULE(name, variable) \ 284 static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &); \ 285 PYBIND11_PLUGIN_IMPL(name) { \ 286 PYBIND11_CHECK_PYTHON_VERSION \ 287 auto m = pybind11::module(PYBIND11_TOSTRING(name)); \ 289 PYBIND11_CONCAT(pybind11_init_, name)(m); \ 291 } PYBIND11_CATCH_INIT_EXCEPTIONS \ 293 void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &variable) 299 using
size_t =
std::
size_t;
355 inline static constexpr
int log2(
size_t n,
int k = 0) {
return (n <= 1) ? k :
log2(n >> 1, k + 1); }
358 inline static constexpr
size_t size_in_ptrs(
size_t s) {
return 1 + ((s - 1) >>
log2(
sizeof(
void *))); }
367 static_assert(
sizeof(std::shared_ptr<int>) >=
sizeof(std::unique_ptr<int>),
368 "pybind assumes std::shared_ptrs are at least as big as std::unique_ptrs");
425 void allocate_layout();
428 void deallocate_layout();
436 static constexpr uint8_t status_holder_constructed = 1;
437 static constexpr uint8_t status_instance_registered = 2;
443 #if defined(PYBIND11_CPP14) && (!defined(_MSC_VER) || _MSC_VER >= 1910) 456 #if defined(PYBIND11_CPP14) 457 using std::index_sequence;
469 :
select_indices_impl<conditional_t<B, index_sequence<IPrev..., I>, index_sequence<IPrev...>>, I + 1, Bs...> {};
480 #if defined(__cpp_fold_expressions) && !(defined(_MSC_VER) && (_MSC_VER < 1916)) 483 #elif !defined(_MSC_VER) 485 template <
class... Ts>
using all_of = std::is_same<
492 template <
class... Ts>
using all_of = std::conjunction<Ts...>;
493 template <
class... Ts>
using any_of = std::disjunction<Ts...>;
503 template <
typename C,
typename R,
typename...
A>
struct remove_class<R (C::*)(A...)> {
typedef R
type(
A...); };
504 template <
typename C,
typename R,
typename...
A>
struct remove_class<R (C::*)(A...) const> {
typedef R
type(
A...); };
523 #ifdef __cpp_fold_expressions 524 template <
typename... Ts> constexpr
size_t constexpr_sum(Ts... ns) {
return (0 + ... +
size_t{ns}); }
527 template <
typename T,
typename... Ts>
534 template <
typename T,
typename... Ts>
535 constexpr
int first(
int i, T v, Ts... vs) {
return v ?
i :
first(i + 1, vs...); }
537 constexpr
int last(
int ,
int result) {
return result; }
538 template <
typename T,
typename... Ts>
539 constexpr
int last(
int i,
int result, T v, Ts... vs) {
return last(i + 1, v ? i : result, vs...); }
542 template <
template<
typename>
class Predicate,
typename... Ts>
548 template <
template<
typename>
class Predicate,
typename... Ts>
552 template <
size_t N,
typename T,
typename... Ts>
554 template <
typename T,
typename... Ts>
559 template <
template<
typename>
class Predicate,
typename Default,
typename... Ts>
562 static_assert(found <= 1,
"Found more than one type matching the predicate");
567 template <
template<
typename>
class P,
typename Default>
570 template <
template<
typename>
class Predicate,
typename Default,
typename... Ts>
587 template <
template<
typename...>
class Base>
589 template <
typename... Us>
static std::true_type check(
Base<Us...> *);
590 static std::false_type check(...);
595 template <
template<
typename...>
class Base,
typename T>
596 #if !defined(_MSC_VER) 598 #else // MSVC2015 has trouble with decltype in template aliases 599 struct is_template_base_of : decltype(is_template_base_of_impl<Base>::check((intrinsic_t<T>*)nullptr)) { };
604 template <
template<
typename...>
class Class,
typename T>
606 template <
template<
typename...>
class Class,
typename... Us>
614 template <
typename T>
626 template <
typename Function,
typename F = remove_reference_t<Function>>
632 std::remove_pointer<F>,
641 std::is_function, std::is_pointer, std::is_member_pointer>;
647 #ifdef __cpp_fold_expressions 648 #define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN) (((PATTERN), void()), ...) 651 #define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN) pybind11::detail::expand_side_effects{ ((PATTERN), void(), false)..., false } 656 class builtin_exception :
public std::runtime_error {
659 using std::runtime_error::runtime_error;
661 virtual void set_error()
const = 0;
664 #define PYBIND11_RUNTIME_EXCEPTION(name, type) \ 665 class name : public builtin_exception { public: \ 666 using builtin_exception::builtin_exception; \ 667 name() : name("") { } \ 668 void set_error() const override { PyErr_SetString(type, what()); } \ 680 [[noreturn]]
PYBIND11_NOINLINE inline
void pybind11_fail(const
char *reason) {
throw std::runtime_error(reason); }
681 [[noreturn]]
PYBIND11_NOINLINE inline void pybind11_fail(
const std::string &reason) {
throw std::runtime_error(reason); }
691 template <typename T, typename SFINAE =
void> struct
is_fmt_numeric {
static constexpr
bool value =
false; };
702 static constexpr
const char value[2] = { c,
'\0' };
703 static std::string
format() {
return std::string(1, c); }
706 #if !defined(PYBIND11_CPP17) 724 template <
typename... Args>
728 template <
typename Return>
729 constexpr
auto operator()(Return (*pf)(Args...)) const noexcept
730 -> decltype(pf) {
return pf; }
732 template <
typename Return,
typename Class>
733 constexpr
auto operator()(Return (Class::*pmf)(Args...), std::false_type = {}) const noexcept
734 -> decltype(pmf) {
return pmf; }
736 template <
typename Return,
typename Class>
737 constexpr
auto operator()(Return (Class::*pmf)(Args...)
const, std::true_type)
const noexcept
738 -> decltype(pmf) {
return pmf; }
743 #if defined(PYBIND11_CPP14) 744 #define PYBIND11_OVERLOAD_CAST 1 745 template <
typename... Args>
756 static constexpr
auto const_ = std::true_type{};
758 #if !defined(PYBIND11_CPP14) // no overload_cast: providing something that static_assert-fails: 761 "pybind11::overload_cast<...> requires compiling in C++14 mode");
763 #endif // overload_cast 770 template <
typename T>
781 template <
typename Container,
typename = enable_if_t<std::is_convertible<decltype(*std::begin(std::declval<const Container &>())), T>::value>>
793 operator std::vector<T> &&() && {
return std::move(v); }
conditional_t< found, typename pack_element< index, Ts... >::type, Default > type
nonsimple_values_and_holders nonsimple
Helper template to strip away type modifiers.
intrinsic_type< T >::type type
Strip the class from a method type.
intrinsic_type< T >::type type
bool has_patients
If true, get_internals().patients has an entry for this object.
std::vector< T > * operator->()
#define PYBIND11_NAMESPACE
static constexpr int log2(size_t n, int k=0)
typename select_indices_impl< index_sequence<>, 0, Bs... >::type select_indices
const std::vector< T > * operator->() const
typename exactly_one< Predicate, Default, Ts... >::type exactly_one_t
RAII wrapper that temporarily clears any Python error state.
constexpr auto operator()(Return(Class::*pmf)(Args...) const, std::true_type) const noexcept -> decltype(pmf)
typename void_t_impl< Ts... >::type void_t
static constexpr size_t size_in_ptrs(size_t s)
intrinsic_type< T >::type type
std::is_same< bools< Ts::value..., true >, bools< true, Ts::value... > > all_of
any_container(std::vector< T > &&v)
bool_constant< std::is_base_of< Base, Derived >::value &&!std::is_same< Base, Derived >::value > is_strict_base_of
const std::vector< T > & operator*() const
static constexpr auto const_
any_container(const Container &c)
#define PYBIND11_NOINLINE
Defer the evaluation of type T until types Us are instantiated.
The 'instance' type which needs to be standard layout (need to be able to use 'offsetof') ...
bool[] expand_side_effects
Apply a function over each element of a parameter pack.
typename pack_element< N - 1, Ts... >::type type
constexpr int constexpr_last()
Return the index of the last type in Ts which satisfies Predicate<T>, or -1 if none match...
index_sequence< S... > type
constexpr auto operator()(Return(Class::*pmf)(Args...), std::false_type={}) const noexcept -> decltype(pmf)
Make an index sequence of the indices of true arguments.
typename deferred_type< T, Us... >::type deferred_t
bool simple_holder_constructed
For simple layout, tracks whether the holder has been constructed.
#define PYBIND11_RUNTIME_EXCEPTION(name, type)
#define NAMESPACE_END(name)
any_container(const std::initializer_list< TIn > &c)
constexpr int first(int i, T v, Ts... vs)
typename intrinsic_type< T >::type intrinsic_t
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
bool owned
If true, the pointer is owned which means we're free to manage it with a holder.
typename make_index_sequence_impl< N >::type make_index_sequence
Helper template which holds a list of types.
bool_constant< std::is_pointer< T >::value &&std::is_function< typename std::remove_pointer< T >::type >::value > is_function_pointer
constexpr int last(int i, int result, T v, Ts... vs)
typename std::remove_reference< T >::type remove_reference_t
Compile-time all/any/none of that check the boolean value of all template types.
intrinsic_type< T >::type type
all_of< Predicates< T >... > satisfies_all_of
any_container(It first, It last)
typename std::conditional< B, T, F >::type conditional_t
constexpr size_t constexpr_sum(T n, Ts... ns)
constexpr int constexpr_first()
conditional_t< B, index_sequence< IPrev..., I >, index_sequence< IPrev... > > type
PyObject * weakrefs
Weak references.
std::integral_constant< bool, B > bool_constant
Backports of std::bool_constant and std::negation to accommodate older compilers. ...
const detail::type_info * type
detail::enable_if_t<!detail::move_never< T >::value, T > move(object &&obj)
decltype(is_template_base_of_impl< Base >::check((intrinsic_t< T > *) nullptr)) is_template_base_of
Helper type to replace 'void' in some expressions.
constexpr auto operator()(Return(*pf)(Args...)) const noexcept -> decltype(pf)
std::vector< T > & operator*()
void ignore_unused(const int *)
Ignore that a variable is unused in compiler warnings.
constexpr size_t instance_simple_holder_in_ptrs()
#define NAMESPACE_BEGIN(name)
Thrown when pybind11::cast or handle::call fail due to a type casting error.
constexpr overload_cast_impl()
typename remove_class< decltype(&F::operator())>::type type
intrinsic_type< T >::type type
bool_constant< std::is_base_of< Base, Derived >::value &&std::is_convertible< Derived *, Base * >::value > is_accessible_base_of
Dummy destructor wrapper that can be used to expose classes with a private destructor.
bool simple_instance_registered
For simple layout, tracks whether the instance is registered in registered_instances ...
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
typename std::remove_cv< T >::type remove_cv_t
Return the Nth element from the parameter pack.
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
intrinsic_type< T >::type type
void ** values_and_holders