You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
9.0 KiB

8 years ago
  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 <valarray>
  16. #if defined(_MSC_VER)
  17. #pragma warning(push)
  18. #pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
  19. #endif
  20. #ifdef __has_include
  21. // std::optional (but including it in c++14 mode isn't allowed)
  22. # if defined(PYBIND11_CPP17) && __has_include(<optional>)
  23. # include <optional>
  24. # define PYBIND11_HAS_OPTIONAL 1
  25. # endif
  26. // std::experimental::optional (but not allowed in c++11 mode)
  27. # if defined(PYBIND11_CPP14) && __has_include(<experimental/optional>)
  28. # include <experimental/optional>
  29. # if __cpp_lib_experimental_optional // just in case
  30. # define PYBIND11_HAS_EXP_OPTIONAL 1
  31. # endif
  32. # endif
  33. #endif
  34. NAMESPACE_BEGIN(pybind11)
  35. NAMESPACE_BEGIN(detail)
  36. template <typename Type, typename Key> struct set_caster {
  37. using type = Type;
  38. using key_conv = make_caster<Key>;
  39. bool load(handle src, bool convert) {
  40. if (!isinstance<pybind11::set>(src))
  41. return false;
  42. auto s = reinterpret_borrow<pybind11::set>(src);
  43. value.clear();
  44. key_conv conv;
  45. for (auto entry : s) {
  46. if (!conv.load(entry, convert))
  47. return false;
  48. value.insert(cast_op<Key>(conv));
  49. }
  50. return true;
  51. }
  52. static handle cast(const type &src, return_value_policy policy, handle parent) {
  53. pybind11::set s;
  54. for (auto const &value: src) {
  55. auto value_ = reinterpret_steal<object>(key_conv::cast(value, policy, parent));
  56. if (!value_ || !s.add(value_))
  57. return handle();
  58. }
  59. return s.release();
  60. }
  61. PYBIND11_TYPE_CASTER(type, _("Set[") + key_conv::name() + _("]"));
  62. };
  63. template <typename Type, typename Key, typename Value> struct map_caster {
  64. using key_conv = make_caster<Key>;
  65. using value_conv = make_caster<Value>;
  66. bool load(handle src, bool convert) {
  67. if (!isinstance<dict>(src))
  68. return false;
  69. auto d = reinterpret_borrow<dict>(src);
  70. key_conv kconv;
  71. value_conv vconv;
  72. value.clear();
  73. for (auto it : d) {
  74. if (!kconv.load(it.first.ptr(), convert) ||
  75. !vconv.load(it.second.ptr(), convert))
  76. return false;
  77. value.emplace(cast_op<Key>(kconv), cast_op<Value>(vconv));
  78. }
  79. return true;
  80. }
  81. static handle cast(const Type &src, return_value_policy policy, handle parent) {
  82. dict d;
  83. for (auto const &kv: src) {
  84. auto key = reinterpret_steal<object>(key_conv::cast(kv.first, policy, parent));
  85. auto value = reinterpret_steal<object>(value_conv::cast(kv.second, policy, parent));
  86. if (!key || !value)
  87. return handle();
  88. d[key] = value;
  89. }
  90. return d.release();
  91. }
  92. PYBIND11_TYPE_CASTER(Type, _("Dict[") + key_conv::name() + _(", ") + value_conv::name() + _("]"));
  93. };
  94. template <typename Type, typename Value> struct list_caster {
  95. using value_conv = make_caster<Value>;
  96. bool load(handle src, bool convert) {
  97. if (!isinstance<sequence>(src))
  98. return false;
  99. auto s = reinterpret_borrow<sequence>(src);
  100. value_conv conv;
  101. value.clear();
  102. reserve_maybe(s, &value);
  103. for (auto it : s) {
  104. if (!conv.load(it, convert))
  105. return false;
  106. value.push_back(cast_op<Value>(conv));
  107. }
  108. return true;
  109. }
  110. private:
  111. template <typename T = Type,
  112. enable_if_t<std::is_same<decltype(std::declval<T>().reserve(0)), void>::value, int> = 0>
  113. void reserve_maybe(sequence s, Type *) { value.reserve(s.size()); }
  114. void reserve_maybe(sequence, void *) { }
  115. public:
  116. static handle cast(const Type &src, return_value_policy policy, handle parent) {
  117. list l(src.size());
  118. size_t index = 0;
  119. for (auto const &value: src) {
  120. auto value_ = reinterpret_steal<object>(value_conv::cast(value, policy, parent));
  121. if (!value_)
  122. return handle();
  123. PyList_SET_ITEM(l.ptr(), (ssize_t) index++, value_.release().ptr()); // steals a reference
  124. }
  125. return l.release();
  126. }
  127. PYBIND11_TYPE_CASTER(Type, _("List[") + value_conv::name() + _("]"));
  128. };
  129. template <typename Type, typename Alloc> struct type_caster<std::vector<Type, Alloc>>
  130. : list_caster<std::vector<Type, Alloc>, Type> { };
  131. template <typename Type, typename Alloc> struct type_caster<std::list<Type, Alloc>>
  132. : list_caster<std::list<Type, Alloc>, Type> { };
  133. template <typename ArrayType, typename Value, bool Resizable, size_t Size = 0> struct array_caster {
  134. using value_conv = make_caster<Value>;
  135. private:
  136. template <bool R = Resizable>
  137. bool require_size(enable_if_t<R, size_t> size) {
  138. if (value.size() != size)
  139. value.resize(size);
  140. return true;
  141. }
  142. template <bool R = Resizable>
  143. bool require_size(enable_if_t<!R, size_t> size) {
  144. return size == Size;
  145. }
  146. public:
  147. bool load(handle src, bool convert) {
  148. if (!isinstance<list>(src))
  149. return false;
  150. auto l = reinterpret_borrow<list>(src);
  151. if (!require_size(l.size()))
  152. return false;
  153. value_conv conv;
  154. size_t ctr = 0;
  155. for (auto it : l) {
  156. if (!conv.load(it, convert))
  157. return false;
  158. value[ctr++] = cast_op<Value>(conv);
  159. }
  160. return true;
  161. }
  162. static handle cast(const ArrayType &src, return_value_policy policy, handle parent) {
  163. list l(src.size());
  164. size_t index = 0;
  165. for (auto const &value: src) {
  166. auto value_ = reinterpret_steal<object>(value_conv::cast(value, policy, parent));
  167. if (!value_)
  168. return handle();
  169. PyList_SET_ITEM(l.ptr(), (ssize_t) index++, value_.release().ptr()); // steals a reference
  170. }
  171. return l.release();
  172. }
  173. PYBIND11_TYPE_CASTER(ArrayType, _("List[") + value_conv::name() + _<Resizable>(_(""), _("[") + _<Size>() + _("]")) + _("]"));
  174. };
  175. template <typename Type, size_t Size> struct type_caster<std::array<Type, Size>>
  176. : array_caster<std::array<Type, Size>, Type, false, Size> { };
  177. template <typename Type> struct type_caster<std::valarray<Type>>
  178. : array_caster<std::valarray<Type>, Type, true> { };
  179. template <typename Key, typename Compare, typename Alloc> struct type_caster<std::set<Key, Compare, Alloc>>
  180. : set_caster<std::set<Key, Compare, Alloc>, Key> { };
  181. template <typename Key, typename Hash, typename Equal, typename Alloc> struct type_caster<std::unordered_set<Key, Hash, Equal, Alloc>>
  182. : set_caster<std::unordered_set<Key, Hash, Equal, Alloc>, Key> { };
  183. template <typename Key, typename Value, typename Compare, typename Alloc> struct type_caster<std::map<Key, Value, Compare, Alloc>>
  184. : map_caster<std::map<Key, Value, Compare, Alloc>, Key, Value> { };
  185. template <typename Key, typename Value, typename Hash, typename Equal, typename Alloc> struct type_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>>
  186. : map_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>, Key, Value> { };
  187. // This type caster is intended to be used for std::optional and std::experimental::optional
  188. template<typename T> struct optional_caster {
  189. using value_conv = make_caster<typename T::value_type>;
  190. static handle cast(const T& src, return_value_policy policy, handle parent) {
  191. if (!src)
  192. return none().inc_ref();
  193. return value_conv::cast(*src, policy, parent);
  194. }
  195. bool load(handle src, bool convert) {
  196. if (!src) {
  197. return false;
  198. } else if (src.is_none()) {
  199. value = {}; // nullopt
  200. return true;
  201. }
  202. value_conv inner_caster;
  203. if (!inner_caster.load(src, convert))
  204. return false;
  205. value.emplace(cast_op<typename T::value_type>(inner_caster));
  206. return true;
  207. }
  208. PYBIND11_TYPE_CASTER(T, _("Optional[") + value_conv::name() + _("]"));
  209. };
  210. #if PYBIND11_HAS_OPTIONAL
  211. template<typename T> struct type_caster<std::optional<T>>
  212. : public optional_caster<std::optional<T>> {};
  213. template<> struct type_caster<std::nullopt_t>
  214. : public void_caster<std::nullopt_t> {};
  215. #endif
  216. #if PYBIND11_HAS_EXP_OPTIONAL
  217. template<typename T> struct type_caster<std::experimental::optional<T>>
  218. : public optional_caster<std::experimental::optional<T>> {};
  219. template<> struct type_caster<std::experimental::nullopt_t>
  220. : public void_caster<std::experimental::nullopt_t> {};
  221. #endif
  222. NAMESPACE_END(detail)
  223. inline std::ostream &operator<<(std::ostream &os, const handle &obj) {
  224. os << (std::string) str(obj);
  225. return os;
  226. }
  227. NAMESPACE_END(pybind11)
  228. #if defined(_MSC_VER)
  229. #pragma warning(pop)
  230. #endif