2015-07-30 13:29:00 +00:00
|
|
|
/*
|
2015-10-15 16:13:33 +00:00
|
|
|
pybind11/complex.h: Complex number support
|
2015-07-30 13:29:00 +00:00
|
|
|
|
|
|
|
Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
|
|
|
|
|
|
|
|
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-07-30 13:29:00 +00:00
|
|
|
#include <map>
|
2015-11-14 18:04:49 +00:00
|
|
|
#include <set>
|
2015-07-30 13:29:00 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
|
|
|
|
#endif
|
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
NAMESPACE_BEGIN(pybind11)
|
2015-07-30 13:29:00 +00:00
|
|
|
NAMESPACE_BEGIN(detail)
|
|
|
|
|
2015-11-14 18:04:49 +00:00
|
|
|
template <typename Value, typename Alloc> struct type_caster<std::vector<Value, Alloc>> {
|
|
|
|
typedef std::vector<Value, Alloc> type;
|
2015-07-30 13:29:00 +00:00
|
|
|
typedef type_caster<Value> 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();
|
2015-11-14 18:04:49 +00:00
|
|
|
value_conv conv;
|
2015-07-30 13:29:00 +00:00
|
|
|
for (size_t i=0; i<size; ++i) {
|
|
|
|
if (!conv.load(PyList_GetItem(src, (ssize_t) i), convert))
|
|
|
|
return false;
|
|
|
|
value.push_back((Value) conv);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) {
|
|
|
|
PyObject *list = PyList_New(src.size());
|
|
|
|
size_t index = 0;
|
|
|
|
for (auto const &value: src) {
|
|
|
|
PyObject *value_ = value_conv::cast(value, policy, parent);
|
|
|
|
if (!value_) {
|
|
|
|
Py_DECREF(list);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-12-18 17:40:22 +00:00
|
|
|
PyList_SET_ITEM(list, index++, value_); // steals a reference
|
2015-07-30 13:29:00 +00:00
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
2015-10-18 14:48:30 +00:00
|
|
|
PYBIND11_TYPE_CASTER(type, detail::descr("list<") + value_conv::name() + detail::descr(">"));
|
2015-07-30 13:29:00 +00:00
|
|
|
};
|
|
|
|
|
2015-11-14 18:04:49 +00:00
|
|
|
template <typename Value, typename Compare, typename Alloc> struct type_caster<std::set<Value, Compare, Alloc>> {
|
|
|
|
typedef std::set<Value, Compare, Alloc> type;
|
|
|
|
typedef type_caster<Value> value_conv;
|
|
|
|
public:
|
|
|
|
bool load(PyObject *src, bool convert) {
|
|
|
|
pybind11::set s(src, true);
|
|
|
|
if (!s.check())
|
|
|
|
return false;
|
|
|
|
value.clear();
|
|
|
|
value_conv conv;
|
|
|
|
for (const object &o: s) {
|
|
|
|
if (!conv.load((PyObject *) o.ptr(), convert))
|
|
|
|
return false;
|
|
|
|
value.insert((Value) conv);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) {
|
|
|
|
PyObject *set = PySet_New(nullptr);
|
|
|
|
for (auto const &value: src) {
|
|
|
|
PyObject *value_ = value_conv::cast(value, policy, parent);
|
2015-12-18 17:40:22 +00:00
|
|
|
if (!value_ || PySet_Add(set, value_) != 0) {
|
|
|
|
Py_XDECREF(value_);
|
2015-11-14 18:04:49 +00:00
|
|
|
Py_DECREF(set);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-12-18 17:40:22 +00:00
|
|
|
Py_DECREF(value_);
|
2015-11-14 18:04:49 +00:00
|
|
|
}
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
PYBIND11_TYPE_CASTER(type, detail::descr("set<") + value_conv::name() + detail::descr(">"));
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Key, typename Value, typename Compare, typename Alloc> struct type_caster<std::map<Key, Value, Compare, Alloc>> {
|
2015-07-30 13:29:00 +00:00
|
|
|
public:
|
2015-11-14 18:04:49 +00:00
|
|
|
typedef std::map<Key, Value, Compare, Alloc> type;
|
2015-07-30 13:29:00 +00:00
|
|
|
typedef type_caster<Key> key_conv;
|
|
|
|
typedef type_caster<Value> 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);
|
2015-12-18 17:40:22 +00:00
|
|
|
if (!key || !value || PyDict_SetItem(dict, key, value) != 0) {
|
2015-07-30 13:29:00 +00:00
|
|
|
Py_XDECREF(key);
|
|
|
|
Py_XDECREF(value);
|
|
|
|
Py_DECREF(dict);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
Py_DECREF(key);
|
|
|
|
Py_DECREF(value);
|
|
|
|
}
|
|
|
|
return dict;
|
|
|
|
}
|
2015-08-24 13:31:24 +00:00
|
|
|
|
2015-10-18 14:48:30 +00:00
|
|
|
PYBIND11_TYPE_CASTER(type, detail::descr("dict<") + key_conv::name() + detail::descr(", ") + value_conv::name() + detail::descr(">"));
|
2015-07-30 13:29:00 +00:00
|
|
|
};
|
|
|
|
|
2015-09-04 21:42:12 +00:00
|
|
|
NAMESPACE_END(detail)
|
|
|
|
|
2015-07-30 13:29:00 +00:00
|
|
|
inline std::ostream &operator<<(std::ostream &os, const object &obj) { os << (const char *) obj.str(); return os; }
|
|
|
|
|
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
|