/* pybind11/stl.h: Transparent conversion for STL data types Copyright (c) 2016 Wenzel Jakob 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 #include "pybind11.h" #include #include #include #include #include #include #if defined(_MSC_VER) #pragma warning(push) #pragma warning(disable: 4127) // warning C4127: Conditional expression is constant #endif NAMESPACE_BEGIN(pybind11) NAMESPACE_BEGIN(detail) template struct set_caster { using type = Type; using key_conv = make_caster; 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(); } PYBIND11_TYPE_CASTER(type, _("Set[") + key_conv::name() + _("]")); }; template struct map_caster { using type = Type; using key_conv = make_caster; using value_conv = make_caster; 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; value.emplace((Key) kconv, (Value) vconv); } 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(); } PYBIND11_TYPE_CASTER(type, _("Dict[") + key_conv::name() + _(", ") + value_conv::name() + _("]")); }; template struct list_caster { using type = Type; using value_conv = make_caster; bool load(handle src, bool convert) { sequence s(src, true); if (!s.check()) return false; value_conv conv; value.clear(); reserve_maybe(s, &value); for (auto it : s) { if (!conv.load(it, convert)) return false; value.push_back((Value) conv); } return true; } template ().reserve(0)), void>::value, int> = 0> void reserve_maybe(sequence s, Type *) { value.reserve(s.size()); } void reserve_maybe(sequence, void *) { } static handle cast(const Type &src, return_value_policy policy, handle parent) { list l(src.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(Type, _("List[") + value_conv::name() + _("]")); }; template struct type_caster> : list_caster, Type> { }; template struct type_caster> : list_caster, Type> { }; template struct type_caster> { using array_type = std::array; using value_conv = make_caster; 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() + _("[") + _() + _("]]")); }; template struct type_caster> : set_caster, Key> { }; template struct type_caster> : set_caster, Key> { }; template struct type_caster> : map_caster, Key, Value> { }; template struct type_caster> : map_caster, Key, Value> { }; NAMESPACE_END(detail) inline std::ostream &operator<<(std::ostream &os, const handle &obj) { os << (std::string) obj.str(); return os; } NAMESPACE_END(pybind11) #if defined(_MSC_VER) #pragma warning(pop) #endif