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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
/*
* SPDX-FileCopyrightText: All Contributors to the PyTango project
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
// This header file is just some template functions moved apart from
// attribute.cpp, and should only be included there.
#pragma once
#include "tango_numpy.h"
#define fast_python_to_tango_buffer_fallback__() \
fast_python_to_tango_buffer_sequence<tangoScalarTypeConst>( \
py_val, pdim_x, pdim_y, fname, isImage, res_dim_x, res_dim_y)
template <long tangoScalarTypeConst>
inline typename TANGO_const2type(tangoScalarTypeConst) * fast_python_to_tango_buffer_numpy(PyObject *py_val,
long *pdim_x,
long *pdim_y,
const std::string &fname,
bool isImage,
long &res_dim_x,
long &res_dim_y)
{
typedef typename TANGO_const2type(tangoScalarTypeConst) TangoScalarType;
static const int typenum = TANGO_const2numpy(tangoScalarTypeConst);
if(!PyArray_Check(py_val))
{
return fast_python_to_tango_buffer_fallback__();
}
int nd = PyArray_NDIM(to_PyArrayObject(py_val));
npy_intp *dims = PyArray_DIMS(to_PyArrayObject(py_val));
long len = 0;
// Is the array exactly what we need? I mean: The type we need and with
// continuous aligned memory?
const bool exact_array = (PyArray_CHKFLAGS(to_PyArrayObject(py_val), NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED) &&
(PyArray_TYPE(to_PyArrayObject(py_val)) == typenum));
if(isImage)
{
// If dimensions are manually specified (nd must be 1), I don't
// know how to handle from numpy
if(nd == 1)
{
return fast_python_to_tango_buffer_fallback__();
}
// Check: This is an image!
if(nd != 2)
{
Tango::Except::throw_exception("PyDs_WrongNumpyArrayDimensions",
"Expecting a 2 dimensional numpy array (IMAGE attribute).",
fname + "()");
}
// If dimensions are manually limited and it's an image, I just
// know that if the limit is the real size, then I can safely
// ignore it, else let the default path take care...
bool dims_ok = true;
if(pdim_x)
{
dims_ok = dims_ok && (*pdim_x == dims[1]);
}
if(pdim_y)
{
dims_ok = dims_ok && (*pdim_y == dims[0]);
}
if(!dims_ok)
{
return fast_python_to_tango_buffer_fallback__();
}
len = static_cast<long>(dims[0] * dims[1]);
res_dim_x = static_cast<long>(dims[1]);
res_dim_y = static_cast<long>(dims[0]);
}
else
{
// Check: This is an spectrum!
if(nd != 1)
{
Tango::Except::throw_exception("PyDs_WrongNumpyArrayDimensions",
"Expecting a 1 dimensional numpy array (SPECTRUM attribute).",
fname + "()");
}
// If x dimension is limited then I only know how to behave
// if the array is exact
if(pdim_x)
{
// if x_dim is wrong, instead of throwing an exception I will let
// fast_python_to_tgbuffer_sequence throw it:
const bool dims_ok = (*pdim_x <= dims[0]);
if(!exact_array || !dims_ok)
{
return fast_python_to_tango_buffer_fallback__();
}
len = *pdim_x;
}
else
{
len = static_cast<long>(dims[0]);
}
res_dim_x = len;
res_dim_y = 0;
}
TangoScalarType *tg_data = new TangoScalarType[len];
void *vd_data = tg_data;
if(exact_array)
{
// The array is exactly what we need, so a plain memcpy is
// enough!
/// @todo If it is read only we need the copy, but if the
/// attribute is read/write, Tango will do the copy himself on
/// the calll to set_value(...), so there's no need for us
/// to make an extra one...
memcpy(vd_data, PyArray_DATA(to_PyArrayObject(py_val)), len * sizeof(TangoScalarType));
}
else
{
// We use numpy to create a copy of the array into the continuous
// memory location that we specify.
PyObject *py_cont;
py_cont = PyArray_SimpleNewFromData(nd, dims, typenum, vd_data);
if(!py_cont)
{
fast_python_to_tango_buffer_deleter__<tangoScalarTypeConst>(tg_data, len);
bopy::throw_error_already_set();
}
if(PyArray_CopyInto((PyArrayObject *) py_cont, (PyArrayObject *) py_val) < 0)
{
Py_DECREF(py_cont);
fast_python_to_tango_buffer_deleter__<tangoScalarTypeConst>(tg_data, len);
bopy::throw_error_already_set();
}
Py_DECREF(py_cont);
}
return tg_data;
}
template <>
inline TANGO_const2type(Tango::DEV_STRING) *
fast_python_to_tango_buffer_numpy<Tango::DEV_STRING>(PyObject *py_val,
long *pdim_x,
long *pdim_y,
const std::string &fname,
bool isImage,
long &res_dim_x,
long &res_dim_y)
{
static const long tangoScalarTypeConst = Tango::DEV_STRING;
return fast_python_to_tango_buffer_fallback__();
}
template <>
inline TANGO_const2type(Tango::DEV_ENCODED) *
fast_python_to_tango_buffer_numpy<Tango::DEV_ENCODED>(PyObject *py_val,
long *pdim_x,
long *pdim_y,
const std::string &fname,
bool isImage,
long &res_dim_x,
long &res_dim_y)
{
static const long tangoScalarTypeConst = Tango::DEV_ENCODED;
return fast_python_to_tango_buffer_fallback__();
}
#define fast_python_to_corba_buffer_fallback__() \
fast_python_to_corba_buffer_sequence<tangoArrayTypeConst>(py_val, pdim_x, fname, res_dim_x)
template <long tangoArrayTypeConst>
inline typename TANGO_const2scalartype(tangoArrayTypeConst) *
fast_python_to_corba_buffer_numpy(PyObject *py_val, long *pdim_x, const std::string &fname, long &res_dim_x)
{
typedef typename TANGO_const2type(tangoArrayTypeConst) TangoArrayType;
typedef typename TANGO_const2scalartype(tangoArrayTypeConst) TangoScalarType;
static const int typenum = TANGO_const2scalarnumpy(tangoArrayTypeConst);
if(!PyArray_Check(py_val))
{
return fast_python_to_corba_buffer_fallback__();
}
int nd = PyArray_NDIM(to_PyArrayObject(py_val));
npy_intp *dims = PyArray_DIMS(to_PyArrayObject(py_val));
long len = 0;
// Is the array exactly what we need? I mean: The type we need and with
// continuous aligned memory?
const bool exact_array = (PyArray_CHKFLAGS(to_PyArrayObject(py_val), NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED) &&
(PyArray_TYPE(to_PyArrayObject(py_val)) == typenum));
// Check: This is an spectrum!
if(nd != 1)
{
Tango::Except::throw_exception("PyDs_WrongNumpyArrayDimensions",
"Expecting a 1 dimensional numpy array (SPECTRUM attribute).",
fname + "()");
}
// If x dimension is limited then I only know how to behave
// if the array is exact
if(pdim_x)
{
// if x_dim is wrong, instead of throwing an exception I will let
// fast_python_to_tgbuffer_sequence throw it:
const bool dims_ok = (*pdim_x <= dims[0]);
if(!exact_array || !dims_ok)
{
return fast_python_to_corba_buffer_fallback__();
}
len = *pdim_x;
}
else
{
len = static_cast<long>(dims[0]);
}
res_dim_x = len;
TangoScalarType *tg_data = TangoArrayType::allocbuf(len);
void *vd_data = tg_data;
if(exact_array)
{
// The array is exactly what we need, so a plain memcpy is
// enough!
/// @todo If it is read only we need the copy, but if the
/// attribute is read/write, Tango will do the copy himself on
/// the calll to set_value(...), so there's no need for us
/// to make an extra one...
memcpy(vd_data, PyArray_DATA(to_PyArrayObject(py_val)), len * sizeof(TangoScalarType));
}
else
{
// We use numpy to create a copy of the array into the continuous
// memory location that we specify.
PyObject *py_cont;
py_cont = PyArray_SimpleNewFromData(nd, dims, typenum, vd_data);
if(!py_cont)
{
TangoArrayType::freebuf(tg_data);
bopy::throw_error_already_set();
}
if(PyArray_CopyInto((PyArrayObject *) py_cont, (PyArrayObject *) py_val) < 0)
{
Py_DECREF(py_cont);
TangoArrayType::freebuf(tg_data);
bopy::throw_error_already_set();
}
Py_DECREF(py_cont);
}
return tg_data;
}
template <>
inline TANGO_const2scalartype(Tango::DEVVAR_STRINGARRAY) *
fast_python_to_corba_buffer_numpy<Tango::DEVVAR_STRINGARRAY>(PyObject *py_val,
long *pdim_x,
const std::string &fname,
long &res_dim_x)
{
static const long tangoArrayTypeConst = Tango::DEVVAR_STRINGARRAY;
return fast_python_to_corba_buffer_fallback__();
}
|