attr.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. pybind11/attr.h: Infrastructure for processing custom
  3. type and function attributes
  4. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  5. All rights reserved. Use of this source code is governed by a
  6. BSD-style license that can be found in the LICENSE file.
  7. */
  8. #pragma once
  9. #include "detail/common.h"
  10. #include "cast.h"
  11. #include <functional>
  12. PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  13. /// \addtogroup annotations
  14. /// @{
  15. /// Annotation for methods
  16. struct is_method {
  17. handle class_;
  18. explicit is_method(const handle &c) : class_(c) {}
  19. };
  20. /// Annotation for setters
  21. struct is_setter {};
  22. /// Annotation for operators
  23. struct is_operator {};
  24. /// Annotation for classes that cannot be subclassed
  25. struct is_final {};
  26. /// Annotation for parent scope
  27. struct scope {
  28. handle value;
  29. explicit scope(const handle &s) : value(s) {}
  30. };
  31. /// Annotation for documentation
  32. struct doc {
  33. const char *value;
  34. explicit doc(const char *value) : value(value) {}
  35. };
  36. /// Annotation for function names
  37. struct name {
  38. const char *value;
  39. explicit name(const char *value) : value(value) {}
  40. };
  41. /// Annotation indicating that a function is an overload associated with a given "sibling"
  42. struct sibling {
  43. handle value;
  44. explicit sibling(const handle &value) : value(value.ptr()) {}
  45. };
  46. /// Annotation indicating that a class derives from another given type
  47. template <typename T>
  48. struct base {
  49. PYBIND11_DEPRECATED(
  50. "base<T>() was deprecated in favor of specifying 'T' as a template argument to class_")
  51. base() = default;
  52. };
  53. /// Keep patient alive while nurse lives
  54. template <size_t Nurse, size_t Patient>
  55. struct keep_alive {};
  56. /// Annotation indicating that a class is involved in a multiple inheritance relationship
  57. struct multiple_inheritance {};
  58. /// Annotation which enables dynamic attributes, i.e. adds `__dict__` to a class
  59. struct dynamic_attr {};
  60. /// Annotation which enables the buffer protocol for a type
  61. struct buffer_protocol {};
  62. /// Annotation which requests that a special metaclass is created for a type
  63. struct metaclass {
  64. handle value;
  65. PYBIND11_DEPRECATED("py::metaclass() is no longer required. It's turned on by default now.")
  66. metaclass() = default;
  67. /// Override pybind11's default metaclass
  68. explicit metaclass(handle value) : value(value) {}
  69. };
  70. /// Specifies a custom callback with signature `void (PyHeapTypeObject*)` that
  71. /// may be used to customize the Python type.
  72. ///
  73. /// The callback is invoked immediately before `PyType_Ready`.
  74. ///
  75. /// Note: This is an advanced interface, and uses of it may require changes to
  76. /// work with later versions of pybind11. You may wish to consult the
  77. /// implementation of `make_new_python_type` in `detail/classes.h` to understand
  78. /// the context in which the callback will be run.
  79. struct custom_type_setup {
  80. using callback = std::function<void(PyHeapTypeObject *heap_type)>;
  81. explicit custom_type_setup(callback value) : value(std::move(value)) {}
  82. callback value;
  83. };
  84. /// Annotation that marks a class as local to the module:
  85. struct module_local {
  86. const bool value;
  87. constexpr explicit module_local(bool v = true) : value(v) {}
  88. };
  89. /// Annotation to mark enums as an arithmetic type
  90. struct arithmetic {};
  91. /// Mark a function for addition at the beginning of the existing overload chain instead of the end
  92. struct prepend {};
  93. /** \rst
  94. A call policy which places one or more guard variables (``Ts...``) around the function call.
  95. For example, this definition:
  96. .. code-block:: cpp
  97. m.def("foo", foo, py::call_guard<T>());
  98. is equivalent to the following pseudocode:
  99. .. code-block:: cpp
  100. m.def("foo", [](args...) {
  101. T scope_guard;
  102. return foo(args...); // forwarded arguments
  103. });
  104. \endrst */
  105. template <typename... Ts>
  106. struct call_guard;
  107. template <>
  108. struct call_guard<> {
  109. using type = detail::void_type;
  110. };
  111. template <typename T>
  112. struct call_guard<T> {
  113. static_assert(std::is_default_constructible<T>::value,
  114. "The guard type must be default constructible");
  115. using type = T;
  116. };
  117. template <typename T, typename... Ts>
  118. struct call_guard<T, Ts...> {
  119. struct type {
  120. T guard{}; // Compose multiple guard types with left-to-right default-constructor order
  121. typename call_guard<Ts...>::type next{};
  122. };
  123. };
  124. /// @} annotations
  125. PYBIND11_NAMESPACE_BEGIN(detail)
  126. /* Forward declarations */
  127. enum op_id : int;
  128. enum op_type : int;
  129. struct undefined_t;
  130. template <op_id id, op_type ot, typename L = undefined_t, typename R = undefined_t>
  131. struct op_;
  132. void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret);
  133. /// Internal data structure which holds metadata about a keyword argument
  134. struct argument_record {
  135. const char *name; ///< Argument name
  136. const char *descr; ///< Human-readable version of the argument value
  137. handle value; ///< Associated Python object
  138. bool convert : 1; ///< True if the argument is allowed to convert when loading
  139. bool none : 1; ///< True if None is allowed when loading
  140. argument_record(const char *name, const char *descr, handle value, bool convert, bool none)
  141. : name(name), descr(descr), value(value), convert(convert), none(none) {}
  142. };
  143. /// Internal data structure which holds metadata about a bound function (signature, overloads,
  144. /// etc.)
  145. struct function_record {
  146. function_record()
  147. : is_constructor(false), is_new_style_constructor(false), is_stateless(false),
  148. is_operator(false), is_method(false), is_setter(false), has_args(false),
  149. has_kwargs(false), prepend(false) {}
  150. /// Function name
  151. char *name = nullptr; /* why no C++ strings? They generate heavier code.. */
  152. // User-specified documentation string
  153. char *doc = nullptr;
  154. /// Human-readable version of the function signature
  155. char *signature = nullptr;
  156. /// List of registered keyword arguments
  157. std::vector<argument_record> args;
  158. /// Pointer to lambda function which converts arguments and performs the actual call
  159. handle (*impl)(function_call &) = nullptr;
  160. /// Storage for the wrapped function pointer and captured data, if any
  161. void *data[3] = {};
  162. /// Pointer to custom destructor for 'data' (if needed)
  163. void (*free_data)(function_record *ptr) = nullptr;
  164. /// Return value policy associated with this function
  165. return_value_policy policy = return_value_policy::automatic;
  166. /// True if name == '__init__'
  167. bool is_constructor : 1;
  168. /// True if this is a new-style `__init__` defined in `detail/init.h`
  169. bool is_new_style_constructor : 1;
  170. /// True if this is a stateless function pointer
  171. bool is_stateless : 1;
  172. /// True if this is an operator (__add__), etc.
  173. bool is_operator : 1;
  174. /// True if this is a method
  175. bool is_method : 1;
  176. /// True if this is a setter
  177. bool is_setter : 1;
  178. /// True if the function has a '*args' argument
  179. bool has_args : 1;
  180. /// True if the function has a '**kwargs' argument
  181. bool has_kwargs : 1;
  182. /// True if this function is to be inserted at the beginning of the overload resolution chain
  183. bool prepend : 1;
  184. /// Number of arguments (including py::args and/or py::kwargs, if present)
  185. std::uint16_t nargs;
  186. /// Number of leading positional arguments, which are terminated by a py::args or py::kwargs
  187. /// argument or by a py::kw_only annotation.
  188. std::uint16_t nargs_pos = 0;
  189. /// Number of leading arguments (counted in `nargs`) that are positional-only
  190. std::uint16_t nargs_pos_only = 0;
  191. /// Python method object
  192. PyMethodDef *def = nullptr;
  193. /// Python handle to the parent scope (a class or a module)
  194. handle scope;
  195. /// Python handle to the sibling function representing an overload chain
  196. handle sibling;
  197. /// Pointer to next overload
  198. function_record *next = nullptr;
  199. };
  200. /// Special data structure which (temporarily) holds metadata about a bound class
  201. struct type_record {
  202. PYBIND11_NOINLINE type_record()
  203. : multiple_inheritance(false), dynamic_attr(false), buffer_protocol(false),
  204. default_holder(true), module_local(false), is_final(false) {}
  205. /// Handle to the parent scope
  206. handle scope;
  207. /// Name of the class
  208. const char *name = nullptr;
  209. // Pointer to RTTI type_info data structure
  210. const std::type_info *type = nullptr;
  211. /// How large is the underlying C++ type?
  212. size_t type_size = 0;
  213. /// What is the alignment of the underlying C++ type?
  214. size_t type_align = 0;
  215. /// How large is the type's holder?
  216. size_t holder_size = 0;
  217. /// The global operator new can be overridden with a class-specific variant
  218. void *(*operator_new)(size_t) = nullptr;
  219. /// Function pointer to class_<..>::init_instance
  220. void (*init_instance)(instance *, const void *) = nullptr;
  221. /// Function pointer to class_<..>::dealloc
  222. void (*dealloc)(detail::value_and_holder &) = nullptr;
  223. /// List of base classes of the newly created type
  224. list bases;
  225. /// Optional docstring
  226. const char *doc = nullptr;
  227. /// Custom metaclass (optional)
  228. handle metaclass;
  229. /// Custom type setup.
  230. custom_type_setup::callback custom_type_setup_callback;
  231. /// Multiple inheritance marker
  232. bool multiple_inheritance : 1;
  233. /// Does the class manage a __dict__?
  234. bool dynamic_attr : 1;
  235. /// Does the class implement the buffer protocol?
  236. bool buffer_protocol : 1;
  237. /// Is the default (unique_ptr) holder type used?
  238. bool default_holder : 1;
  239. /// Is the class definition local to the module shared object?
  240. bool module_local : 1;
  241. /// Is the class inheritable from python classes?
  242. bool is_final : 1;
  243. PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *) ) {
  244. auto *base_info = detail::get_type_info(base, false);
  245. if (!base_info) {
  246. std::string tname(base.name());
  247. detail::clean_type_id(tname);
  248. pybind11_fail("generic_type: type \"" + std::string(name)
  249. + "\" referenced unknown base type \"" + tname + "\"");
  250. }
  251. if (default_holder != base_info->default_holder) {
  252. std::string tname(base.name());
  253. detail::clean_type_id(tname);
  254. pybind11_fail("generic_type: type \"" + std::string(name) + "\" "
  255. + (default_holder ? "does not have" : "has")
  256. + " a non-default holder type while its base \"" + tname + "\" "
  257. + (base_info->default_holder ? "does not" : "does"));
  258. }
  259. bases.append((PyObject *) base_info->type);
  260. #if PY_VERSION_HEX < 0x030B0000
  261. dynamic_attr |= base_info->type->tp_dictoffset != 0;
  262. #else
  263. dynamic_attr |= (base_info->type->tp_flags & Py_TPFLAGS_MANAGED_DICT) != 0;
  264. #endif
  265. if (caster) {
  266. base_info->implicit_casts.emplace_back(type, caster);
  267. }
  268. }
  269. };
  270. inline function_call::function_call(const function_record &f, handle p) : func(f), parent(p) {
  271. args.reserve(f.nargs);
  272. args_convert.reserve(f.nargs);
  273. }
  274. /// Tag for a new-style `__init__` defined in `detail/init.h`
  275. struct is_new_style_constructor {};
  276. /**
  277. * Partial template specializations to process custom attributes provided to
  278. * cpp_function_ and class_. These are either used to initialize the respective
  279. * fields in the type_record and function_record data structures or executed at
  280. * runtime to deal with custom call policies (e.g. keep_alive).
  281. */
  282. template <typename T, typename SFINAE = void>
  283. struct process_attribute;
  284. template <typename T>
  285. struct process_attribute_default {
  286. /// Default implementation: do nothing
  287. static void init(const T &, function_record *) {}
  288. static void init(const T &, type_record *) {}
  289. static void precall(function_call &) {}
  290. static void postcall(function_call &, handle) {}
  291. };
  292. /// Process an attribute specifying the function's name
  293. template <>
  294. struct process_attribute<name> : process_attribute_default<name> {
  295. static void init(const name &n, function_record *r) { r->name = const_cast<char *>(n.value); }
  296. };
  297. /// Process an attribute specifying the function's docstring
  298. template <>
  299. struct process_attribute<doc> : process_attribute_default<doc> {
  300. static void init(const doc &n, function_record *r) { r->doc = const_cast<char *>(n.value); }
  301. };
  302. /// Process an attribute specifying the function's docstring (provided as a C-style string)
  303. template <>
  304. struct process_attribute<const char *> : process_attribute_default<const char *> {
  305. static void init(const char *d, function_record *r) { r->doc = const_cast<char *>(d); }
  306. static void init(const char *d, type_record *r) { r->doc = d; }
  307. };
  308. template <>
  309. struct process_attribute<char *> : process_attribute<const char *> {};
  310. /// Process an attribute indicating the function's return value policy
  311. template <>
  312. struct process_attribute<return_value_policy> : process_attribute_default<return_value_policy> {
  313. static void init(const return_value_policy &p, function_record *r) { r->policy = p; }
  314. };
  315. /// Process an attribute which indicates that this is an overloaded function associated with a
  316. /// given sibling
  317. template <>
  318. struct process_attribute<sibling> : process_attribute_default<sibling> {
  319. static void init(const sibling &s, function_record *r) { r->sibling = s.value; }
  320. };
  321. /// Process an attribute which indicates that this function is a method
  322. template <>
  323. struct process_attribute<is_method> : process_attribute_default<is_method> {
  324. static void init(const is_method &s, function_record *r) {
  325. r->is_method = true;
  326. r->scope = s.class_;
  327. }
  328. };
  329. /// Process an attribute which indicates that this function is a setter
  330. template <>
  331. struct process_attribute<is_setter> : process_attribute_default<is_setter> {
  332. static void init(const is_setter &, function_record *r) { r->is_setter = true; }
  333. };
  334. /// Process an attribute which indicates the parent scope of a method
  335. template <>
  336. struct process_attribute<scope> : process_attribute_default<scope> {
  337. static void init(const scope &s, function_record *r) { r->scope = s.value; }
  338. };
  339. /// Process an attribute which indicates that this function is an operator
  340. template <>
  341. struct process_attribute<is_operator> : process_attribute_default<is_operator> {
  342. static void init(const is_operator &, function_record *r) { r->is_operator = true; }
  343. };
  344. template <>
  345. struct process_attribute<is_new_style_constructor>
  346. : process_attribute_default<is_new_style_constructor> {
  347. static void init(const is_new_style_constructor &, function_record *r) {
  348. r->is_new_style_constructor = true;
  349. }
  350. };
  351. inline void check_kw_only_arg(const arg &a, function_record *r) {
  352. if (r->args.size() > r->nargs_pos && (!a.name || a.name[0] == '\0')) {
  353. pybind11_fail("arg(): cannot specify an unnamed argument after a kw_only() annotation or "
  354. "args() argument");
  355. }
  356. }
  357. inline void append_self_arg_if_needed(function_record *r) {
  358. if (r->is_method && r->args.empty()) {
  359. r->args.emplace_back("self", nullptr, handle(), /*convert=*/true, /*none=*/false);
  360. }
  361. }
  362. /// Process a keyword argument attribute (*without* a default value)
  363. template <>
  364. struct process_attribute<arg> : process_attribute_default<arg> {
  365. static void init(const arg &a, function_record *r) {
  366. append_self_arg_if_needed(r);
  367. r->args.emplace_back(a.name, nullptr, handle(), !a.flag_noconvert, a.flag_none);
  368. check_kw_only_arg(a, r);
  369. }
  370. };
  371. /// Process a keyword argument attribute (*with* a default value)
  372. template <>
  373. struct process_attribute<arg_v> : process_attribute_default<arg_v> {
  374. static void init(const arg_v &a, function_record *r) {
  375. if (r->is_method && r->args.empty()) {
  376. r->args.emplace_back(
  377. "self", /*descr=*/nullptr, /*parent=*/handle(), /*convert=*/true, /*none=*/false);
  378. }
  379. if (!a.value) {
  380. #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
  381. std::string descr("'");
  382. if (a.name) {
  383. descr += std::string(a.name) + ": ";
  384. }
  385. descr += a.type + "'";
  386. if (r->is_method) {
  387. if (r->name) {
  388. descr += " in method '" + (std::string) str(r->scope) + "."
  389. + (std::string) r->name + "'";
  390. } else {
  391. descr += " in method of '" + (std::string) str(r->scope) + "'";
  392. }
  393. } else if (r->name) {
  394. descr += " in function '" + (std::string) r->name + "'";
  395. }
  396. pybind11_fail("arg(): could not convert default argument " + descr
  397. + " into a Python object (type not registered yet?)");
  398. #else
  399. pybind11_fail("arg(): could not convert default argument "
  400. "into a Python object (type not registered yet?). "
  401. "#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for "
  402. "more information.");
  403. #endif
  404. }
  405. r->args.emplace_back(a.name, a.descr, a.value.inc_ref(), !a.flag_noconvert, a.flag_none);
  406. check_kw_only_arg(a, r);
  407. }
  408. };
  409. /// Process a keyword-only-arguments-follow pseudo argument
  410. template <>
  411. struct process_attribute<kw_only> : process_attribute_default<kw_only> {
  412. static void init(const kw_only &, function_record *r) {
  413. append_self_arg_if_needed(r);
  414. if (r->has_args && r->nargs_pos != static_cast<std::uint16_t>(r->args.size())) {
  415. pybind11_fail("Mismatched args() and kw_only(): they must occur at the same relative "
  416. "argument location (or omit kw_only() entirely)");
  417. }
  418. r->nargs_pos = static_cast<std::uint16_t>(r->args.size());
  419. }
  420. };
  421. /// Process a positional-only-argument maker
  422. template <>
  423. struct process_attribute<pos_only> : process_attribute_default<pos_only> {
  424. static void init(const pos_only &, function_record *r) {
  425. append_self_arg_if_needed(r);
  426. r->nargs_pos_only = static_cast<std::uint16_t>(r->args.size());
  427. if (r->nargs_pos_only > r->nargs_pos) {
  428. pybind11_fail("pos_only(): cannot follow a py::args() argument");
  429. }
  430. // It also can't follow a kw_only, but a static_assert in pybind11.h checks that
  431. }
  432. };
  433. /// Process a parent class attribute. Single inheritance only (class_ itself already guarantees
  434. /// that)
  435. template <typename T>
  436. struct process_attribute<T, enable_if_t<is_pyobject<T>::value>>
  437. : process_attribute_default<handle> {
  438. static void init(const handle &h, type_record *r) { r->bases.append(h); }
  439. };
  440. /// Process a parent class attribute (deprecated, does not support multiple inheritance)
  441. template <typename T>
  442. struct process_attribute<base<T>> : process_attribute_default<base<T>> {
  443. static void init(const base<T> &, type_record *r) { r->add_base(typeid(T), nullptr); }
  444. };
  445. /// Process a multiple inheritance attribute
  446. template <>
  447. struct process_attribute<multiple_inheritance> : process_attribute_default<multiple_inheritance> {
  448. static void init(const multiple_inheritance &, type_record *r) {
  449. r->multiple_inheritance = true;
  450. }
  451. };
  452. template <>
  453. struct process_attribute<dynamic_attr> : process_attribute_default<dynamic_attr> {
  454. static void init(const dynamic_attr &, type_record *r) { r->dynamic_attr = true; }
  455. };
  456. template <>
  457. struct process_attribute<custom_type_setup> {
  458. static void init(const custom_type_setup &value, type_record *r) {
  459. r->custom_type_setup_callback = value.value;
  460. }
  461. };
  462. template <>
  463. struct process_attribute<is_final> : process_attribute_default<is_final> {
  464. static void init(const is_final &, type_record *r) { r->is_final = true; }
  465. };
  466. template <>
  467. struct process_attribute<buffer_protocol> : process_attribute_default<buffer_protocol> {
  468. static void init(const buffer_protocol &, type_record *r) { r->buffer_protocol = true; }
  469. };
  470. template <>
  471. struct process_attribute<metaclass> : process_attribute_default<metaclass> {
  472. static void init(const metaclass &m, type_record *r) { r->metaclass = m.value; }
  473. };
  474. template <>
  475. struct process_attribute<module_local> : process_attribute_default<module_local> {
  476. static void init(const module_local &l, type_record *r) { r->module_local = l.value; }
  477. };
  478. /// Process a 'prepend' attribute, putting this at the beginning of the overload chain
  479. template <>
  480. struct process_attribute<prepend> : process_attribute_default<prepend> {
  481. static void init(const prepend &, function_record *r) { r->prepend = true; }
  482. };
  483. /// Process an 'arithmetic' attribute for enums (does nothing here)
  484. template <>
  485. struct process_attribute<arithmetic> : process_attribute_default<arithmetic> {};
  486. template <typename... Ts>
  487. struct process_attribute<call_guard<Ts...>> : process_attribute_default<call_guard<Ts...>> {};
  488. /**
  489. * Process a keep_alive call policy -- invokes keep_alive_impl during the
  490. * pre-call handler if both Nurse, Patient != 0 and use the post-call handler
  491. * otherwise
  492. */
  493. template <size_t Nurse, size_t Patient>
  494. struct process_attribute<keep_alive<Nurse, Patient>>
  495. : public process_attribute_default<keep_alive<Nurse, Patient>> {
  496. template <size_t N = Nurse, size_t P = Patient, enable_if_t<N != 0 && P != 0, int> = 0>
  497. static void precall(function_call &call) {
  498. keep_alive_impl(Nurse, Patient, call, handle());
  499. }
  500. template <size_t N = Nurse, size_t P = Patient, enable_if_t<N != 0 && P != 0, int> = 0>
  501. static void postcall(function_call &, handle) {}
  502. template <size_t N = Nurse, size_t P = Patient, enable_if_t<N == 0 || P == 0, int> = 0>
  503. static void precall(function_call &) {}
  504. template <size_t N = Nurse, size_t P = Patient, enable_if_t<N == 0 || P == 0, int> = 0>
  505. static void postcall(function_call &call, handle ret) {
  506. keep_alive_impl(Nurse, Patient, call, ret);
  507. }
  508. };
  509. /// Recursively iterate over variadic template arguments
  510. template <typename... Args>
  511. struct process_attributes {
  512. static void init(const Args &...args, function_record *r) {
  513. PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r);
  514. PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r);
  515. using expander = int[];
  516. (void) expander{
  517. 0, ((void) process_attribute<typename std::decay<Args>::type>::init(args, r), 0)...};
  518. }
  519. static void init(const Args &...args, type_record *r) {
  520. PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r);
  521. PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r);
  522. using expander = int[];
  523. (void) expander{0,
  524. (process_attribute<typename std::decay<Args>::type>::init(args, r), 0)...};
  525. }
  526. static void precall(function_call &call) {
  527. PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call);
  528. using expander = int[];
  529. (void) expander{0,
  530. (process_attribute<typename std::decay<Args>::type>::precall(call), 0)...};
  531. }
  532. static void postcall(function_call &call, handle fn_ret) {
  533. PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call, fn_ret);
  534. PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(fn_ret);
  535. using expander = int[];
  536. (void) expander{
  537. 0, (process_attribute<typename std::decay<Args>::type>::postcall(call, fn_ret), 0)...};
  538. }
  539. };
  540. template <typename T>
  541. using is_call_guard = is_instantiation<call_guard, T>;
  542. /// Extract the ``type`` from the first `call_guard` in `Extras...` (or `void_type` if none found)
  543. template <typename... Extra>
  544. using extract_guard_t = typename exactly_one_t<is_call_guard, call_guard<>, Extra...>::type;
  545. /// Check the number of named arguments at compile time
  546. template <typename... Extra,
  547. size_t named = constexpr_sum(std::is_base_of<arg, Extra>::value...),
  548. size_t self = constexpr_sum(std::is_same<is_method, Extra>::value...)>
  549. constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs) {
  550. PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(nargs, has_args, has_kwargs);
  551. return named == 0 || (self + named + size_t(has_args) + size_t(has_kwargs)) == nargs;
  552. }
  553. PYBIND11_NAMESPACE_END(detail)
  554. PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)