File: scalar_cpp_to_python.h

package info (click to toggle)
pytango 10.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,304 kB
  • sloc: python: 27,795; cpp: 16,150; sql: 252; sh: 152; makefile: 43
file content (116 lines) | stat: -rw-r--r-- 5,040 bytes parent folder | download
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
/*
 * SPDX-FileCopyrightText: All Contributors to the PyTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#pragma once

#include "common_header.h"
#include "types_structs_macros.h"
#include "convertors/attributes/extract_value.h"
#include "convertors/type_casters.h"

namespace PyDeviceAttribute {
template <int tangoTypeConst>
static inline void scalar_value_from_cpp_into_python(Tango::DeviceAttribute &self,
                                                     py::object &py_value,
                                                     [[maybe_unused]] PyTango::ExtractAs extract_as) {
    using TangoScalarType = typename TANGO_const2type(tangoTypeConst);

    if(self.get_written_dim_x() > 0) {
        std::vector<TangoScalarType> value;
        self.extract_read(value);
        // In the following lines, the cast is absolutely necessary because
        // vector<TangoScalarType> may not be a vector<TangoScalarType> at
        // compile time. For example, for vector<DevBoolean>, the compiler
        // may create a std::_Bit_reference type.
        py_value.attr(value_attr_name) = cpp_to_python_scalar<tangoTypeConst>::convert(value[0]);
        self.extract_set(value);
        py_value.attr(w_value_attr_name) = cpp_to_python_scalar<tangoTypeConst>::convert(value[0]);
    } else {
        TangoScalarType value;
        EXTRACT_VALUE(self, value)
        py_value.attr(value_attr_name) = cpp_to_python_scalar<tangoTypeConst>::convert(value);
        py_value.attr(w_value_attr_name) = py::none();
    }
}

template <>
inline void scalar_value_from_cpp_into_python<Tango::DEV_STRING>(Tango::DeviceAttribute &self,
                                                                 py::object &py_value,
                                                                 [[maybe_unused]] PyTango::ExtractAs extract_as) {
    if(self.get_written_dim_x() > 0) {
        std::vector<std::string> r_val, w_val;
        self.extract_read(r_val);
        py_value.attr(value_attr_name) = from_cpp_str_to_pybind11_str(r_val[0]);
        self.extract_set(w_val);
        py_value.attr(w_value_attr_name) = from_cpp_str_to_pybind11_str(w_val[0]);
    } else {
        std::string rvalue;
        EXTRACT_VALUE(self, rvalue)
        py_value.attr(value_attr_name) = from_cpp_str_to_pybind11_str(rvalue);
        py_value.attr(w_value_attr_name) = py::none();
    }
}

template <>
inline void scalar_value_from_cpp_into_python<Tango::DEV_ENCODED>(Tango::DeviceAttribute &self,
                                                                  py::object &py_value,
                                                                  PyTango::ExtractAs extract_as) {
    Tango::DevVarEncodedArray *value_ptr;
    EXTRACT_VALUE(self, value_ptr)
    std::unique_ptr<Tango::DevVarEncodedArray> guard(value_ptr);

    Tango::DevEncoded *buffer = value_ptr->get_buffer();
    Tango::DevEncoded &read_buffer = buffer[0];

    py_value.attr(value_attr_name) = cpp_to_python_scalar<Tango::DEV_ENCODED>::convert(read_buffer,
                                                                                       extract_as);

    if(self.get_written_dim_x() > 0) {
        if(value_ptr->length() < 2) {
            py_value.attr(w_value_attr_name) = cpp_to_python_scalar<Tango::DEV_ENCODED>::convert(read_buffer,
                                                                                                 extract_as);
        } else {
            Tango::DevEncoded &write_buffer = buffer[1];
            py_value.attr(w_value_attr_name) = cpp_to_python_scalar<Tango::DEV_ENCODED>::convert(write_buffer,
                                                                                                 extract_as);
        }
    } else {
        py_value.attr(w_value_attr_name) = py::none();
    }
}

template <>
inline void scalar_value_from_cpp_into_python<Tango::DEV_PIPE_BLOB>([[maybe_unused]] Tango::DeviceAttribute &self,
                                                                    [[maybe_unused]] py::object &py_value,
                                                                    [[maybe_unused]] PyTango::ExtractAs extract_as) {
    assert(false);
}
} // namespace PyDeviceAttribute

namespace PyWAttribute {
template <int tangoTypeConst>
inline void scalar_value_from_cpp_into_python(Tango::WAttribute &att,
                                              py::object &py_value) {
    using TangoScalarType = typename TANGO_const2type(tangoTypeConst);

    TangoScalarType value;
    att.get_write_value(value);
    py_value = cpp_to_python_scalar<tangoTypeConst>::convert(value);
}

template <>
inline void scalar_value_from_cpp_into_python<Tango::DEV_STRING>(Tango::WAttribute &att,
                                                                 py::object &py_value) {
    Tango::DevString value = nullptr;
    att.get_write_value(value);

    if(value == nullptr) {
        py_value = py::none();
    } else {
        py_value = from_cpp_str_to_pybind11_str(value);
    }
}
} // namespace PyWAttribute