File: device_data.cpp

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 (232 lines) | stat: -rw-r--r-- 7,276 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
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
/*
 * SPDX-FileCopyrightText: All Contributors to the PyTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include "precompiled_header.hpp"
#include "pytgutils.h"
#include "fast_from_py.h"
#include "to_py_numpy.hpp"

namespace PyDeviceData
{

Tango::CmdArgType get_type(Tango::DeviceData &self)
{
    /// @todo This should change in Tango itself, get_type should not return int!!
    return static_cast<Tango::CmdArgType>(self.get_type());
}

/// @name Scalar Insertion
/// @{
template <long tangoTypeConst>
void insert_scalar(Tango::DeviceData &self, bopy::object py_value)
{
    typedef typename TANGO_const2type(tangoTypeConst) TangoScalarType;
    TangoScalarType value;
    from_py<tangoTypeConst>::convert(py_value.ptr(), value);
    self << value;
}

template <>
void insert_scalar<Tango::DEV_STRING>(Tango::DeviceData &self, bopy::object py_value)
{
    PyObject *py_value_ptr = py_value.ptr();
    if(PyUnicode_Check(py_value_ptr))
    {
        PyObject *obj_bytes_ptr = EncodeAsLatin1(py_value_ptr);
        Tango::DevString value = PyBytes_AsString(obj_bytes_ptr);
        self << value;
        Py_DECREF(obj_bytes_ptr);
    }
    else if(PyBytes_Check(py_value_ptr))
    {
        Tango::DevString value = PyBytes_AsString(py_value_ptr);
        self << value;
    }
    else
    {
        raise_(PyExc_TypeError, "can't translate python object to C char* in insert_scalar<Tango::DEV_STRING>");
    }
}

template <>
void insert_scalar<Tango::DEV_ENCODED>(Tango::DeviceData &self, bopy::object py_value)
{
    Tango::DevEncoded val;
    bopy::object p0 = py_value[0];
    const char *encoded_format = bopy::extract<const char *>(p0.ptr());
    val.encoded_format = CORBA::string_dup(encoded_format);

    view_pybytes_as_char_array(py_value[1], val.encoded_data);
    // By giving a value (not a pointer) to << the data will be copied by CORBA

    self << val;
}

template <>
void insert_scalar<Tango::DEV_VOID>(Tango::DeviceData &self, bopy::object py_value)
{
    raise_(PyExc_TypeError, "Trying to insert a value in a DEV_VOID DeviceData!");
}

template <>
void insert_scalar<Tango::DEV_PIPE_BLOB>(Tango::DeviceData &self, bopy::object py_value)
{
    assert(false);
}

/// @}
// ~Scalar Insertion
// -----------------------------------------------------------------------

/// @name Array Insertion
/// @{
template <long tangoArrayTypeConst>
void insert_array(Tango::DeviceData &self, bopy::object py_value)
{
    typedef typename TANGO_const2type(tangoArrayTypeConst) TangoArrayType;

    // self << val; -->> This ends up doing:
    // inline void operator << (DevVarUShortArray* datum)
    // { any.inout() <<= datum;}
    // So:
    //  - We loose ownership of the pointer, should not remove it
    //  - it's a CORBA object who gets ownership, not a buggy Tango
    //    thing. So the last parameter to fast_convert2array is false
    TangoArrayType *val = fast_convert2array<tangoArrayTypeConst>(py_value);
    self << val;
}

/// @}
// ~Array Insertion
// -----------------------------------------------------------------------

/// @name Scalar Extraction
/// @{
template <long tangoTypeConst>
bopy::object extract_scalar(Tango::DeviceData &self)
{
    typedef typename TANGO_const2type(tangoTypeConst) TangoScalarType;
    /// @todo CONST_DEV_STRING ell el tracta com DEV_STRING
    TangoScalarType val;
    self >> val;
    return bopy::object(val);
}

template <>
bopy::object extract_scalar<Tango::DEV_VOID>(Tango::DeviceData &self)
{
    return bopy::object();
}

template <>
bopy::object extract_scalar<Tango::DEV_STRING>(Tango::DeviceData &self)
{
    typedef std::string TangoScalarType;
    TangoScalarType val;
    self >> val;
    return from_char_to_boost_str(val);
}

template <>
bopy::object extract_scalar<Tango::DEV_PIPE_BLOB>(Tango::DeviceData &self)
{
    assert(false);
    return bopy::object();
}

/// @}
// ~Scalar Extraction
// -----------------------------------------------------------------------

/// @name Array extraction
/// @{

template <long tangoArrayTypeConst>
bopy::object extract_array(Tango::DeviceData &self, bopy::object &py_self, PyTango::ExtractAs extract_as)
{
    typedef typename TANGO_const2type(tangoArrayTypeConst) TangoArrayType;

    // const is the pointed, not the pointer. So cannot modify the data.
    // And that's because the data is still inside "self" after extracting.
    // This also means that we are not supposed to "delete" tmp_ptr.
    const TangoArrayType *tmp_ptr;
    self >> tmp_ptr;

    switch(extract_as)
    {
    default:
    case PyTango::ExtractAsNumpy:
        return to_py_numpy<tangoArrayTypeConst>(tmp_ptr, py_self);
    case PyTango::ExtractAsList:
    case PyTango::ExtractAsPyTango3:
        return to_py_list(tmp_ptr);
    case PyTango::ExtractAsTuple:
        return to_py_tuple(tmp_ptr);
    case PyTango::ExtractAsString: /// @todo
    case PyTango::ExtractAsNothing:
        return bopy::object();
    }
}

template <>
bopy::object extract_array<Tango::DEVVAR_STATEARRAY>(Tango::DeviceData &self,
                                                     bopy::object &py_self,
                                                     PyTango::ExtractAs extract_as)
{
    assert(false);
    return bopy::object();
}

/// @}
// ~Array Extraction
// -----------------------------------------------------------------------

bopy::object extract(bopy::object py_self, PyTango::ExtractAs extract_as)
{
    Tango::DeviceData &self = bopy::extract<Tango::DeviceData &>(py_self);

    TANGO_DO_ON_DEVICE_DATA_TYPE_ID(self.get_type(), return extract_scalar<tangoTypeConst>(self);
                                    , return extract_array<tangoTypeConst>(self, py_self, extract_as););
    return bopy::object();
}

void insert(Tango::DeviceData &self, long data_type, bopy::object py_value)
{
    TANGO_DO_ON_DEVICE_DATA_TYPE_ID(data_type, insert_scalar<tangoTypeConst>(self, py_value);
                                    , insert_array<tangoTypeConst>(self, py_value););
}
} // namespace PyDeviceData

void export_device_data()
{
    bopy::class_<Tango::DeviceData> DeviceData("DeviceData", bopy::init<>());

    bopy::scope scope_dd = DeviceData;

    /// @todo get rid of except_flags everywhere... or really use and export them everywhere!
    bopy::enum_<Tango::DeviceData::except_flags>("except_flags")
        .value("isempty_flag", Tango::DeviceData::isempty_flag)
        .value("wrongtype_flag", Tango::DeviceData::wrongtype_flag)
        .value("numFlags", Tango::DeviceData::numFlags);

    DeviceData
        .def(bopy::init<const Tango::DeviceData &>())

        .def("extract", &PyDeviceData::extract, (arg_("self"), arg_("extract_as") = PyTango::ExtractAsNumpy))

        .def("insert", &PyDeviceData::insert, (arg_("self"), arg_("data_type"), arg_("value")))

        /// @todo do not throw exceptions!!
        .def("is_empty", &Tango::DeviceData::is_empty)

        // TODO
        //	void exceptions(bitset<numFlags> fl) {exceptions_flags = fl;}
        //	bitset<numFlags> exceptions() {return exceptions_flags;}
        //	void reset_exceptions(except_flags fl) {exceptions_flags.reset((size_t)fl);}
        //	void set_exceptions(except_flags fl) {exceptions_flags.set((size_t)fl);}

        .def("get_type", &PyDeviceData::get_type);
}