attr.h 23 KB

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