ChimeraTK-ApplicationCore 04.06.00
Loading...
Searching...
No Matches
PyTransferElement.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3#pragma once
4
5#include <pybind11/pybind11.h>
6// pybind11.h must come first
7
8#include <ChimeraTK/TransferElementAbstractor.h>
9
10namespace py = pybind11;
11
12namespace ChimeraTK {
13
14 /********************************************************************************************************************/
15
17 public:
18 virtual ~PyTransferElementBase() = default;
19
20 virtual const TransferElementAbstractor& getTE() const = 0;
21 virtual TransferElementAbstractor& getTE() = 0;
22
23 static void bind(py::module& mod);
24
25 static const std::set<std::string> specialFunctionsToEmulateNumeric;
26
27 static const std::set<std::string> specialUnaryFunctionsToEmulateNumeric;
28 };
29
30 /********************************************************************************************************************/
31
32 template<class DerivedAccessor>
34 public:
35 void read() {
36 py::gil_scoped_release release;
37 visit([](auto& acc) { acc.read(); });
38 }
40 py::gil_scoped_release release;
41 return visit([&](auto& acc) { return acc.readNonBlocking(); });
42 }
43 bool readLatest() {
44 py::gil_scoped_release release;
45 return visit([&](auto& acc) { return acc.readLatest(); });
46 }
47 void write() {
48 py::gil_scoped_release release;
49 visit([](auto& acc) { acc.write(); });
50 }
52 py::gil_scoped_release release;
53 visit([](auto& acc) { acc.writeDestructively(); });
54 }
55
56 auto getName() const { return getTE().getName(); }
57 auto getUnit() const { return getTE().getUnit(); }
58 auto getDescription() const { return getTE().getDescription(); }
59 DataType getValueType() const { return getTE().getValueType(); }
60 auto getVersionNumber() const { return getTE().getVersionNumber(); }
61 auto isReadOnly() const { return getTE().isReadOnly(); }
62 auto isReadable() const { return getTE().isReadable(); }
63 auto isWriteable() const { return getTE().isWriteable(); }
64 auto getId() const { return getTE().getId(); }
65 auto dataValidity() const { return getTE().dataValidity(); }
66
67 // Note: using this function will bypass code added in ApplicationCore's ScalarAccessor/ArrayAccessor classes and
68 // instead run functions as defined in DeviceAccess. Do not use for write operations.
69 [[nodiscard]] const TransferElementAbstractor& getTE() const final {
70 const auto* self = static_cast<const DerivedAccessor*>(this);
71 TransferElementAbstractor* te;
72 std::visit([&](auto& acc) { te = &acc; }, self->_accessor);
73 return *te;
74 }
75
76 // non-const version which can be used to modify the original TransferElementAbstractor, e.g. for decoration
77 [[nodiscard]] TransferElementAbstractor& getTE() final {
78 const auto* self = static_cast<const DerivedAccessor*>(this);
79 TransferElementAbstractor* te;
80 std::visit([&](auto& acc) { te = &acc; }, self->_accessor);
81 return *te;
82 }
83
84 // Pass the actual accessor type (e.g. ScalarAccessor<int>) as argument to the given callable
85 template<typename CALLABLE>
86 auto visit(CALLABLE fn) const {
87 const auto* self = static_cast<const DerivedAccessor*>(this);
88 return std::visit(fn, self->_accessor);
89 }
90 };
91
92 /********************************************************************************************************************/
93
94 template<template<typename> class AccessorType>
96
97 /********************************************************************************************************************/
98
99} // namespace ChimeraTK
static const std::set< std::string > specialUnaryFunctionsToEmulateNumeric
virtual ~PyTransferElementBase()=default
static void bind(py::module &mod)
virtual const TransferElementAbstractor & getTE() const =0
static const std::set< std::string > specialFunctionsToEmulateNumeric
virtual TransferElementAbstractor & getTE()=0
const TransferElementAbstractor & getTE() const final
auto visit(CALLABLE fn) const
TransferElementAbstractor & getTE() final
InvalidityTracer application module.
module_ module