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) {
|
2016-01-17 21:36:41 +00:00
|
|
|
list l(src, true);
|
|
|
|
if (!l.check())
|
2015-07-30 13:29:00 +00:00
|
|
|
return false;
|
2016-01-17 21:36:41 +00:00
|
|
|
value.reserve(l.size());
|
2015-07-30 13:29:00 +00:00
|
|
|
value.clear();
|
2015-11-14 18:04:49 +00:00
|
|
|
value_conv conv;
|
2016-01-17 21:36:41 +00:00
|
|
|
for (auto it : l) {
|
|
|
|
if (!conv.load(it.ptr(), convert))
|
2015-07-30 13:29:00 +00:00
|
|
|
return false;
|
|
|
|
value.push_back((Value) conv);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *cast(const type &src, return_value_policy policy, PyObject *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:41 +00:00
|
|
|
object value_(value_conv::cast(value, policy, parent), false);
|
2015-12-30 20:03:57 +00:00
|
|
|
if (!value_)
|
2015-07-30 13:29:00 +00:00
|
|
|
return nullptr;
|
2016-01-17 21:36:41 +00:00
|
|
|
PyList_SET_ITEM(l.ptr(), index++, value_.release()); // 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-01-17 21:36:36 +00:00
|
|
|
PYBIND11_TYPE_CASTER(type, _("list<") + value_conv::name() + _(">"));
|
2015-07-30 13:29:00 +00:00
|
|
|
};
|
|
|
|
|
2016-01-17 21:36:36 +00:00
|
|
|
template <typename Key, typename Compare, typename Alloc> struct type_caster<std::set<Key, Compare, Alloc>> {
|
|
|
|
typedef std::set<Key, Compare, Alloc> type;
|
|
|
|
typedef type_caster<Key> key_conv;
|
2015-11-14 18:04:49 +00:00
|
|
|
public:
|
|
|
|
bool load(PyObject *src, bool convert) {
|
|
|
|
pybind11::set s(src, true);
|
|
|
|
if (!s.check())
|
|
|
|
return false;
|
|
|
|
value.clear();
|
2016-01-17 21:36:36 +00:00
|
|
|
key_conv conv;
|
2016-01-17 21:36:41 +00:00
|
|
|
for (auto entry : s) {
|
|
|
|
if (!conv.load(entry.ptr(), convert))
|
2015-11-14 18:04:49 +00:00
|
|
|
return false;
|
2016-01-17 21:36:36 +00:00
|
|
|
value.insert((Key) conv);
|
2015-11-14 18:04:49 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) {
|
2016-01-17 21:36:41 +00:00
|
|
|
pybind11::set s;
|
2015-11-14 18:04:49 +00:00
|
|
|
for (auto const &value: src) {
|
2016-01-17 21:36:36 +00:00
|
|
|
object value_(key_conv::cast(value, policy, parent), false);
|
2016-01-17 21:36:41 +00:00
|
|
|
if (!value_ || !s.add(value))
|
2015-11-14 18:04:49 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-01-17 21:36:41 +00:00
|
|
|
return s.release();
|
2015-11-14 18:04:49 +00:00
|
|
|
}
|
2016-01-17 21:36:36 +00:00
|
|
|
PYBIND11_TYPE_CASTER(type, _("set<") + key_conv::name() + _(">"));
|
2015-11-14 18:04:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
2016-01-17 21:36:41 +00:00
|
|
|
dict d(src, true);
|
|
|
|
if (!d.check())
|
2015-07-30 13:29:00 +00:00
|
|
|
return false;
|
|
|
|
key_conv kconv;
|
|
|
|
value_conv vconv;
|
2016-01-17 21:36:41 +00:00
|
|
|
value.clear();
|
|
|
|
for (auto it : d) {
|
|
|
|
if (!kconv.load(it.first.ptr(), convert) ||
|
|
|
|
!vconv.load(it.second.ptr(), convert))
|
2015-07-30 13:29:00 +00:00
|
|
|
return false;
|
2015-12-26 12:37:59 +00:00
|
|
|
value[(Key) kconv] = (Value) vconv;
|
2015-07-30 13:29:00 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) {
|
2016-01-17 21:36:41 +00:00
|
|
|
dict d;
|
2015-07-30 13:29:00 +00:00
|
|
|
for (auto const &kv: src) {
|
2015-12-30 20:03:57 +00:00
|
|
|
object key(key_conv::cast(kv.first, policy, parent), false);
|
|
|
|
object value(value_conv::cast(kv.second, policy, parent), false);
|
2016-01-17 21:36:41 +00:00
|
|
|
if (!key || !value)
|
2015-07-30 13:29:00 +00:00
|
|
|
return nullptr;
|
2016-01-17 21:36:41 +00:00
|
|
|
d[key] = value;
|
2015-07-30 13:29:00 +00:00
|
|
|
}
|
2016-01-17 21:36:41 +00:00
|
|
|
return d.release();
|
2015-07-30 13:29:00 +00:00
|
|
|
}
|
2015-08-24 13:31:24 +00:00
|
|
|
|
2016-01-17 21:36:36 +00:00
|
|
|
PYBIND11_TYPE_CASTER(type, _("dict<") + key_conv::name() + _(", ") + value_conv::name() + _(">"));
|
2015-07-30 13:29:00 +00:00
|
|
|
};
|
|
|
|
|
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
|