stl.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. pybind11/stl.h: Transparent conversion for STL data types
  3. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  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 "pybind11.h"
  9. #include "detail/common.h"
  10. #include <deque>
  11. #include <list>
  12. #include <map>
  13. #include <ostream>
  14. #include <set>
  15. #include <unordered_map>
  16. #include <unordered_set>
  17. #include <valarray>
  18. // See `detail/common.h` for implementation of these guards.
  19. #if defined(PYBIND11_HAS_OPTIONAL)
  20. # include <optional>
  21. #elif defined(PYBIND11_HAS_EXP_OPTIONAL)
  22. # include <experimental/optional>
  23. #endif
  24. #if defined(PYBIND11_HAS_VARIANT)
  25. # include <variant>
  26. #endif
  27. PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  28. PYBIND11_NAMESPACE_BEGIN(detail)
  29. /// Extracts an const lvalue reference or rvalue reference for U based on the type of T (e.g. for
  30. /// forwarding a container element). Typically used indirect via forwarded_type(), below.
  31. template <typename T, typename U>
  32. using forwarded_type = conditional_t<std::is_lvalue_reference<T>::value,
  33. remove_reference_t<U> &,
  34. remove_reference_t<U> &&>;
  35. /// Forwards a value U as rvalue or lvalue according to whether T is rvalue or lvalue; typically
  36. /// used for forwarding a container's elements.
  37. template <typename T, typename U>
  38. constexpr forwarded_type<T, U> forward_like(U &&u) {
  39. return std::forward<detail::forwarded_type<T, U>>(std::forward<U>(u));
  40. }
  41. // Checks if a container has a STL style reserve method.
  42. // This will only return true for a `reserve()` with a `void` return.
  43. template <typename C>
  44. using has_reserve_method = std::is_same<decltype(std::declval<C>().reserve(0)), void>;
  45. template <typename Type, typename Key>
  46. struct set_caster {
  47. using type = Type;
  48. using key_conv = make_caster<Key>;
  49. private:
  50. template <typename T = Type, enable_if_t<has_reserve_method<T>::value, int> = 0>
  51. void reserve_maybe(const anyset &s, Type *) {
  52. value.reserve(s.size());
  53. }
  54. void reserve_maybe(const anyset &, void *) {}
  55. public:
  56. bool load(handle src, bool convert) {
  57. if (!isinstance<anyset>(src)) {
  58. return false;
  59. }
  60. auto s = reinterpret_borrow<anyset>(src);
  61. value.clear();
  62. reserve_maybe(s, &value);
  63. for (auto entry : s) {
  64. key_conv conv;
  65. if (!conv.load(entry, convert)) {
  66. return false;
  67. }
  68. value.insert(cast_op<Key &&>(std::move(conv)));
  69. }
  70. return true;
  71. }
  72. template <typename T>
  73. static handle cast(T &&src, return_value_policy policy, handle parent) {
  74. if (!std::is_lvalue_reference<T>::value) {
  75. policy = return_value_policy_override<Key>::policy(policy);
  76. }
  77. pybind11::set s;
  78. for (auto &&value : src) {
  79. auto value_ = reinterpret_steal<object>(
  80. key_conv::cast(detail::forward_like<T>(value), policy, parent));
  81. if (!value_ || !s.add(std::move(value_))) {
  82. return handle();
  83. }
  84. }
  85. return s.release();
  86. }
  87. PYBIND11_TYPE_CASTER(type, const_name("Set[") + key_conv::name + const_name("]"));
  88. };
  89. template <typename Type, typename Key, typename Value>
  90. struct map_caster {
  91. using key_conv = make_caster<Key>;
  92. using value_conv = make_caster<Value>;
  93. private:
  94. template <typename T = Type, enable_if_t<has_reserve_method<T>::value, int> = 0>
  95. void reserve_maybe(const dict &d, Type *) {
  96. value.reserve(d.size());
  97. }
  98. void reserve_maybe(const dict &, void *) {}
  99. public:
  100. bool load(handle src, bool convert) {
  101. if (!isinstance<dict>(src)) {
  102. return false;
  103. }
  104. auto d = reinterpret_borrow<dict>(src);
  105. value.clear();
  106. reserve_maybe(d, &value);
  107. for (auto it : d) {
  108. key_conv kconv;
  109. value_conv vconv;
  110. if (!kconv.load(it.first.ptr(), convert) || !vconv.load(it.second.ptr(), convert)) {
  111. return false;
  112. }
  113. value.emplace(cast_op<Key &&>(std::move(kconv)), cast_op<Value &&>(std::move(vconv)));
  114. }
  115. return true;
  116. }
  117. template <typename T>
  118. static handle cast(T &&src, return_value_policy policy, handle parent) {
  119. dict d;
  120. return_value_policy policy_key = policy;
  121. return_value_policy policy_value = policy;
  122. if (!std::is_lvalue_reference<T>::value) {
  123. policy_key = return_value_policy_override<Key>::policy(policy_key);
  124. policy_value = return_value_policy_override<Value>::policy(policy_value);
  125. }
  126. for (auto &&kv : src) {
  127. auto key = reinterpret_steal<object>(
  128. key_conv::cast(detail::forward_like<T>(kv.first), policy_key, parent));
  129. auto value = reinterpret_steal<object>(
  130. value_conv::cast(detail::forward_like<T>(kv.second), policy_value, parent));
  131. if (!key || !value) {
  132. return handle();
  133. }
  134. d[std::move(key)] = std::move(value);
  135. }
  136. return d.release();
  137. }
  138. PYBIND11_TYPE_CASTER(Type,
  139. const_name("Dict[") + key_conv::name + const_name(", ") + value_conv::name
  140. + const_name("]"));
  141. };
  142. template <typename Type, typename Value>
  143. struct list_caster {
  144. using value_conv = make_caster<Value>;
  145. bool load(handle src, bool convert) {
  146. if (!isinstance<sequence>(src) || isinstance<bytes>(src) || isinstance<str>(src)) {
  147. return false;
  148. }
  149. auto s = reinterpret_borrow<sequence>(src);
  150. value.clear();
  151. reserve_maybe(s, &value);
  152. for (auto it : s) {
  153. value_conv conv;
  154. if (!conv.load(it, convert)) {
  155. return false;
  156. }
  157. value.push_back(cast_op<Value &&>(std::move(conv)));
  158. }
  159. return true;
  160. }
  161. private:
  162. template <typename T = Type, enable_if_t<has_reserve_method<T>::value, int> = 0>
  163. void reserve_maybe(const sequence &s, Type *) {
  164. value.reserve(s.size());
  165. }
  166. void reserve_maybe(const sequence &, void *) {}
  167. public:
  168. template <typename T>
  169. static handle cast(T &&src, return_value_policy policy, handle parent) {
  170. if (!std::is_lvalue_reference<T>::value) {
  171. policy = return_value_policy_override<Value>::policy(policy);
  172. }
  173. list l(src.size());
  174. ssize_t index = 0;
  175. for (auto &&value : src) {
  176. auto value_ = reinterpret_steal<object>(
  177. value_conv::cast(detail::forward_like<T>(value), policy, parent));
  178. if (!value_) {
  179. return handle();
  180. }
  181. PyList_SET_ITEM(l.ptr(), index++, value_.release().ptr()); // steals a reference
  182. }
  183. return l.release();
  184. }
  185. PYBIND11_TYPE_CASTER(Type, const_name("List[") + value_conv::name + const_name("]"));
  186. };
  187. template <typename Type, typename Alloc>
  188. struct type_caster<std::vector<Type, Alloc>> : list_caster<std::vector<Type, Alloc>, Type> {};
  189. template <typename Type, typename Alloc>
  190. struct type_caster<std::deque<Type, Alloc>> : list_caster<std::deque<Type, Alloc>, Type> {};
  191. template <typename Type, typename Alloc>
  192. struct type_caster<std::list<Type, Alloc>> : list_caster<std::list<Type, Alloc>, Type> {};
  193. template <typename ArrayType, typename Value, bool Resizable, size_t Size = 0>
  194. struct array_caster {
  195. using value_conv = make_caster<Value>;
  196. private:
  197. template <bool R = Resizable>
  198. bool require_size(enable_if_t<R, size_t> size) {
  199. if (value.size() != size) {
  200. value.resize(size);
  201. }
  202. return true;
  203. }
  204. template <bool R = Resizable>
  205. bool require_size(enable_if_t<!R, size_t> size) {
  206. return size == Size;
  207. }
  208. public:
  209. bool load(handle src, bool convert) {
  210. if (!isinstance<sequence>(src)) {
  211. return false;
  212. }
  213. auto l = reinterpret_borrow<sequence>(src);
  214. if (!require_size(l.size())) {
  215. return false;
  216. }
  217. size_t ctr = 0;
  218. for (auto it : l) {
  219. value_conv conv;
  220. if (!conv.load(it, convert)) {
  221. return false;
  222. }
  223. value[ctr++] = cast_op<Value &&>(std::move(conv));
  224. }
  225. return true;
  226. }
  227. template <typename T>
  228. static handle cast(T &&src, return_value_policy policy, handle parent) {
  229. list l(src.size());
  230. ssize_t index = 0;
  231. for (auto &&value : src) {
  232. auto value_ = reinterpret_steal<object>(
  233. value_conv::cast(detail::forward_like<T>(value), policy, parent));
  234. if (!value_) {
  235. return handle();
  236. }
  237. PyList_SET_ITEM(l.ptr(), index++, value_.release().ptr()); // steals a reference
  238. }
  239. return l.release();
  240. }
  241. PYBIND11_TYPE_CASTER(ArrayType,
  242. const_name("List[") + value_conv::name
  243. + const_name<Resizable>(const_name(""),
  244. const_name("[") + const_name<Size>()
  245. + const_name("]"))
  246. + const_name("]"));
  247. };
  248. template <typename Type, size_t Size>
  249. struct type_caster<std::array<Type, Size>>
  250. : array_caster<std::array<Type, Size>, Type, false, Size> {};
  251. template <typename Type>
  252. struct type_caster<std::valarray<Type>> : array_caster<std::valarray<Type>, Type, true> {};
  253. template <typename Key, typename Compare, typename Alloc>
  254. struct type_caster<std::set<Key, Compare, Alloc>>
  255. : set_caster<std::set<Key, Compare, Alloc>, Key> {};
  256. template <typename Key, typename Hash, typename Equal, typename Alloc>
  257. struct type_caster<std::unordered_set<Key, Hash, Equal, Alloc>>
  258. : set_caster<std::unordered_set<Key, Hash, Equal, Alloc>, Key> {};
  259. template <typename Key, typename Value, typename Compare, typename Alloc>
  260. struct type_caster<std::map<Key, Value, Compare, Alloc>>
  261. : map_caster<std::map<Key, Value, Compare, Alloc>, Key, Value> {};
  262. template <typename Key, typename Value, typename Hash, typename Equal, typename Alloc>
  263. struct type_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>>
  264. : map_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>, Key, Value> {};
  265. // This type caster is intended to be used for std::optional and std::experimental::optional
  266. template <typename Type, typename Value = typename Type::value_type>
  267. struct optional_caster {
  268. using value_conv = make_caster<Value>;
  269. template <typename T>
  270. static handle cast(T &&src, return_value_policy policy, handle parent) {
  271. if (!src) {
  272. return none().release();
  273. }
  274. if (!std::is_lvalue_reference<T>::value) {
  275. policy = return_value_policy_override<Value>::policy(policy);
  276. }
  277. return value_conv::cast(*std::forward<T>(src), policy, parent);
  278. }
  279. bool load(handle src, bool convert) {
  280. if (!src) {
  281. return false;
  282. }
  283. if (src.is_none()) {
  284. return true; // default-constructed value is already empty
  285. }
  286. value_conv inner_caster;
  287. if (!inner_caster.load(src, convert)) {
  288. return false;
  289. }
  290. value.emplace(cast_op<Value &&>(std::move(inner_caster)));
  291. return true;
  292. }
  293. PYBIND11_TYPE_CASTER(Type, const_name("Optional[") + value_conv::name + const_name("]"));
  294. };
  295. #if defined(PYBIND11_HAS_OPTIONAL)
  296. template <typename T>
  297. struct type_caster<std::optional<T>> : public optional_caster<std::optional<T>> {};
  298. template <>
  299. struct type_caster<std::nullopt_t> : public void_caster<std::nullopt_t> {};
  300. #endif
  301. #if defined(PYBIND11_HAS_EXP_OPTIONAL)
  302. template <typename T>
  303. struct type_caster<std::experimental::optional<T>>
  304. : public optional_caster<std::experimental::optional<T>> {};
  305. template <>
  306. struct type_caster<std::experimental::nullopt_t>
  307. : public void_caster<std::experimental::nullopt_t> {};
  308. #endif
  309. /// Visit a variant and cast any found type to Python
  310. struct variant_caster_visitor {
  311. return_value_policy policy;
  312. handle parent;
  313. using result_type = handle; // required by boost::variant in C++11
  314. template <typename T>
  315. result_type operator()(T &&src) const {
  316. return make_caster<T>::cast(std::forward<T>(src), policy, parent);
  317. }
  318. };
  319. /// Helper class which abstracts away variant's `visit` function. `std::variant` and similar
  320. /// `namespace::variant` types which provide a `namespace::visit()` function are handled here
  321. /// automatically using argument-dependent lookup. Users can provide specializations for other
  322. /// variant-like classes, e.g. `boost::variant` and `boost::apply_visitor`.
  323. template <template <typename...> class Variant>
  324. struct visit_helper {
  325. template <typename... Args>
  326. static auto call(Args &&...args) -> decltype(visit(std::forward<Args>(args)...)) {
  327. return visit(std::forward<Args>(args)...);
  328. }
  329. };
  330. /// Generic variant caster
  331. template <typename Variant>
  332. struct variant_caster;
  333. template <template <typename...> class V, typename... Ts>
  334. struct variant_caster<V<Ts...>> {
  335. static_assert(sizeof...(Ts) > 0, "Variant must consist of at least one alternative.");
  336. template <typename U, typename... Us>
  337. bool load_alternative(handle src, bool convert, type_list<U, Us...>) {
  338. auto caster = make_caster<U>();
  339. if (caster.load(src, convert)) {
  340. value = cast_op<U>(std::move(caster));
  341. return true;
  342. }
  343. return load_alternative(src, convert, type_list<Us...>{});
  344. }
  345. bool load_alternative(handle, bool, type_list<>) { return false; }
  346. bool load(handle src, bool convert) {
  347. // Do a first pass without conversions to improve constructor resolution.
  348. // E.g. `py::int_(1).cast<variant<double, int>>()` needs to fill the `int`
  349. // slot of the variant. Without two-pass loading `double` would be filled
  350. // because it appears first and a conversion is possible.
  351. if (convert && load_alternative(src, false, type_list<Ts...>{})) {
  352. return true;
  353. }
  354. return load_alternative(src, convert, type_list<Ts...>{});
  355. }
  356. template <typename Variant>
  357. static handle cast(Variant &&src, return_value_policy policy, handle parent) {
  358. return visit_helper<V>::call(variant_caster_visitor{policy, parent},
  359. std::forward<Variant>(src));
  360. }
  361. using Type = V<Ts...>;
  362. PYBIND11_TYPE_CASTER(Type,
  363. const_name("Union[") + detail::concat(make_caster<Ts>::name...)
  364. + const_name("]"));
  365. };
  366. #if defined(PYBIND11_HAS_VARIANT)
  367. template <typename... Ts>
  368. struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> {};
  369. template <>
  370. struct type_caster<std::monostate> : public void_caster<std::monostate> {};
  371. #endif
  372. PYBIND11_NAMESPACE_END(detail)
  373. inline std::ostream &operator<<(std::ostream &os, const handle &obj) {
  374. #ifdef PYBIND11_HAS_STRING_VIEW
  375. os << str(obj).cast<std::string_view>();
  376. #else
  377. os << (std::string) str(obj);
  378. #endif
  379. return os;
  380. }
  381. PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)