ChimeraTK-DeviceAccess  03.18.00
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"
6 #include "ForwardDeclarations.h"
7 #include "TransferElement.h"
9 
10 #include <boost/container/static_vector.hpp>
11 #include <boost/make_shared.hpp>
12 
13 namespace ChimeraTK {
14 
19  template<typename UserType>
20  class NDRegisterAccessor : public TransferElement {
21  public:
24  NDRegisterAccessor(std::string const& name, AccessModeFlags accessModeFlags,
25  std::string const& unit = std::string(TransferElement::unitNotSet),
26  std::string const& description = std::string())
27  : TransferElement(name, accessModeFlags, unit, description) {
31  }
32 
36  UserType& accessData(size_t sample) { return buffer_2D[0][sample]; }
37  const UserType& accessData(size_t sample) const { return buffer_2D[0][sample]; }
38 
43  UserType& accessData(unsigned int channel, unsigned int sample) { return buffer_2D[channel][sample]; }
44  const UserType& accessData(unsigned int channel, unsigned int sample) const { return buffer_2D[channel][sample]; }
45 
49  std::vector<UserType>& accessChannel(unsigned int channel) { return buffer_2D[channel]; }
50  const std::vector<UserType>& accessChannel(unsigned int channel) const { return buffer_2D[channel]; }
51 
53  std::vector<std::vector<UserType>>& accessChannels() { return buffer_2D; }
54  const std::vector<std::vector<UserType>>& accessChannels() const { return buffer_2D; }
55 
57  unsigned int getNumberOfSamples() const { return buffer_2D[0].size(); }
58 
60  unsigned int getNumberOfChannels() const { return buffer_2D.size(); }
61 
62  const std::type_info& getValueType() const override { return typeid(UserType); }
63 
64  template<typename COOKED_TYPE>
65  COOKED_TYPE getAsCooked(unsigned int channel, unsigned int sample) const;
66  DEFINE_VIRTUAL_FUNCTION_TEMPLATE_VTABLE(getAsCooked_impl, T const(unsigned int, unsigned int));
67  template<typename COOKED_TYPE>
68  COOKED_TYPE getAsCooked_impl(unsigned int channel, unsigned int sample) const;
69 
70  template<typename COOKED_TYPE>
71  void setAsCooked(unsigned int channel, unsigned int sample, COOKED_TYPE value);
72  DEFINE_VIRTUAL_FUNCTION_TEMPLATE_VTABLE(setAsCooked_impl, void(unsigned int, unsigned int, T));
73  template<typename COOKED_TYPE>
74  void setAsCooked_impl(unsigned int channel, unsigned int sample, COOKED_TYPE value);
75 
76  boost::shared_ptr<TransferElement> makeCopyRegisterDecorator() override;
77 
86  struct Buffer {
87  Buffer(size_t nChannels, size_t nElements) : value(nChannels) {
88  for(auto& channel : value) channel.resize(nElements);
89  }
90 
91  Buffer() = default;
92 
93  Buffer(Buffer&& other) noexcept
94  : value(std::move(other.value)), versionNumber(other.versionNumber), dataValidity(other.dataValidity) {}
95 
96  Buffer& operator=(Buffer&& other) noexcept {
97  value = std::move(other.value);
98  versionNumber = other.versionNumber;
99  dataValidity = other.dataValidity;
100  return *this;
101  }
102 
104  std::vector<std::vector<UserType>> value;
105 
108 
111  };
112 
113  protected:
123  std::vector<std::vector<UserType>> buffer_2D;
124  // boost::container::vector<boost::container::vector<UserType>> buffer_2D;
125 
127  friend class MultiplexedDataAccessor<UserType>;
128  friend class RegisterAccessor;
129 
132  };
133 
134  template<typename UserType>
135  template<typename COOKED_TYPE>
136  COOKED_TYPE NDRegisterAccessor<UserType>::getAsCooked(unsigned int channel, unsigned int sample) const {
137  return CALL_VIRTUAL_FUNCTION_TEMPLATE(getAsCooked_impl, COOKED_TYPE, channel, sample);
138  }
139 
140  template<typename UserType>
141  template<typename COOKED_TYPE>
142  COOKED_TYPE NDRegisterAccessor<UserType>::getAsCooked_impl(unsigned int /*channel*/, unsigned int /*sample*/) const {
143  throw ChimeraTK::logic_error("Reading as cooked is not available for this accessor");
144  }
145 
146  template<typename UserType>
147  template<typename COOKED_TYPE>
148  void NDRegisterAccessor<UserType>::setAsCooked(unsigned int channel, unsigned int sample, COOKED_TYPE value) {
149  CALL_VIRTUAL_FUNCTION_TEMPLATE(setAsCooked_impl, COOKED_TYPE, channel, sample, value);
150  }
151 
152  template<typename UserType>
153  template<typename COOKED_TYPE>
155  unsigned int /*channel*/, unsigned int /*sample*/, COOKED_TYPE /*value*/) {
156  throw ChimeraTK::logic_error("Setting as cooked is not available for this accessor");
157  }
158 
160 
161 } /* namespace ChimeraTK */
ChimeraTK::NDRegisterAccessor::accessChannel
const std::vector< UserType > & accessChannel(unsigned int channel) const
Definition: NDRegisterAccessor.h:50
ChimeraTK::NDRegisterAccessor::getNumberOfChannels
unsigned int getNumberOfChannels() const
Return number of channels.
Definition: NDRegisterAccessor.h:60
ChimeraTK::NDRegisterAccessor::Buffer::versionNumber
ChimeraTK::VersionNumber versionNumber
Version number of this data.
Definition: NDRegisterAccessor.h:107
ForwardDeclarations.h
ChimeraTK::NDRegisterAccessor::Buffer::Buffer
Buffer(Buffer &&other) noexcept
Definition: NDRegisterAccessor.h:93
ChimeraTK::NDRegisterAccessor::DEFINE_VIRTUAL_FUNCTION_TEMPLATE_VTABLE
DEFINE_VIRTUAL_FUNCTION_TEMPLATE_VTABLE(getAsCooked_impl, T const(unsigned int, unsigned int))
ChimeraTK::NDRegisterAccessor::setAsCooked_impl
void setAsCooked_impl(unsigned int channel, unsigned int sample, COOKED_TYPE value)
Definition: NDRegisterAccessor.h:154
ChimeraTK::NDRegisterAccessor::DEFINE_VIRTUAL_FUNCTION_TEMPLATE_VTABLE_FILLER
DEFINE_VIRTUAL_FUNCTION_TEMPLATE_VTABLE_FILLER(NDRegisterAccessor< UserType >, getAsCooked_impl, 2)
ChimeraTK::NDRegisterAccessor::getAsCooked
COOKED_TYPE getAsCooked(unsigned int channel, unsigned int sample) const
Definition: NDRegisterAccessor.h:136
ChimeraTK::NDRegisterAccessor::getNumberOfSamples
unsigned int getNumberOfSamples() const
Return number of elements per channel.
Definition: NDRegisterAccessor.h:57
TransferElement.h
VirtualFunctionTemplate.h
ChimeraTK::NDRegisterAccessor::accessChannels
const std::vector< std::vector< UserType > > & accessChannels() const
Definition: NDRegisterAccessor.h:54
FILL_VIRTUAL_FUNCTION_TEMPLATE_VTABLE
#define FILL_VIRTUAL_FUNCTION_TEMPLATE_VTABLE(functionName)
Fill the vtable of a virtual function template defined with DEFINE_VIRTUAL_FUNCTION_TEMPLATE.
Definition: VirtualFunctionTemplate.h:84
ChimeraTK::NDRegisterAccessor::buffer_2D
std::vector< std::vector< UserType > > buffer_2D
Buffer of converted data elements.
Definition: NDRegisterAccessor.h:123
ChimeraTK::NDRegisterAccessor::NDRegisterAccessor
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).
Definition: NDRegisterAccessor.h:24
ChimeraTK::NDRegisterAccessor::accessData
const UserType & accessData(size_t sample) const
Definition: NDRegisterAccessor.h:37
ChimeraTK::NDRegisterAccessor::accessData
UserType & accessData(unsigned int channel, unsigned int sample)
Get or set register accessor's buffer content (2D version).
Definition: NDRegisterAccessor.h:43
ChimeraTK::NDRegisterAccessor::accessData
const UserType & accessData(unsigned int channel, unsigned int sample) const
Definition: NDRegisterAccessor.h:44
ChimeraTK::NDRegisterAccessor::accessChannels
std::vector< std::vector< UserType > > & accessChannels()
Get or set register accessor's 2D channel vector.
Definition: NDRegisterAccessor.h:53
ChimeraTK::TransferElement::unitNotSet
static constexpr char unitNotSet[]
Constant string to be used as a unit when the unit is not provided or known.
Definition: TransferElement.h:742
ChimeraTK::NDRegisterAccessor::setAsCooked
void setAsCooked(unsigned int channel, unsigned int sample, COOKED_TYPE value)
Definition: NDRegisterAccessor.h:148
ChimeraTK::NDRegisterAccessor::Buffer::operator=
Buffer & operator=(Buffer &&other) noexcept
Definition: NDRegisterAccessor.h:96
ChimeraTK::DataValidity::ok
@ ok
ChimeraTK::DataValidity
DataValidity
The current state of the data.
Definition: TransferElement.h:41
ChimeraTK::NDRegisterAccessor::makeCopyRegisterDecorator
boost::shared_ptr< TransferElement > makeCopyRegisterDecorator() override
Definition: NDRegisterAccessor.cc:11
ChimeraTK::NDRegisterAccessor::RegisterAccessor
friend class RegisterAccessor
Definition: NDRegisterAccessor.h:128
ChimeraTK::NDRegisterAccessor::getValueType
const std::type_info & getValueType() const override
Definition: NDRegisterAccessor.h:62
ChimeraTK::TransferElement::makeUniqueId
void makeUniqueId()
Allow generating a unique ID from derived classes.
Definition: TransferElement.h:816
ChimeraTK::TransferElement
Base class for register accessors which can be part of a TransferGroup.
Definition: TransferElement.h:67
ChimeraTK::NDRegisterAccessor::Buffer::Buffer
Buffer()=default
ChimeraTK::NDRegisterAccessor::getAsCooked_impl
COOKED_TYPE getAsCooked_impl(unsigned int channel, unsigned int sample) const
Definition: NDRegisterAccessor.h:142
ChimeraTK::VersionNumber
Class for generating and holding version numbers without exposing a numeric representation.
Definition: VersionNumber.h:23
ChimeraTK::NDRegisterAccessor::Buffer::value
std::vector< std::vector< UserType > > value
The actual data contained in this buffer.
Definition: NDRegisterAccessor.h:104
CALL_VIRTUAL_FUNCTION_TEMPLATE
#define CALL_VIRTUAL_FUNCTION_TEMPLATE(functionName, templateArgument,...)
Execute the virtual function template call using the vtable defined with the DEFINE_VIRTUAL_FUNCTION_...
Definition: VirtualFunctionTemplate.h:70
ChimeraTK::AccessModeFlags
Set of AccessMode flags with additional functionality for an easier handling.
Definition: AccessMode.h:48
Exception.h
ChimeraTK::NDRegisterAccessor::Buffer
Data type to create individual buffers.
Definition: NDRegisterAccessor.h:86
ChimeraTK::NDRegisterAccessor::Buffer::Buffer
Buffer(size_t nChannels, size_t nElements)
Definition: NDRegisterAccessor.h:87
ChimeraTK::DECLARE_TEMPLATE_FOR_CHIMERATK_USER_TYPES
DECLARE_TEMPLATE_FOR_CHIMERATK_USER_TYPES(DummyInterruptTriggerAccessor)
ChimeraTK::NDRegisterAccessor::Buffer::dataValidity
ChimeraTK::DataValidity dataValidity
Whether or not the data in the buffer is considered valid.
Definition: NDRegisterAccessor.h:110
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::MultiplexedDataAccessor
Definition: ForwardDeclarations.h:23
ChimeraTK::NDRegisterAccessor
N-dimensional register accessor.
Definition: ForwardDeclarations.h:17
ChimeraTK::NDRegisterAccessor::accessChannel
std::vector< UserType > & accessChannel(unsigned int channel)
Get or set register accessor's channel vector.
Definition: NDRegisterAccessor.h:49
ChimeraTK::logic_error
Exception thrown when a logic error has occured.
Definition: Exception.h:51
ChimeraTK::NDRegisterAccessor::accessData
UserType & accessData(size_t sample)
Get or set register accessor's buffer content (1D version).
Definition: NDRegisterAccessor.h:36