tdaq-develop-2025-02-12
TableGroupKey.cc
1 #include "otsdaq/TableCore/TableGroupKey.h"
2 
3 #include "otsdaq/Macros/CoutMacros.h"
4 
5 #include <string.h> //for strlen
6 #include <stdexcept> //for runtime_error
7 
8 using namespace ots;
9 
10 const unsigned int TableGroupKey::INVALID = -1;
11 const unsigned int TableGroupKey::DEFAULT = 0;
12 
13 //==============================================================================
14 TableGroupKey::TableGroupKey(unsigned int key) : key_(key) {}
15 
16 //==============================================================================
18 TableGroupKey::TableGroupKey(char* const& groupString)
19 {
20  if(!groupString)
21  {
22  key_ = TableGroupKey::INVALID;
23  return;
24  }
25 
26  // find last character that is not part of key
27  // key consists of numeric, dash, underscore, and period
28  int i = strlen(groupString) - 1;
29  for(; i >= 0; --i)
30  if((groupString[i] < '0' || groupString[i] > '9') && groupString[i] != '-' &&
31  groupString[i] != '_' && groupString[i] != '.')
32  break; // not part of key,... likely a 'v' if using "_v" syntax for version
33 
34  if(i == (int)strlen(groupString) - 1) // no key characters found
35  {
36  key_ = TableGroupKey::DEFAULT;
37  return;
38  }
39  else if(i < 0) // only key characters found, so assume group key string was given
40  i = 0;
41  else
42  ++i;
43 
44  // at this point, i is start of key sequence
45  sscanf(&groupString[i], "%u", &key_);
46 }
47 
48 //==============================================================================
49 TableGroupKey::TableGroupKey(const std::string& groupString)
50  : TableGroupKey((char*)groupString.c_str())
51 {
52 }
53 
54 //==============================================================================
55 TableGroupKey::~TableGroupKey(void) {}
56 
57 //==============================================================================
58 unsigned int TableGroupKey::key(void) const { return key_; }
59 
60 //==============================================================================
62 bool TableGroupKey::operator==(unsigned int key) const { return (key_ == key); }
63 bool TableGroupKey::operator==(const TableGroupKey& key) const
64 {
65  return (key_ == key.key_);
66 }
67 
68 //==============================================================================
72 {
73  key_ *= a;
74  return *this;
75 }
76 //==============================================================================
80 {
81  key_ *= a.key_;
82  return *this;
83 }
84 //==============================================================================
88 {
89  key_ += a.key_;
90  return *this;
91 }
92 //==============================================================================
96 {
97  key_ -= a.key_;
98  return *this;
99 }
100 //==============================================================================
104 {
105  key_ /= a.key_;
106  return *this;
107 }
108 
109 //==============================================================================
111 std::string TableGroupKey::toString(void) const
112 {
113  // represent invalid/temporary versions as negative number strings
114  return (isInvalid()) ? std::to_string((int)key_) : std::to_string(key_);
115 }
116 
117 //==============================================================================
119 TableGroupKey& TableGroupKey::operator=(const unsigned int key)
120 {
121  key_ = key;
122  return *this;
123 }
124 
125 //==============================================================================
126 bool TableGroupKey::operator!=(unsigned int key) const { return (key_ != key); }
127 bool TableGroupKey::operator!=(const TableGroupKey& key) const
128 {
129  return (key_ != key.key_);
130 }
131 
132 //==============================================================================
135 {
136  return (key_ < key.key_);
137 }
138 
139 //==============================================================================
142 {
143  return (key_ > key.key_);
144 }
145 
146 //==============================================================================
148 bool TableGroupKey::isInvalid() const { return (key_ == INVALID); }
149 
150 //==============================================================================
157 {
158  TableGroupKey retKey(key.key_ + 1); // DEFAULT := INVALID + 1
159  return retKey; // if retKey is invalid, then INVALID is returned already
160 }
161 
162 //==============================================================================
163 unsigned int TableGroupKey::getDefaultKey(void) { return DEFAULT; }
164 
165 //==============================================================================
166 unsigned int TableGroupKey::getInvalidKey(void) { return INVALID; }
167 
168 //==============================================================================
174 std::string TableGroupKey::getFullGroupString(const std::string& groupName,
175  const TableGroupKey& key,
176  const std::string& preKey /* = "_v" */,
177  const std::string& postKey /* = "" */)
178 {
179  if(groupName.size() == 0)
180  {
181  __SS__
182  << ("TableGroupKey::getFullGroupString() Illegal Group Name! The Group Name "
183  "was not provided.\n");
184  __COUT_ERR__ << ss.str();
185  __SS_THROW__;
186  }
187  else if(groupName.size() == 1)
188  {
189  __SS__ << ("TableGroupKey::getFullGroupString() Illegal Group Name! The Group "
190  "Name is too short: \"" +
191  groupName + "\"")
192  << std::endl;
193  __COUT_ERR__ << ss.str();
194  __SS_THROW__;
195  }
196  else
197  {
198  for(unsigned int i = 0; i < groupName.size(); ++i)
199  {
200  if(!( // alphaNumeric
201  (groupName[i] >= 'A' && groupName[i] <= 'Z') ||
202  (groupName[i] >= 'a' && groupName[i] <= 'z') ||
203  (groupName[i] >= '0' && groupName[i] <= '9')))
204  {
205  __SS__ << ("TableGroupKey::getFullGroupString() Illegal Group Name! "
206  "Group Name must be alpha-numeric: \"" +
207  groupName + "\"")
208  << std::endl;
209  __COUT_ERR__ << ss.str();
210  __SS_THROW__;
211  }
212  }
213  }
214 
215  return groupName + preKey + std::to_string(key.key_) + postKey;
216 }
217 
218 //==============================================================================
220 void TableGroupKey::getGroupNameAndKey(const std::string& fullGroupString,
221  std::string& groupName,
222  TableGroupKey& key)
223 {
224  auto i = fullGroupString.rfind("_v");
225  groupName = fullGroupString.substr(0, i);
226  key = TableGroupKey(fullGroupString.substr(i + 2));
227 }
std::string toString(void) const
toString
static TableGroupKey getNextKey(const TableGroupKey &key=TableGroupKey())
bool operator>(const TableGroupKey &key) const
operator>
bool operator==(unsigned int key) const
operator==
TableGroupKey & operator=(const unsigned int key)
Operators.
static void getGroupNameAndKey(const std::string &fullGroupString, std::string &groupName, TableGroupKey &key)
requires fullGroupString created as name + "_v" + key + ""
bool isInvalid(void) const
isInvalid
TableGroupKey & operator/=(const TableGroupKey a)
to support StringMacros on TableGroupKey types
TableGroupKey & operator-=(const TableGroupKey a)
to support StringMacros on TableGroupKey types
TableGroupKey & operator+=(const TableGroupKey a)
to support StringMacros on TableGroupKey types
TableGroupKey & operator*=(const unsigned int a)
to support StringMacros on TableGroupKey types
static std::string getFullGroupString(const std::string &groupName, const TableGroupKey &key, const std::string &preKey="_v", const std::string &postKey="")
bool operator<(const TableGroupKey &key) const
operator<