ChimeraTK-DeviceAccess  03.18.00
AccessMode.cc
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 
4 #include "AccessMode.h"
5 
6 #include "Exception.h"
7 
8 namespace ChimeraTK {
9 
10  /********************************************************************************************************************/
11 
12  AccessModeFlags::AccessModeFlags(std::set<AccessMode> flags) : _flags(std::move(flags)) {}
13 
14  /********************************************************************************************************************/
15 
16  AccessModeFlags::AccessModeFlags(const std::initializer_list<AccessMode>& flags) : _flags(flags) {}
17 
18  /********************************************************************************************************************/
19 
20  bool AccessModeFlags::has(AccessMode flag) const {
21  return (_flags.count(flag) != 0);
22  }
23 
24  /********************************************************************************************************************/
25 
26  bool AccessModeFlags::empty() const {
27  return _flags.empty();
28  }
29 
30  /********************************************************************************************************************/
31 
32  void AccessModeFlags::checkForUnknownFlags(const std::set<AccessMode>& knownFlags) const {
33  for(auto flag : _flags) {
34  if(knownFlags.count(flag) == 0) {
35  throw ChimeraTK::logic_error("Access mode flag '" + getString(flag) + "' is not known by this backend.");
36  }
37  }
38  }
39 
40  /********************************************************************************************************************/
41 
42  bool AccessModeFlags::operator==(const AccessModeFlags& other) const {
43  // fortunately the std::set already has a comparison operator which does exacty what we want
44  return _flags == other._flags;
45  }
46 
47  /********************************************************************************************************************/
48 
49  bool AccessModeFlags::operator<(const AccessModeFlags& other) const {
50  // fortunately the std::set already has a comparison operator which does exacty what we want
51  return _flags < other._flags;
52  }
53 
54  /********************************************************************************************************************/
55 
57  _flags.erase(flag);
58  }
59 
60  /********************************************************************************************************************/
61 
63  _flags.insert(flag);
64  }
65 
66  /********************************************************************************************************************/
67 
68  std::string AccessModeFlags::serialize() const {
69  std::string list{};
70  for(const auto& f : _flags) {
71  list += getString(f) + ",";
72  }
73  if(!list.empty()) {
74  list.pop_back(); // remove trailing ','
75  }
76  return list;
77  }
78 
79  /********************************************************************************************************************/
80 
81  const std::string& AccessModeFlags::getString(const AccessMode flag) {
82  return getStringMap().at(flag);
83  }
84 
85  /********************************************************************************************************************/
86 
87  AccessModeFlags AccessModeFlags::deserialize(const std::string& listOfflags) {
88  std::vector<std::string> names = split(listOfflags);
89  std::set<AccessMode> flagList;
90  for(const auto& flagName : names) {
91  flagList.insert(getAccessMode(flagName));
92  }
93  return AccessModeFlags{flagList};
94  }
95 
96  /********************************************************************************************************************/
97 
98  const std::map<AccessMode, std::string>& AccessModeFlags::getStringMap() {
99  static std::map<AccessMode, std::string> m = {
100  {AccessMode::raw, "raw"}, {AccessMode::wait_for_new_data, "wait_for_new_data"}};
101  return m;
102  }
103 
104  /********************************************************************************************************************/
105 
106  AccessMode AccessModeFlags::getAccessMode(const std::string& flagName) {
107  static std::map<std::string, AccessMode> reverse_m;
108  for(const auto& m : getStringMap()) {
109  reverse_m[m.second] = m.first;
110  }
111  try {
112  return reverse_m.at(flagName);
113  }
114  catch(std::out_of_range& e) {
115  throw ChimeraTK::logic_error("Unknown flag string: " + flagName);
116  }
117  }
118 
119  /********************************************************************************************************************/
120 
121  std::vector<std::string> AccessModeFlags::split(const std::string& s) {
122  std::vector<std::string> list;
123  std::string tmp;
124  char delimiter = ',';
125 
126  std::istringstream stream(s);
127  while(getline(stream, tmp, delimiter)) {
128  list.push_back(tmp);
129  }
130  return list;
131  }
132 
133  /********************************************************************************************************************/
134 
135 } // namespace ChimeraTK
ChimeraTK::AccessModeFlags::serialize
std::string serialize() const
Get a comma seperated list of all flag strings contained in the class.
Definition: AccessMode.cc:68
ChimeraTK::AccessModeFlags::getString
static const std::string & getString(AccessMode flag)
Get a string representation of the given flag.
Definition: AccessMode.cc:81
ChimeraTK::AccessMode::raw
@ raw
Raw access: disable any possible conversion from the original hardware data type into the given UserT...
ChimeraTK::AccessModeFlags::operator==
bool operator==(const AccessModeFlags &other) const
Check whether two sets of acces mode flags are the same.
Definition: AccessMode.cc:42
ChimeraTK::AccessModeFlags::add
void add(AccessMode flag)
Add the given flag to the set.
Definition: AccessMode.cc:62
ChimeraTK::AccessModeFlags::remove
void remove(AccessMode flag)
Remove the given flag from the set.
Definition: AccessMode.cc:56
ChimeraTK::AccessModeFlags::operator<
bool operator<(const AccessModeFlags &other) const
"Less than" operator, e.g.
Definition: AccessMode.cc:49
ChimeraTK::AccessModeFlags::has
bool has(AccessMode flag) const
Check if a certain flag is in the set.
Definition: AccessMode.cc:20
ChimeraTK::AccessModeFlags::AccessModeFlags
AccessModeFlags()=default
ChimeraTK::AccessModeFlags::deserialize
static AccessModeFlags deserialize(const std::string &listOfflags)
Get an AcessModeFlags object from a comma seperated list of flag strings.
Definition: AccessMode.cc:87
ChimeraTK::AccessModeFlags::empty
bool empty() const
Check if the set is empty (i.e.
Definition: AccessMode.cc:26
ChimeraTK::AccessMode::wait_for_new_data
@ wait_for_new_data
Make any read blocking until new data has arrived since the last read.
ChimeraTK::AccessModeFlags::checkForUnknownFlags
void checkForUnknownFlags(const std::set< AccessMode > &knownFlags) const
Check of any flag which is not in the given set "knownFlags" is set.
Definition: AccessMode.cc:32
ChimeraTK::AccessMode
AccessMode
Enum type with access mode flags for register accessors.
Definition: AccessMode.h:20
AccessMode.h
ChimeraTK::AccessModeFlags
Set of AccessMode flags with additional functionality for an easier handling.
Definition: AccessMode.h:48
Exception.h
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::logic_error
Exception thrown when a logic error has occured.
Definition: Exception.h:51