117 size_t start, stop, step, length;
118 if(!slice.compute(acc.getNElements(), &start, &stop, &step, &length)) {
119 throw pybind11::error_already_set();
122 auto value = userTypeToUserType<
typename std::remove_reference<
decltype(acc)>::type::value_type>(v);
123 for(
size_t i = start; i < stop; i += step) {
136 return "<ArrayAccessor(not initialized)>";
139 std::string rep{
"<ArrayAccessor(type="};
140 rep.append(py::cast<py::object>(py::cast(&acc).attr(
"getValueType")()).attr(
"__repr__")().cast<std::string>());
141 rep.append(
", name=");
142 rep.append(py::cast(&acc).attr(
"getName")().cast<std::string>());
143 rep.append(
", data=");
144 rep.append(py::cast<py::object>(py::cast(&acc).attr(
"__str__")()).cast<std::string>());
145 rep.append(
", versionNumber=");
146 rep.append(py::cast<py::object>(py::cast(&acc).attr(
"getVersionNumber")()).attr(
"__repr__")().cast<std::string>());
147 rep.append(
", dataValidity=");
148 rep.append(py::cast<py::object>(py::cast(&acc).attr(
"dataValidity")()).attr(
"__repr__")().cast<std::string>());
155 py::buffer_info info;
158 using ACC =
typename std::remove_reference<
decltype(acc)>::type;
159 using userType =
typename ACC::value_type;
160 auto ndacc = boost::dynamic_pointer_cast<NDRegisterAccessor<userType>>(acc.getHighLevelImplElement());
161 if constexpr(std::is_same<userType, ChimeraTK::Boolean>::value) {
162 info.format = py::format_descriptor<bool>::format();
164 else if constexpr(std::is_same<userType, std::string>::value) {
169 info.format = py::format_descriptor<userType>::format();
171 info.ptr = ndacc->accessChannel(0).data();
172 info.itemsize =
sizeof(userType);
174 info.shape = {acc.getNElements()};
175 info.strides = {
sizeof(userType)};
188 py::class_<PyArrayAccessor, PyTransferElementBase, std::unique_ptr<PyArrayAccessor, py::nodelete>> arrayacc(
189 m,
"ArrayAccessor", py::buffer_protocol());
190 arrayacc.def(py::init<>())
193 "Read the data from the device.\n\nIf AccessMode::wait_for_new_data was set, this function will block "
194 "until new data has arrived. Otherwise it still might block for a short time until the data transfer was "
197 "Read the next value, if available in the input buffer.\n\nIf AccessMode::wait_for_new_data was set, this "
198 "function returns immediately and the return value indicated if a new value was available (true) or not "
199 "(false).\n\nIf AccessMode::wait_for_new_data was not set, this function is identical to read(), which "
200 "will still return quickly. Depending on the actual transfer implementation, the backend might need to "
201 "transfer data to obtain the current value before returning. Also this function is not guaranteed to be "
202 "lock free. The return value will be always true in this mode.")
204 "Read the latest value, discarding any other update since the last read if present.\n\nOtherwise this "
205 "function is identical to readNonBlocking(), i.e. it will never wait for new values and it will return "
206 "whether a new value was available if AccessMode::wait_for_new_data is set.")
208 "Write the data to device.\n\nThe return value is true, old data was lost on the write transfer (e.g. due "
209 "to an buffer overflow). In case of an unbuffered write transfer, the return value will always be false.")
211 "Just like write(), but allows the implementation to destroy the content of the user buffer in the "
212 "process.\n\nThis is an optional optimisation, hence there is a default implementation which just calls "
213 "the normal doWriteTransfer(). In any case, the application must expect the user buffer of the "
214 "TransferElement to contain undefined data after calling this function.")
217 "Returns the engineering unit.\n\nIf none was specified, it will default to ' n./ a.'")
220 "Returns the std::type_info for the value type of this transfer element.\n\nThis can be used to determine "
221 "the type at runtime.")
223 "Returns the version number that is associated with the last transfer (i.e. last read or write)")
225 "Check if transfer element is read only, i.e. it is readable but not writeable.")
229 "Obtain unique ID for the actual implementation of this TransferElement.\n\nThis means that e.g. two "
230 "instances of ScalarRegisterAccessor created by the same call to Device::getScalarRegisterAccessor() (e.g. "
231 "by copying the accessor to another using NDRegisterAccessorBridge::replace()) will have the same ID, "
232 "while two instances obtained by to difference calls to Device::getScalarRegisterAccessor() will have a "
233 "different ID even when accessing the very same register.")
235 "Return current validity of the data.\n\nWill always return DataValidity.ok if the backend does not "
239 .def(
"set", &
PyArrayAccessor::set,
"Set the values of the array of UserType.", py::arg(
"newValue"))
241 "Convenience function to set and write new value.\n\nThe given version number. If versionNumber == {}, a "
242 "new version number is generated.",
252 arrayacc.def(fn.c_str(),
255 fn.c_str(), [fn](
PyArrayAccessor& acc, py::object& other) {
return acc.
get().attr(fn.c_str())(other); });
258 arrayacc.def(fn.c_str(), [fn](
PyArrayAccessor& acc) {
return acc.get().attr(fn.c_str())(); });
266 [](ChimeraTK::DataType type,
VariableGroup& owner,
const std::string& name,
const std::string& unit,
267 size_t nElements,
const std::string& description) {
268 return dynamic_cast<PyOwningObject&
>(owner).make_child<PyArrayAccessor>(
271 py::return_value_policy::reference,
"");
278 [](ChimeraTK::DataType type,
VariableGroup& owner,
const std::string& name,
const std::string& unit,
279 size_t nElements,
const std::string& description) {
280 return dynamic_cast<PyOwningObject&
>(owner).make_child<PyArrayAccessor>(
283 py::return_value_policy::reference,
"");
290 [](ChimeraTK::DataType type,
VariableGroup& owner,
const std::string& name,
const std::string& unit,
291 size_t nElements,
const std::string& description) {
292 return dynamic_cast<PyOwningObject&
>(owner).make_child<PyArrayAccessor>(
295 py::return_value_policy::reference,
"");
302 [](ChimeraTK::DataType type,
VariableGroup& owner,
const std::string& name,
const std::string& unit,
303 size_t nElements,
const std::string& description) {
304 return dynamic_cast<PyOwningObject&
>(owner).make_child<PyArrayAccessor>(
307 py::return_value_policy::reference,
"");
314 [](ChimeraTK::DataType type,
VariableGroup& owner,
const std::string& name,
const std::string& unit,
315 size_t nElements,
const std::string& description) {
316 return dynamic_cast<PyOwningObject&
>(owner).make_child<PyArrayAccessor>(
319 py::return_value_policy::reference,
"");
325 "ArrayOutputReverseRecovery",
326 [](ChimeraTK::DataType type,
VariableGroup& owner,
const std::string& name,
const std::string& unit,
327 size_t nElements,
const std::string& description) {
328 return dynamic_cast<PyOwningObject&
>(owner).make_child<PyArrayAccessor>(
331 py::return_value_policy::reference,
"");