ComIn 0.5.1
ICON Community Interface
Loading...
Searching...
No Matches
descrdata_properties.h
Go to the documentation of this file.
1/* @authors 11/2023 :: ICON Community Interface <comin@icon-model.org>
2
3 SPDX-License-Identifier: BSD-3-Clause
4
5 Please see the file LICENSE in the root of the source tree for this code.
6 Where software is supplied by third parties, it is indicated in the
7 headers of the routines. */
8
9#ifndef PY_COMIN_DESCRDATA_PROPERTIES
10#define PY_COMIN_DESCRDATA_PROPERTIES
11
12#include "comin.h"
13
14namespace comin::python {
15
16static PyObject* BuildValue_FromPtr(std::string ctype, void* data) {
17 if (ctype == "double")
18 return Py_BuildValue("f", *(double*)data);
19 if (ctype == "int")
20 return Py_BuildValue("i", *(int*)data);
21 return PyErr_Format(PyExc_RuntimeError, "Unknown data type: %s",
22 ctype.data());
23}
24
25// helper function to generate dict from properties array
26static PyObject*
27descrdata_properties_to_dict(const comin_descrdata_property_t* properties) {
28 PyObject* dict = PyDict_New();
29 const comin_descrdata_property_t* prop = properties;
30 while (prop->name != NULL) {
31 PyDict_SetItemString(
32 dict, prop->name,
33 PyCapsule_New((void*)prop, "descrdata_property", NULL));
34 prop++;
35 }
36 return dict;
37}
38
39static PyObject* descrdata_eval_property(PyObject* self, PyObject* args,
40 PyObject* kwargs) {
41 PyObject* prop_cap;
42 int jg = 0;
43 static char const* kwlist[] = {"property", "jg", NULL};
44 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i", (char**)&kwlist,
45 &prop_cap, &jg))
46 return NULL;
47 comin_descrdata_property_t* prop =
48 (comin_descrdata_property_t*)PyCapsule_GetPointer(prop_cap,
49 "descrdata_property");
50 if (prop == NULL)
51 return NULL;
52 void* dataptr;
53 if (std::string(prop->datatype) ==
54 "void") { // return the dict of subproperties
55 return descrdata_properties_to_dict(prop->subtypes);
56 } else if (prop->ndims == 0) { // return scalar
57 if (std::string(prop->datatype) == "int") {
58 int val;
59 if (prop->has_jg)
60 val = ((int (*)(int))prop->get_function)(jg);
61 else
62 val = ((int (*)())prop->get_function)();
63 return PyLong_FromLong(val);
64 }
65 if (std::string(prop->datatype) == "double") {
66 double val;
67 if (prop->has_jg)
68 val = ((double (*)(int))prop->get_function)(jg);
69 else
70 val = ((double (*)())prop->get_function)();
71 return PyFloat_FromDouble(val);
72 }
73 if (std::string(prop->datatype) == "bool") {
74 bool val;
75 if (prop->has_jg)
76 val = ((bool (*)(int))prop->get_function)(jg);
77 else
78 val = ((bool (*)())prop->get_function)();
79 if (val)
80 Py_RETURN_TRUE;
81 else
82 Py_RETURN_FALSE;
83 }
84 return PyErr_Format(PyExc_RuntimeError,
85 "datatype %s for scalar value %s not implemented",
86 prop->datatype, prop->name);
87 } else {
88 std::vector<int> arr_size(prop->ndims);
89 if (prop->has_jg)
90 ((void (*)(int, void**, int*))prop->get_function)(jg, &dataptr,
91 arr_size.data());
92 else
93 ((void (*)(void**, int*))prop->get_function)(&dataptr, arr_size.data());
94 if (dataptr == NULL)
95 return PyErr_Format(PyExc_RuntimeError, "%s get function returned NULL",
96 prop->name);
97 if (prop->ndims == 0) { // return the value directly
98 return BuildValue_FromPtr(prop->datatype, dataptr);
99 }
100 if (std::string(prop->datatype) == "char" && prop->ndims == 1)
101 return Py_BuildValue("s#", dataptr, (Py_ssize_t)arr_size[0]);
102 // return MemoryView
103 Py_buffer buffer;
104 if (std::string(prop->datatype) == "double") {
105 fill_buffer<double>(&buffer, dataptr, arr_size.data(), prop->ndims, 1);
106 } else if (std::string(prop->datatype) == "int") {
107 fill_buffer<int>(&buffer, dataptr, arr_size.data(), prop->ndims, 1);
108 } else if (std::string(prop->datatype) == "int8_t") {
109 fill_buffer<int8_t>(&buffer, dataptr, arr_size.data(), prop->ndims, 1);
110 } else
111 return PyErr_Format(PyExc_RuntimeError,
112 "datatype %s for property %s not implemented",
113 prop->datatype, prop->name);
114 buffer.obj = self;
115 return PyMemoryView_FromBuffer(&buffer);
116 }
117}
118} // namespace comin::python
119#endif
C interface for the ICON Community Interface.
static PyObject * descrdata_eval_property(PyObject *self, PyObject *args, PyObject *kwargs)
static PyObject * descrdata_properties_to_dict(const comin_descrdata_property_t *properties)
void fill_buffer(Py_buffer *buffer, void *mem, int *shape, int ndims, int readonly=1)
Definition util.h:28
static PyObject * BuildValue_FromPtr(std::string ctype, void *data)
const int jg
double * buffer