ChimeraTK-DeviceAccess  03.18.00
DeviceBackendImpl.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 
6 #include "DeviceBackend.h"
7 #include "Exception.h"
8 #include <condition_variable>
9 #include <shared_mutex>
10 
11 #include <ChimeraTK/cppext/finally.hpp>
12 
13 #include <atomic>
14 #include <list>
15 #include <mutex>
16 
17 namespace ChimeraTK {
18 
19  /********************************************************************************************************************/
20 
26  public:
27  bool isOpen() override { return _opened; }
28 
29  bool isConnected() final {
30  std::cerr << "Removed function DeviceBackendImpl::isConnected() called." << std::endl;
31  std::cerr << "Do not use. This function has no valid meaning." << std::endl;
32  std::terminate();
33  }
34 
35  MetadataCatalogue getMetadataCatalogue() const override { return {}; }
36 
41  virtual void setExceptionImpl() noexcept {}
42 
47  void checkActiveException() final;
48 
49  void setException(const std::string& message) noexcept final;
50 
51  bool isFunctional() const noexcept final;
52 
53  std::string getActiveExceptionMessage() noexcept;
54 
55  protected:
57  void setOpenedAndClearException() noexcept;
58 
60  std::atomic<bool> _opened{false};
61 
67 
68  private:
70  std::atomic<bool> _hasActiveException{false};
71 
76  std::string _activeExceptionMessage;
77 
79  std::mutex _mx_activeExceptionMessage;
80  };
81 
82  /********************************************************************************************************************/
83 
84  // This function is rather often called and hence implemented as inline in the header for performance reasons.
85  inline bool DeviceBackendImpl::isFunctional() const noexcept {
86  if(!_opened) return false;
87  if(_hasActiveException) return false;
88  return true;
89  }
90 
91  /********************************************************************************************************************/
92 
93  // This function is rather often called and hence implemented as inline in the header for performance reasons.
95  if(_hasActiveException) {
96  std::lock_guard<std::mutex> lk(_mx_activeExceptionMessage);
97  throw ChimeraTK::runtime_error(_activeExceptionMessage);
98  }
99  }
100 
101  /********************************************************************************************************************/
102 
103 } // namespace ChimeraTK
DomainsContainer.h
DeviceBackend.h
ChimeraTK::DeviceBackendImpl::setOpenedAndClearException
void setOpenedAndClearException() noexcept
Backends should call this function at the end of a (successful) open() call.
Definition: DeviceBackendImpl.cc:12
ChimeraTK::MetadataCatalogue
Container for backend metadata.
Definition: MetadataCatalogue.h:17
ChimeraTK::DeviceBackendImpl::isConnected
bool isConnected() final
Deprecated since 2022-03-03.
Definition: DeviceBackendImpl.h:29
ChimeraTK::DeviceBackendImpl::_opened
std::atomic< bool > _opened
flag if backend is opened
Definition: DeviceBackendImpl.h:60
ChimeraTK::DeviceBackendImpl::setExceptionImpl
virtual void setExceptionImpl() noexcept
Function to be (optionally) implemented by backends if additional actions are needed when switching t...
Definition: DeviceBackendImpl.h:41
ChimeraTK::DeviceBackendImpl::checkActiveException
void checkActiveException() final
Function to be called by backends when needing to check for an active exception.
Definition: DeviceBackendImpl.h:94
ChimeraTK::runtime_error
Exception thrown when a runtime error has occured.
Definition: Exception.h:18
ChimeraTK::DeviceBackend
The base class for backends providing IO functionality for the Device class.
Definition: DeviceBackend.h:28
ChimeraTK::DeviceBackendImpl::_asyncDomainsContainer
async::DomainsContainer _asyncDomainsContainer
Container for async::Domains to support wait_for_new_data.
Definition: DeviceBackendImpl.h:66
ChimeraTK::DeviceBackendImpl::getActiveExceptionMessage
std::string getActiveExceptionMessage() noexcept
Definition: DeviceBackendImpl.cc:50
ChimeraTK::DeviceBackendImpl::setException
void setException(const std::string &message) noexcept final
Set the backend into an exception state.
Definition: DeviceBackendImpl.cc:26
ChimeraTK::async::DomainsContainer
The DomainsContainer has a container with Domains and is performing actions on all of them.
Definition: DomainsContainer.h:34
ChimeraTK::DeviceBackendImpl::isFunctional
bool isFunctional() const noexcept final
Return whether a device is working as intended, usually this means it is opened and does not have any...
Definition: DeviceBackendImpl.h:85
ChimeraTK::DeviceBackendImpl
DeviceBackendImpl implements some basic functionality which should be available for all backends.
Definition: DeviceBackendImpl.h:25
ChimeraTK::DeviceBackendImpl::getMetadataCatalogue
MetadataCatalogue getMetadataCatalogue() const override
Return the device metadata catalogue.
Definition: DeviceBackendImpl.h:35
Exception.h
ChimeraTK::DeviceBackendImpl::isOpen
bool isOpen() override
Return whether a device has been opened or not.
Definition: DeviceBackendImpl.h:27
ChimeraTK
Definition: DummyBackend.h:16