forked from TRIQS/cpp2py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_types.hpp
More file actions
58 lines (49 loc) · 1.93 KB
/
basic_types.hpp
File metadata and controls
58 lines (49 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#pragma once
#include "./complex.hpp"
namespace cpp2py {
// PyObject *
template <> struct py_converter<PyObject *> {
static PyObject *c2py(PyObject *ob) { return ob; }
static PyObject *py2c(PyObject *ob) { return ob; }
static bool is_convertible(PyObject *ob, bool raise_exception) { return true; }
};
// --- bool
template <> struct py_converter<bool> {
static PyObject *c2py(bool b) {
if (b)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
static bool py2c(PyObject *ob) { return ob == Py_True; }
static bool is_convertible(PyObject *ob, bool raise_exception) {
if (PyBool_Check(ob)) return true;
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to bool"); }
return false;
}
};
// --- long
template <> struct py_converter<long> {
static PyObject *c2py(long i) { return PyInt_FromLong(i); }
static long py2c(PyObject *ob) { return PyInt_AsLong(ob); }
static bool is_convertible(PyObject *ob, bool raise_exception) {
if (PyInt_Check(ob)) return true;
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to long"); }
return false;
}
};
template <> struct py_converter<int> : py_converter<long> {};
template <> struct py_converter<unsigned int> : py_converter<long> {};
template <> struct py_converter<unsigned long> : py_converter<long> {};
template <> struct py_converter<unsigned long long> : py_converter<long> {};
// --- double
template <> struct py_converter<double> {
static PyObject *c2py(double x) { return PyFloat_FromDouble(x); }
static double py2c(PyObject *ob) { return PyFloat_AsDouble(ob); }
static bool is_convertible(PyObject *ob, bool raise_exception) {
if (PyFloat_Check(ob) || PyInt_Check(ob)) return true;
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to double"); }
return false;
}
};
} // namespace cpp2py