ComIn 0.5.1
ICON Community Interface
Loading...
Searching...
No Matches
util.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 UTIL_H
10#define UTIL_H
11
12#include <Python.h>
13#include <comin.h>
14#include <comin_global.inc>
15
16namespace comin::python {
17
18// format characters - see:
19// https://docs.python.org/3/library/struct.html#format-characters
20template <class T> const char* get_format_char();
21
22template <> inline const char* get_format_char<double>() { return "d"; }
23template <> inline const char* get_format_char<float>() { return "f"; }
24template <> inline const char* get_format_char<int>() { return "i"; }
25template <> inline const char* get_format_char<int8_t>() { return "i1"; }
26
27template <class T>
28void fill_buffer(Py_buffer* buffer, void* mem, int* shape, int ndims,
29 int readonly = 1) {
30 buffer->obj = NULL;
31 buffer->buf = (void*)mem;
32 buffer->len = sizeof(T);
33 for (int i = 0; i < ndims; buffer->len *= shape[i++])
34 ;
35 buffer->readonly = readonly;
36 buffer->itemsize = sizeof(T);
37 buffer->format = (char*)get_format_char<T>(); // T
38 buffer->ndim = ndims;
39 buffer->shape = new Py_ssize_t[ndims];
40 for (int i = 0; i < ndims; ++i)
41 buffer->shape[i] = shape[i];
42
43 buffer->strides = new Py_ssize_t[ndims];
44 buffer->strides[0] = sizeof(T);
45 for (int i = 1; i < buffer->ndim; i++)
46 buffer->strides[i] = buffer->strides[i - 1] * buffer->shape[i - 1];
47 buffer->suboffsets = NULL;
48 buffer->internal = NULL;
49}
50} // namespace comin::python
51#endif
C interface for the ICON Community Interface.
const char * get_format_char< int >()
Definition util.h:24
const char * get_format_char()
void fill_buffer(Py_buffer *buffer, void *mem, int *shape, int ndims, int readonly=1)
Definition util.h:28
const char * get_format_char< double >()
Definition util.h:22
const char * get_format_char< float >()
Definition util.h:23
const char * get_format_char< int8_t >()
Definition util.h:25
double * buffer