10 template<
typename UserType,
bool isRaw>
12 const boost::shared_ptr<DeviceBackend>& dev,
const RegisterPath& registerPathName,
size_t numberOfWords,
20 _dev = boost::dynamic_pointer_cast<NumericAddressedBackend>(dev);
22 throw ChimeraTK::logic_error(
"NumericAddressedBackendRegisterAccessor is used with a backend which is not "
23 "a NumericAddressedBackend.");
49 if(numberOfWords == 0) {
52 if(numberOfWords > 1) {
54 "Requested number of words is larger than 1 in VOID register '" + registerPathName +
"'!");
56 if(wordOffsetInRegister > 0) {
61 if(numberOfWords == 0) {
72 registerPathName +
"'!");
82 _rawAccessor = boost::make_shared<NumericAddressedLowLevelTransferElement>(
89 if constexpr(!isRaw) {
91 RawConverter::ConverterLoopHelper::makeConverterLoopHelper<UserType>(
_registerInfo, 0, 0, *
this);
101 throw ChimeraTK::logic_error(
"Given UserType when obtaining the NumericAddressedBackendRegisterAccessor in "
102 "raw mode does not match the expected type. Use an " +
104 " instead! (Register name: " + registerPathName +
"')");
114 template<
typename UserType,
bool isRaw>
116 _rawAccessor->readTransfer();
121 template<
typename UserType,
bool isRaw>
125 _rawAccessor->writeTransfer(versionNumber);
131 template<
typename UserType,
bool isRaw>
133 if(!_dev->isOpen()) {
138 _rawAccessor->setActiveException(this->_activeException);
139 _rawAccessor->postRead(type, hasNewData);
148 buffer_2D[0].swap(_readRegistration.staging());
153 this->_versionNumber = _rawAccessor->getVersionNumber();
154 this->_dataValidity = _rawAccessor->dataValidity();
159 template<
typename UserType,
bool isRaw>
164 static_assert(std::is_same_v<UserType, CookedType>);
165 if constexpr(!isRaw) {
166 if constexpr(!std::is_same_v<RawType, ChimeraTK::Void>) {
167 auto* begin = _rawAccessor->begin(_registerInfo.address);
168 assert(begin !=
nullptr);
170 const size_t stride = _registerInfo.elementPitchBits / 8;
171 for(
auto [itsrc, itdst] = std::make_pair(begin, buffer_2D[0].begin()); itdst != buffer_2D[0].end();
172 itsrc += stride, ++itdst) {
174 memcpy(&temp, itsrc,
sizeof(RawType));
187 template<
typename UserType,
bool isRaw>
190 if(!_dev->isOpen()) {
195 _rawAccessor->preWrite(type, versionNumber);
197 if constexpr(!isRaw || std::is_same<UserType, std::string>::value) {
198 if(!_registerInfo.isWriteable()) {
200 "NumericAddressedBackend: Writing to a non-writeable register is not allowed (Register name: " +
201 _registerInfo.getRegisterName() +
").");
203 _converterLoopHelper->doPreWrite();
208 auto* itdst = _rawAccessor->begin(_registerInfo.address);
209 auto* itsrc = buffer_2D[0].data();
210 const size_t stride = _registerInfo.elementPitchBits / 8;
211 for(
size_t i = 0; i < buffer_2D[0].size(); ++i) {
212 memcpy(itdst + i * stride, itsrc + i,
sizeof(UserType));
216 _rawAccessor->setDataValidity(this->_dataValidity);
221 template<
typename UserType,
bool isRaw>
226 static_assert(std::is_same_v<UserType, CookedType>);
227 if constexpr(!isRaw) {
228 if constexpr(!std::is_same_v<RawType, ChimeraTK::Void>) {
229 auto* begin = _rawAccessor->begin(_registerInfo.address);
231 const size_t stride = _registerInfo.elementPitchBits / 8;
232 for(
auto [itsrc, itdst] = std::make_pair(buffer_2D[0].begin(), begin); itsrc != buffer_2D[0].end();
233 ++itsrc, itdst += stride) {
234 RawType temp = converter.
toRaw(*itsrc);
235 memcpy(itdst, &temp,
sizeof(RawType));
247 template<
typename UserType,
bool isRaw>
249 if(!_dev->isOpen()) {
252 if(!_registerInfo.isReadable()) {
254 "NumericAddressedBackend: Reading from a non-readable register is not allowed (Register name: " +
255 _registerInfo.getRegisterName() +
").");
257 _rawAccessor->preRead(type);
262 template<
typename UserType,
bool isRaw>
265 if(!_dev->isOpen()) {
269 _rawAccessor->setActiveException(this->_activeException);
270 _rawAccessor->postWrite(type, versionNumber);
275 template<
typename UserType,
bool isRaw>
277 const boost::shared_ptr<TransferElement const>& other)
const {
278 auto rhsCasted = boost::dynamic_pointer_cast<const NumericAddressedBackendRegisterAccessor<UserType, isRaw>>(other);
279 if(rhsCasted.get() ==
this) {
285 if(_dev != rhsCasted->_dev) {
288 if(_registerInfo != rhsCasted->_registerInfo) {
297 template<
typename UserType,
bool isRaw>
299 return isReadable() && !isWriteable();
304 template<
typename UserType,
bool isRaw>
306 return _registerInfo.isReadable();
311 template<
typename UserType,
bool isRaw>
313 return _registerInfo.isWriteable();
318 template<
typename UserType,
bool isRaw>
320 boost::shared_ptr<DeviceBackend> exceptionBackend) {
321 this->_exceptionBackend = exceptionBackend;
322 _rawAccessor->setExceptionBackend(exceptionBackend);
327 template<
typename UserType,
bool isRaw>
329 _readRegistration = detail::ReadCoordinator::Registration<UserType>(&_rawAccessor->_readCoordinator, _registerInfo,
330 isRaw, _registerInfo.address - _rawAccessor->_startAddress, _registerInfo.elementPitchBits / 8,
331 _registerInfo.nElements);
336 template<
typename UserType,
bool isRaw>
338 isRaw>::getHardwareAccessingElements() {
344 template<
typename UserType,
bool isRaw>
346 isRaw>::getInternalElements() {
347 return {_rawAccessor};
352 template<
typename UserType,
bool isRaw>
354 boost::shared_ptr<TransferElement> newElement) {
355 auto casted = boost::dynamic_pointer_cast<NumericAddressedLowLevelTransferElement>(newElement);
356 if(casted && casted->isMergeable(_rawAccessor)) {
357 size_t newStartAddress = std::min(casted->_startAddress, _rawAccessor->_startAddress);
358 size_t newStopAddress = std::max(
359 casted->_startAddress + casted->_numberOfBytes, _rawAccessor->_startAddress + _rawAccessor->_numberOfBytes);
360 size_t newNumberOfBytes = newStopAddress - newStartAddress;
363 _readRegistration.reset();
364 casted->changeAddress(newStartAddress, newNumberOfBytes);
365 _rawAccessor = casted;
366 registerWithReadCoordinator();
368 _rawAccessor->setExceptionBackend(this->_exceptionBackend);
373 template<
typename UserType,
bool isRaw>
374 template<
typename COOKED_TYPE>
376 unsigned int channel,
unsigned int sample) {
377 if constexpr(isRaw && std::is_integral_v<UserType>) {
378 if constexpr(isRawType<std::make_unsigned_t<UserType>>) {
381 RawConverter::withConverter<COOKED_TYPE, std::make_unsigned_t<UserType>>(_registerInfo, 0, [&](
auto converter) {
382 rv = converter.toCooked(
393 template<
typename UserType,
bool isRaw>
394 template<
typename COOKED_TYPE>
396 unsigned int channel,
unsigned int sample, COOKED_TYPE value) {
397 if constexpr(isRaw && std::is_integral_v<UserType>) {
398 if constexpr(isRawType<std::make_unsigned_t<UserType>>) {
400 RawConverter::withConverter<COOKED_TYPE, std::make_unsigned_t<UserType>>(_registerInfo, 0, [&](
auto converter) {
#define INSTANTIATE_MULTI_TEMPLATE_FOR_CHIMERATK_USER_TYPES(TemplateClass,...)
#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.
bool has(AccessMode flag) const
Check if a certain flag is in the set.
void checkForUnknownFlags(const std::set< AccessMode > &knownFlags) const
Check of any flag which is not in the given set "knownFlags" is set.
DataType rawDataType() const
Get the raw data type.
A class to describe which of the supported data types is used.
std::string getAsString() const
Return string representation of the data type.
N-dimensional register accessor.
Base class for address-based device backends (e.g.
Implementation of the NDRegisterAccessor for NumericAddressedBackends for scalar and 1D registers.
void doPostWrite(TransferType type, VersionNumber versionNumber) override
Backend specific implementation of postWrite().
std::vector< boost::shared_ptr< TransferElement > > getHardwareAccessingElements() override
Obtain the underlying TransferElements with actual hardware access.
void doPreWriteImpl(RawConverter::Converter< CookedType, RawType, sc, fc, isSigned > converter, size_t implParameter)
void doPostReadImpl(RawConverter::Converter< CookedType, RawType, sc, fc, isSigned > converter, size_t implParameter)
bool isReadOnly() const override
Check if transfer element is read only, i.e.
void setExceptionBackend(boost::shared_ptr< DeviceBackend > exceptionBackend) override
Set the backend to which the exception has to be reported.
bool mayReplaceOther(const boost::shared_ptr< TransferElement const > &other) const override
Check whether the TransferElement can be used in places where the TransferElement "other" is currentl...
void doPostRead(TransferType type, bool hasNewData) override
Backend specific implementation of postRead().
bool doWriteTransfer(ChimeraTK::VersionNumber versionNumber) override
Implementation version of writeTransfer().
std::unique_ptr< RawConverter::ConverterLoopHelper > _converterLoopHelper
Converter to interpret the data.
void registerWithReadCoordinator()
(Re-)register this read consumer with the read coordinator of the current raw element.
void setAsCooked_impl(unsigned int channel, unsigned int sample, COOKED_TYPE value)
bool isWriteable() const override
Check if transfer element is writeable.
void doPreRead(TransferType type) override
Backend specific implementation of preRead().
void doReadTransferSynchronously() override
Implementation version of readTransfer() for synchronous reads.
void replaceTransferElement(boost::shared_ptr< TransferElement > newElement) override
Search for all underlying TransferElements which are considered identical (see sameRegister()) with t...
boost::shared_ptr< NumericAddressedBackend > _dev
the backend to use for the actual hardware access
NumericAddressedRegisterInfo _registerInfo
Address, size and fixed-point representation information of the register from the map file.
boost::shared_ptr< NumericAddressedLowLevelTransferElement > _rawAccessor
raw accessor
COOKED_TYPE getAsCooked_impl(unsigned int channel, unsigned int sample)
void doPreWrite(TransferType type, VersionNumber versionNumber) override
Backend specific implementation of preWrite().
bool isReadable() const override
Check if transfer element is readable.
NumericAddressedBackendRegisterAccessor(const boost::shared_ptr< DeviceBackend > &dev, const RegisterPath ®isterPathName, size_t numberOfWords, size_t wordOffsetInRegister, AccessModeFlags flags)
uint32_t nElements
Number of elements in register.
std::vector< ChannelInfo > channels
Define per-channel information (bit interpretation etc.), 1D/scalars have exactly one entry.
uint64_t bar
Upper part of the address (name originally from PCIe, meaning now generalised)
const DataDescriptor & getDataDescriptor() const override
Return description of the actual payload data for this register.
uint32_t elementPitchBits
Distance in bits (!) between two elements (of the same channel)
uint64_t address
Lower part of the address relative to BAR, in bytes.
std::string engineeringUnit
unsigned int getNumberOfElements() const override
Return number of elements per channel.
Converter class for conversions from raw to cooked values.
UserType toCooked(RawType rawValue)
RawType toRaw(UserType cookedValue)
Class to store a register path name.
std::string _description
Description of this variable/register.
std::string _unit
Engineering unit.
bool _isInTransferGroup
Flag whether this TransferElement has been added to a TransferGroup or not.
Class for generating and holding version numbers without exposing a numeric representation.
Exception thrown when a logic error has occured.
This header collects some conversion routines which should be useful both for ApplicationCore Python ...
@ raw
Raw access: disable any possible conversion from the original hardware data type into the given UserT...
TransferType
Used to indicate the applicable operation on a Transferelement.
std::string to_string(const std::string &v)