stl.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. /*
  3. pybind11/stl.h: Transparent conversion for STL data types
  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 "pybind11.h"
  10. #include "detail/common.h"
  11. #include "detail/descr.h"
  12. #include "detail/type_caster_base.h"
  13. #include <deque>
  14. #include <initializer_list>
  15. #include <list>
  16. #include <map>
  17. #include <memory>
  18. #include <ostream>
  19. #include <set>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22. #include <valarray>
  23. // See `detail/common.h` for implementation of these guards.
  24. #if defined(PYBIND11_HAS_OPTIONAL)
  25. # include <optional>
  26. #elif defined(PYBIND11_HAS_EXP_OPTIONAL)
  27. # include <experimental/optional>
  28. #endif
  29. #if defined(PYBIND11_HAS_VARIANT)
  30. # include <variant>
  31. #endif
  32. PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  33. PYBIND11_NAMESPACE_BEGIN(detail)
  34. //
  35. // Begin: Equivalent of
  36. // https://github.com/google/clif/blob/ae4eee1de07cdf115c0c9bf9fec9ff28efce6f6c/clif/python/runtime.cc#L388-L438
  37. /*
  38. The three `object_is_convertible_to_*()` functions below are
  39. the result of converging the behaviors of pybind11 and PyCLIF
  40. (http://github.com/google/clif).
  41. Originally PyCLIF was extremely far on the permissive side of the spectrum,
  42. while pybind11 was very far on the strict side. Originally PyCLIF accepted any
  43. Python iterable as input for a C++ `vector`/`set`/`map` argument, as long as
  44. the elements were convertible. The obvious (in hindsight) problem was that
  45. any empty Python iterable could be passed to any of these C++ types, e.g. `{}`
  46. was accepted for C++ `vector`/`set` arguments, or `[]` for C++ `map` arguments.
  47. The functions below strike a practical permissive-vs-strict compromise,
  48. informed by tens of thousands of use cases in the wild. A main objective is
  49. to prevent accidents and improve readability:
  50. - Python literals must match the C++ types.
  51. - For C++ `set`: The potentially reducing conversion from a Python sequence
  52. (e.g. Python `list` or `tuple`) to a C++ `set` must be explicit, by going
  53. through a Python `set`.
  54. - However, a Python `set` can still be passed to a C++ `vector`. The rationale
  55. is that this conversion is not reducing. Implicit conversions of this kind
  56. are also fairly commonly used, therefore enforcing explicit conversions
  57. would have an unfavorable cost : benefit ratio; more sloppily speaking,
  58. such an enforcement would be more annoying than helpful.
  59. Additional checks have been added to allow types derived from `collections.abc.Set` and
  60. `collections.abc.Mapping` (`collections.abc.Sequence` is already allowed by `PySequence_Check`).
  61. */
  62. inline bool object_is_instance_with_one_of_tp_names(PyObject *obj,
  63. std::initializer_list<const char *> tp_names) {
  64. if (PyType_Check(obj)) {
  65. return false;
  66. }
  67. const char *obj_tp_name = Py_TYPE(obj)->tp_name;
  68. for (const auto *tp_name : tp_names) {
  69. if (std::strcmp(obj_tp_name, tp_name) == 0) {
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75. inline bool object_is_convertible_to_std_vector(const handle &src) {
  76. // Allow sequence-like objects, but not (byte-)string-like objects.
  77. if (PySequence_Check(src.ptr()) != 0) {
  78. return !PyUnicode_Check(src.ptr()) && !PyBytes_Check(src.ptr());
  79. }
  80. // Allow generators, set/frozenset and several common iterable types.
  81. return (PyGen_Check(src.ptr()) != 0) || (PyAnySet_Check(src.ptr()) != 0)
  82. || object_is_instance_with_one_of_tp_names(
  83. src.ptr(), {"dict_keys", "dict_values", "dict_items", "map", "zip"});
  84. }
  85. inline bool object_is_convertible_to_std_set(const handle &src, bool convert) {
  86. // Allow set/frozenset and dict keys.
  87. // In convert mode: also allow types derived from collections.abc.Set.
  88. return ((PyAnySet_Check(src.ptr()) != 0)
  89. || object_is_instance_with_one_of_tp_names(src.ptr(), {"dict_keys"}))
  90. || (convert && isinstance(src, module_::import("collections.abc").attr("Set")));
  91. }
  92. inline bool object_is_convertible_to_std_map(const handle &src, bool convert) {
  93. // Allow dict.
  94. if (PyDict_Check(src.ptr())) {
  95. return true;
  96. }
  97. // Allow types conforming to Mapping Protocol.
  98. // According to https://docs.python.org/3/c-api/mapping.html, `PyMappingCheck()` checks for
  99. // `__getitem__()` without checking the type of keys. In order to restrict the allowed types
  100. // closer to actual Mapping-like types, we also check for the `items()` method.
  101. if (PyMapping_Check(src.ptr()) != 0) {
  102. PyObject *items = PyObject_GetAttrString(src.ptr(), "items");
  103. if (items != nullptr) {
  104. bool is_convertible = (PyCallable_Check(items) != 0);
  105. Py_DECREF(items);
  106. if (is_convertible) {
  107. return true;
  108. }
  109. } else {
  110. PyErr_Clear();
  111. }
  112. }
  113. // In convert mode: Allow types derived from collections.abc.Mapping
  114. return convert && isinstance(src, module_::import("collections.abc").attr("Mapping"));
  115. }
  116. //
  117. // End: Equivalent of clif/python/runtime.cc
  118. //
  119. /// Extracts an const lvalue reference or rvalue reference for U based on the type of T (e.g. for
  120. /// forwarding a container element). Typically used indirect via forwarded_type(), below.
  121. template <typename T, typename U>
  122. using forwarded_type = conditional_t<std::is_lvalue_reference<T>::value,
  123. remove_reference_t<U> &,
  124. remove_reference_t<U> &&>;
  125. /// Forwards a value U as rvalue or lvalue according to whether T is rvalue or lvalue; typically
  126. /// used for forwarding a container's elements.
  127. template <typename T, typename U>
  128. constexpr forwarded_type<T, U> forward_like(U &&u) {
  129. return std::forward<detail::forwarded_type<T, U>>(std::forward<U>(u));
  130. }
  131. // Checks if a container has a STL style reserve method.
  132. // This will only return true for a `reserve()` with a `void` return.
  133. template <typename C>
  134. using has_reserve_method = std::is_same<decltype(std::declval<C>().reserve(0)), void>;
  135. template <typename Type, typename Key>
  136. struct set_caster {
  137. using type = Type;
  138. using key_conv = make_caster<Key>;
  139. private:
  140. template <typename T = Type, enable_if_t<has_reserve_method<T>::value, int> = 0>
  141. void reserve_maybe(const anyset &s, Type *) {
  142. value.reserve(s.size());
  143. }
  144. void reserve_maybe(const anyset &, void *) {}
  145. bool convert_iterable(const iterable &itbl, bool convert) {
  146. for (const auto &it : itbl) {
  147. key_conv conv;
  148. if (!conv.load(it, convert)) {
  149. return false;
  150. }
  151. value.insert(cast_op<Key &&>(std::move(conv)));
  152. }
  153. return true;
  154. }
  155. bool convert_anyset(const anyset &s, bool convert) {
  156. value.clear();
  157. reserve_maybe(s, &value);
  158. return convert_iterable(s, convert);
  159. }
  160. public:
  161. bool load(handle src, bool convert) {
  162. if (!object_is_convertible_to_std_set(src, convert)) {
  163. return false;
  164. }
  165. if (isinstance<anyset>(src)) {
  166. value.clear();
  167. return convert_anyset(reinterpret_borrow<anyset>(src), convert);
  168. }
  169. if (!convert) {
  170. return false;
  171. }
  172. assert(isinstance<iterable>(src));
  173. value.clear();
  174. return convert_iterable(reinterpret_borrow<iterable>(src), convert);
  175. }
  176. template <typename T>
  177. static handle cast(T &&src, return_value_policy policy, handle parent) {
  178. if (!std::is_lvalue_reference<T>::value) {
  179. policy = return_value_policy_override<Key>::policy(policy);
  180. }
  181. pybind11::set s;
  182. for (auto &&value : src) {
  183. auto value_ = reinterpret_steal<object>(
  184. key_conv::cast(detail::forward_like<T>(value), policy, parent));
  185. if (!value_ || !s.add(std::move(value_))) {
  186. return handle();
  187. }
  188. }
  189. return s.release();
  190. }
  191. PYBIND11_TYPE_CASTER(type,
  192. io_name("collections.abc.Set", "set") + const_name("[") + key_conv::name
  193. + const_name("]"));
  194. };
  195. template <typename Type, typename Key, typename Value>
  196. struct map_caster {
  197. using key_conv = make_caster<Key>;
  198. using value_conv = make_caster<Value>;
  199. private:
  200. template <typename T = Type, enable_if_t<has_reserve_method<T>::value, int> = 0>
  201. void reserve_maybe(const dict &d, Type *) {
  202. value.reserve(d.size());
  203. }
  204. void reserve_maybe(const dict &, void *) {}
  205. bool convert_elements(const dict &d, bool convert) {
  206. value.clear();
  207. reserve_maybe(d, &value);
  208. for (const auto &it : d) {
  209. key_conv kconv;
  210. value_conv vconv;
  211. if (!kconv.load(it.first.ptr(), convert) || !vconv.load(it.second.ptr(), convert)) {
  212. return false;
  213. }
  214. value.emplace(cast_op<Key &&>(std::move(kconv)), cast_op<Value &&>(std::move(vconv)));
  215. }
  216. return true;
  217. }
  218. public:
  219. bool load(handle src, bool convert) {
  220. if (!object_is_convertible_to_std_map(src, convert)) {
  221. return false;
  222. }
  223. if (isinstance<dict>(src)) {
  224. return convert_elements(reinterpret_borrow<dict>(src), convert);
  225. }
  226. if (!convert) {
  227. return false;
  228. }
  229. auto items = reinterpret_steal<object>(PyMapping_Items(src.ptr()));
  230. if (!items) {
  231. throw error_already_set();
  232. }
  233. assert(isinstance<iterable>(items));
  234. return convert_elements(dict(reinterpret_borrow<iterable>(items)), convert);
  235. }
  236. template <typename T>
  237. static handle cast(T &&src, return_value_policy policy, handle parent) {
  238. dict d;
  239. return_value_policy policy_key = policy;
  240. return_value_policy policy_value = policy;
  241. if (!std::is_lvalue_reference<T>::value) {
  242. policy_key = return_value_policy_override<Key>::policy(policy_key);
  243. policy_value = return_value_policy_override<Value>::policy(policy_value);
  244. }
  245. for (auto &&kv : src) {
  246. auto key = reinterpret_steal<object>(
  247. key_conv::cast(detail::forward_like<T>(kv.first), policy_key, parent));
  248. auto value = reinterpret_steal<object>(
  249. value_conv::cast(detail::forward_like<T>(kv.second), policy_value, parent));
  250. if (!key || !value) {
  251. return handle();
  252. }
  253. d[std::move(key)] = std::move(value);
  254. }
  255. return d.release();
  256. }
  257. PYBIND11_TYPE_CASTER(Type,
  258. io_name("collections.abc.Mapping", "dict") + const_name("[")
  259. + key_conv::name + const_name(", ") + value_conv::name
  260. + const_name("]"));
  261. };
  262. template <typename Type, typename Value>
  263. struct list_caster {
  264. using value_conv = make_caster<Value>;
  265. bool load(handle src, bool convert) {
  266. if (!object_is_convertible_to_std_vector(src)) {
  267. return false;
  268. }
  269. if (isinstance<sequence>(src)) {
  270. return convert_elements(src, convert);
  271. }
  272. if (!convert) {
  273. return false;
  274. }
  275. // Designed to be behavior-equivalent to passing tuple(src) from Python:
  276. // The conversion to a tuple will first exhaust the generator object, to ensure that
  277. // the generator is not left in an unpredictable (to the caller) partially-consumed
  278. // state.
  279. assert(isinstance<iterable>(src));
  280. return convert_elements(tuple(reinterpret_borrow<iterable>(src)), convert);
  281. }
  282. private:
  283. template <typename T = Type, enable_if_t<has_reserve_method<T>::value, int> = 0>
  284. void reserve_maybe(const sequence &s, Type *) {
  285. value.reserve(s.size());
  286. }
  287. void reserve_maybe(const sequence &, void *) {}
  288. bool convert_elements(handle seq, bool convert) {
  289. auto s = reinterpret_borrow<sequence>(seq);
  290. value.clear();
  291. reserve_maybe(s, &value);
  292. for (const auto &it : seq) {
  293. value_conv conv;
  294. if (!conv.load(it, convert)) {
  295. return false;
  296. }
  297. value.push_back(cast_op<Value &&>(std::move(conv)));
  298. }
  299. return true;
  300. }
  301. public:
  302. template <typename T>
  303. static handle cast(T &&src, return_value_policy policy, handle parent) {
  304. if (!std::is_lvalue_reference<T>::value) {
  305. policy = return_value_policy_override<Value>::policy(policy);
  306. }
  307. list l(src.size());
  308. ssize_t index = 0;
  309. for (auto &&value : src) {
  310. auto value_ = reinterpret_steal<object>(
  311. value_conv::cast(detail::forward_like<T>(value), policy, parent));
  312. if (!value_) {
  313. return handle();
  314. }
  315. PyList_SET_ITEM(l.ptr(), index++, value_.release().ptr()); // steals a reference
  316. }
  317. return l.release();
  318. }
  319. PYBIND11_TYPE_CASTER(Type,
  320. io_name("collections.abc.Sequence", "list") + const_name("[")
  321. + value_conv::name + const_name("]"));
  322. };
  323. template <typename Type, typename Alloc>
  324. struct type_caster<std::vector<Type, Alloc>> : list_caster<std::vector<Type, Alloc>, Type> {};
  325. template <typename Type, typename Alloc>
  326. struct type_caster<std::deque<Type, Alloc>> : list_caster<std::deque<Type, Alloc>, Type> {};
  327. template <typename Type, typename Alloc>
  328. struct type_caster<std::list<Type, Alloc>> : list_caster<std::list<Type, Alloc>, Type> {};
  329. template <typename ArrayType, typename V, size_t... I>
  330. ArrayType vector_to_array_impl(V &&v, index_sequence<I...>) {
  331. return {{std::move(v[I])...}};
  332. }
  333. // Based on https://en.cppreference.com/w/cpp/container/array/to_array
  334. template <typename ArrayType, size_t N, typename V>
  335. ArrayType vector_to_array(V &&v) {
  336. return vector_to_array_impl<ArrayType, V>(std::forward<V>(v), make_index_sequence<N>{});
  337. }
  338. template <typename ArrayType, typename Value, bool Resizable, size_t Size = 0>
  339. struct array_caster {
  340. using value_conv = make_caster<Value>;
  341. private:
  342. std::unique_ptr<ArrayType> value;
  343. template <bool R = Resizable, enable_if_t<R, int> = 0>
  344. bool convert_elements(handle seq, bool convert) {
  345. auto l = reinterpret_borrow<sequence>(seq);
  346. value.reset(new ArrayType{});
  347. // Using `resize` to preserve the behavior exactly as it was before PR #5305
  348. // For the `resize` to work, `Value` must be default constructible.
  349. // For `std::valarray`, this is a requirement:
  350. // https://en.cppreference.com/w/cpp/named_req/NumericType
  351. value->resize(l.size());
  352. size_t ctr = 0;
  353. for (const auto &it : l) {
  354. value_conv conv;
  355. if (!conv.load(it, convert)) {
  356. return false;
  357. }
  358. (*value)[ctr++] = cast_op<Value &&>(std::move(conv));
  359. }
  360. return true;
  361. }
  362. template <bool R = Resizable, enable_if_t<!R, int> = 0>
  363. bool convert_elements(handle seq, bool convert) {
  364. auto l = reinterpret_borrow<sequence>(seq);
  365. if (l.size() != Size) {
  366. return false;
  367. }
  368. // The `temp` storage is needed to support `Value` types that are not
  369. // default-constructible.
  370. // Deliberate choice: no template specializations, for simplicity, and
  371. // because the compile time overhead for the specializations is deemed
  372. // more significant than the runtime overhead for the `temp` storage.
  373. std::vector<Value> temp;
  374. temp.reserve(l.size());
  375. for (auto it : l) {
  376. value_conv conv;
  377. if (!conv.load(it, convert)) {
  378. return false;
  379. }
  380. temp.emplace_back(cast_op<Value &&>(std::move(conv)));
  381. }
  382. value.reset(new ArrayType(vector_to_array<ArrayType, Size>(std::move(temp))));
  383. return true;
  384. }
  385. public:
  386. bool load(handle src, bool convert) {
  387. if (!object_is_convertible_to_std_vector(src)) {
  388. return false;
  389. }
  390. if (isinstance<sequence>(src)) {
  391. return convert_elements(src, convert);
  392. }
  393. if (!convert) {
  394. return false;
  395. }
  396. // Designed to be behavior-equivalent to passing tuple(src) from Python:
  397. // The conversion to a tuple will first exhaust the generator object, to ensure that
  398. // the generator is not left in an unpredictable (to the caller) partially-consumed
  399. // state.
  400. assert(isinstance<iterable>(src));
  401. return convert_elements(tuple(reinterpret_borrow<iterable>(src)), convert);
  402. }
  403. template <typename T>
  404. static handle cast(T &&src, return_value_policy policy, handle parent) {
  405. list l(src.size());
  406. ssize_t index = 0;
  407. for (auto &&value : src) {
  408. auto value_ = reinterpret_steal<object>(
  409. value_conv::cast(detail::forward_like<T>(value), policy, parent));
  410. if (!value_) {
  411. return handle();
  412. }
  413. PyList_SET_ITEM(l.ptr(), index++, value_.release().ptr()); // steals a reference
  414. }
  415. return l.release();
  416. }
  417. // Code copied from PYBIND11_TYPE_CASTER macro.
  418. // Intentionally preserving the behavior exactly as it was before PR #5305
  419. template <typename T_, enable_if_t<std::is_same<ArrayType, remove_cv_t<T_>>::value, int> = 0>
  420. static handle cast(T_ *src, return_value_policy policy, handle parent) {
  421. if (!src) {
  422. return none().release();
  423. }
  424. if (policy == return_value_policy::take_ownership) {
  425. auto h = cast(std::move(*src), policy, parent);
  426. delete src; // WARNING: Assumes `src` was allocated with `new`.
  427. return h;
  428. }
  429. return cast(*src, policy, parent);
  430. }
  431. // NOLINTNEXTLINE(google-explicit-constructor)
  432. operator ArrayType *() { return &(*value); }
  433. // NOLINTNEXTLINE(google-explicit-constructor)
  434. operator ArrayType &() { return *value; }
  435. // NOLINTNEXTLINE(google-explicit-constructor)
  436. operator ArrayType &&() && { return std::move(*value); }
  437. template <typename T_>
  438. using cast_op_type = movable_cast_op_type<T_>;
  439. static constexpr auto name
  440. = const_name<Resizable>(const_name(""), const_name("typing.Annotated["))
  441. + io_name("collections.abc.Sequence", "list") + const_name("[") + value_conv::name
  442. + const_name("]")
  443. + const_name<Resizable>(const_name(""),
  444. const_name(", \"FixedSize(") + const_name<Size>()
  445. + const_name(")\"]"));
  446. };
  447. template <typename Type, size_t Size>
  448. struct type_caster<std::array<Type, Size>>
  449. : array_caster<std::array<Type, Size>, Type, false, Size> {};
  450. template <typename Type>
  451. struct type_caster<std::valarray<Type>> : array_caster<std::valarray<Type>, Type, true> {};
  452. template <typename Key, typename Compare, typename Alloc>
  453. struct type_caster<std::set<Key, Compare, Alloc>>
  454. : set_caster<std::set<Key, Compare, Alloc>, Key> {};
  455. template <typename Key, typename Hash, typename Equal, typename Alloc>
  456. struct type_caster<std::unordered_set<Key, Hash, Equal, Alloc>>
  457. : set_caster<std::unordered_set<Key, Hash, Equal, Alloc>, Key> {};
  458. template <typename Key, typename Value, typename Compare, typename Alloc>
  459. struct type_caster<std::map<Key, Value, Compare, Alloc>>
  460. : map_caster<std::map<Key, Value, Compare, Alloc>, Key, Value> {};
  461. template <typename Key, typename Value, typename Hash, typename Equal, typename Alloc>
  462. struct type_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>>
  463. : map_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>, Key, Value> {};
  464. // This type caster is intended to be used for std::optional and std::experimental::optional
  465. template <typename Type, typename Value = typename Type::value_type>
  466. struct optional_caster {
  467. using value_conv = make_caster<Value>;
  468. template <typename T>
  469. static handle cast(T &&src, return_value_policy policy, handle parent) {
  470. if (!src) {
  471. return none().release();
  472. }
  473. if (!std::is_lvalue_reference<T>::value) {
  474. policy = return_value_policy_override<Value>::policy(policy);
  475. }
  476. // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
  477. return value_conv::cast(*std::forward<T>(src), policy, parent);
  478. }
  479. bool load(handle src, bool convert) {
  480. if (!src) {
  481. return false;
  482. }
  483. if (src.is_none()) {
  484. return true; // default-constructed value is already empty
  485. }
  486. value_conv inner_caster;
  487. if (!inner_caster.load(src, convert)) {
  488. return false;
  489. }
  490. value.emplace(cast_op<Value &&>(std::move(inner_caster)));
  491. return true;
  492. }
  493. PYBIND11_TYPE_CASTER(Type, value_conv::name | make_caster<none>::name);
  494. };
  495. #if defined(PYBIND11_HAS_OPTIONAL)
  496. template <typename T>
  497. struct type_caster<std::optional<T>> : public optional_caster<std::optional<T>> {};
  498. template <>
  499. struct type_caster<std::nullopt_t> : public void_caster<std::nullopt_t> {};
  500. #endif
  501. #if defined(PYBIND11_HAS_EXP_OPTIONAL)
  502. template <typename T>
  503. struct type_caster<std::experimental::optional<T>>
  504. : public optional_caster<std::experimental::optional<T>> {};
  505. template <>
  506. struct type_caster<std::experimental::nullopt_t>
  507. : public void_caster<std::experimental::nullopt_t> {};
  508. #endif
  509. /// Visit a variant and cast any found type to Python
  510. struct variant_caster_visitor {
  511. return_value_policy policy;
  512. handle parent;
  513. using result_type = handle; // required by boost::variant in C++11
  514. template <typename T>
  515. result_type operator()(T &&src) const {
  516. return make_caster<T>::cast(std::forward<T>(src), policy, parent);
  517. }
  518. };
  519. /// Helper class which abstracts away variant's `visit` function. `std::variant` and similar
  520. /// `namespace::variant` types which provide a `namespace::visit()` function are handled here
  521. /// automatically using argument-dependent lookup. Users can provide specializations for other
  522. /// variant-like classes, e.g. `boost::variant` and `boost::apply_visitor`.
  523. template <template <typename...> class Variant>
  524. struct visit_helper {
  525. template <typename... Args>
  526. static auto call(Args &&...args) -> decltype(visit(std::forward<Args>(args)...)) {
  527. return visit(std::forward<Args>(args)...);
  528. }
  529. };
  530. /// Generic variant caster
  531. template <typename Variant>
  532. struct variant_caster;
  533. template <template <typename...> class V, typename... Ts>
  534. struct variant_caster<V<Ts...>> {
  535. static_assert(sizeof...(Ts) > 0, "Variant must consist of at least one alternative.");
  536. template <typename U, typename... Us>
  537. bool load_alternative(handle src, bool convert, type_list<U, Us...>) {
  538. auto caster = make_caster<U>();
  539. if (caster.load(src, convert)) {
  540. value = cast_op<U>(std::move(caster));
  541. return true;
  542. }
  543. return load_alternative(src, convert, type_list<Us...>{});
  544. }
  545. bool load_alternative(handle, bool, type_list<>) { return false; }
  546. bool load(handle src, bool convert) {
  547. // Do a first pass without conversions to improve constructor resolution.
  548. // E.g. `py::int_(1).cast<variant<double, int>>()` needs to fill the `int`
  549. // slot of the variant. Without two-pass loading `double` would be filled
  550. // because it appears first and a conversion is possible.
  551. if (convert && load_alternative(src, false, type_list<Ts...>{})) {
  552. return true;
  553. }
  554. return load_alternative(src, convert, type_list<Ts...>{});
  555. }
  556. template <typename Variant>
  557. static handle cast(Variant &&src, return_value_policy policy, handle parent) {
  558. return visit_helper<V>::call(variant_caster_visitor{policy, parent},
  559. std::forward<Variant>(src));
  560. }
  561. using Type = V<Ts...>;
  562. PYBIND11_TYPE_CASTER(Type, ::pybind11::detail::union_concat(make_caster<Ts>::name...));
  563. };
  564. #if defined(PYBIND11_HAS_VARIANT)
  565. template <typename... Ts>
  566. struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> {};
  567. template <>
  568. struct type_caster<std::monostate> : public void_caster<std::monostate> {};
  569. #endif
  570. PYBIND11_NAMESPACE_END(detail)
  571. inline std::ostream &operator<<(std::ostream &os, const handle &obj) {
  572. #ifdef PYBIND11_HAS_STRING_VIEW
  573. os << str(obj).cast<std::string_view>();
  574. #else
  575. os << (std::string) str(obj);
  576. #endif
  577. return os;
  578. }
  579. PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
  580. #else
  581. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  582. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)