stl.h 14 KB

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