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