/* pybind/complex.h: Complex number support Copyright (c) 2015 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 #include #include #if defined(_MSC_VER) #pragma warning(push) #pragma warning(disable: 4127) // warning C4127: Conditional expression is constant #endif NAMESPACE_BEGIN(pybind) NAMESPACE_BEGIN(detail) template struct type_caster> { typedef std::vector type; typedef type_caster value_conv; public: bool load(PyObject *src, bool convert) { if (!PyList_Check(src)) return false; size_t size = (size_t) PyList_GET_SIZE(src); value.reserve(size); value.clear(); for (size_t i=0; i")); }; template struct type_caster> { public: typedef std::map type; typedef type_caster key_conv; typedef type_caster value_conv; bool load(PyObject *src, bool convert) { if (!PyDict_Check(src)) return false; value.clear(); PyObject *key_, *value_; ssize_t pos = 0; key_conv kconv; value_conv vconv; while (PyDict_Next(src, &pos, &key_, &value_)) { if (!kconv.load(key_, convert) || !vconv.load(value_, convert)) return false; value[kconv] = vconv; } return true; } static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) { PyObject *dict = PyDict_New(); for (auto const &kv: src) { PyObject *key = key_conv::cast(kv.first, policy, parent); PyObject *value = value_conv::cast(kv.second, policy, parent); if (!key || !value || PyDict_SetItem(dict, key, value) < 0) { Py_XDECREF(key); Py_XDECREF(value); Py_DECREF(dict); return nullptr; } Py_DECREF(key); Py_DECREF(value); } return dict; } PYBIND_TYPE_CASTER(type, detail::descr("dict<") + key_conv::name() + detail::descr(", ") + value_conv::name() + detail::descr(">")); }; NAMESPACE_END(detail) inline std::ostream &operator<<(std::ostream &os, const object &obj) { os << (const char *) obj.str(); return os; } NAMESPACE_END(pybind) #if defined(_MSC_VER) #pragma warning(pop) #endif