From d2e2207e2fbaabf97ba2bed316eec926cc2187e6 Mon Sep 17 00:00:00 2001
From: twoone3 <3197653242@qq.com>
Date: Mon, 7 Feb 2022 21:40:51 +0800
Subject: [PATCH] =?UTF-8?q?1.7.8.=E6=9B=B4=E6=96=B0=20-=20=E4=BF=AE?=
=?UTF-8?q?=E5=A4=8DEntity.md=E9=87=8C=E9=9D=A2=E7=9A=84=E9=94=99=E8=AF=AF?=
=?UTF-8?q?=20-=20=E6=96=B0=E5=A2=9E=E4=BA=86pybind11=E5=BA=93=E9=A2=84?=
=?UTF-8?q?=E5=A4=87=E4=BD=BF=E7=94=A8=20-=20=E4=BF=AE=E5=A4=8DbroadcastTe?=
=?UTF-8?q?xt/broadcastTitle=E6=9C=AA=E8=A3=85=E8=BD=BD=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
BDSpyrunner.vcxproj | 3 +-
BDSpyrunner.vcxproj.filters | 3 +
docs/Class/Entity.md | 23 +-
include/pybind11/attr.h | 613 +++++
include/pybind11/buffer_info.h | 144 ++
include/pybind11/cast.h | 1422 +++++++++++
include/pybind11/chrono.h | 211 ++
include/pybind11/common.h | 2 +
include/pybind11/complex.h | 65 +
include/pybind11/detail/class.h | 709 ++++++
include/pybind11/detail/common.h | 1063 ++++++++
include/pybind11/detail/descr.h | 133 +
include/pybind11/detail/init.h | 346 +++
include/pybind11/detail/internals.h | 553 +++++
include/pybind11/detail/type_caster_base.h | 985 ++++++++
include/pybind11/detail/typeid.h | 55 +
include/pybind11/eigen.h | 608 +++++
include/pybind11/embed.h | 296 +++
include/pybind11/eval.h | 163 ++
include/pybind11/functional.h | 121 +
include/pybind11/gil.h | 193 ++
include/pybind11/iostream.h | 275 +++
include/pybind11/numpy.h | 1741 +++++++++++++
include/pybind11/operators.h | 163 ++
include/pybind11/options.h | 65 +
include/pybind11/pybind11.h | 2573 ++++++++++++++++++++
include/pybind11/pytypes.h | 1924 +++++++++++++++
include/pybind11/stl.h | 375 +++
include/pybind11/stl/filesystem.h | 103 +
include/pybind11/stl_bind.h | 747 ++++++
mod/Main.cpp | 11 +-
mod/Module.cpp | 2 +
mod/PyContainer.cpp | 0
mod/PyEntity.cpp | 3 +-
mod/PyUtils.cpp | 13 +
mod/PyUtils.h | 7 +-
mod/Version.h | 4 +-
37 files changed, 15697 insertions(+), 20 deletions(-)
create mode 100644 include/pybind11/attr.h
create mode 100644 include/pybind11/buffer_info.h
create mode 100644 include/pybind11/cast.h
create mode 100644 include/pybind11/chrono.h
create mode 100644 include/pybind11/common.h
create mode 100644 include/pybind11/complex.h
create mode 100644 include/pybind11/detail/class.h
create mode 100644 include/pybind11/detail/common.h
create mode 100644 include/pybind11/detail/descr.h
create mode 100644 include/pybind11/detail/init.h
create mode 100644 include/pybind11/detail/internals.h
create mode 100644 include/pybind11/detail/type_caster_base.h
create mode 100644 include/pybind11/detail/typeid.h
create mode 100644 include/pybind11/eigen.h
create mode 100644 include/pybind11/embed.h
create mode 100644 include/pybind11/eval.h
create mode 100644 include/pybind11/functional.h
create mode 100644 include/pybind11/gil.h
create mode 100644 include/pybind11/iostream.h
create mode 100644 include/pybind11/numpy.h
create mode 100644 include/pybind11/operators.h
create mode 100644 include/pybind11/options.h
create mode 100644 include/pybind11/pybind11.h
create mode 100644 include/pybind11/pytypes.h
create mode 100644 include/pybind11/stl.h
create mode 100644 include/pybind11/stl/filesystem.h
create mode 100644 include/pybind11/stl_bind.h
create mode 100644 mod/PyContainer.cpp
diff --git a/BDSpyrunner.vcxproj b/BDSpyrunner.vcxproj
index 837f84e7..d5935b15 100644
--- a/BDSpyrunner.vcxproj
+++ b/BDSpyrunner.vcxproj
@@ -34,7 +34,7 @@
false
- SDK\Header;$(IncludePath)
+ include;SDK\Header;$(IncludePath)
true
false
false
@@ -75,6 +75,7 @@ copy $(OutputPath)$(ProjectName).pdb ..\BDS\plugins\$(ProjectName).pdb
+
diff --git a/BDSpyrunner.vcxproj.filters b/BDSpyrunner.vcxproj.filters
index 11d5484e..de8bed17 100644
--- a/BDSpyrunner.vcxproj.filters
+++ b/BDSpyrunner.vcxproj.filters
@@ -42,6 +42,9 @@
源文件
+
+ 源文件
+
diff --git a/docs/Class/Entity.md b/docs/Class/Entity.md
index ffb84573..f7387d43 100644
--- a/docs/Class/Entity.md
+++ b/docs/Class/Entity.md
@@ -22,9 +22,9 @@
## getPlatformOnlineId() -> str
## getPlatform() -> int
## getIP() -> str
-## setHand(nbt:mcNBT) -> None
+## setHand(nbt:mc.NBT) -> None
设置玩家手中物品
-## addItem(nbt:mcNBT) -> None
+## addItem(nbt:mc.NBT) -> None
为玩家新添物品
## removeItem(slot:int, count:int) -> None
移除玩家背包slot格子的count个物品
@@ -65,12 +65,21 @@
例:`player.setSidebar('自定义侧边栏', '{"第一行":0, "第二行":2, "我在哪行?":3}')`
## removeSidebar() -> None
移除玩家计分板侧边栏
-## sendCustomForm(nbt:mcNBT, callback:function) -> None
-向指定的玩家发送一个自定义表单,回调函数原型为callback(Entity:player,selected_nbt:mcNBT) -> None
-例:player.sendCustomForm('{"content":[{"type":"label","text":"这是一个文本标签"},{"placeholder":"水印文本","default":"","type":"input","text":""},{"default":true,"type":"toggle","text":"开关~或许是吧"},{"min":0.0,"max":10.0,"step":2.0,"default":3.0,"type":"slider","text":"游标滑块!?"},{"default":1,"steps":["Step1","Step2","Step 3"],"type":"step_slider","text":"矩阵滑块?!"},{"default":1,"options":["Option 1","Option2","Option3"],"type":"dropdown","text":"如你所见 下拉框"}], "type":"custom_form","title":"这是一个自定义窗体"}',cb)
+## sendCustomForm(data:str, callback:function) -> None
+向指定的玩家发送一个自定义表单,回调函数原型为callback(Entity:player,selected_data:str) -> None
+例:
+```py
+player.sendCustomForm('{"content":[{"type":"label","text":"这是一个文本标签"},{"placeholder":"水印文本","default":"","type":"input","text":""},{"default":true,"type":"toggle","text":"开关~或许是吧"},{"min":0.0,"max":10.0,"step":2.0,"default":3.0,"type":"slider","text":"游标滑块!?"},{"default":1,"steps":["Step1","Step2","Step 3"],"type":"step_slider","text":"矩阵滑块?!"},{"default":1,"options":["Option 1","Option2","Option3"],"type":"dropdown","text":"如你所见 下拉框"}], "type":"custom_form","title":"这是一个自定义窗体"}',cb)
+```
## sendSimpleForm(title:str, content:str, buttons:list, images:list, callback:function) -> None
向指定的玩家发送一个简单表单,回调函数原型为callback(Entity:player,selected_item:int) -> None
-例:player.sendSimpleForm('标题', '内容', ["生存","死亡","求助"], ['','',''], cb)
+例:
+```py
+player.sendSimpleForm('标题', '内容', ["生存","死亡","求助"], ['','',''], cb)
+```
## sendModalForm(title:str, content:str, button1:str, button2:str, callback:function) -> None
向指定的玩家发送一个模式对话框,回调函数原型为callback(Entity:player,selected_item:bool) -> None
-例:player.sendModalForm('标题', '内容', '按钮1', '按钮2', cb)
+例:
+```py
+player.sendModalForm('标题', '内容', '按钮1', '按钮2', cb)
+```
diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h
new file mode 100644
index 00000000..f1b66fb8
--- /dev/null
+++ b/include/pybind11/attr.h
@@ -0,0 +1,613 @@
+/*
+ pybind11/attr.h: Infrastructure for processing custom
+ type and function attributes
+
+ Copyright (c) 2016 Wenzel Jakob
+
+ All rights reserved. Use of this source code is governed by a
+ BSD-style license that can be found in the LICENSE file.
+*/
+
+#pragma once
+
+#include "cast.h"
+
+#include
+
+PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
+
+/// \addtogroup annotations
+/// @{
+
+/// Annotation for methods
+struct is_method { handle class_;
+ explicit is_method(const handle &c) : class_(c) {}
+};
+
+/// Annotation for operators
+struct is_operator { };
+
+/// Annotation for classes that cannot be subclassed
+struct is_final { };
+
+/// Annotation for parent scope
+struct scope { handle value;
+ explicit scope(const handle &s) : value(s) {}
+};
+
+/// Annotation for documentation
+struct doc { const char *value;
+ explicit doc(const char *value) : value(value) {}
+};
+
+/// Annotation for function names
+struct name { const char *value;
+ explicit name(const char *value) : value(value) {}
+};
+
+/// Annotation indicating that a function is an overload associated with a given "sibling"
+struct sibling { handle value;
+ explicit sibling(const handle &value) : value(value.ptr()) {}
+};
+
+/// Annotation indicating that a class derives from another given type
+template struct base {
+
+ PYBIND11_DEPRECATED("base() was deprecated in favor of specifying 'T' as a template argument to class_")
+ base() { } // NOLINT(modernize-use-equals-default): breaks MSVC 2015 when adding an attribute
+};
+
+/// Keep patient alive while nurse lives
+template struct keep_alive { };
+
+/// Annotation indicating that a class is involved in a multiple inheritance relationship
+struct multiple_inheritance { };
+
+/// Annotation which enables dynamic attributes, i.e. adds `__dict__` to a class
+struct dynamic_attr { };
+
+/// Annotation which enables the buffer protocol for a type
+struct buffer_protocol { };
+
+/// Annotation which requests that a special metaclass is created for a type
+struct metaclass {
+ handle value;
+
+ PYBIND11_DEPRECATED("py::metaclass() is no longer required. It's turned on by default now.")
+ // NOLINTNEXTLINE(modernize-use-equals-default): breaks MSVC 2015 when adding an attribute
+ metaclass() {}
+
+ /// Override pybind11's default metaclass
+ explicit metaclass(handle value) : value(value) { }
+};
+
+/// Specifies a custom callback with signature `void (PyHeapTypeObject*)` that
+/// may be used to customize the Python type.
+///
+/// The callback is invoked immediately before `PyType_Ready`.
+///
+/// Note: This is an advanced interface, and uses of it may require changes to
+/// work with later versions of pybind11. You may wish to consult the
+/// implementation of `make_new_python_type` in `detail/classes.h` to understand
+/// the context in which the callback will be run.
+struct custom_type_setup {
+ using callback = std::function;
+
+ explicit custom_type_setup(callback value) : value(std::move(value)) {}
+
+ callback value;
+};
+
+/// Annotation that marks a class as local to the module:
+struct module_local { const bool value;
+ constexpr explicit module_local(bool v = true) : value(v) {}
+};
+
+/// Annotation to mark enums as an arithmetic type
+struct arithmetic { };
+
+/// Mark a function for addition at the beginning of the existing overload chain instead of the end
+struct prepend { };
+
+/** \rst
+ A call policy which places one or more guard variables (``Ts...``) around the function call.
+
+ For example, this definition:
+
+ .. code-block:: cpp
+
+ m.def("foo", foo, py::call_guard());
+
+ is equivalent to the following pseudocode:
+
+ .. code-block:: cpp
+
+ m.def("foo", [](args...) {
+ T scope_guard;
+ return foo(args...); // forwarded arguments
+ });
+ \endrst */
+template struct call_guard;
+
+template <> struct call_guard<> { using type = detail::void_type; };
+
+template
+struct call_guard {
+ static_assert(std::is_default_constructible::value,
+ "The guard type must be default constructible");
+
+ using type = T;
+};
+
+template
+struct call_guard {
+ struct type {
+ T guard{}; // Compose multiple guard types with left-to-right default-constructor order
+ typename call_guard::type next{};
+ };
+};
+
+/// @} annotations
+
+PYBIND11_NAMESPACE_BEGIN(detail)
+/* Forward declarations */
+enum op_id : int;
+enum op_type : int;
+struct undefined_t;
+template struct op_;
+void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret);
+
+/// Internal data structure which holds metadata about a keyword argument
+struct argument_record {
+ const char *name; ///< Argument name
+ const char *descr; ///< Human-readable version of the argument value
+ handle value; ///< Associated Python object
+ bool convert : 1; ///< True if the argument is allowed to convert when loading
+ bool none : 1; ///< True if None is allowed when loading
+
+ argument_record(const char *name, const char *descr, handle value, bool convert, bool none)
+ : name(name), descr(descr), value(value), convert(convert), none(none) { }
+};
+
+/// Internal data structure which holds metadata about a bound function (signature, overloads, etc.)
+struct function_record {
+ function_record()
+ : is_constructor(false), is_new_style_constructor(false), is_stateless(false),
+ is_operator(false), is_method(false), has_args(false),
+ has_kwargs(false), prepend(false) { }
+
+ /// Function name
+ char *name = nullptr; /* why no C++ strings? They generate heavier code.. */
+
+ // User-specified documentation string
+ char *doc = nullptr;
+
+ /// Human-readable version of the function signature
+ char *signature = nullptr;
+
+ /// List of registered keyword arguments
+ std::vector args;
+
+ /// Pointer to lambda function which converts arguments and performs the actual call
+ handle (*impl) (function_call &) = nullptr;
+
+ /// Storage for the wrapped function pointer and captured data, if any
+ void *data[3] = { };
+
+ /// Pointer to custom destructor for 'data' (if needed)
+ void (*free_data) (function_record *ptr) = nullptr;
+
+ /// Return value policy associated with this function
+ return_value_policy policy = return_value_policy::automatic;
+
+ /// True if name == '__init__'
+ bool is_constructor : 1;
+
+ /// True if this is a new-style `__init__` defined in `detail/init.h`
+ bool is_new_style_constructor : 1;
+
+ /// True if this is a stateless function pointer
+ bool is_stateless : 1;
+
+ /// True if this is an operator (__add__), etc.
+ bool is_operator : 1;
+
+ /// True if this is a method
+ bool is_method : 1;
+
+ /// True if the function has a '*args' argument
+ bool has_args : 1;
+
+ /// True if the function has a '**kwargs' argument
+ bool has_kwargs : 1;
+
+ /// True if this function is to be inserted at the beginning of the overload resolution chain
+ bool prepend : 1;
+
+ /// Number of arguments (including py::args and/or py::kwargs, if present)
+ std::uint16_t nargs;
+
+ /// Number of leading positional arguments, which are terminated by a py::args or py::kwargs
+ /// argument or by a py::kw_only annotation.
+ std::uint16_t nargs_pos = 0;
+
+ /// Number of leading arguments (counted in `nargs`) that are positional-only
+ std::uint16_t nargs_pos_only = 0;
+
+ /// Python method object
+ PyMethodDef *def = nullptr;
+
+ /// Python handle to the parent scope (a class or a module)
+ handle scope;
+
+ /// Python handle to the sibling function representing an overload chain
+ handle sibling;
+
+ /// Pointer to next overload
+ function_record *next = nullptr;
+};
+
+/// Special data structure which (temporarily) holds metadata about a bound class
+struct type_record {
+ PYBIND11_NOINLINE type_record()
+ : multiple_inheritance(false), dynamic_attr(false), buffer_protocol(false),
+ default_holder(true), module_local(false), is_final(false) { }
+
+ /// Handle to the parent scope
+ handle scope;
+
+ /// Name of the class
+ const char *name = nullptr;
+
+ // Pointer to RTTI type_info data structure
+ const std::type_info *type = nullptr;
+
+ /// How large is the underlying C++ type?
+ size_t type_size = 0;
+
+ /// What is the alignment of the underlying C++ type?
+ size_t type_align = 0;
+
+ /// How large is the type's holder?
+ size_t holder_size = 0;
+
+ /// The global operator new can be overridden with a class-specific variant
+ void *(*operator_new)(size_t) = nullptr;
+
+ /// Function pointer to class_<..>::init_instance
+ void (*init_instance)(instance *, const void *) = nullptr;
+
+ /// Function pointer to class_<..>::dealloc
+ void (*dealloc)(detail::value_and_holder &) = nullptr;
+
+ /// List of base classes of the newly created type
+ list bases;
+
+ /// Optional docstring
+ const char *doc = nullptr;
+
+ /// Custom metaclass (optional)
+ handle metaclass;
+
+ /// Custom type setup.
+ custom_type_setup::callback custom_type_setup_callback;
+
+ /// Multiple inheritance marker
+ bool multiple_inheritance : 1;
+
+ /// Does the class manage a __dict__?
+ bool dynamic_attr : 1;
+
+ /// Does the class implement the buffer protocol?
+ bool buffer_protocol : 1;
+
+ /// Is the default (unique_ptr) holder type used?
+ bool default_holder : 1;
+
+ /// Is the class definition local to the module shared object?
+ bool module_local : 1;
+
+ /// Is the class inheritable from python classes?
+ bool is_final : 1;
+
+ PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *)) {
+ auto base_info = detail::get_type_info(base, false);
+ if (!base_info) {
+ std::string tname(base.name());
+ detail::clean_type_id(tname);
+ pybind11_fail("generic_type: type \"" + std::string(name) +
+ "\" referenced unknown base type \"" + tname + "\"");
+ }
+
+ if (default_holder != base_info->default_holder) {
+ std::string tname(base.name());
+ detail::clean_type_id(tname);
+ pybind11_fail("generic_type: type \"" + std::string(name) + "\" " +
+ (default_holder ? "does not have" : "has") +
+ " a non-default holder type while its base \"" + tname + "\" " +
+ (base_info->default_holder ? "does not" : "does"));
+ }
+
+ bases.append((PyObject *) base_info->type);
+
+ if (base_info->type->tp_dictoffset != 0)
+ dynamic_attr = true;
+
+ if (caster)
+ base_info->implicit_casts.emplace_back(type, caster);
+ }
+};
+
+inline function_call::function_call(const function_record &f, handle p) :
+ func(f), parent(p) {
+ args.reserve(f.nargs);
+ args_convert.reserve(f.nargs);
+}
+
+/// Tag for a new-style `__init__` defined in `detail/init.h`
+struct is_new_style_constructor { };
+
+/**
+ * Partial template specializations to process custom attributes provided to
+ * cpp_function_ and class_. These are either used to initialize the respective
+ * fields in the type_record and function_record data structures or executed at
+ * runtime to deal with custom call policies (e.g. keep_alive).
+ */
+template struct process_attribute;
+
+template struct process_attribute_default {
+ /// Default implementation: do nothing
+ static void init(const T &, function_record *) { }
+ static void init(const T &, type_record *) { }
+ static void precall(function_call &) { }
+ static void postcall(function_call &, handle) { }
+};
+
+/// Process an attribute specifying the function's name
+template <> struct process_attribute : process_attribute_default {
+ static void init(const name &n, function_record *r) { r->name = const_cast(n.value); }
+};
+
+/// Process an attribute specifying the function's docstring
+template <> struct process_attribute : process_attribute_default {
+ static void init(const doc &n, function_record *r) { r->doc = const_cast(n.value); }
+};
+
+/// Process an attribute specifying the function's docstring (provided as a C-style string)
+template <> struct process_attribute : process_attribute_default {
+ static void init(const char *d, function_record *r) { r->doc = const_cast(d); }
+ static void init(const char *d, type_record *r) { r->doc = const_cast(d); }
+};
+template <> struct process_attribute : process_attribute { };
+
+/// Process an attribute indicating the function's return value policy
+template <> struct process_attribute : process_attribute_default {
+ static void init(const return_value_policy &p, function_record *r) { r->policy = p; }
+};
+
+/// Process an attribute which indicates that this is an overloaded function associated with a given sibling
+template <> struct process_attribute : process_attribute_default {
+ static void init(const sibling &s, function_record *r) { r->sibling = s.value; }
+};
+
+/// Process an attribute which indicates that this function is a method
+template <> struct process_attribute : process_attribute_default {
+ static void init(const is_method &s, function_record *r) { r->is_method = true; r->scope = s.class_; }
+};
+
+/// Process an attribute which indicates the parent scope of a method
+template <> struct process_attribute : process_attribute_default {
+ static void init(const scope &s, function_record *r) { r->scope = s.value; }
+};
+
+/// Process an attribute which indicates that this function is an operator
+template <> struct process_attribute : process_attribute_default {
+ static void init(const is_operator &, function_record *r) { r->is_operator = true; }
+};
+
+template <> struct process_attribute : process_attribute_default {
+ static void init(const is_new_style_constructor &, function_record *r) { r->is_new_style_constructor = true; }
+};
+
+inline void check_kw_only_arg(const arg &a, function_record *r) {
+ if (r->args.size() > r->nargs_pos && (!a.name || a.name[0] == '\0'))
+ pybind11_fail("arg(): cannot specify an unnamed argument after a kw_only() annotation or args() argument");
+}
+
+inline void append_self_arg_if_needed(function_record *r) {
+ if (r->is_method && r->args.empty())
+ r->args.emplace_back("self", nullptr, handle(), /*convert=*/ true, /*none=*/ false);
+}
+
+/// Process a keyword argument attribute (*without* a default value)
+template <> struct process_attribute : process_attribute_default {
+ static void init(const arg &a, function_record *r) {
+ append_self_arg_if_needed(r);
+ r->args.emplace_back(a.name, nullptr, handle(), !a.flag_noconvert, a.flag_none);
+
+ check_kw_only_arg(a, r);
+ }
+};
+
+/// Process a keyword argument attribute (*with* a default value)
+template <> struct process_attribute : process_attribute_default {
+ static void init(const arg_v &a, function_record *r) {
+ if (r->is_method && r->args.empty())
+ r->args.emplace_back("self", /*descr=*/ nullptr, /*parent=*/ handle(), /*convert=*/ true, /*none=*/ false);
+
+ if (!a.value) {
+#if !defined(NDEBUG)
+ std::string descr("'");
+ if (a.name) descr += std::string(a.name) + ": ";
+ descr += a.type + "'";
+ if (r->is_method) {
+ if (r->name)
+ descr += " in method '" + (std::string) str(r->scope) + "." + (std::string) r->name + "'";
+ else
+ descr += " in method of '" + (std::string) str(r->scope) + "'";
+ } else if (r->name) {
+ descr += " in function '" + (std::string) r->name + "'";
+ }
+ pybind11_fail("arg(): could not convert default argument "
+ + descr + " into a Python object (type not registered yet?)");
+#else
+ pybind11_fail("arg(): could not convert default argument "
+ "into a Python object (type not registered yet?). "
+ "Compile in debug mode for more information.");
+#endif
+ }
+ r->args.emplace_back(a.name, a.descr, a.value.inc_ref(), !a.flag_noconvert, a.flag_none);
+
+ check_kw_only_arg(a, r);
+ }
+};
+
+/// Process a keyword-only-arguments-follow pseudo argument
+template <> struct process_attribute : process_attribute_default {
+ static void init(const kw_only &, function_record *r) {
+ append_self_arg_if_needed(r);
+ if (r->has_args && r->nargs_pos != static_cast(r->args.size()))
+ pybind11_fail("Mismatched args() and kw_only(): they must occur at the same relative argument location (or omit kw_only() entirely)");
+ r->nargs_pos = static_cast(r->args.size());
+ }
+};
+
+/// Process a positional-only-argument maker
+template <> struct process_attribute : process_attribute_default {
+ static void init(const pos_only &, function_record *r) {
+ append_self_arg_if_needed(r);
+ r->nargs_pos_only = static_cast(r->args.size());
+ if (r->nargs_pos_only > r->nargs_pos)
+ pybind11_fail("pos_only(): cannot follow a py::args() argument");
+ // It also can't follow a kw_only, but a static_assert in pybind11.h checks that
+ }
+};
+
+/// Process a parent class attribute. Single inheritance only (class_ itself already guarantees that)
+template
+struct process_attribute::value>> : process_attribute_default {
+ static void init(const handle &h, type_record *r) { r->bases.append(h); }
+};
+
+/// Process a parent class attribute (deprecated, does not support multiple inheritance)
+template
+struct process_attribute> : process_attribute_default> {
+ static void init(const base &, type_record *r) { r->add_base(typeid(T), nullptr); }
+};
+
+/// Process a multiple inheritance attribute
+template <>
+struct process_attribute : process_attribute_default {
+ static void init(const multiple_inheritance &, type_record *r) { r->multiple_inheritance = true; }
+};
+
+template <>
+struct process_attribute : process_attribute_default {
+ static void init(const dynamic_attr &, type_record *r) { r->dynamic_attr = true; }
+};
+
+template <>
+struct process_attribute {
+ static void init(const custom_type_setup &value, type_record *r) {
+ r->custom_type_setup_callback = value.value;
+ }
+};
+
+template <>
+struct process_attribute : process_attribute_default {
+ static void init(const is_final &, type_record *r) { r->is_final = true; }
+};
+
+template <>
+struct process_attribute : process_attribute_default {
+ static void init(const buffer_protocol &, type_record *r) { r->buffer_protocol = true; }
+};
+
+template <>
+struct process_attribute : process_attribute_default {
+ static void init(const metaclass &m, type_record *r) { r->metaclass = m.value; }
+};
+
+template <>
+struct process_attribute : process_attribute_default {
+ static void init(const module_local &l, type_record *r) { r->module_local = l.value; }
+};
+
+/// Process a 'prepend' attribute, putting this at the beginning of the overload chain
+template <>
+struct process_attribute : process_attribute_default {
+ static void init(const prepend &, function_record *r) { r->prepend = true; }
+};
+
+/// Process an 'arithmetic' attribute for enums (does nothing here)
+template <>
+struct process_attribute : process_attribute_default {};
+
+template
+struct process_attribute> : process_attribute_default> { };
+
+/**
+ * Process a keep_alive call policy -- invokes keep_alive_impl during the
+ * pre-call handler if both Nurse, Patient != 0 and use the post-call handler
+ * otherwise
+ */
+template struct process_attribute> : public process_attribute_default> {
+ template = 0>
+ static void precall(function_call &call) { keep_alive_impl(Nurse, Patient, call, handle()); }
+ template = 0>
+ static void postcall(function_call &, handle) { }
+ template = 0>
+ static void precall(function_call &) { }
+ template = 0>
+ static void postcall(function_call &call, handle ret) { keep_alive_impl(Nurse, Patient, call, ret); }
+};
+
+/// Recursively iterate over variadic template arguments
+template struct process_attributes {
+ static void init(const Args&... args, function_record *r) {
+ PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r);
+ PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r);
+ using expander = int[];
+ (void) expander{
+ 0, ((void) process_attribute::type>::init(args, r), 0)...};
+ }
+ static void init(const Args&... args, type_record *r) {
+ PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r);
+ PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r);
+ using expander = int[];
+ (void) expander{0,
+ (process_attribute::type>::init(args, r), 0)...};
+ }
+ static void precall(function_call &call) {
+ PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call);
+ using expander = int[];
+ (void) expander{0,
+ (process_attribute::type>::precall(call), 0)...};
+ }
+ static void postcall(function_call &call, handle fn_ret) {
+ PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call, fn_ret);
+ PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(fn_ret);
+ using expander = int[];
+ (void) expander{
+ 0, (process_attribute::type>::postcall(call, fn_ret), 0)...};
+ }
+};
+
+template
+using is_call_guard = is_instantiation;
+
+/// Extract the ``type`` from the first `call_guard` in `Extras...` (or `void_type` if none found)
+template
+using extract_guard_t = typename exactly_one_t, Extra...>::type;
+
+/// Check the number of named arguments at compile time
+template ::value...),
+ size_t self = constexpr_sum(std::is_same::value...)>
+constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs) {
+ PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(nargs, has_args, has_kwargs);
+ return named == 0 || (self + named + size_t(has_args) + size_t(has_kwargs)) == nargs;
+}
+
+PYBIND11_NAMESPACE_END(detail)
+PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
diff --git a/include/pybind11/buffer_info.h b/include/pybind11/buffer_info.h
new file mode 100644
index 00000000..eba68d1a
--- /dev/null
+++ b/include/pybind11/buffer_info.h
@@ -0,0 +1,144 @@
+/*
+ pybind11/buffer_info.h: Python buffer object interface
+
+ Copyright (c) 2016 Wenzel Jakob
+
+ All rights reserved. Use of this source code is governed by a
+ BSD-style license that can be found in the LICENSE file.
+*/
+
+#pragma once
+
+#include "detail/common.h"
+
+PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
+
+PYBIND11_NAMESPACE_BEGIN(detail)
+
+// Default, C-style strides
+inline std::vector c_strides(const std::vector &shape, ssize_t itemsize) {
+ auto ndim = shape.size();
+ std::vector strides(ndim, itemsize);
+ if (ndim > 0)
+ for (size_t i = ndim - 1; i > 0; --i)
+ strides[i - 1] = strides[i] * shape[i];
+ return strides;
+}
+
+// F-style strides; default when constructing an array_t with `ExtraFlags & f_style`
+inline std::vector f_strides(const std::vector &shape, ssize_t itemsize) {
+ auto ndim = shape.size();
+ std::vector strides(ndim, itemsize);
+ for (size_t i = 1; i < ndim; ++i)
+ strides[i] = strides[i - 1] * shape[i - 1];
+ return strides;
+}
+
+PYBIND11_NAMESPACE_END(detail)
+
+/// Information record describing a Python buffer object
+struct buffer_info {
+ void *ptr = nullptr; // Pointer to the underlying storage
+ ssize_t itemsize = 0; // Size of individual items in bytes
+ ssize_t size = 0; // Total number of entries
+ std::string format; // For homogeneous buffers, this should be set to format_descriptor::format()
+ ssize_t ndim = 0; // Number of dimensions
+ std::vector shape; // Shape of the tensor (1 entry per dimension)
+ std::vector strides; // Number of bytes between adjacent entries (for each per dimension)
+ bool readonly = false; // flag to indicate if the underlying storage may be written to
+
+ buffer_info() = default;
+
+ buffer_info(void *ptr, ssize_t itemsize, const std::string &format, ssize_t ndim,
+ detail::any_container shape_in, detail::any_container strides_in, bool readonly=false)
+ : ptr(ptr), itemsize(itemsize), size(1), format(format), ndim(ndim),
+ shape(std::move(shape_in)), strides(std::move(strides_in)), readonly(readonly) {
+ if (ndim != (ssize_t) shape.size() || ndim != (ssize_t) strides.size())
+ pybind11_fail("buffer_info: ndim doesn't match shape and/or strides length");
+ for (size_t i = 0; i < (size_t) ndim; ++i)
+ size *= shape[i];
+ }
+
+ template
+ buffer_info(T *ptr, detail::any_container shape_in, detail::any_container strides_in, bool readonly=false)
+ : buffer_info(private_ctr_tag(), ptr, sizeof(T), format_descriptor::format(), static_cast(shape_in->size()), std::move(shape_in), std::move(strides_in), readonly) { }
+
+ buffer_info(void *ptr, ssize_t itemsize, const std::string &format, ssize_t size, bool readonly=false)
+ : buffer_info(ptr, itemsize, format, 1, {size}, {itemsize}, readonly) { }
+
+ template
+ buffer_info(T *ptr, ssize_t size, bool readonly=false)
+ : buffer_info(ptr, sizeof(T), format_descriptor::format(), size, readonly) { }
+
+ template
+ buffer_info(const T *ptr, ssize_t size, bool readonly=true)
+ : buffer_info(const_cast(ptr), sizeof(T), format_descriptor::format(), size, readonly) { }
+
+ explicit buffer_info(Py_buffer *view, bool ownview = true)
+ : buffer_info(view->buf, view->itemsize, view->format, view->ndim,
+ {view->shape, view->shape + view->ndim},
+ /* Though buffer::request() requests PyBUF_STRIDES, ctypes objects
+ * ignore this flag and return a view with NULL strides.
+ * When strides are NULL, build them manually. */
+ view->strides
+ ? std::vector(view->strides, view->strides + view->ndim)
+ : detail::c_strides({view->shape, view->shape + view->ndim}, view->itemsize),
+ (view->readonly != 0)) {
+ this->m_view = view;
+ this->ownview = ownview;
+ }
+
+ buffer_info(const buffer_info &) = delete;
+ buffer_info& operator=(const buffer_info &) = delete;
+
+ buffer_info(buffer_info &&other) noexcept { (*this) = std::move(other); }
+
+ buffer_info &operator=(buffer_info &&rhs) noexcept {
+ ptr = rhs.ptr;
+ itemsize = rhs.itemsize;
+ size = rhs.size;
+ format = std::move(rhs.format);
+ ndim = rhs.ndim;
+ shape = std::move(rhs.shape);
+ strides = std::move(rhs.strides);
+ std::swap(m_view, rhs.m_view);
+ std::swap(ownview, rhs.ownview);
+ readonly = rhs.readonly;
+ return *this;
+ }
+
+ ~buffer_info() {
+ if (m_view && ownview) { PyBuffer_Release(m_view); delete m_view; }
+ }
+
+ Py_buffer *view() const { return m_view; }
+ Py_buffer *&view() { return m_view; }
+private:
+ struct private_ctr_tag { };
+
+ buffer_info(private_ctr_tag, void *ptr, ssize_t itemsize, const std::string &format, ssize_t ndim,
+ detail::any_container &&shape_in, detail::any_container &&strides_in, bool readonly)
+ : buffer_info(ptr, itemsize, format, ndim, std::move(shape_in), std::move(strides_in), readonly) { }
+
+ Py_buffer *m_view = nullptr;
+ bool ownview = false;
+};
+
+PYBIND11_NAMESPACE_BEGIN(detail)
+
+template struct compare_buffer_info {
+ static bool compare(const buffer_info& b) {
+ return b.format == format_descriptor::format() && b.itemsize == (ssize_t) sizeof(T);
+ }
+};
+
+template struct compare_buffer_info::value>> {
+ static bool compare(const buffer_info& b) {
+ return (size_t) b.itemsize == sizeof(T) && (b.format == format_descriptor::value ||
+ ((sizeof(T) == sizeof(long)) && b.format == (std::is_unsigned::value ? "L" : "l")) ||
+ ((sizeof(T) == sizeof(size_t)) && b.format == (std::is_unsigned::value ? "N" : "n")));
+ }
+};
+
+PYBIND11_NAMESPACE_END(detail)
+PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h
new file mode 100644
index 00000000..16510244
--- /dev/null
+++ b/include/pybind11/cast.h
@@ -0,0 +1,1422 @@
+/*
+ pybind11/cast.h: Partial template specializations to cast between
+ C++ and Python types
+
+ Copyright (c) 2016 Wenzel Jakob
+
+ All rights reserved. Use of this source code is governed by a
+ BSD-style license that can be found in the LICENSE file.
+*/
+
+#pragma once
+
+#include "pytypes.h"
+#include "detail/common.h"
+#include "detail/descr.h"
+#include "detail/type_caster_base.h"
+#include "detail/typeid.h"
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
+PYBIND11_NAMESPACE_BEGIN(detail)
+
+template class type_caster : public type_caster_base { };
+template using make_caster = type_caster>;
+
+// Shortcut for calling a caster's `cast_op_type` cast operator for casting a type_caster to a T
+template typename make_caster::template cast_op_type cast_op(make_caster &caster) {
+ return caster.operator typename make_caster::template cast_op_type();
+}
+template typename make_caster::template cast_op_type::type>
+cast_op(make_caster &&caster) {
+ return std::move(caster).operator
+ typename make_caster::template cast_op_type::type>();
+}
+
+template class type_caster> {
+private:
+ using caster_t = make_caster;
+ caster_t subcaster;
+ using reference_t = type&;
+ using subcaster_cast_op_type =
+ typename caster_t::template cast_op_type;
+
+ static_assert(std::is_same::type &, subcaster_cast_op_type>::value ||
+ std::is_same::value,
+ "std::reference_wrapper caster requires T to have a caster with an "
+ "`operator T &()` or `operator const T &()`");
+public:
+ bool load(handle src, bool convert) { return subcaster.load(src, convert); }
+ static constexpr auto name = caster_t::name;
+ static handle cast(const std::reference_wrapper &src, return_value_policy policy, handle parent) {
+ // It is definitely wrong to take ownership of this pointer, so mask that rvp
+ if (policy == return_value_policy::take_ownership || policy == return_value_policy::automatic)
+ policy = return_value_policy::automatic_reference;
+ return caster_t::cast(&src.get(), policy, parent);
+ }
+ template using cast_op_type = std::reference_wrapper;
+ explicit operator std::reference_wrapper() { return cast_op(subcaster); }
+};
+
+#define PYBIND11_TYPE_CASTER(type, py_name) \
+protected: \
+ type value; \
+ \
+public: \
+ static constexpr auto name = py_name; \
+ template >::value, int> = 0> \
+ static handle cast(T_ *src, return_value_policy policy, handle parent) { \
+ if (!src) \
+ return none().release(); \
+ if (policy == return_value_policy::take_ownership) { \
+ auto h = cast(std::move(*src), policy, parent); \
+ delete src; \
+ return h; \
+ } \
+ return cast(*src, policy, parent); \
+ } \
+ operator type *() { return &value; } /* NOLINT(bugprone-macro-parentheses) */ \
+ operator type &() { return value; } /* NOLINT(bugprone-macro-parentheses) */ \
+ operator type &&() && { return std::move(value); } /* NOLINT(bugprone-macro-parentheses) */ \
+ template \
+ using cast_op_type = pybind11::detail::movable_cast_op_type
+
+template using is_std_char_type = any_of<
+ std::is_same, /* std::string */
+#if defined(PYBIND11_HAS_U8STRING)
+ std::is_same, /* std::u8string */
+#endif
+ std::is_same, /* std::u16string */
+ std::is_same, /* std::u32string */
+ std::is_same /* std::wstring */
+>;
+
+
+template
+struct type_caster::value && !is_std_char_type::value>> {
+ using _py_type_0 = conditional_t;
+ using _py_type_1 = conditional_t::value, _py_type_0, typename std::make_unsigned<_py_type_0>::type>;
+ using py_type = conditional_t::value, double, _py_type_1>;
+public:
+
+ bool load(handle src, bool convert) {
+ py_type py_value;
+
+ if (!src)
+ return false;
+
+#if !defined(PYPY_VERSION)
+ auto index_check = [](PyObject *o) { return PyIndex_Check(o); };
+#else
+ // In PyPy 7.3.3, `PyIndex_Check` is implemented by calling `__index__`,
+ // while CPython only considers the existence of `nb_index`/`__index__`.
+ auto index_check = [](PyObject *o) { return hasattr(o, "__index__"); };
+#endif
+
+ if (std::is_floating_point::value) {
+ if (convert || PyFloat_Check(src.ptr()))
+ py_value = (py_type) PyFloat_AsDouble(src.ptr());
+ else
+ return false;
+ } else if (PyFloat_Check(src.ptr())
+ || (!convert && !PYBIND11_LONG_CHECK(src.ptr()) && !index_check(src.ptr()))) {
+ return false;
+ } else {
+ handle src_or_index = src;
+ // PyPy: 7.3.7's 3.8 does not implement PyLong_*'s __index__ calls.
+#if PY_VERSION_HEX < 0x03080000 || defined(PYPY_VERSION)
+ object index;
+ if (!PYBIND11_LONG_CHECK(src.ptr())) { // So: index_check(src.ptr())
+ index = reinterpret_steal