ChimeraTK-DeviceAccess 03.25.00
Loading...
Searching...
No Matches
NDRegisterAccessor.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 "Exception.h"
7#include "TransferElement.h"
9
10#include <boost/container/static_vector.hpp>
11#include <boost/make_shared.hpp>
12
13namespace ChimeraTK {
14
20 template<user_type UserType>
22 public:
25 NDRegisterAccessor(std::string const& name, AccessModeFlags accessModeFlags,
26 std::string const& unit = std::string(TransferElement::unitNotSet),
27 std::string const& description = std::string())
28 : TransferElement(name, accessModeFlags, unit, description) {
32 }
33
37 UserType& accessData(size_t sample) { return buffer_2D[0][sample]; }
38 const UserType& accessData(size_t sample) const { return buffer_2D[0][sample]; }
39
44 UserType& accessData(unsigned int channel, unsigned int sample) { return buffer_2D[channel][sample]; }
45 const UserType& accessData(unsigned int channel, unsigned int sample) const { return buffer_2D[channel][sample]; }
46
50 std::vector<UserType>& accessChannel(unsigned int channel) { return buffer_2D[channel]; }
51 const std::vector<UserType>& accessChannel(unsigned int channel) const { return buffer_2D[channel]; }
52
54 std::vector<std::vector<UserType>>& accessChannels() { return buffer_2D; }
55 const std::vector<std::vector<UserType>>& accessChannels() const { return buffer_2D; }
56
58 unsigned int getNumberOfSamples() const { return buffer_2D[0].size(); }
59
61 unsigned int getNumberOfChannels() const { return buffer_2D.size(); }
62
63 const std::type_info& getValueType() const override { return typeid(UserType); }
64
65 template<typename COOKED_TYPE>
66 COOKED_TYPE getAsCooked(unsigned int channel, unsigned int sample) const;
67 DEFINE_VIRTUAL_FUNCTION_TEMPLATE_VTABLE(getAsCooked_impl, T const(unsigned int, unsigned int));
68 template<typename COOKED_TYPE>
69 COOKED_TYPE getAsCooked_impl(unsigned int channel, unsigned int sample) const;
70
71 template<typename COOKED_TYPE>
72 void setAsCooked(unsigned int channel, unsigned int sample, COOKED_TYPE value);
73 DEFINE_VIRTUAL_FUNCTION_TEMPLATE_VTABLE(setAsCooked_impl, void(unsigned int, unsigned int, T));
74 template<typename COOKED_TYPE>
75 void setAsCooked_impl(unsigned int channel, unsigned int sample, COOKED_TYPE value);
76
77 boost::shared_ptr<TransferElement> makeCopyRegisterDecorator() override;
78
95 [[nodiscard]] virtual boost::shared_ptr<NDRegisterAccessor<UserType>> decorateDeepInside(
96 [[maybe_unused]] std::function<boost::shared_ptr<NDRegisterAccessor<UserType>>(
97 const boost::shared_ptr<NDRegisterAccessor<UserType>>&)> factory) {
98 return {};
99 }
100
109 struct Buffer {
110 Buffer(size_t nChannels, size_t nElements) : value(nChannels) {
111 for(auto& channel : value) channel.resize(nElements);
112 }
113
114 Buffer() = default;
115
116 Buffer(Buffer&& other) noexcept
117 : value(std::move(other.value)), versionNumber(other.versionNumber), dataValidity(other.dataValidity) {}
118
119 Buffer& operator=(Buffer&& other) noexcept {
120 value = std::move(other.value);
121 versionNumber = other.versionNumber;
122 dataValidity = other.dataValidity;
123 return *this;
124 }
125
127 std::vector<std::vector<UserType>> value;
128
131
134 };
135
136 protected:
146 std::vector<std::vector<UserType>> buffer_2D;
147 // boost::container::vector<boost::container::vector<UserType>> buffer_2D;
148
150 friend class RegisterAccessor;
151
154 };
155
156 template<user_type UserType>
157 template<typename COOKED_TYPE>
158 COOKED_TYPE NDRegisterAccessor<UserType>::getAsCooked(unsigned int channel, unsigned int sample) const {
159 return CALL_VIRTUAL_FUNCTION_TEMPLATE(getAsCooked_impl, COOKED_TYPE, channel, sample);
160 }
161
162 template<user_type UserType>
163 template<typename COOKED_TYPE>
164 COOKED_TYPE NDRegisterAccessor<UserType>::getAsCooked_impl(unsigned int /*channel*/, unsigned int /*sample*/) const {
165 throw ChimeraTK::logic_error("Reading as cooked is not available for this accessor");
166 }
167
168 template<user_type UserType>
169 template<typename COOKED_TYPE>
170 void NDRegisterAccessor<UserType>::setAsCooked(unsigned int channel, unsigned int sample, COOKED_TYPE value) {
171 CALL_VIRTUAL_FUNCTION_TEMPLATE(setAsCooked_impl, COOKED_TYPE, channel, sample, value);
172 }
173
174 template<user_type UserType>
175 template<typename COOKED_TYPE>
177 unsigned int /*channel*/, unsigned int /*sample*/, COOKED_TYPE /*value*/) {
178 throw ChimeraTK::logic_error("Setting as cooked is not available for this accessor");
179 }
180
182
183} /* namespace ChimeraTK */
#define DECLARE_TEMPLATE_FOR_CHIMERATK_USER_TYPES(TemplateClass)
#define CALL_VIRTUAL_FUNCTION_TEMPLATE(functionName, templateArgument,...)
Execute the virtual function template call using the vtable defined with the DEFINE_VIRTUAL_FUNCTION_...
#define FILL_VIRTUAL_FUNCTION_TEMPLATE_VTABLE(functionName)
Fill the vtable of a virtual function template defined with DEFINE_VIRTUAL_FUNCTION_TEMPLATE.
Set of AccessMode flags with additional functionality for an easier handling.
Definition AccessMode.h:48
N-dimensional register accessor.
const std::type_info & getValueType() const override
Returns the std::type_info for the value type of this transfer element.
COOKED_TYPE getAsCooked_impl(unsigned int channel, unsigned int sample) const
DEFINE_VIRTUAL_FUNCTION_TEMPLATE_VTABLE(setAsCooked_impl, void(unsigned int, unsigned int, T))
NDRegisterAccessor(std::string const &name, AccessModeFlags accessModeFlags, std::string const &unit=std::string(TransferElement::unitNotSet), std::string const &description=std::string())
Creates an NDRegisterAccessor with the specified name (passed on to the transfer element).
friend class RegisterAccessor
the compatibility layers need access to the buffer_2D
unsigned int getNumberOfSamples() const
Return number of elements per channel.
DEFINE_VIRTUAL_FUNCTION_TEMPLATE_VTABLE_FILLER(NDRegisterAccessor< UserType >, setAsCooked_impl, 3)
UserType & accessData(unsigned int channel, unsigned int sample)
Get or set register accessor's buffer content (2D version).
const std::vector< std::vector< UserType > > & accessChannels() const
virtual boost::shared_ptr< NDRegisterAccessor< UserType > > decorateDeepInside(std::function< boost::shared_ptr< NDRegisterAccessor< UserType > >(const boost::shared_ptr< NDRegisterAccessor< UserType > > &)> factory)
Decorate the innermost TransferElement of the stack of decorators or decorator-like accessors.
std::vector< UserType > & accessChannel(unsigned int channel)
Get or set register accessor's channel vector.
std::vector< std::vector< UserType > > buffer_2D
Buffer of converted data elements.
std::vector< std::vector< UserType > > & accessChannels()
Get or set register accessor's 2D channel vector.
UserType & accessData(size_t sample)
Get or set register accessor's buffer content (1D version).
DEFINE_VIRTUAL_FUNCTION_TEMPLATE_VTABLE_FILLER(NDRegisterAccessor< UserType >, getAsCooked_impl, 2)
void setAsCooked(unsigned int channel, unsigned int sample, COOKED_TYPE value)
void setAsCooked_impl(unsigned int channel, unsigned int sample, COOKED_TYPE value)
boost::shared_ptr< TransferElement > makeCopyRegisterDecorator() override
Create a CopyRegisterDecorator of the right type decorating this TransferElement.
COOKED_TYPE getAsCooked(unsigned int channel, unsigned int sample) const
const std::vector< UserType > & accessChannel(unsigned int channel) const
const UserType & accessData(unsigned int channel, unsigned int sample) const
DEFINE_VIRTUAL_FUNCTION_TEMPLATE_VTABLE(getAsCooked_impl, T const(unsigned int, unsigned int))
unsigned int getNumberOfChannels() const
Return number of channels.
const UserType & accessData(size_t sample) const
Base class for register accessors which can be part of a TransferGroup.
void makeUniqueId()
Allow generating a unique ID from derived classes.
static constexpr char unitNotSet[]
Constant string to be used as a unit when the unit is not provided or known.
Class for generating and holding version numbers without exposing a numeric representation.
Exception thrown when a logic error has occured.
Definition Exception.h:51
DataValidity
The current state of the data.
Data type to create individual buffers.
Buffer & operator=(Buffer &&other) noexcept
std::vector< std::vector< UserType > > value
The actual data contained in this buffer.
ChimeraTK::VersionNumber versionNumber
Version number of this data.
ChimeraTK::DataValidity dataValidity
Whether or not the data in the buffer is considered valid.
Buffer(size_t nChannels, size_t nElements)