init.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. pybind11/detail/init.h: init factory function implementation and support code.
  3. Copyright (c) 2017 Jason Rhinelander <jason@imaginary.ca>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #pragma once
  8. #include "class.h"
  9. #include "using_smart_holder.h"
  10. PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  11. PYBIND11_WARNING_DISABLE_MSVC(4127)
  12. PYBIND11_NAMESPACE_BEGIN(detail)
  13. template <>
  14. class type_caster<value_and_holder> {
  15. public:
  16. bool load(handle h, bool) {
  17. value = reinterpret_cast<value_and_holder *>(h.ptr());
  18. return true;
  19. }
  20. template <typename>
  21. using cast_op_type = value_and_holder &;
  22. explicit operator value_and_holder &() { return *value; }
  23. static constexpr auto name = const_name<value_and_holder>();
  24. private:
  25. value_and_holder *value = nullptr;
  26. };
  27. PYBIND11_NAMESPACE_BEGIN(initimpl)
  28. inline void no_nullptr(const void *ptr) {
  29. if (!ptr) {
  30. throw type_error("pybind11::init(): factory function returned nullptr");
  31. }
  32. }
  33. // Implementing functions for all forms of py::init<...> and py::init(...)
  34. template <typename Class>
  35. using Cpp = typename Class::type;
  36. template <typename Class>
  37. using Alias = typename Class::type_alias;
  38. template <typename Class>
  39. using Holder = typename Class::holder_type;
  40. template <typename Class>
  41. using is_alias_constructible = std::is_constructible<Alias<Class>, Cpp<Class> &&>;
  42. // Takes a Cpp pointer and returns true if it actually is a polymorphic Alias instance.
  43. template <typename Class, enable_if_t<Class::has_alias, int> = 0>
  44. bool is_alias(Cpp<Class> *ptr) {
  45. return dynamic_cast<Alias<Class> *>(ptr) != nullptr;
  46. }
  47. // Failing fallback version of the above for a no-alias class (always returns false)
  48. template <typename /*Class*/>
  49. constexpr bool is_alias(const void *) {
  50. return false;
  51. }
  52. // Constructs and returns a new object; if the given arguments don't map to a constructor, we fall
  53. // back to brace aggregate initialization so that for aggregate initialization can be used with
  54. // py::init, e.g. `py::init<int, int>` to initialize a `struct T { int a; int b; }`. For
  55. // non-aggregate types, we need to use an ordinary T(...) constructor (invoking as `T{...}` usually
  56. // works, but will not do the expected thing when `T` has an `initializer_list<T>` constructor).
  57. template <typename Class,
  58. typename... Args,
  59. detail::enable_if_t<std::is_constructible<Class, Args...>::value, int> = 0>
  60. inline Class *construct_or_initialize(Args &&...args) {
  61. return new Class(std::forward<Args>(args)...);
  62. }
  63. template <typename Class,
  64. typename... Args,
  65. detail::enable_if_t<!std::is_constructible<Class, Args...>::value, int> = 0>
  66. inline Class *construct_or_initialize(Args &&...args) {
  67. return new Class{std::forward<Args>(args)...};
  68. }
  69. // Attempts to constructs an alias using a `Alias(Cpp &&)` constructor. This allows types with
  70. // an alias to provide only a single Cpp factory function as long as the Alias can be
  71. // constructed from an rvalue reference of the base Cpp type. This means that Alias classes
  72. // can, when appropriate, simply define a `Alias(Cpp &&)` constructor rather than needing to
  73. // inherit all the base class constructors.
  74. template <typename Class>
  75. void construct_alias_from_cpp(std::true_type /*is_alias_constructible*/,
  76. value_and_holder &v_h,
  77. Cpp<Class> &&base) {
  78. v_h.value_ptr() = new Alias<Class>(std::move(base));
  79. }
  80. template <typename Class>
  81. [[noreturn]] void construct_alias_from_cpp(std::false_type /*!is_alias_constructible*/,
  82. value_and_holder &,
  83. Cpp<Class> &&) {
  84. throw type_error("pybind11::init(): unable to convert returned instance to required "
  85. "alias class: no `Alias<Class>(Class &&)` constructor available");
  86. }
  87. // Error-generating fallback for factories that don't match one of the below construction
  88. // mechanisms.
  89. template <typename Class>
  90. void construct(...) {
  91. static_assert(!std::is_same<Class, Class>::value /* always false */,
  92. "pybind11::init(): init function must return a compatible pointer, "
  93. "holder, or value");
  94. }
  95. // Pointer return v1: the factory function returns a class pointer for a registered class.
  96. // If we don't need an alias (because this class doesn't have one, or because the final type is
  97. // inherited on the Python side) we can simply take over ownership. Otherwise we need to try to
  98. // construct an Alias from the returned base instance.
  99. template <typename Class>
  100. void construct(value_and_holder &v_h, Cpp<Class> *ptr, bool need_alias) {
  101. PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias);
  102. no_nullptr(ptr);
  103. if (Class::has_alias && need_alias && !is_alias<Class>(ptr)) {
  104. // We're going to try to construct an alias by moving the cpp type. Whether or not
  105. // that succeeds, we still need to destroy the original cpp pointer (either the
  106. // moved away leftover, if the alias construction works, or the value itself if we
  107. // throw an error), but we can't just call `delete ptr`: it might have a special
  108. // deleter, or might be shared_from_this. So we construct a holder around it as if
  109. // it was a normal instance, then steal the holder away into a local variable; thus
  110. // the holder and destruction happens when we leave the C++ scope, and the holder
  111. // class gets to handle the destruction however it likes.
  112. v_h.value_ptr() = ptr;
  113. v_h.set_instance_registered(true); // Trick to prevent init_instance from registering it
  114. // DANGER ZONE BEGIN: exceptions will leave v_h in an invalid state.
  115. v_h.type->init_instance(v_h.inst, nullptr); // Set up the holder
  116. Holder<Class> temp_holder(std::move(v_h.holder<Holder<Class>>())); // Steal the holder
  117. v_h.type->dealloc(v_h); // Destroys the moved-out holder remains, resets value ptr to null
  118. v_h.set_instance_registered(false);
  119. // DANGER ZONE END.
  120. construct_alias_from_cpp<Class>(is_alias_constructible<Class>{}, v_h, std::move(*ptr));
  121. } else {
  122. // Otherwise the type isn't inherited, so we don't need an Alias
  123. v_h.value_ptr() = ptr;
  124. }
  125. }
  126. // Pointer return v2: a factory that always returns an alias instance ptr. We simply take over
  127. // ownership of the pointer.
  128. template <typename Class, enable_if_t<Class::has_alias, int> = 0>
  129. void construct(value_and_holder &v_h, Alias<Class> *alias_ptr, bool) {
  130. no_nullptr(alias_ptr);
  131. v_h.value_ptr() = static_cast<Cpp<Class> *>(alias_ptr);
  132. }
  133. // Holder return: copy its pointer, and move or copy the returned holder into the new instance's
  134. // holder. This also handles types like std::shared_ptr<T> and std::unique_ptr<T> where T is a
  135. // derived type (through those holder's implicit conversion from derived class holder
  136. // constructors).
  137. template <typename Class, detail::enable_if_t<!is_smart_holder<Holder<Class>>::value, int> = 0>
  138. void construct(value_and_holder &v_h, Holder<Class> holder, bool need_alias) {
  139. PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias);
  140. auto *ptr = holder_helper<Holder<Class>>::get(holder);
  141. no_nullptr(ptr);
  142. // If we need an alias, check that the held pointer is actually an alias instance
  143. if (Class::has_alias && need_alias && !is_alias<Class>(ptr)) {
  144. throw type_error("pybind11::init(): construction failed: returned holder-wrapped instance "
  145. "is not an alias instance");
  146. }
  147. // Cast away constness to store in void* storage.
  148. // The value_and_holder storage is fundamentally untyped (void**), so we lose
  149. // const-correctness here by design. The const qualifier will be restored
  150. // when the pointer is later retrieved and cast back to the original type.
  151. // This explicit const_cast makes the const-removal clearly visible.
  152. v_h.value_ptr() = const_cast<void *>(static_cast<const void *>(ptr));
  153. v_h.type->init_instance(v_h.inst, &holder);
  154. }
  155. // return-by-value version 1: returning a cpp class by value. If the class has an alias and an
  156. // alias is required the alias must have an `Alias(Cpp &&)` constructor so that we can construct
  157. // the alias from the base when needed (i.e. because of Python-side inheritance). When we don't
  158. // need it, we simply move-construct the cpp value into a new instance.
  159. template <typename Class>
  160. void construct(value_and_holder &v_h, Cpp<Class> &&result, bool need_alias) {
  161. PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias);
  162. static_assert(is_move_constructible<Cpp<Class>>::value,
  163. "pybind11::init() return-by-value factory function requires a movable class");
  164. if (Class::has_alias && need_alias) {
  165. construct_alias_from_cpp<Class>(is_alias_constructible<Class>{}, v_h, std::move(result));
  166. } else {
  167. v_h.value_ptr() = new Cpp<Class>(std::move(result));
  168. }
  169. }
  170. // return-by-value version 2: returning a value of the alias type itself. We move-construct an
  171. // Alias instance (even if no the python-side inheritance is involved). The is intended for
  172. // cases where Alias initialization is always desired.
  173. template <typename Class>
  174. void construct(value_and_holder &v_h, Alias<Class> &&result, bool) {
  175. static_assert(
  176. is_move_constructible<Alias<Class>>::value,
  177. "pybind11::init() return-by-alias-value factory function requires a movable alias class");
  178. v_h.value_ptr() = new Alias<Class>(std::move(result));
  179. }
  180. template <typename T, typename D>
  181. smart_holder init_smart_holder_from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr,
  182. bool void_cast_raw_ptr) {
  183. void *void_ptr = void_cast_raw_ptr ? static_cast<void *>(unq_ptr.get()) : nullptr;
  184. return smart_holder::from_unique_ptr(std::move(unq_ptr), void_ptr);
  185. }
  186. template <typename Class,
  187. typename D = std::default_delete<Cpp<Class>>,
  188. detail::enable_if_t<is_smart_holder<Holder<Class>>::value, int> = 0>
  189. void construct(value_and_holder &v_h, std::unique_ptr<Cpp<Class>, D> &&unq_ptr, bool need_alias) {
  190. PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias);
  191. auto *ptr = unq_ptr.get();
  192. no_nullptr(ptr);
  193. if (Class::has_alias && need_alias && !is_alias<Class>(ptr)) {
  194. throw type_error("pybind11::init(): construction failed: returned std::unique_ptr pointee "
  195. "is not an alias instance");
  196. }
  197. // Here and below: if the new object is a trampoline, the shared_from_this mechanism needs
  198. // to be prevented from accessing the smart_holder vptr, because it does not keep the
  199. // trampoline Python object alive. For types that don't inherit from enable_shared_from_this
  200. // it does not matter if void_cast_raw_ptr is true or false, therefore it's not necessary
  201. // to also inspect the type.
  202. auto smhldr = init_smart_holder_from_unique_ptr(
  203. std::move(unq_ptr), /*void_cast_raw_ptr*/ Class::has_alias && is_alias<Class>(ptr));
  204. v_h.value_ptr() = ptr;
  205. v_h.type->init_instance(v_h.inst, &smhldr);
  206. }
  207. template <typename Class,
  208. typename D = std::default_delete<Alias<Class>>,
  209. detail::enable_if_t<is_smart_holder<Holder<Class>>::value, int> = 0>
  210. void construct(value_and_holder &v_h,
  211. std::unique_ptr<Alias<Class>, D> &&unq_ptr,
  212. bool /*need_alias*/) {
  213. auto *ptr = unq_ptr.get();
  214. no_nullptr(ptr);
  215. auto smhldr
  216. = init_smart_holder_from_unique_ptr(std::move(unq_ptr), /*void_cast_raw_ptr*/ true);
  217. v_h.value_ptr() = ptr;
  218. v_h.type->init_instance(v_h.inst, &smhldr);
  219. }
  220. template <typename PtrType, typename Class>
  221. void construct_from_shared_ptr(value_and_holder &v_h,
  222. std::shared_ptr<PtrType> &&shd_ptr,
  223. bool need_alias) {
  224. static_assert(std::is_same<PtrType, Cpp<Class>>::value
  225. || std::is_same<PtrType, const Cpp<Class>>::value,
  226. "Expected (const) Cpp<Class> as shared_ptr pointee");
  227. auto *ptr = shd_ptr.get();
  228. no_nullptr(ptr);
  229. if (Class::has_alias && need_alias && !is_alias<Class>(ptr)) {
  230. throw type_error("pybind11::init(): construction failed: returned std::shared_ptr pointee "
  231. "is not an alias instance");
  232. }
  233. // Cast to non-const if needed, consistent with internal design
  234. auto smhldr
  235. = smart_holder::from_shared_ptr(std::const_pointer_cast<Cpp<Class>>(std::move(shd_ptr)));
  236. v_h.value_ptr() = const_cast<Cpp<Class> *>(ptr);
  237. v_h.type->init_instance(v_h.inst, &smhldr);
  238. }
  239. template <typename Class, detail::enable_if_t<is_smart_holder<Holder<Class>>::value, int> = 0>
  240. void construct(value_and_holder &v_h, std::shared_ptr<Cpp<Class>> &&shd_ptr, bool need_alias) {
  241. construct_from_shared_ptr<Cpp<Class>, Class>(v_h, std::move(shd_ptr), need_alias);
  242. }
  243. template <typename Class, detail::enable_if_t<is_smart_holder<Holder<Class>>::value, int> = 0>
  244. void construct(value_and_holder &v_h,
  245. std::shared_ptr<const Cpp<Class>> &&shd_ptr,
  246. bool need_alias) {
  247. construct_from_shared_ptr<const Cpp<Class>, Class>(v_h, std::move(shd_ptr), need_alias);
  248. }
  249. template <typename Class, detail::enable_if_t<is_smart_holder<Holder<Class>>::value, int> = 0>
  250. void construct(value_and_holder &v_h,
  251. std::shared_ptr<Alias<Class>> &&shd_ptr,
  252. bool /*need_alias*/) {
  253. auto *ptr = shd_ptr.get();
  254. no_nullptr(ptr);
  255. auto smhldr = smart_holder::from_shared_ptr(shd_ptr);
  256. v_h.value_ptr() = ptr;
  257. v_h.type->init_instance(v_h.inst, &smhldr);
  258. }
  259. // Implementing class for py::init<...>()
  260. template <typename... Args>
  261. struct constructor {
  262. template <typename Class, typename... Extra, enable_if_t<!Class::has_alias, int> = 0>
  263. static void execute(Class &cl, const Extra &...extra) {
  264. cl.def(
  265. "__init__",
  266. [](value_and_holder &v_h,
  267. Args... args) { // NOLINT(performance-unnecessary-value-param)
  268. v_h.value_ptr() = construct_or_initialize<Cpp<Class>>(std::forward<Args>(args)...);
  269. },
  270. is_new_style_constructor(),
  271. extra...);
  272. }
  273. template <
  274. typename Class,
  275. typename... Extra,
  276. enable_if_t<Class::has_alias && std::is_constructible<Cpp<Class>, Args...>::value, int>
  277. = 0>
  278. static void execute(Class &cl, const Extra &...extra) {
  279. cl.def(
  280. "__init__",
  281. [](value_and_holder &v_h, Args... args) {
  282. if (Py_TYPE(v_h.inst) == v_h.type->type) {
  283. v_h.value_ptr()
  284. = construct_or_initialize<Cpp<Class>>(std::forward<Args>(args)...);
  285. } else {
  286. v_h.value_ptr()
  287. = construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
  288. }
  289. },
  290. is_new_style_constructor(),
  291. extra...);
  292. }
  293. template <
  294. typename Class,
  295. typename... Extra,
  296. enable_if_t<Class::has_alias && !std::is_constructible<Cpp<Class>, Args...>::value, int>
  297. = 0>
  298. static void execute(Class &cl, const Extra &...extra) {
  299. cl.def(
  300. "__init__",
  301. [](value_and_holder &v_h, Args... args) {
  302. v_h.value_ptr()
  303. = construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
  304. },
  305. is_new_style_constructor(),
  306. extra...);
  307. }
  308. };
  309. // Implementing class for py::init_alias<...>()
  310. template <typename... Args>
  311. struct alias_constructor {
  312. template <
  313. typename Class,
  314. typename... Extra,
  315. enable_if_t<Class::has_alias && std::is_constructible<Alias<Class>, Args...>::value, int>
  316. = 0>
  317. static void execute(Class &cl, const Extra &...extra) {
  318. cl.def(
  319. "__init__",
  320. [](value_and_holder &v_h, Args... args) {
  321. v_h.value_ptr()
  322. = construct_or_initialize<Alias<Class>>(std::forward<Args>(args)...);
  323. },
  324. is_new_style_constructor(),
  325. extra...);
  326. }
  327. };
  328. // Implementation class for py::init(Func) and py::init(Func, AliasFunc)
  329. template <typename CFunc,
  330. typename AFunc = void_type (*)(),
  331. typename = function_signature_t<CFunc>,
  332. typename = function_signature_t<AFunc>>
  333. struct factory;
  334. // Specialization for py::init(Func)
  335. template <typename Func, typename Return, typename... Args>
  336. struct factory<Func, void_type (*)(), Return(Args...)> {
  337. remove_reference_t<Func> class_factory;
  338. // NOLINTNEXTLINE(google-explicit-constructor)
  339. factory(Func &&f) : class_factory(std::forward<Func>(f)) {}
  340. // The given class either has no alias or has no separate alias factory;
  341. // this always constructs the class itself. If the class is registered with an alias
  342. // type and an alias instance is needed (i.e. because the final type is a Python class
  343. // inheriting from the C++ type) the returned value needs to either already be an alias
  344. // instance, or the alias needs to be constructible from a `Class &&` argument.
  345. template <typename Class, typename... Extra>
  346. void execute(Class &cl, const Extra &...extra) && {
  347. #if defined(PYBIND11_CPP14)
  348. cl.def(
  349. "__init__",
  350. [func = std::move(class_factory)]
  351. #else
  352. auto &func = class_factory;
  353. cl.def(
  354. "__init__",
  355. [func]
  356. #endif
  357. (value_and_holder &v_h, Args... args) {
  358. construct<Class>(
  359. v_h, func(std::forward<Args>(args)...), Py_TYPE(v_h.inst) != v_h.type->type);
  360. },
  361. is_new_style_constructor(),
  362. extra...);
  363. }
  364. };
  365. // Specialization for py::init(Func, AliasFunc)
  366. template <typename CFunc,
  367. typename AFunc,
  368. typename CReturn,
  369. typename... CArgs,
  370. typename AReturn,
  371. typename... AArgs>
  372. struct factory<CFunc, AFunc, CReturn(CArgs...), AReturn(AArgs...)> {
  373. static_assert(sizeof...(CArgs) == sizeof...(AArgs),
  374. "pybind11::init(class_factory, alias_factory): class and alias factories "
  375. "must have identical argument signatures");
  376. static_assert(all_of<std::is_same<CArgs, AArgs>...>::value,
  377. "pybind11::init(class_factory, alias_factory): class and alias factories "
  378. "must have identical argument signatures");
  379. remove_reference_t<CFunc> class_factory;
  380. remove_reference_t<AFunc> alias_factory;
  381. factory(CFunc &&c, AFunc &&a)
  382. : class_factory(std::forward<CFunc>(c)), alias_factory(std::forward<AFunc>(a)) {}
  383. // The class factory is called when the `self` type passed to `__init__` is the direct
  384. // class (i.e. not inherited), the alias factory when `self` is a Python-side subtype.
  385. template <typename Class, typename... Extra>
  386. void execute(Class &cl, const Extra &...extra) && {
  387. static_assert(Class::has_alias,
  388. "The two-argument version of `py::init()` can "
  389. "only be used if the class has an alias");
  390. #if defined(PYBIND11_CPP14)
  391. cl.def(
  392. "__init__",
  393. [class_func = std::move(class_factory), alias_func = std::move(alias_factory)]
  394. #else
  395. auto &class_func = class_factory;
  396. auto &alias_func = alias_factory;
  397. cl.def(
  398. "__init__",
  399. [class_func, alias_func]
  400. #endif
  401. (value_and_holder &v_h, CArgs... args) {
  402. if (Py_TYPE(v_h.inst) == v_h.type->type) {
  403. // If the instance type equals the registered type we don't have inheritance,
  404. // so don't need the alias and can construct using the class function:
  405. construct<Class>(v_h, class_func(std::forward<CArgs>(args)...), false);
  406. } else {
  407. construct<Class>(v_h, alias_func(std::forward<CArgs>(args)...), true);
  408. }
  409. },
  410. is_new_style_constructor(),
  411. extra...);
  412. }
  413. };
  414. /// Set just the C++ state. Same as `__init__`.
  415. template <typename Class, typename T>
  416. void setstate(value_and_holder &v_h, T &&result, bool need_alias) {
  417. construct<Class>(v_h, std::forward<T>(result), need_alias);
  418. }
  419. /// Set both the C++ and Python states
  420. template <typename Class,
  421. typename T,
  422. typename O,
  423. enable_if_t<std::is_convertible<O, handle>::value, int> = 0>
  424. void setstate(value_and_holder &v_h, std::pair<T, O> &&result, bool need_alias) {
  425. construct<Class>(v_h, std::move(result.first), need_alias);
  426. auto d = handle(result.second);
  427. if (PyDict_Check(d.ptr()) && PyDict_Size(d.ptr()) == 0) {
  428. // Skipping setattr below, to not force use of py::dynamic_attr() for Class unnecessarily.
  429. // See PR #2972 for details.
  430. return;
  431. }
  432. // Our tests never run into an unset dict, but being careful here for now (see #5658)
  433. auto dict = getattr((PyObject *) v_h.inst, "__dict__", none());
  434. if (dict.is_none()) {
  435. setattr((PyObject *) v_h.inst, "__dict__", d);
  436. } else {
  437. // Keep the original object dict and just update it
  438. if (PyDict_Update(dict.ptr(), d.ptr()) < 0) {
  439. throw error_already_set();
  440. }
  441. }
  442. }
  443. /// Implementation for py::pickle(GetState, SetState)
  444. template <typename Get,
  445. typename Set,
  446. typename = function_signature_t<Get>,
  447. typename = function_signature_t<Set>>
  448. struct pickle_factory;
  449. template <typename Get,
  450. typename Set,
  451. typename RetState,
  452. typename Self,
  453. typename NewInstance,
  454. typename ArgState>
  455. struct pickle_factory<Get, Set, RetState(Self), NewInstance(ArgState)> {
  456. static_assert(std::is_same<intrinsic_t<RetState>, intrinsic_t<ArgState>>::value,
  457. "The type returned by `__getstate__` must be the same "
  458. "as the argument accepted by `__setstate__`");
  459. remove_reference_t<Get> get;
  460. remove_reference_t<Set> set;
  461. pickle_factory(Get get, Set set) : get(std::forward<Get>(get)), set(std::forward<Set>(set)) {}
  462. template <typename Class, typename... Extra>
  463. void execute(Class &cl, const Extra &...extra) && {
  464. cl.def("__getstate__", std::move(get), pos_only());
  465. #if defined(PYBIND11_CPP14)
  466. cl.def(
  467. "__setstate__",
  468. [func = std::move(set)]
  469. #else
  470. auto &func = set;
  471. cl.def(
  472. "__setstate__",
  473. [func]
  474. #endif
  475. (value_and_holder &v_h, ArgState state) {
  476. setstate<Class>(
  477. v_h, func(std::forward<ArgState>(state)), Py_TYPE(v_h.inst) != v_h.type->type);
  478. },
  479. is_new_style_constructor(),
  480. extra...);
  481. }
  482. };
  483. PYBIND11_NAMESPACE_END(initimpl)
  484. PYBIND11_NAMESPACE_END(detail)
  485. PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)