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, *
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())
return;
126 _rawAccessor->setActiveException(this->_activeException);
127 _rawAccessor->postRead(type, hasNewData);
129 if(!hasNewData)
return;
131 if constexpr(!isRaw || std::is_same<UserType, std::string>::value) {
132 _converterLoopHelper->doPostRead();
136 auto* itsrc = _rawAccessor->begin(_registerInfo.address);
137 auto* itdst = buffer_2D[0].data();
138 memcpy(itdst, itsrc, buffer_2D[0].size() *
sizeof(UserType));
144 this->_versionNumber = _rawAccessor->getVersionNumber();
145 this->_dataValidity = _rawAccessor->dataValidity();
150 template<
typename UserType,
bool isRaw>
155 static_assert(std::is_same_v<UserType, CookedType>);
156 if constexpr(!isRaw) {
157 auto* begin =
reinterpret_cast<RawType*
>(_rawAccessor->begin(_registerInfo.address));
158 for(
auto [itsrc, itdst] = std::make_pair(begin, buffer_2D[0].begin()); itdst != buffer_2D[0].end();
160 *itdst = converter.
toCooked(*itsrc);
171 template<
typename UserType,
bool isRaw>
177 _rawAccessor->preWrite(type, versionNumber);
179 if constexpr(!isRaw || std::is_same<UserType, std::string>::value) {
180 if(!_registerInfo.isWriteable()) {
182 "NumericAddressedBackend: Writing to a non-writeable register is not allowed (Register name: " +
183 _registerInfo.getRegisterName() +
").");
185 _converterLoopHelper->doPreWrite();
189 auto* itdst = _rawAccessor->begin(_registerInfo.address);
190 auto itsrc = buffer_2D[0].begin();
191 memcpy(&(*itdst), &(*itsrc), buffer_2D[0].size() *
sizeof(UserType));
194 _rawAccessor->setDataValidity(this->_dataValidity);
199 template<
typename UserType,
bool isRaw>
204 static_assert(std::is_same_v<UserType, CookedType>);
205 if constexpr(!isRaw) {
206 auto* begin =
reinterpret_cast<RawType*
>(_rawAccessor->begin(_registerInfo.address));
207 for(
auto [itsrc, itdst] = std::make_pair(buffer_2D[0].begin(), begin); itsrc != buffer_2D[0].end();
209 *itdst = converter.
toRaw(*itsrc);
220 template<
typename UserType,
bool isRaw>
223 if(!_registerInfo.isReadable()) {
225 "NumericAddressedBackend: Reading from a non-readable register is not allowed (Register name: " +
226 _registerInfo.getRegisterName() +
").");
228 _rawAccessor->preRead(type);
233 template<
typename UserType,
bool isRaw>
236 if(!_dev->isOpen())
return;
237 _rawAccessor->setActiveException(this->_activeException);
238 _rawAccessor->postWrite(type, versionNumber);
243 template<
typename UserType,
bool isRaw>
245 const boost::shared_ptr<TransferElement const>& other)
const {
246 auto rhsCasted = boost::dynamic_pointer_cast<const NumericAddressedBackendRegisterAccessor<UserType, isRaw>>(other);
247 if(rhsCasted.get() ==
this) {
250 if(!rhsCasted)
return false;
251 if(_dev != rhsCasted->_dev)
return false;
252 if(_registerInfo != rhsCasted->_registerInfo)
return false;
259 template<
typename UserType,
bool isRaw>
261 return isReadable() && !isWriteable();
266 template<
typename UserType,
bool isRaw>
268 return _registerInfo.isReadable();
273 template<
typename UserType,
bool isRaw>
275 return _registerInfo.isWriteable();
280 template<
typename UserType,
bool isRaw>
282 boost::shared_ptr<DeviceBackend> exceptionBackend) {
283 this->_exceptionBackend = exceptionBackend;
284 _rawAccessor->setExceptionBackend(exceptionBackend);
289 template<
typename UserType,
bool isRaw>
291 isRaw>::getHardwareAccessingElements() {
297 template<
typename UserType,
bool isRaw>
299 isRaw>::getInternalElements() {
300 return {_rawAccessor};
305 template<
typename UserType,
bool isRaw>
307 boost::shared_ptr<TransferElement> newElement) {
308 auto casted = boost::dynamic_pointer_cast<NumericAddressedLowLevelTransferElement>(newElement);
309 if(casted && casted->isMergeable(_rawAccessor)) {
310 size_t newStartAddress = std::min(casted->_startAddress, _rawAccessor->_startAddress);
311 size_t newStopAddress = std::max(
312 casted->_startAddress + casted->_numberOfBytes, _rawAccessor->_startAddress + _rawAccessor->_numberOfBytes);
313 size_t newNumberOfBytes = newStopAddress - newStartAddress;
314 casted->changeAddress(newStartAddress, newNumberOfBytes);
315 _rawAccessor = casted;
317 _rawAccessor->setExceptionBackend(this->_exceptionBackend);
322 template<
typename UserType,
bool isRaw>
323 template<
typename COOKED_TYPE>
325 unsigned int channel,
unsigned int sample) {
326 if constexpr(isRaw && std::is_integral_v<UserType>) {
327 if constexpr(isRawType<std::make_unsigned_t<UserType>>) {
330 RawConverter::withConverter<COOKED_TYPE, std::make_unsigned_t<UserType>>(_registerInfo, 0, [&](
auto converter) {
331 rv = converter.toCooked(
342 template<
typename UserType,
bool isRaw>
343 template<
typename COOKED_TYPE>
345 unsigned int channel,
unsigned int sample, COOKED_TYPE value) {
346 if constexpr(isRaw && std::is_integral_v<UserType>) {
347 if constexpr(isRawType<std::make_unsigned_t<UserType>>) {
349 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.
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.
void doPreWriteImpl(RawConverter::Converter< CookedType, RawType, sc, fc, isSigned > converter, size_t channelIndex)
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...
void doPostReadImpl(RawConverter::Converter< CookedType, RawType, sc, fc, isSigned > converter, size_t channelIndex)
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)