ChimeraTK-ApplicationCore 04.08.00
Loading...
Searching...
No Matches
PyArrayAccessor.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 "AccessorVariant.h"
9#include "ArrayAccessor.h"
11#include "PyTransferElement.h"
12
13#include <pybind11/numpy.h>
14
15namespace py = pybind11;
16
17namespace ChimeraTK {
18
19 /********************************************************************************************************************/
20
24 class PyArrayAccessor : public PyTransferElement<PyArrayAccessor>, public PyOwnedObject {
25 // Helper for constructor - note: we can move templates to the .cc file if we use them only in the same .cc file
26 template<template<typename> class AccessorType>
27 static ArrayAccessorVariant createAccessor(ChimeraTK::DataType type, Module* owner, const std::string& name,
28 std::string unit, size_t nElements, const std::string& description,
29 const std::unordered_set<std::string>& tags);
30
31 public:
35
36 template<template<typename> class AccessorType>
37 PyArrayAccessor(AccessorTypeTag<AccessorType>, ChimeraTK::DataType type, Module* owner, const std::string& name,
38 std::string unit, size_t nElements, const std::string& description,
39 const std::unordered_set<std::string>& tags = {})
40 : _accessor(createAccessor<AccessorType>(type, owner, name, unit, nElements, description, tags)) {}
41
42 // UserTypeTemplateVariantNoVoid expects a single template argument, std::vector has multiple (with defaults)...
43 template<typename T>
44 using Vector = std::vector<T>;
45
46 py::object readAndGet();
47
48 void setAndWrite(const UserTypeTemplateVariantNoVoid<Vector>& vec);
49
50 size_t getNElements();
51
52 void set(const UserTypeTemplateVariantNoVoid<Vector>& vec);
53
54 py::object get() const;
55
56 py::object getitem(size_t index) const;
57
58 void setitem(size_t index, const UserTypeVariantNoVoid& val);
59
60 void setslice(const py::slice& slice, const UserTypeVariantNoVoid& val);
61
62 static std::string repr(py::object& acc);
63
64 py::buffer_info getBufferInfo();
65
66 py::object getattr(const std::string& name) const { return get().attr(name.c_str()); }
67
68 static void bind(py::module& mod);
69
71 };
72
73 /********************************************************************************************************************/
74
75} // namespace ChimeraTK
Convenience class for output array accessors (always UpdateMode::push)
Base class for ApplicationModule and DeviceModule, to have a common interface for these module types.
Definition Module.h:21
Helper class acting as a ArrayAccessor with a variant UserType.
py::object getitem(size_t index) const
void set(const UserTypeTemplateVariantNoVoid< Vector > &vec)
static void bind(py::module &mod)
void setAndWrite(const UserTypeTemplateVariantNoVoid< Vector > &vec)
void setslice(const py::slice &slice, const UserTypeVariantNoVoid &val)
static std::string repr(py::object &acc)
PyArrayAccessor(PyArrayAccessor &&)=default
void setitem(size_t index, const UserTypeVariantNoVoid &val)
py::buffer_info getBufferInfo()
ArrayAccessorVariant _accessor
py::object getattr(const std::string &name) const
PyArrayAccessor(AccessorTypeTag< AccessorType >, ChimeraTK::DataType type, Module *owner, const std::string &name, std::string unit, size_t nElements, const std::string &description, const std::unordered_set< std::string > &tags={})
Base class used for all objects in the Python world which can be owned by another object.
InvalidityTracer application module.
typename detail::NestedVariant< UserTypeTemplateVariantNoVoid< ArrayAccessor >, UserTypeTemplateVariantNoVoid< ArrayPollInput >, UserTypeTemplateVariantNoVoid< ArrayPushInput >, UserTypeTemplateVariantNoVoid< ArrayOutput >, UserTypeTemplateVariantNoVoid< ArrayPushInputWB >, UserTypeTemplateVariantNoVoid< ArrayOutputPushRB >, UserTypeTemplateVariantNoVoid< ArrayOutputReverseRecovery >, UserTypeTemplateVariantNoVoid< ArrayPushInputNoInitialValue >, UserTypeTemplateVariantNoVoid< ArrayPollInputNoInitialValue > >::type ArrayAccessorVariant
All concrete array accessor types used by the Python bindings.
module_ module