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>
|
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>
|
|
|
|
# if __cpp_lib_experimental_optional // just in case
|
|
|
|
# define PYBIND11_HAS_EXP_OPTIONAL 1
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
#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) {
|
|
|
|
pybind11::set s(src, true);
|
|
|
|
if (!s.check())
|
|
|
|
return false;
|
|
|
|
value.clear();
|
|
|
|
key_conv conv;
|
|
|
|
for (auto entry : s) {
|
|
|
|
if (!conv.load(entry, convert))
|
|
|
|
return false;
|
|
|
|
value.insert((Key) conv);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static handle cast(const type &src, return_value_policy policy, handle parent) {
|
|
|
|
pybind11::set s;
|
|
|
|
for (auto const &value: src) {
|
|
|
|
object value_ = object(key_conv::cast(value, policy, parent), false);
|
|
|
|
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 type = Type;
|
|
|
|
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) {
|
|
|
|
dict d(src, true);
|
|
|
|
if (!d.check())
|
|
|
|
return false;
|
|
|
|
key_conv kconv;
|
|
|
|
value_conv vconv;
|
|
|
|
value.clear();
|
|
|
|
for (auto it : d) {
|
|
|
|
if (!kconv.load(it.first.ptr(), convert) ||
|
|
|
|
!vconv.load(it.second.ptr(), convert))
|
|
|
|
return false;
|
2016-03-05 22:36:46 +00:00
|
|
|
value.emplace((Key) kconv, (Value) vconv);
|
2016-02-12 23:20:21 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static handle cast(const type &src, return_value_policy policy, handle parent) {
|
|
|
|
dict d;
|
|
|
|
for (auto const &kv: src) {
|
|
|
|
object key = object(key_conv::cast(kv.first, policy, parent), false);
|
|
|
|
object value = object(value_conv::cast(kv.second, policy, parent), false);
|
|
|
|
if (!key || !value)
|
|
|
|
return handle();
|
|
|
|
d[key] = value;
|
|
|
|
}
|
|
|
|
return d.release();
|
|
|
|
}
|
|
|
|
|
2016-08-03 23:40:40 +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 type = Type;
|
|
|
|
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-12 16:00:53 +00:00
|
|
|
sequence s(src, true);
|
|
|
|
if (!s.check())
|
2015-07-30 13:29:00 +00:00
|
|
|
return false;
|
2015-11-14 18:04:49 +00:00
|
|
|
value_conv conv;
|
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) {
|
2016-01-17 21:36:44 +00:00
|
|
|
if (!conv.load(it, convert))
|
2015-07-30 13:29:00 +00:00
|
|
|
return false;
|
2016-02-23 16:28:45 +00:00
|
|
|
value.push_back((Value) conv);
|
2015-07-30 13:29:00 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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-01-17 21:36:44 +00:00
|
|
|
object value_ = object(value_conv::cast(value, policy, parent), false);
|
2015-12-30 20:03:57 +00:00
|
|
|
if (!value_)
|
2016-01-17 21:36:44 +00:00
|
|
|
return handle();
|
|
|
|
PyList_SET_ITEM(l.ptr(), 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-02-07 15:36:26 +00:00
|
|
|
template <typename Type, size_t Size> struct type_caster<std::array<Type, Size>> {
|
2016-09-05 12:30:56 +00:00
|
|
|
using array_type = std::array<Type, Size>;
|
|
|
|
using value_conv = make_caster<Type>;
|
2016-02-12 23:20:21 +00:00
|
|
|
|
2016-02-07 15:36:26 +00:00
|
|
|
bool load(handle src, bool convert) {
|
|
|
|
list l(src, true);
|
|
|
|
if (!l.check())
|
|
|
|
return false;
|
|
|
|
if (l.size() != Size)
|
|
|
|
return false;
|
|
|
|
value_conv conv;
|
|
|
|
size_t ctr = 0;
|
|
|
|
for (auto it : l) {
|
|
|
|
if (!conv.load(it, convert))
|
|
|
|
return false;
|
|
|
|
value[ctr++] = (Type) conv;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static handle cast(const array_type &src, return_value_policy policy, handle parent) {
|
|
|
|
list l(Size);
|
|
|
|
size_t index = 0;
|
|
|
|
for (auto const &value: src) {
|
|
|
|
object value_ = object(value_conv::cast(value, policy, parent), false);
|
|
|
|
if (!value_)
|
|
|
|
return handle();
|
|
|
|
PyList_SET_ITEM(l.ptr(), index++, value_.release().ptr()); // steals a reference
|
|
|
|
}
|
|
|
|
return l.release();
|
|
|
|
}
|
2016-08-03 23:40:40 +00:00
|
|
|
PYBIND11_TYPE_CASTER(array_type, _("List[") + value_conv::name() + _("[") + _<Size>() + _("]]"));
|
2015-07-30 13:29:00 +00:00
|
|
|
};
|
|
|
|
|
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 {
|
|
|
|
using value_type = typename intrinsic_type<typename T::value_type>::type;
|
|
|
|
using caster_type = type_caster<value_type>;
|
|
|
|
|
|
|
|
static handle cast(const T& src, return_value_policy policy, handle parent) {
|
|
|
|
if (!src)
|
|
|
|
return none();
|
|
|
|
return caster_type::cast(*src, policy, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool load(handle src, bool convert) {
|
|
|
|
if (!src) {
|
|
|
|
return false;
|
|
|
|
} else if (src.is_none()) {
|
|
|
|
value = {}; // nullopt
|
|
|
|
return true;
|
|
|
|
} else if (!inner.load(src, convert)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
value.emplace(static_cast<const value_type&>(inner));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PYBIND11_TYPE_CASTER(T, _("Optional[") + caster_type::name() + _("]"));
|
|
|
|
|
|
|
|
private:
|
|
|
|
caster_type inner;
|
|
|
|
};
|
|
|
|
|
|
|
|
#if PYBIND11_HAS_OPTIONAL
|
|
|
|
template<typename T> struct type_caster<std::optional<T>>
|
|
|
|
: public optional_caster<std::optional<T>> {};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if PYBIND11_HAS_EXP_OPTIONAL
|
|
|
|
template<typename T> struct type_caster<std::experimental::optional<T>>
|
|
|
|
: public optional_caster<std::experimental::optional<T>> {};
|
|
|
|
#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) {
|
|
|
|
os << (std::string) obj.str();
|
|
|
|
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
|