File: base_types_numpy.hpp

package info (click to toggle)
pytango 10.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,216 kB
  • sloc: python: 28,206; cpp: 16,380; sql: 255; sh: 82; makefile: 43
file content (95 lines) | stat: -rw-r--r-- 2,819 bytes parent folder | download | duplicates (3)
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
/*
 * SPDX-FileCopyrightText: All Contributors to the PyTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#pragma once

template <long tangoTypeConst>
struct convert_numpy_to_integer
{
    typedef typename TANGO_const2type(tangoTypeConst) TangoScalarType;
    static const long NumpyType = TANGO_const2numpy(tangoTypeConst);

    convert_numpy_to_integer()
    {
        bopy::converter::registry::push_back(&convertible, &construct, bopy::type_id<TangoScalarType>());
    }

    static void *convertible(PyObject *obj)
    {
        if(!PyArray_CheckScalar(obj))
        {
            return 0;
        }

        PyArray_Descr *type = PyArray_DescrFromScalar(obj);
        if(PyDataType_ISINTEGER(type))
        {
            return obj;
        }
        return 0;
    }

    static void construct(PyObject *obj, bopy::converter::rvalue_from_python_stage1_data *data)
    {
        typedef bopy::converter::rvalue_from_python_storage<TangoScalarType> tango_storage;
        void *const storage = reinterpret_cast<tango_storage *>(data)->storage.bytes;
        TangoScalarType *ptr = new(storage) TangoScalarType();

        PyObject *native_obj = PyObject_CallMethod(obj, const_cast<char *>("__int__"), NULL);
        if(native_obj == NULL)
        {
            bopy::throw_error_already_set();
        }
        from_py<tangoTypeConst>::convert(native_obj, *ptr);
        Py_DECREF(native_obj);

        data->convertible = storage;
    }
};

template <long tangoTypeConst>
struct convert_numpy_to_float
{
    typedef typename TANGO_const2type(tangoTypeConst) TangoScalarType;
    static const long NumpyType = TANGO_const2numpy(tangoTypeConst);

    convert_numpy_to_float()
    {
        bopy::converter::registry::push_back(&convertible, &construct, bopy::type_id<TangoScalarType>());
    }

    static void *convertible(PyObject *obj)
    {
        if(!PyArray_CheckScalar(obj))
        {
            return 0;
        }

        PyArray_Descr *type = PyArray_DescrFromScalar(obj);
        if(PyDataType_ISINTEGER(type) || PyDataType_ISFLOAT(type))
        {
            return obj;
        }
        return 0;
    }

    static void construct(PyObject *obj, bopy::converter::rvalue_from_python_stage1_data *data)
    {
        typedef bopy::converter::rvalue_from_python_storage<TangoScalarType> tango_storage;
        void *const storage = reinterpret_cast<tango_storage *>(data)->storage.bytes;
        TangoScalarType *ptr = new(storage) TangoScalarType();

        PyObject *native_obj = PyObject_CallMethod(obj, const_cast<char *>("__float__"), NULL);
        if(native_obj == NULL)
        {
            bopy::throw_error_already_set();
        }
        from_py<tangoTypeConst>::convert(native_obj, *ptr);
        Py_DECREF(native_obj);

        data->convertible = storage;
    }
};