Repository navigation
Expand file tree
/
Copy pathPythonInterface.cxx
More file actions
173 lines (143 loc) · 5.66 KB
/
Copy pathPythonInterface.cxx
File metadata and controls
173 lines (143 loc) · 5.66 KB
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
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file PythonInterface.cxx
/// \brief Python interface for the Slow Control library
///
/// \author Kostas Alexopoulos ([email protected])
#include <boost/python.hpp>
#include "IcPythonInterface.h"
#include "ScaPythonInterface.h"
#include "SwtPythonInterface.h"
namespace o2
{
namespace alf
{
namespace python
{
namespace bp = boost::python;
struct pairConverter {
template <typename T1, typename T2>
pairConverter&
fromPython()
{
bp::converter::registry::push_back(
&pairConverter::convertible,
&pairConverter::construct<T1, T2>,
bp::type_id<std::pair<T1, T2>>());
return *this;
}
static void* convertible(PyObject* obj)
{
if (!PyTuple_CheckExact(obj))
return 0;
if (PyTuple_Size(obj) != 2)
return 0;
return obj;
}
template <typename T1, typename T2>
static void construct(PyObject* obj, bp::converter::rvalue_from_python_stage1_data* data)
{
bp::tuple tuple(bp::borrowed(obj));
void* storage = ((bp::converter::rvalue_from_python_storage<std::pair<T1, T2>>*)data)->storage.bytes;
// GCC 15 emits a false-positive -Wmaybe-uninitialized when the std::string
// materialized from boost::python::extract<> is copied into the std::pair.
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
new (storage) std::pair<T1, T2>(bp::extract<T1>(tuple[0]), bp::extract<T2>(tuple[1]));
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
data->convertible = storage;
}
};
struct iterableConverter {
template <typename T>
iterableConverter&
fromPython()
{
bp::converter::registry::push_back(
&iterableConverter::convertible,
&iterableConverter::construct<T>,
bp::type_id<T>());
return *this;
}
static void* convertible(PyObject* object)
{
return PyObject_GetIter(object) ? object : NULL;
}
template <typename T>
static void construct(
PyObject* object,
bp::converter::rvalue_from_python_stage1_data* data)
{
bp::handle<> handle(bp::borrowed(object));
typedef bp::converter::rvalue_from_python_storage<T> storage_type;
void* storage = reinterpret_cast<storage_type*>(data)->storage.bytes;
typedef bp::stl_input_iterator<typename T::value_type> iterator;
new (storage) T(
iterator(bp::object(handle)),
iterator());
data->convertible = storage;
}
};
BOOST_PYTHON_MODULE(libO2Alf)
{
//PyEval_InitThreads(); // enable boost::python multi-threading
bp::to_python_converter<std::vector<std::pair<Ic::Operation, Ic::Data>>, IcOperationDataVectorToPythonListOfTuples>();
bp::to_python_converter<Ic::Data, IcDataToPython>();
bp::to_python_converter<std::vector<std::pair<Sca::Operation, Sca::Data>>, ScaOperationDataVectorToPythonListOfTuples>();
bp::to_python_converter<Sca::Data, ScaDataToPython>();
bp::to_python_converter<std::vector<SwtWord>, SwtWordVectorToPythonList>();
bp::to_python_converter<std::vector<std::pair<Swt::Operation, Swt::Data>>, SwtOperationDataVectorToPythonListOfTuples>();
bp::to_python_converter<Swt::Data, SwtDataToPython>();
pairConverter()
.fromPython<uint32_t, uint32_t>()
.fromPython<std::string, uint32_t>();
IcArgsVariantConverter()
.fromPython();
ScaArgsVariantConverter()
.fromPython();
SwtArgsVariantConverter()
.fromPython();
iterableConverter()
.fromPython<std::vector<IcArgsVariant>>()
.fromPython<std::vector<ScaArgsVariant>>()
.fromPython<std::vector<SwtArgsVariant>>();
bp::class_<SwtInterface>("SwtInterface", bp::init<std::string, int>(sSwtInitDoc))
.def("set_channel", &SwtInterface::setChannel, sSwtSetChannelDoc)
.def("sc_reset", &SwtInterface::scReset, sSwtScResetDoc)
.def("write", &SwtInterface::write, sSwtWriteDoc)
.def("read", &SwtInterface::read, sSwtReadDoc)
.def("read", &SwtInterface::readDefault, sSwtReadDoc)
.def("sequence", &SwtInterface::sequence, sSwtSequenceDoc)
.def("sequence", &SwtInterface::sequenceDefault, sSwtSequenceDoc);
bp::class_<ScaInterface>("ScaInterface", bp::init<std::string, int>(sScaInitDoc))
.def("set_channel", &ScaInterface::setChannel, sScaSetChannelDoc)
.def("sc_reset", &ScaInterface::scReset, sScaScResetDoc)
.def("svl_reset", &ScaInterface::svlReset, sScaSvlResetDoc)
.def("svl_connect", &ScaInterface::svlConnect, sScaSvlConnectDoc)
.def("execute_command", &ScaInterface::executeCommand, sScaExecuteCommandDoc)
.def("sequence", &ScaInterface::sequence, sScaSequenceDoc)
.def("sequence", &ScaInterface::sequenceDefault, sScaSequenceDoc);
bp::class_<IcInterface>("IcInterface", bp::init<std::string, int>(sIcInitDoc))
.def("set_channel", &IcInterface::setChannel, sIcSetChannelDoc)
.def("sc_reset", &IcInterface::scReset, sIcScResetDoc)
.def("read", &IcInterface::read, sIcReadDoc)
.def("write", &IcInterface::write, sIcWriteDoc)
.def("write_gbt_i2c", &IcInterface::writeGbtI2c, sIcWriteGbtI2cDoc)
.def("sequence", &IcInterface::sequence, sIcSequenceDoc)
.def("sequence", &IcInterface::sequenceDefault, sIcSequenceDoc);
}
} // namespace python
} // namespace alf
} // namespace o2