ChimeraTK-ApplicationCore 04.06.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 "ArrayAccessor.h"
10#include "PyTransferElement.h"
11
12#include <ChimeraTK/VariantUserTypes.h>
13
14#include <pybind11/numpy.h>
15
16namespace py = pybind11;
17
18namespace ChimeraTK {
19
20 /********************************************************************************************************************/
21
25 class PyArrayAccessor : public PyTransferElement<PyArrayAccessor>, public PyOwnedObject {
26 // Helper for constructor - note: we can move templates to the .cc file if we use them only in the same .cc file
27 template<template<typename> class AccessorType>
28 static UserTypeTemplateVariantNoVoid<ArrayAccessor> createAccessor(ChimeraTK::DataType type, Module* owner,
29 const std::string& name, std::string unit, size_t nElements, const std::string& description,
30 const std::unordered_set<std::string>& tags);
31
32 public:
36
37 template<template<typename> class AccessorType>
38 PyArrayAccessor(AccessorTypeTag<AccessorType>, ChimeraTK::DataType type, Module* owner, const std::string& name,
39 std::string unit, size_t nElements, const std::string& description,
40 const std::unordered_set<std::string>& tags = {})
41 : _accessor(createAccessor<AccessorType>(type, owner, name, unit, nElements, description, tags)) {}
42
43 // UserTypeTemplateVariantNoVoid expects a single template argument, std::vector has multiple (with defaults)...
44 template<typename T>
45 using Vector = std::vector<T>;
46
47 py::object readAndGet();
48
49 void setAndWrite(const UserTypeTemplateVariantNoVoid<Vector>& vec);
50
51 size_t getNElements();
52
53 void set(const UserTypeTemplateVariantNoVoid<Vector>& vec);
54
55 py::object get() const;
56
57 py::object getitem(size_t index) const;
58
59 void setitem(size_t index, const UserTypeVariantNoVoid& val);
60
61 void setslice(const py::slice& slice, const UserTypeVariantNoVoid& val);
62
63 static std::string repr(py::object& acc);
64
65 py::buffer_info getBufferInfo();
66
67 py::object getattr(const std::string& name) const { return get().attr(name.c_str()); }
68
69 static void bind(py::module& mod);
70
71 mutable UserTypeTemplateVariantNoVoid<ArrayAccessor> _accessor;
72 };
73
74 /********************************************************************************************************************/
75
76} // namespace ChimeraTK
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)
UserTypeTemplateVariantNoVoid< ArrayAccessor > _accessor
py::buffer_info getBufferInfo()
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.
module_ module
Convenience class for output array accessors (always UpdateMode::push)