mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-12 01:10:34 +00:00
transparent std::array conversion (fixes #97)
This commit is contained in:
@@ -78,6 +78,15 @@ template <size_t Size> constexpr descr<Size - 1, 0> _(char const(&text)[Size]) {
|
||||
return descr<Size - 1, 0>(text, { nullptr });
|
||||
}
|
||||
|
||||
template <size_t Rem, size_t... Digits> struct int_to_str : int_to_str<Rem/10, Rem%10, Digits...> { };
|
||||
template <size_t...Digits> struct int_to_str<0, Digits...> {
|
||||
static constexpr auto digits = descr<sizeof...(Digits), 0>({ ('0' + Digits)..., '\0' }, { nullptr });
|
||||
};
|
||||
|
||||
template <size_t Size> auto constexpr _() {
|
||||
return int_to_str<Size / 10, Size % 10>::digits;
|
||||
}
|
||||
|
||||
template <typename Type> constexpr descr<1, 1> _() {
|
||||
return descr<1, 1>({ '%', '\0' }, { &typeid(Type), nullptr });
|
||||
}
|
||||
@@ -149,6 +158,11 @@ template <typename Type> PYBIND11_NOINLINE descr _() {
|
||||
return descr("%", types);
|
||||
}
|
||||
|
||||
template <size_t Size> PYBIND11_NOINLINE descr _() {
|
||||
const std::type_info *types[1] = { nullptr };
|
||||
return descr(std::to_string(Size).c_str(), types);
|
||||
}
|
||||
|
||||
PYBIND11_NOINLINE inline descr concat() { return _(""); }
|
||||
PYBIND11_NOINLINE inline descr concat(descr &&d) { return d; }
|
||||
template <typename... Args> PYBIND11_NOINLINE descr concat(descr &&d, Args&&... args) { return std::move(d) + _(", ") + concat(std::forward<Args>(args)...); }
|
||||
|
||||
@@ -22,9 +22,9 @@
|
||||
NAMESPACE_BEGIN(pybind11)
|
||||
NAMESPACE_BEGIN(detail)
|
||||
|
||||
template <typename Value, typename Alloc> struct type_caster<std::vector<Value, Alloc>> {
|
||||
typedef std::vector<Value, Alloc> type;
|
||||
typedef type_caster<Value> value_conv;
|
||||
template <typename Type, typename Alloc> struct type_caster<std::vector<Type, Alloc>> {
|
||||
typedef std::vector<Type, Alloc> vector_type;
|
||||
typedef type_caster<Type> value_conv;
|
||||
public:
|
||||
bool load(handle src, bool convert) {
|
||||
list l(src, true);
|
||||
@@ -36,12 +36,12 @@ public:
|
||||
for (auto it : l) {
|
||||
if (!conv.load(it, convert))
|
||||
return false;
|
||||
value.push_back((Value) conv);
|
||||
value.push_back((Type) conv);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static handle cast(const type &src, return_value_policy policy, handle parent) {
|
||||
static handle cast(const vector_type &src, return_value_policy policy, handle parent) {
|
||||
list l(src.size());
|
||||
size_t index = 0;
|
||||
for (auto const &value: src) {
|
||||
@@ -52,7 +52,41 @@ public:
|
||||
}
|
||||
return l.release();
|
||||
}
|
||||
PYBIND11_TYPE_CASTER(type, _("list<") + value_conv::name() + _(">"));
|
||||
PYBIND11_TYPE_CASTER(vector_type, _("list<") + value_conv::name() + _(">"));
|
||||
};
|
||||
|
||||
template <typename Type, size_t Size> struct type_caster<std::array<Type, Size>> {
|
||||
typedef std::array<Type, Size> array_type;
|
||||
typedef type_caster<Type> value_conv;
|
||||
public:
|
||||
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();
|
||||
}
|
||||
PYBIND11_TYPE_CASTER(array_type, _("list<") + value_conv::name() + _(">") + _("[") + _<Size>() + _("]"));
|
||||
};
|
||||
|
||||
template <typename Key, typename Compare, typename Alloc> struct type_caster<std::set<Key, Compare, Alloc>> {
|
||||
|
||||
Reference in New Issue
Block a user