ChimeraTK-DeviceAccess  03.18.00
TransferElementTestAccessor.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 "NDRegisterAccessor.h"
6 
7 namespace ChimeraTK {
8 
10  template<typename UserType>
12  public:
13  TransferElementTestAccessor(AccessModeFlags flags) : NDRegisterAccessor<UserType>("someName", flags) {
14  // this accessor uses a queue length of 3
15  this->_readQueue = {3};
16  this->buffer_2D.resize(1);
17  this->buffer_2D[0].resize(1);
18  }
19 
21 
22  void doPreRead(TransferType type) override {
23  _transferType_pre = type;
26  ++_currentIndex;
27  try {
28  if(_throwLogicErr) throw ChimeraTK::logic_error("Test");
30  }
31  catch(...) {
32  _thrownException = std::current_exception();
33  throw;
34  }
35  }
36 
37  void doPreWrite(TransferType type, VersionNumber versionNumber) override {
38  _transferType_pre = type;
41  ++_currentIndex;
42  _preWrite_version = versionNumber;
43  try {
44  if(_throwLogicErr) throw ChimeraTK::logic_error("Test");
46  if(_throwNumericCast) throw boost::numeric::bad_numeric_cast();
47  }
48  catch(...) {
49  _thrownException = std::current_exception();
50  throw;
51  }
52  }
53 
54  void doReadTransferSynchronously() override {
57  ++_currentIndex;
58  try {
60  }
61  catch(...) {
62  _thrownException = std::current_exception();
63  throw;
64  }
65  }
66 
67  bool doWriteTransfer(ChimeraTK::VersionNumber versionNumber) override {
70  ++_currentIndex;
71  _writeTransfer_version = versionNumber;
72  try {
74  }
75  catch(...) {
76  _thrownException = std::current_exception();
77  throw;
78  }
79  return _previousDataLost;
80  }
81 
85  ++_currentIndex;
86  _writeTransfer_version = versionNumber;
87  try {
89  }
90  catch(...) {
91  _thrownException = std::current_exception();
92  throw;
93  }
94  return _previousDataLost;
95  }
96 
97  void doPostRead(TransferType type, bool updateDataBuffer) override {
98  _transferType_post = type;
101  ++_currentIndex;
102  _updateDataBuffer = updateDataBuffer;
104 
105  if(_setPostReadVersion == VersionNumber{nullptr}) {
107  }
108  else {
110  }
111 
112  this->buffer_2D[0][0] = _setPostReadData;
113 
114  try {
115  if(_throwNumericCast) throw boost::numeric::bad_numeric_cast();
116  }
117  catch(...) {
118  _thrownException = std::current_exception();
119  throw;
120  }
121  }
122 
123  void doPostWrite(TransferType type, VersionNumber versionNumber) override {
124  _transferType_post = type;
127  ++_currentIndex;
128  _postWrite_version = versionNumber;
130  }
131 
132  bool mayReplaceOther(const boost::shared_ptr<TransferElement const>& other) const override {
133  return _listMayReplaceElements.count(other->getId());
134  }
135  std::vector<boost::shared_ptr<TransferElement>> getHardwareAccessingElements() override {
136  if(_hardwareAccessingElements.size() == 0) {
137  // cannot call shared_from_this() in constructor, so we cannot put it by default into the list...
138  return {boost::enable_shared_from_this<TransferElement>::shared_from_this()};
139  }
141  }
142  std::list<boost::shared_ptr<TransferElement>> getInternalElements() override {
143  std::list<boost::shared_ptr<TransferElement>> r;
144  for(auto& e : _internalElements) r.push_back(e);
145  return r;
146  }
147  void replaceTransferElement(boost::shared_ptr<TransferElement> newElement) override {
148  _listReplacementElements.push_back(newElement->getId());
149  }
150 
151  bool isReadOnly() const override {
153  return !_writeable && _readable;
154  }
155 
156  bool isReadable() const override {
158  return _readable;
159  }
160 
161  bool isWriteable() const override {
163  return _writeable;
164  }
165 
166  void interrupt() override { this->interrupt_impl(this->_readQueue); }
167 
168  bool _writeable{true};
169  bool _readable{true};
170 
171  // counter flags to check which functions have been called how many times and in which order (via index)
172  size_t _preRead_counter{0};
173  size_t _preWrite_counter{0};
177  size_t _postRead_counter{0};
179  size_t _preIndex{999999};
180  size_t _transferIndex{999999};
181  size_t _postIndex{999999};
182  static std::atomic<size_t> _currentIndex; // allows comparison across transfer elements
183 
184  // recorded function arguments etc.
185  TransferType _transferType_pre, _transferType_post; // TransferType as seen in pre/postXxx()
186  bool _updateDataBuffer; // updateDataBuffer as seen in postRead() (set there)
190  std::exception_ptr _seenActiveException{nullptr}; // _activeException as seen in postXxx()
191  std::exception_ptr _thrownException{nullptr}; // the exception thrown by request (via command flags below)
192 
193  // command flags to steer behaviour of this TE
194  bool _previousDataLost{false}; // flag to return by writeTransfer()/writeTransferDestructively()
195  bool _throwLogicErr{false}; // always in doPreXxx()
198  bool _throwRuntimeErrInPreconditions{false}; // throw in isReadable/isWriteable/isReadOnly
199  bool _throwNumericCast{false}; // in doPreWrite() or doPreRead() depending on operation
200  VersionNumber _setPostReadVersion{nullptr}; // if nullptr, a new version will be generated
201  UserType _setPostReadData{UserType()}; // data to be copied into the user buffer in postRead
202 
203  // lists, counters etc. used for the TransferGroup tests
204  std::list<TransferElementID> _listReplacementElements; // list of all arguments of replaceTransferElement()
205  std::vector<boost::shared_ptr<TransferElementTestAccessor<UserType>>> _internalElements; // returned by
206  // getInternalElements()
207  std::vector<boost::shared_ptr<TransferElement>> _hardwareAccessingElements; // returned by
208  // getHardwareAccessingElements()
209  std::set<TransferElementID> _listMayReplaceElements; // mayReplaceOther() returns true if ID is found in this set
210 
211  // reset all counters and revert command flags to defaults
212  void resetCounters() {
213  _preRead_counter = 0;
214  _preWrite_counter = 0;
218  _postRead_counter = 0;
219  _postWrite_counter = 0;
220  _preIndex = 999;
221  _transferIndex = 999;
222  _postIndex = 999;
223  _currentIndex = 0;
224  _throwLogicErr = false;
225  _throwRuntimeErrInPre = false;
227  _throwNumericCast = false;
228  _preWrite_version = VersionNumber{nullptr};
231  _seenActiveException = nullptr;
232  _thrownException = nullptr;
233  _previousDataLost = false;
234  _throwLogicErr = false;
236  _throwRuntimeErrInPre = false;
237  _throwNumericCast = false;
239  _listReplacementElements.clear();
240  }
241 
242  // convenience function to put exceptions onto the readQueue (see also interrupt())
244  try {
245  throw ChimeraTK::runtime_error("Test");
246  }
247  catch(...) {
248  _thrownException = std::current_exception();
249  this->_readQueue.push_exception(std::current_exception());
250  }
251  }
253  try {
254  throw ChimeraTK::detail::DiscardValueException();
255  }
256  catch(...) {
257  _thrownException = std::current_exception();
258  this->_readQueue.push_exception(std::current_exception());
259  }
260  }
261  // simulate a reveiver thread by manually putting data into the queue
262  bool push() { return this->_readQueue.push(); }
263 
267  };
268 
269  template<typename T>
270  std::atomic<size_t> TransferElementTestAccessor<T>::_currentIndex(0);
271 
272 } // namespace ChimeraTK
ChimeraTK::TransferElementTestAccessor::_seenActiveException
std::exception_ptr _seenActiveException
Definition: TransferElementTestAccessor.h:190
ChimeraTK::TransferElementTestAccessor::_throwLogicErr
bool _throwLogicErr
Definition: TransferElementTestAccessor.h:195
ChimeraTK::TransferElementTestAccessor::isReadOnly
bool isReadOnly() const override
Definition: TransferElementTestAccessor.h:151
ChimeraTK::TransferElementTestAccessor::getHardwareAccessingElements
std::vector< boost::shared_ptr< TransferElement > > getHardwareAccessingElements() override
Definition: TransferElementTestAccessor.h:135
ChimeraTK::TransferElementTestAccessor::isWriteable
bool isWriteable() const override
Definition: TransferElementTestAccessor.h:161
ChimeraTK::TransferElementTestAccessor::push
bool push()
Definition: TransferElementTestAccessor.h:262
ChimeraTK::TransferElementTestAccessor::mayReplaceOther
bool mayReplaceOther(const boost::shared_ptr< TransferElement const > &other) const override
Definition: TransferElementTestAccessor.h:132
ChimeraTK::TransferElementTestAccessor::doWriteTransferDestructively
bool doWriteTransferDestructively(ChimeraTK::VersionNumber versionNumber) override
Definition: TransferElementTestAccessor.h:82
ChimeraTK::TransferElementTestAccessor::_writeTransfer_version
VersionNumber _writeTransfer_version
Definition: TransferElementTestAccessor.h:188
ChimeraTK::TransferElementTestAccessor::doPreWrite
void doPreWrite(TransferType type, VersionNumber versionNumber) override
Definition: TransferElementTestAccessor.h:37
ChimeraTK::TransferElementTestAccessor::_setPostReadVersion
VersionNumber _setPostReadVersion
Definition: TransferElementTestAccessor.h:200
ChimeraTK::TransferElementTestAccessor::_writeTransferDestructively_counter
size_t _writeTransferDestructively_counter
Definition: TransferElementTestAccessor.h:176
ChimeraTK::TransferElementTestAccessor::_previousDataLost
bool _previousDataLost
Definition: TransferElementTestAccessor.h:194
ChimeraTK::TransferElementTestAccessor::_preWrite_version
VersionNumber _preWrite_version
Definition: TransferElementTestAccessor.h:187
ChimeraTK::NDRegisterAccessor::buffer_2D
std::vector< std::vector< UserType > > buffer_2D
Buffer of converted data elements.
Definition: NDRegisterAccessor.h:123
ChimeraTK::TransferElement::_activeException
std::exception_ptr _activeException
Exception to be rethrown in postXXX() in case hasSeenException == true Can be set via setActiveExcept...
Definition: TransferElement.h:859
ChimeraTK::runtime_error
Exception thrown when a runtime error has occured.
Definition: Exception.h:18
ChimeraTK::TransferElementTestAccessor::_internalElements
std::vector< boost::shared_ptr< TransferElementTestAccessor< UserType > > > _internalElements
Definition: TransferElementTestAccessor.h:205
ChimeraTK::TransferElementTestAccessor::_throwNumericCast
bool _throwNumericCast
Definition: TransferElementTestAccessor.h:199
ChimeraTK::TransferElementTestAccessor::_preIndex
size_t _preIndex
Definition: TransferElementTestAccessor.h:179
ChimeraTK::TransferElementTestAccessor::doPostWrite
void doPostWrite(TransferType type, VersionNumber versionNumber) override
Definition: TransferElementTestAccessor.h:123
ChimeraTK::TransferElementTestAccessor::doWriteTransfer
bool doWriteTransfer(ChimeraTK::VersionNumber versionNumber) override
Definition: TransferElementTestAccessor.h:67
ChimeraTK::TransferElementTestAccessor::_listMayReplaceElements
std::set< TransferElementID > _listMayReplaceElements
Definition: TransferElementTestAccessor.h:209
ChimeraTK::TransferElementTestAccessor::_writeable
bool _writeable
Definition: TransferElementTestAccessor.h:168
ChimeraTK::TransferElementTestAccessor::_thrownException
std::exception_ptr _thrownException
Definition: TransferElementTestAccessor.h:191
ChimeraTK::TransferElementTestAccessor::doPreRead
void doPreRead(TransferType type) override
Definition: TransferElementTestAccessor.h:22
ChimeraTK::TransferElementTestAccessor::_writeTransfer_counter
size_t _writeTransfer_counter
Definition: TransferElementTestAccessor.h:175
ChimeraTK::TransferElementTestAccessor::getInternalElements
std::list< boost::shared_ptr< TransferElement > > getInternalElements() override
Definition: TransferElementTestAccessor.h:142
ChimeraTK::TransferElementTestAccessor::_postIndex
size_t _postIndex
Definition: TransferElementTestAccessor.h:181
ChimeraTK::TransferElementTestAccessor::TransferElementTestAccessor
TransferElementTestAccessor(AccessModeFlags flags)
Definition: TransferElementTestAccessor.h:13
NDRegisterAccessor.h
ChimeraTK::TransferElementTestAccessor
Special accessor used to test the behaviour of the TransferElement base class and the TransferGroup.
Definition: TransferElementTestAccessor.h:11
ChimeraTK::TransferElementTestAccessor::doReadTransferSynchronously
void doReadTransferSynchronously() override
Definition: TransferElementTestAccessor.h:54
ChimeraTK::TransferElementTestAccessor::_activeException
std::exception_ptr _activeException
Exception to be rethrown in postXXX() in case hasSeenException == true Can be set via setActiveExcept...
Definition: TransferElement.h:859
ChimeraTK::TransferElementTestAccessor::_preWrite_counter
size_t _preWrite_counter
Definition: TransferElementTestAccessor.h:173
ChimeraTK::TransferElementTestAccessor::_readTransfer_counter
size_t _readTransfer_counter
Definition: TransferElementTestAccessor.h:174
ChimeraTK::TransferElementTestAccessor::interrupt
void interrupt() override
Definition: TransferElementTestAccessor.h:166
ChimeraTK::TransferElementTestAccessor::_postWrite_version
VersionNumber _postWrite_version
Definition: TransferElementTestAccessor.h:189
ChimeraTK::TransferType
TransferType
Used to indicate the applicable operation on a Transferelement.
Definition: TransferElement.h:51
ChimeraTK::TransferElementTestAccessor::_throwRuntimeErrInTransfer
bool _throwRuntimeErrInTransfer
Definition: TransferElementTestAccessor.h:196
ChimeraTK::TransferElementTestAccessor::_transferIndex
size_t _transferIndex
Definition: TransferElementTestAccessor.h:180
ChimeraTK::TransferElementTestAccessor::_updateDataBuffer
bool _updateDataBuffer
Definition: TransferElementTestAccessor.h:186
ChimeraTK::TransferElementTestAccessor::~TransferElementTestAccessor
~TransferElementTestAccessor() override
Definition: TransferElementTestAccessor.h:20
ChimeraTK::TransferElementTestAccessor::_setPostReadData
UserType _setPostReadData
Definition: TransferElementTestAccessor.h:201
ChimeraTK::TransferElementTestAccessor::_throwRuntimeErrInPre
bool _throwRuntimeErrInPre
Definition: TransferElementTestAccessor.h:197
ChimeraTK::TransferElementTestAccessor::putDiscardValueOnQueue
void putDiscardValueOnQueue()
Definition: TransferElementTestAccessor.h:252
ChimeraTK::TransferElementTestAccessor::_readQueue
cppext::future_queue< void > _readQueue
The queue for asynchronous read transfers.
Definition: TransferElement.h:847
ChimeraTK::TransferElementTestAccessor::_postRead_counter
size_t _postRead_counter
Definition: TransferElementTestAccessor.h:177
ChimeraTK::TransferElementTestAccessor::_transferType_pre
TransferType _transferType_pre
Definition: TransferElementTestAccessor.h:185
ChimeraTK::TransferElementTestAccessor::replaceTransferElement
void replaceTransferElement(boost::shared_ptr< TransferElement > newElement) override
Definition: TransferElementTestAccessor.h:147
ChimeraTK::VersionNumber
Class for generating and holding version numbers without exposing a numeric representation.
Definition: VersionNumber.h:23
ChimeraTK::TransferElementTestAccessor::_preRead_counter
size_t _preRead_counter
Definition: TransferElementTestAccessor.h:172
ChimeraTK::TransferElement::_readQueue
cppext::future_queue< void > _readQueue
The queue for asynchronous read transfers.
Definition: TransferElement.h:847
ChimeraTK::TransferElementTestAccessor::_listReplacementElements
std::list< TransferElementID > _listReplacementElements
Definition: TransferElementTestAccessor.h:204
ChimeraTK::TransferElementTestAccessor::_currentIndex
static std::atomic< size_t > _currentIndex
Definition: TransferElementTestAccessor.h:182
ChimeraTK::AccessModeFlags
Set of AccessMode flags with additional functionality for an easier handling.
Definition: AccessMode.h:48
ChimeraTK::TransferElementTestAccessor::putRuntimeErrorOnQueue
void putRuntimeErrorOnQueue()
Definition: TransferElementTestAccessor.h:243
ChimeraTK::TransferElementTestAccessor::_readable
bool _readable
Definition: TransferElementTestAccessor.h:169
ChimeraTK::TransferElement::_accessModeFlags
AccessModeFlags _accessModeFlags
The access mode flags for this transfer element.
Definition: TransferElement.h:826
ChimeraTK::TransferElementTestAccessor::doPostRead
void doPostRead(TransferType type, bool updateDataBuffer) override
Definition: TransferElementTestAccessor.h:97
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::TransferElementTestAccessor::resetCounters
void resetCounters()
Definition: TransferElementTestAccessor.h:212
ChimeraTK::TransferElementTestAccessor::_throwRuntimeErrInPreconditions
bool _throwRuntimeErrInPreconditions
Definition: TransferElementTestAccessor.h:198
ChimeraTK::TransferElementTestAccessor::_transferType_post
TransferType _transferType_post
Definition: TransferElementTestAccessor.h:185
ChimeraTK::TransferElementTestAccessor::_postWrite_counter
size_t _postWrite_counter
Definition: TransferElementTestAccessor.h:178
ChimeraTK::NDRegisterAccessor
N-dimensional register accessor.
Definition: ForwardDeclarations.h:17
ChimeraTK::TransferElementTestAccessor::isReadable
bool isReadable() const override
Definition: TransferElementTestAccessor.h:156
ChimeraTK::logic_error
Exception thrown when a logic error has occured.
Definition: Exception.h:51
ChimeraTK::TransferElementTestAccessor::_hardwareAccessingElements
std::vector< boost::shared_ptr< TransferElement > > _hardwareAccessingElements
Definition: TransferElementTestAccessor.h:207
ChimeraTK::TransferElement::_versionNumber
VersionNumber _versionNumber
The version number of the last successful transfer.
Definition: TransferElement.h:851