2015-07-30 13:29:00 +00:00
|
|
|
/*
|
2016-05-05 18:33:54 +00:00
|
|
|
pybind11/stl.h: Transparent conversion for STL data types
|
2015-07-30 13:29:00 +00:00
|
|
|
|
2016-04-17 18:21:41 +00:00
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
2015-07-30 13:29:00 +00:00
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
#include "pybind11.h"
|
2015-11-14 18:04:49 +00:00
|
|
|
#include <set>
|
2016-02-12 23:20:21 +00:00
|
|
|
#include <unordered_set>
|
|
|
|
#include <map>
|
|
|
|
#include <unordered_map>
|
2015-07-30 13:29:00 +00:00
|
|
|
#include <iostream>
|
2016-02-23 16:28:45 +00:00
|
|
|
#include <list>
|
2016-12-07 23:43:29 +00:00
|
|
|
#include <valarray>
|
2015-07-30 13:29:00 +00:00
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
|
|
|
|
#endif
|
|
|
|
|
2016-11-04 13:49:37 +00:00
|
|
|
#ifdef __has_include
|
|
|
|
// std::optional (but including it in c++14 mode isn't allowed)
|
|
|
|
# if defined(PYBIND11_CPP17) && __has_include(<optional>)
|
2016-11-03 12:42:46 +00:00
|
|
|
# include <optional>
|
|
|
|
# define PYBIND11_HAS_OPTIONAL 1
|
|
|
|
# endif
|
2016-11-04 13:49:37 +00:00
|
|
|
// std::experimental::optional (but not allowed in c++11 mode)
|
|
|
|
# if defined(PYBIND11_CPP14) && __has_include(<experimental/optional>)
|
2016-11-03 12:42:46 +00:00
|
|
|
# include <experimental/optional>
|
2017-06-08 19:57:49 +00:00
|
|
|
# define PYBIND11_HAS_EXP_OPTIONAL 1
|
2016-11-03 12:42:46 +00:00
|
|
|
# endif
|
2017-04-21 21:54:41 +00:00
|
|
|
// std::variant
|
|
|
|
# if defined(PYBIND11_CPP17) && __has_include(<variant>)
|
|
|
|
# include <variant>
|
|
|
|
# define PYBIND11_HAS_VARIANT 1
|
|
|
|
# endif
|
2017-05-09 18:34:45 +00:00
|
|
|
#elif defined(_MSC_VER) && defined(PYBIND11_CPP17)
|
|
|
|
# include <optional>
|
|
|
|
# include <variant>
|
|
|
|
# define PYBIND11_HAS_OPTIONAL 1
|
|
|
|
# define PYBIND11_HAS_VARIANT 1
|
2016-11-03 12:42:46 +00:00
|
|
|
#endif
|
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
NAMESPACE_BEGIN(pybind11)
|
2015-07-30 13:29:00 +00:00
|
|
|
NAMESPACE_BEGIN(detail)
|
|
|
|
|
2016-02-12 23:20:21 +00:00
|
|
|
template <typename Type, typename Key> struct set_caster {
|
2016-09-05 12:30:56 +00:00
|
|
|
using type = Type;
|
|
|
|
using key_conv = make_caster<Key>;
|
2016-02-12 23:20:21 +00:00
|
|
|
|
|
|
|
bool load(handle src, bool convert) {
|
2016-10-23 12:50:08 +00:00
|
|
|
if (!isinstance<pybind11::set>(src))
|
2016-02-12 23:20:21 +00:00
|
|
|
return false;
|
2016-10-28 01:08:15 +00:00
|
|
|
auto s = reinterpret_borrow<pybind11::set>(src);
|
2016-02-12 23:20:21 +00:00
|
|
|
value.clear();
|
|
|
|
for (auto entry : s) {
|
2017-05-14 19:57:26 +00:00
|
|
|
key_conv conv;
|
2016-02-12 23:20:21 +00:00
|
|
|
if (!conv.load(entry, convert))
|
|
|
|
return false;
|
2017-05-14 19:57:26 +00:00
|
|
|
value.insert(cast_op<Key &&>(std::move(conv)));
|
2016-02-12 23:20:21 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static handle cast(const type &src, return_value_policy policy, handle parent) {
|
|
|
|
pybind11::set s;
|
|
|
|
for (auto const &value: src) {
|
2016-10-28 01:08:15 +00:00
|
|
|
auto value_ = reinterpret_steal<object>(key_conv::cast(value, policy, parent));
|
2016-02-12 23:20:21 +00:00
|
|
|
if (!value_ || !s.add(value_))
|
|
|
|
return handle();
|
|
|
|
}
|
|
|
|
return s.release();
|
|
|
|
}
|
|
|
|
|
2016-08-03 23:40:40 +00:00
|
|
|
PYBIND11_TYPE_CASTER(type, _("Set[") + key_conv::name() + _("]"));
|
2016-02-12 23:20:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Type, typename Key, typename Value> struct map_caster {
|
2016-09-05 12:30:56 +00:00
|
|
|
using key_conv = make_caster<Key>;
|
|
|
|
using value_conv = make_caster<Value>;
|
2016-02-12 23:20:21 +00:00
|
|
|
|
|
|
|
bool load(handle src, bool convert) {
|
2016-10-23 12:50:08 +00:00
|
|
|
if (!isinstance<dict>(src))
|
2016-02-12 23:20:21 +00:00
|
|
|
return false;
|
2016-10-28 01:08:15 +00:00
|
|
|
auto d = reinterpret_borrow<dict>(src);
|
2016-02-12 23:20:21 +00:00
|
|
|
value.clear();
|
|
|
|
for (auto it : d) {
|
2017-05-14 19:57:26 +00:00
|
|
|
key_conv kconv;
|
|
|
|
value_conv vconv;
|
2016-02-12 23:20:21 +00:00
|
|
|
if (!kconv.load(it.first.ptr(), convert) ||
|
|
|
|
!vconv.load(it.second.ptr(), convert))
|
|
|
|
return false;
|
2017-05-14 19:57:26 +00:00
|
|
|
value.emplace(cast_op<Key &&>(std::move(kconv)), cast_op<Value &&>(std::move(vconv)));
|
2016-02-12 23:20:21 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-07 23:43:29 +00:00
|
|
|
static handle cast(const Type &src, return_value_policy policy, handle parent) {
|
2016-02-12 23:20:21 +00:00
|
|
|
dict d;
|
|
|
|
for (auto const &kv: src) {
|
2016-10-28 01:08:15 +00:00
|
|
|
auto key = reinterpret_steal<object>(key_conv::cast(kv.first, policy, parent));
|
|
|
|
auto value = reinterpret_steal<object>(value_conv::cast(kv.second, policy, parent));
|
2016-02-12 23:20:21 +00:00
|
|
|
if (!key || !value)
|
|
|
|
return handle();
|
|
|
|
d[key] = value;
|
|
|
|
}
|
|
|
|
return d.release();
|
|
|
|
}
|
|
|
|
|
2016-12-07 23:43:29 +00:00
|
|
|
PYBIND11_TYPE_CASTER(Type, _("Dict[") + key_conv::name() + _(", ") + value_conv::name() + _("]"));
|
2016-02-12 23:20:21 +00:00
|
|
|
};
|
|
|
|
|
2016-02-23 16:28:45 +00:00
|
|
|
template <typename Type, typename Value> struct list_caster {
|
2016-09-05 12:30:56 +00:00
|
|
|
using value_conv = make_caster<Value>;
|
2016-02-12 23:20:21 +00:00
|
|
|
|
2016-01-17 21:36:44 +00:00
|
|
|
bool load(handle src, bool convert) {
|
2016-10-23 12:50:08 +00:00
|
|
|
if (!isinstance<sequence>(src))
|
2015-07-30 13:29:00 +00:00
|
|
|
return false;
|
2016-10-28 01:08:15 +00:00
|
|
|
auto s = reinterpret_borrow<sequence>(src);
|
2016-02-23 16:28:45 +00:00
|
|
|
value.clear();
|
2016-10-12 16:00:53 +00:00
|
|
|
reserve_maybe(s, &value);
|
|
|
|
for (auto it : s) {
|
2017-05-14 19:57:26 +00:00
|
|
|
value_conv conv;
|
2016-01-17 21:36:44 +00:00
|
|
|
if (!conv.load(it, convert))
|
2015-07-30 13:29:00 +00:00
|
|
|
return false;
|
2017-05-14 19:57:26 +00:00
|
|
|
value.push_back(cast_op<Value &&>(std::move(conv)));
|
2015-07-30 13:29:00 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-07 23:43:29 +00:00
|
|
|
private:
|
2016-02-23 16:28:45 +00:00
|
|
|
template <typename T = Type,
|
2016-09-12 15:36:43 +00:00
|
|
|
enable_if_t<std::is_same<decltype(std::declval<T>().reserve(0)), void>::value, int> = 0>
|
2016-10-12 16:00:53 +00:00
|
|
|
void reserve_maybe(sequence s, Type *) { value.reserve(s.size()); }
|
|
|
|
void reserve_maybe(sequence, void *) { }
|
2016-02-23 16:28:45 +00:00
|
|
|
|
2016-12-07 23:43:29 +00:00
|
|
|
public:
|
2016-02-23 16:28:45 +00:00
|
|
|
static handle cast(const Type &src, return_value_policy policy, handle parent) {
|
2016-01-17 21:36:41 +00:00
|
|
|
list l(src.size());
|
2015-07-30 13:29:00 +00:00
|
|
|
size_t index = 0;
|
|
|
|
for (auto const &value: src) {
|
2016-10-28 01:08:15 +00:00
|
|
|
auto value_ = reinterpret_steal<object>(value_conv::cast(value, policy, parent));
|
2015-12-30 20:03:57 +00:00
|
|
|
if (!value_)
|
2016-01-17 21:36:44 +00:00
|
|
|
return handle();
|
2016-12-16 14:00:46 +00:00
|
|
|
PyList_SET_ITEM(l.ptr(), (ssize_t) index++, value_.release().ptr()); // steals a reference
|
2015-07-30 13:29:00 +00:00
|
|
|
}
|
2016-01-17 21:36:41 +00:00
|
|
|
return l.release();
|
2015-07-30 13:29:00 +00:00
|
|
|
}
|
2016-02-23 16:28:45 +00:00
|
|
|
|
2016-08-03 23:40:40 +00:00
|
|
|
PYBIND11_TYPE_CASTER(Type, _("List[") + value_conv::name() + _("]"));
|
2016-02-07 15:36:26 +00:00
|
|
|
};
|
|
|
|
|
2016-02-23 16:28:45 +00:00
|
|
|
template <typename Type, typename Alloc> struct type_caster<std::vector<Type, Alloc>>
|
|
|
|
: list_caster<std::vector<Type, Alloc>, Type> { };
|
|
|
|
|
|
|
|
template <typename Type, typename Alloc> struct type_caster<std::list<Type, Alloc>>
|
|
|
|
: list_caster<std::list<Type, Alloc>, Type> { };
|
|
|
|
|
2016-12-07 23:43:29 +00:00
|
|
|
template <typename ArrayType, typename Value, bool Resizable, size_t Size = 0> struct array_caster {
|
|
|
|
using value_conv = make_caster<Value>;
|
2016-02-12 23:20:21 +00:00
|
|
|
|
2016-12-07 23:43:29 +00:00
|
|
|
private:
|
|
|
|
template <bool R = Resizable>
|
|
|
|
bool require_size(enable_if_t<R, size_t> size) {
|
|
|
|
if (value.size() != size)
|
|
|
|
value.resize(size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
template <bool R = Resizable>
|
|
|
|
bool require_size(enable_if_t<!R, size_t> size) {
|
|
|
|
return size == Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2016-02-07 15:36:26 +00:00
|
|
|
bool load(handle src, bool convert) {
|
2016-10-23 12:50:08 +00:00
|
|
|
if (!isinstance<list>(src))
|
2016-02-07 15:36:26 +00:00
|
|
|
return false;
|
2016-10-28 01:08:15 +00:00
|
|
|
auto l = reinterpret_borrow<list>(src);
|
2016-12-07 23:43:29 +00:00
|
|
|
if (!require_size(l.size()))
|
2016-02-07 15:36:26 +00:00
|
|
|
return false;
|
|
|
|
size_t ctr = 0;
|
|
|
|
for (auto it : l) {
|
2017-05-14 19:57:26 +00:00
|
|
|
value_conv conv;
|
2016-02-07 15:36:26 +00:00
|
|
|
if (!conv.load(it, convert))
|
|
|
|
return false;
|
2017-05-14 19:57:26 +00:00
|
|
|
value[ctr++] = cast_op<Value &&>(std::move(conv));
|
2016-02-07 15:36:26 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-07 23:43:29 +00:00
|
|
|
static handle cast(const ArrayType &src, return_value_policy policy, handle parent) {
|
|
|
|
list l(src.size());
|
2016-02-07 15:36:26 +00:00
|
|
|
size_t index = 0;
|
|
|
|
for (auto const &value: src) {
|
2016-10-28 01:08:15 +00:00
|
|
|
auto value_ = reinterpret_steal<object>(value_conv::cast(value, policy, parent));
|
2016-02-07 15:36:26 +00:00
|
|
|
if (!value_)
|
|
|
|
return handle();
|
2016-12-16 14:00:46 +00:00
|
|
|
PyList_SET_ITEM(l.ptr(), (ssize_t) index++, value_.release().ptr()); // steals a reference
|
2016-02-07 15:36:26 +00:00
|
|
|
}
|
|
|
|
return l.release();
|
|
|
|
}
|
2016-12-07 23:43:29 +00:00
|
|
|
|
|
|
|
PYBIND11_TYPE_CASTER(ArrayType, _("List[") + value_conv::name() + _<Resizable>(_(""), _("[") + _<Size>() + _("]")) + _("]"));
|
2015-07-30 13:29:00 +00:00
|
|
|
};
|
|
|
|
|
2016-12-07 23:43:29 +00:00
|
|
|
template <typename Type, size_t Size> struct type_caster<std::array<Type, Size>>
|
|
|
|
: array_caster<std::array<Type, Size>, Type, false, Size> { };
|
|
|
|
|
|
|
|
template <typename Type> struct type_caster<std::valarray<Type>>
|
|
|
|
: array_caster<std::valarray<Type>, Type, true> { };
|
|
|
|
|
2016-02-12 23:20:21 +00:00
|
|
|
template <typename Key, typename Compare, typename Alloc> struct type_caster<std::set<Key, Compare, Alloc>>
|
|
|
|
: set_caster<std::set<Key, Compare, Alloc>, Key> { };
|
2015-07-30 13:29:00 +00:00
|
|
|
|
2016-02-12 23:20:21 +00:00
|
|
|
template <typename Key, typename Hash, typename Equal, typename Alloc> struct type_caster<std::unordered_set<Key, Hash, Equal, Alloc>>
|
|
|
|
: set_caster<std::unordered_set<Key, Hash, Equal, Alloc>, Key> { };
|
2015-07-30 13:29:00 +00:00
|
|
|
|
2016-02-12 23:20:21 +00:00
|
|
|
template <typename Key, typename Value, typename Compare, typename Alloc> struct type_caster<std::map<Key, Value, Compare, Alloc>>
|
|
|
|
: map_caster<std::map<Key, Value, Compare, Alloc>, Key, Value> { };
|
2015-08-24 13:31:24 +00:00
|
|
|
|
2016-02-12 23:20:21 +00:00
|
|
|
template <typename Key, typename Value, typename Hash, typename Equal, typename Alloc> struct type_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>>
|
|
|
|
: map_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>, Key, Value> { };
|
2015-07-30 13:29:00 +00:00
|
|
|
|
2016-11-03 12:42:46 +00:00
|
|
|
// This type caster is intended to be used for std::optional and std::experimental::optional
|
|
|
|
template<typename T> struct optional_caster {
|
2016-11-25 12:06:18 +00:00
|
|
|
using value_conv = make_caster<typename T::value_type>;
|
2016-11-03 12:42:46 +00:00
|
|
|
|
|
|
|
static handle cast(const T& src, return_value_policy policy, handle parent) {
|
|
|
|
if (!src)
|
2016-11-15 12:00:38 +00:00
|
|
|
return none().inc_ref();
|
2016-11-25 12:06:18 +00:00
|
|
|
return value_conv::cast(*src, policy, parent);
|
2016-11-03 12:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool load(handle src, bool convert) {
|
|
|
|
if (!src) {
|
|
|
|
return false;
|
|
|
|
} else if (src.is_none()) {
|
2017-05-26 06:52:54 +00:00
|
|
|
return true; // default-constructed value is already empty
|
2016-11-03 12:42:46 +00:00
|
|
|
}
|
2016-11-25 12:06:18 +00:00
|
|
|
value_conv inner_caster;
|
|
|
|
if (!inner_caster.load(src, convert))
|
|
|
|
return false;
|
2016-11-03 12:42:46 +00:00
|
|
|
|
2017-05-14 19:57:26 +00:00
|
|
|
value.emplace(cast_op<typename T::value_type &&>(std::move(inner_caster)));
|
2016-11-25 12:06:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-11-03 12:42:46 +00:00
|
|
|
|
2016-11-25 12:06:18 +00:00
|
|
|
PYBIND11_TYPE_CASTER(T, _("Optional[") + value_conv::name() + _("]"));
|
2016-11-03 12:42:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#if PYBIND11_HAS_OPTIONAL
|
|
|
|
template<typename T> struct type_caster<std::optional<T>>
|
|
|
|
: public optional_caster<std::optional<T>> {};
|
2016-11-15 12:00:38 +00:00
|
|
|
|
|
|
|
template<> struct type_caster<std::nullopt_t>
|
|
|
|
: public void_caster<std::nullopt_t> {};
|
2016-11-03 12:42:46 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if PYBIND11_HAS_EXP_OPTIONAL
|
|
|
|
template<typename T> struct type_caster<std::experimental::optional<T>>
|
|
|
|
: public optional_caster<std::experimental::optional<T>> {};
|
2016-11-15 12:00:38 +00:00
|
|
|
|
|
|
|
template<> struct type_caster<std::experimental::nullopt_t>
|
|
|
|
: public void_caster<std::experimental::nullopt_t> {};
|
2016-11-03 12:42:46 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-21 21:54:41 +00:00
|
|
|
/// Visit a variant and cast any found type to Python
|
|
|
|
struct variant_caster_visitor {
|
|
|
|
return_value_policy policy;
|
|
|
|
handle parent;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
handle operator()(T &&src) const {
|
|
|
|
return make_caster<T>::cast(std::forward<T>(src), policy, parent);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Helper class which abstracts away variant's `visit` function. `std::variant` and similar
|
|
|
|
/// `namespace::variant` types which provide a `namespace::visit()` function are handled here
|
|
|
|
/// automatically using argument-dependent lookup. Users can provide specializations for other
|
|
|
|
/// variant-like classes, e.g. `boost::variant` and `boost::apply_visitor`.
|
|
|
|
template <template<typename...> class Variant>
|
|
|
|
struct visit_helper {
|
|
|
|
template <typename... Args>
|
|
|
|
static auto call(Args &&...args) -> decltype(visit(std::forward<Args>(args)...)) {
|
|
|
|
return visit(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Generic variant caster
|
|
|
|
template <typename Variant> struct variant_caster;
|
|
|
|
|
|
|
|
template <template<typename...> class V, typename... Ts>
|
|
|
|
struct variant_caster<V<Ts...>> {
|
|
|
|
static_assert(sizeof...(Ts) > 0, "Variant must consist of at least one alternative.");
|
|
|
|
|
|
|
|
template <typename U, typename... Us>
|
|
|
|
bool load_alternative(handle src, bool convert, type_list<U, Us...>) {
|
|
|
|
auto caster = make_caster<U>();
|
|
|
|
if (caster.load(src, convert)) {
|
|
|
|
value = cast_op<U>(caster);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return load_alternative(src, convert, type_list<Us...>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool load_alternative(handle, bool, type_list<>) { return false; }
|
|
|
|
|
|
|
|
bool load(handle src, bool convert) {
|
2017-05-10 09:15:17 +00:00
|
|
|
// Do a first pass without conversions to improve constructor resolution.
|
|
|
|
// E.g. `py::int_(1).cast<variant<double, int>>()` needs to fill the `int`
|
|
|
|
// slot of the variant. Without two-pass loading `double` would be filled
|
|
|
|
// because it appears first and a conversion is possible.
|
|
|
|
if (convert && load_alternative(src, false, type_list<Ts...>{}))
|
|
|
|
return true;
|
2017-04-21 21:54:41 +00:00
|
|
|
return load_alternative(src, convert, type_list<Ts...>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Variant>
|
|
|
|
static handle cast(Variant &&src, return_value_policy policy, handle parent) {
|
|
|
|
return visit_helper<V>::call(variant_caster_visitor{policy, parent},
|
|
|
|
std::forward<Variant>(src));
|
|
|
|
}
|
|
|
|
|
|
|
|
using Type = V<Ts...>;
|
|
|
|
PYBIND11_TYPE_CASTER(Type, _("Union[") + detail::concat(make_caster<Ts>::name()...) + _("]"));
|
|
|
|
};
|
|
|
|
|
|
|
|
#if PYBIND11_HAS_VARIANT
|
|
|
|
template <typename... Ts>
|
|
|
|
struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> { };
|
|
|
|
#endif
|
2015-09-04 21:42:12 +00:00
|
|
|
NAMESPACE_END(detail)
|
|
|
|
|
2016-01-17 21:36:41 +00:00
|
|
|
inline std::ostream &operator<<(std::ostream &os, const handle &obj) {
|
2016-10-25 20:12:39 +00:00
|
|
|
os << (std::string) str(obj);
|
2016-01-17 21:36:41 +00:00
|
|
|
return os;
|
|
|
|
}
|
2015-07-30 13:29:00 +00:00
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
NAMESPACE_END(pybind11)
|
2015-07-30 13:29:00 +00:00
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|