blob: 36568a056073d04eb91a68d7fed748ee9d63aa31 [file] [log] [blame]
Wenzel Jakob281aa0e2015-07-30 15:29:00 +02001/*
2 pybind/complex.h: Complex number support
3
4 Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/
9
10#pragma once
11
12#include <pybind/pybind.h>
13#include <map>
14#include <iostream>
15
16
17#if defined(_MSC_VER)
18#pragma warning(push)
19#pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
20#endif
21
22NAMESPACE_BEGIN(pybind)
23NAMESPACE_BEGIN(detail)
24
25template <typename Value> struct type_caster<std::vector<Value>> {
26 typedef std::vector<Value> type;
27 typedef type_caster<Value> value_conv;
28public:
29 bool load(PyObject *src, bool convert) {
30 if (!PyList_Check(src))
31 return false;
32 size_t size = (size_t) PyList_GET_SIZE(src);
33 value.reserve(size);
34 value.clear();
35 for (size_t i=0; i<size; ++i) {
36 value_conv conv;
37 if (!conv.load(PyList_GetItem(src, (ssize_t) i), convert))
38 return false;
39 value.push_back((Value) conv);
40 }
41 return true;
42 }
43
44 static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) {
45 PyObject *list = PyList_New(src.size());
46 size_t index = 0;
47 for (auto const &value: src) {
48 PyObject *value_ = value_conv::cast(value, policy, parent);
49 if (!value_) {
50 Py_DECREF(list);
51 return nullptr;
52 }
53 PyList_SetItem(list, index++, value_);
54 }
55 return list;
56 }
Wenzel Jakobf5fae922015-08-24 15:31:24 +020057 PYBIND_TYPE_CASTER(type, detail::descr("list<") + value_conv::descr() + detail::descr(">"));
Wenzel Jakob281aa0e2015-07-30 15:29:00 +020058};
59
60template <typename Key, typename Value> struct type_caster<std::map<Key, Value>> {
61public:
62 typedef std::map<Key, Value> type;
63 typedef type_caster<Key> key_conv;
64 typedef type_caster<Value> value_conv;
65
66 bool load(PyObject *src, bool convert) {
67 if (!PyDict_Check(src))
68 return false;
69
70 value.clear();
71 PyObject *key_, *value_;
72 ssize_t pos = 0;
73 key_conv kconv;
74 value_conv vconv;
75 while (PyDict_Next(src, &pos, &key_, &value_)) {
76 if (!kconv.load(key_, convert) || !vconv.load(value_, convert))
77 return false;
78 value[kconv] = vconv;
79 }
80 return true;
81 }
82
83 static PyObject *cast(const type &src, return_value_policy policy, PyObject *parent) {
84 PyObject *dict = PyDict_New();
85 for (auto const &kv: src) {
86 PyObject *key = key_conv::cast(kv.first, policy, parent);
87 PyObject *value = value_conv::cast(kv.second, policy, parent);
88 if (!key || !value || PyDict_SetItem(dict, key, value) < 0) {
89 Py_XDECREF(key);
90 Py_XDECREF(value);
91 Py_DECREF(dict);
92 return nullptr;
93 }
94 Py_DECREF(key);
95 Py_DECREF(value);
96 }
97 return dict;
98 }
Wenzel Jakobf5fae922015-08-24 15:31:24 +020099
100 PYBIND_TYPE_CASTER(type, detail::descr("dict<") + key_conv::descr() + detail::descr(", ") + value_conv::descr() + detail::descr(">"));
Wenzel Jakob281aa0e2015-07-30 15:29:00 +0200101};
102
103inline std::ostream &operator<<(std::ostream &os, const object &obj) { os << (const char *) obj.str(); return os; }
104
105NAMESPACE_END(detail)
106NAMESPACE_END(pybind)
107
108#if defined(_MSC_VER)
109#pragma warning(pop)
110#endif