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.");
45 if(numberOfWords == 0) {
48 if(numberOfWords > 1) {
50 "Requested number of words is larger than 1 in VOID register '" + registerPathName +
"'!");
52 if(wordOffsetInRegister > 0) {
57 if(numberOfWords == 0) {
68 registerPathName +
"'!");
78 _rawAccessor = boost::make_shared<NumericAddressedLowLevelTransferElement>(
85 if constexpr(!isRaw) {
87 RawConverter::ConverterLoopHelper::makeConverterLoopHelper<UserType>(
_registerInfo, 0, 0, *
this);
92 throw ChimeraTK::logic_error(
"Given UserType when obtaining the NumericAddressedBackendRegisterAccessor in "
93 "raw mode does not match the expected type. Use an " +
95 " instead! (Register name: " + registerPathName +
"')");
105 template<
typename UserType,
bool isRaw>
107 _rawAccessor->readTransfer();
112 template<
typename UserType,
bool isRaw>
116 _rawAccessor->writeTransfer(versionNumber);
122 template<
typename UserType,
bool isRaw>
124 if(!_dev->isOpen()) {
129 _rawAccessor->setActiveException(this->_activeException);
130 _rawAccessor->postRead(type, hasNewData);
136 if constexpr(!isRaw || std::is_same<UserType, std::string>::value) {
137 _converterLoopHelper->doPostRead();
141 auto* itsrc = _rawAccessor->begin(_registerInfo.address);
142 auto* itdst = buffer_2D[0].data();
143 memcpy(itdst, itsrc, buffer_2D[0].size() *
sizeof(UserType));
149 this->_versionNumber = _rawAccessor->getVersionNumber();
150 this->_dataValidity = _rawAccessor->dataValidity();
155 template<
typename UserType,
bool isRaw>
160 static_assert(std::is_same_v<UserType, CookedType>);
161 if constexpr(!isRaw) {
162 if constexpr(!std::is_same_v<RawType, ChimeraTK::Void>) {
163 auto* begin = _rawAccessor->begin(_registerInfo.address);
164 assert(begin !=
nullptr);
165 for(
auto [itsrc, itdst] = std::make_pair(begin, buffer_2D[0].begin()); itdst != buffer_2D[0].end();
166 itsrc +=
sizeof(RawType), ++itdst) {
168 memcpy(&temp, itsrc,
sizeof(RawType));
181 template<
typename UserType,
bool isRaw>
184 if(!_dev->isOpen()) {
189 _rawAccessor->preWrite(type, versionNumber);
191 if constexpr(!isRaw || std::is_same<UserType, std::string>::value) {
192 if(!_registerInfo.isWriteable()) {
194 "NumericAddressedBackend: Writing to a non-writeable register is not allowed (Register name: " +
195 _registerInfo.getRegisterName() +
").");
197 _converterLoopHelper->doPreWrite();
201 auto* itdst = _rawAccessor->begin(_registerInfo.address);
202 auto itsrc = buffer_2D[0].begin();
203 memcpy(&(*itdst), &(*itsrc), buffer_2D[0].size() *
sizeof(UserType));
206 _rawAccessor->setDataValidity(this->_dataValidity);
211 template<
typename UserType,
bool isRaw>
216 static_assert(std::is_same_v<UserType, CookedType>);
217 if constexpr(!isRaw) {
218 if constexpr(!std::is_same_v<RawType, ChimeraTK::Void>) {
219 auto* begin = _rawAccessor->begin(_registerInfo.address);
220 for(
auto [itsrc, itdst] = std::make_pair(buffer_2D[0].begin(), begin); itsrc != buffer_2D[0].end();
221 ++itsrc, itdst +=
sizeof(RawType)) {
222 RawType temp = converter.
toRaw(*itsrc);
223 memcpy(itdst, &temp,
sizeof(RawType));
235 template<
typename UserType,
bool isRaw>
237 if(!_dev->isOpen()) {
240 if(!_registerInfo.isReadable()) {
242 "NumericAddressedBackend: Reading from a non-readable register is not allowed (Register name: " +
243 _registerInfo.getRegisterName() +
").");
245 _rawAccessor->preRead(type);
250 template<
typename UserType,
bool isRaw>
253 if(!_dev->isOpen()) {
257 _rawAccessor->setActiveException(this->_activeException);
258 _rawAccessor->postWrite(type, versionNumber);
263 template<
typename UserType,
bool isRaw>
265 const boost::shared_ptr<TransferElement const>& other)
const {
266 auto rhsCasted = boost::dynamic_pointer_cast<const NumericAddressedBackendRegisterAccessor<UserType, isRaw>>(other);
267 if(rhsCasted.get() ==
this) {
273 if(_dev != rhsCasted->_dev) {
276 if(_registerInfo != rhsCasted->_registerInfo) {
285 template<
typename UserType,
bool isRaw>
287 return isReadable() && !isWriteable();
292 template<
typename UserType,
bool isRaw>
294 return _registerInfo.isReadable();
299 template<
typename UserType,
bool isRaw>
301 return _registerInfo.isWriteable();
306 template<
typename UserType,
bool isRaw>
308 boost::shared_ptr<DeviceBackend> exceptionBackend) {
309 this->_exceptionBackend = exceptionBackend;
310 _rawAccessor->setExceptionBackend(exceptionBackend);
315 template<
typename UserType,
bool isRaw>
317 isRaw>::getHardwareAccessingElements() {
323 template<
typename UserType,
bool isRaw>
325 isRaw>::getInternalElements() {
326 return {_rawAccessor};
331 template<
typename UserType,
bool isRaw>
333 boost::shared_ptr<TransferElement> newElement) {
334 auto casted = boost::dynamic_pointer_cast<NumericAddressedLowLevelTransferElement>(newElement);
335 if(casted && casted->isMergeable(_rawAccessor)) {
336 size_t newStartAddress = std::min(casted->_startAddress, _rawAccessor->_startAddress);
337 size_t newStopAddress = std::max(
338 casted->_startAddress + casted->_numberOfBytes, _rawAccessor->_startAddress + _rawAccessor->_numberOfBytes);
339 size_t newNumberOfBytes = newStopAddress - newStartAddress;
340 casted->changeAddress(newStartAddress, newNumberOfBytes);
341 _rawAccessor = casted;
343 _rawAccessor->setExceptionBackend(this->_exceptionBackend);
348 template<
typename UserType,
bool isRaw>
349 template<
typename COOKED_TYPE>
351 unsigned int channel,
unsigned int sample) {
352 if constexpr(isRaw && std::is_integral_v<UserType>) {
353 if constexpr(isRawType<std::make_unsigned_t<UserType>>) {
356 RawConverter::withConverter<COOKED_TYPE, std::make_unsigned_t<UserType>>(_registerInfo, 0, [&](
auto converter) {
357 rv = converter.toCooked(
368 template<
typename UserType,
bool isRaw>
369 template<
typename COOKED_TYPE>
371 unsigned int channel,
unsigned int sample, COOKED_TYPE value) {
372 if constexpr(isRaw && std::is_integral_v<UserType>) {
373 if constexpr(isRawType<std::make_unsigned_t<UserType>>) {
375 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 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.
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.
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.
@ 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)