ATLAS Offline Software
Token.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #include <cstdio>
8 #include <cstring>
9 #include <climits>
10 #include <atomic>
11 #include <format>
12 #include <charconv>
13 #include <string_view>
14 
15 constexpr std::string_view LABEL_DB = "[DB=";
16 constexpr std::string_view LABEL_CNT = "[CNT=";
17 constexpr std::string_view LABEL_CLID = "[CLID=";
18 constexpr std::string_view LABEL_TECH = "[TECH=";
19 constexpr std::string_view LABEL_OID = "[OID=";
20 
21 static const int KEY_MASK = (~0u) << CHAR_BIT;
22 static std::atomic<int> s_numCount { 0 };
23 
24 int Token::numInstances() { return s_numCount; }
25 
27 Token::Token() : m_refCount(1),
28  m_technology(0),
29  m_dbID(Guid::null()),
30  m_classID(Guid::null()),
31  m_oid(OID_t(~0x0LL, ~0x0LL)),
32  m_type(0) {
33  s_numCount.fetch_add(1, std::memory_order_relaxed);
34 }
35 
37 Token::Token(const Token& copy) : m_refCount(1),
38  m_technology(copy.m_technology),
39  m_dbID(copy.m_dbID),
40  m_classID(copy.m_classID),
41  m_oid(copy.m_oid),
42  m_type(0) {
43  copy.setData(this);
44  s_numCount.fetch_add(1, std::memory_order_relaxed);
45 }
46 
48 Token::Token(const Token* source) : m_refCount(1),
49  m_technology(0),
50  m_dbID(Guid::null()),
51  m_classID(Guid::null()),
52  m_oid(OID_t(~0x0LL, ~0x0LL)),
53  m_type(0) {
54  if (source != 0) {
55  source->setData(this);
56  }
57  s_numCount.fetch_add(1, std::memory_order_relaxed);
58 }
59 
62  : m_refCount (1),
63  m_technology (source.m_technology),
64  m_dbID (std::move (source.m_dbID)),
65  m_cntID (std::move (source.m_cntID)),
66  m_classID (std::move (source.m_classID)),
67  m_oid (std::move (source.m_oid)),
68  m_type (source.m_type),
69  m_auxString (std::move (source.m_auxString))
70 {
71  s_numCount.fetch_add(1, std::memory_order_relaxed);
72 }
73 
74 
76  s_numCount.fetch_sub(1, std::memory_order_relaxed);
77 }
78 
81  int cnt = --m_refCount;
82  if (0 >= cnt) {
83  delete this;
84  }
85  return cnt;
86 }
87 
90  if (&copy != this) {
91  copy.setData(this);
92  }
93  return *this;
94 }
95 
97 bool Token::equal(const Token& copy) const {
98  if (&copy != this) {
99  if (m_oid.second == copy.m_oid.second) {
100  if (m_classID == copy.m_classID) {
101  if (m_dbID == copy.m_dbID) {
102  if (m_cntID == copy.m_cntID) {
103  return true;
104  }
105  }
106  }
107  }
108  return false;
109  }
110  return true;
111 }
112 
114 bool Token::less(const Token& copy) const {
115  if (&copy != this) {
116  if (m_oid.second < copy.m_oid.second)
117  return true;
118  else if (m_oid.second > copy.m_oid.second)
119  return false;
120  if (!(m_classID == copy.m_classID)) {
121  return (m_classID < copy.m_classID);
122  }
123  if (!(m_dbID == copy.m_dbID)) {
124  return (m_dbID < copy.m_dbID);
125  }
126  int res = m_cntID.compare(copy.m_cntID);
127  if (res != 0) {
128  return (res < 0);
129  }
130  }
131  return false;
132 }
133 
134 const std::string Token::toString() const {
135  return std::format(
136  "[DB={}][CNT={}][CLID={}][TECH={:08X}][OID={:016X}-{:016X}]{}",
137  m_dbID.toString(),
138  m_cntID,
140  m_technology,
141  static_cast<uint64_t>(m_oid.first),
142  static_cast<uint64_t>(m_oid.second),
144  );
145 }
146 
147 Token& Token::fromString(const std::string_view src) {
148  m_auxString.clear();
149  size_t pos = 0;
150  while (pos < src.size()) {
151  size_t start = src.find('[', pos);
152  if (start == std::string_view::npos) break;
153  size_t eq = src.find('=', start);
154  size_t end = src.find(']', start);
155  if (eq != std::string_view::npos && end != std::string_view::npos) {
156  std::string_view label = src.substr(start, eq - start + 1);
157  if (label == LABEL_DB) {
158  m_dbID.fromString(src.substr(eq + 1, end - eq - 1));
159  } else if (label == LABEL_CNT) {
160  m_cntID = std::string(src.substr(eq + 1, end - eq - 1));
161  } else if (label == LABEL_CLID) {
162  m_classID.fromString(src.substr(eq + 1, end - eq - 1));
163  } else if (label == LABEL_TECH) {
164  std::string_view num_str = src.substr(eq + 1, end - eq - 1);
165  int tech = 0;
166  std::from_chars(num_str.data(), num_str.data() + num_str.size(), tech, 16);
167  m_technology = tech;
168  } else if (label == LABEL_OID) {
169  std::string_view oid_str = src.substr(eq + 1, end - eq - 1);
170  size_t dash = oid_str.find('-');
171  if (dash != std::string_view::npos) {
172  std::string_view first_str = oid_str.substr(0, dash);
173  std::string_view second_str = oid_str.substr(dash + 1);
174 
175  // Check if this is legacy format (8 digits) vs modern format (16 digits)
176  // Legacy format: 5 + 8 + 1 + 8 + 1 = 23 characters total for [OID=XXXXXXXX-XXXXXXXX]
177  // Modern format: 5 + 16 + 1 + 16 + 1 = 39 characters total for [OID=XXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX]
178  bool is_legacy = (end - start + 1) == 23; // Total bracket length check
179 
180  uint64_t first = 0;
181  uint64_t second = 0;
182  std::from_chars(first_str.data(), first_str.data() + first_str.size(), first, 16);
183  std::from_chars(second_str.data(), second_str.data() + second_str.size(), second, 16);
184 
185  if (is_legacy) {
186  // Handle legacy format: extend 32-bit ~0x0 to 64-bit ~0x0LL
187  if (static_cast<uint32_t>(first) == ~0x0U) first = ~0x0ULL;
188  if (static_cast<uint32_t>(second) == ~0x0U) second = ~0x0ULL;
189  }
190 
191  m_oid.first = static_cast<long long int>(first);
192  m_oid.second = static_cast<long long int>(second);
193  }
194  } else {
195  m_auxString += src.substr(start, end - start + 1);
196  }
197  pos = end + 1;
198  } else {
199  break;
200  }
201  }
202  return *this;
203 }
204 
206 const std::string Token::key() const {
207  return std::format(
208  "[DB={}][CNT={}][CLID={}][TECH={:08X}]",
209  m_dbID.toString(),
210  m_cntID,
212  m_technology & KEY_MASK
213  );
214 }
215 
216 const Token& Token::set(Token* pToken) const {
217  pToken->m_technology = m_technology;
218  pToken->m_dbID = m_dbID;
219  pToken->m_cntID = m_cntID;
220  pToken->m_classID = m_classID;
221  pToken->m_oid.first = m_oid.first;
222  return *this;
223 }
224 
225 const Token& Token::setData(Token* pToken) const {
226  this->set(pToken);
227  pToken->m_oid.second = m_oid.second;
228  pToken->m_type = m_type;
229  pToken->m_auxString = m_auxString;
230  return *this;
231 }
Token::m_type
int m_type
Token type.
Definition: Token.h:123
Guid::toString
constexpr void toString(std::span< char, 36 > buf, bool uppercase=true) const noexcept
Automatic conversion to string representation.
Definition: Guid.h:179
vtune_athena.format
format
Definition: vtune_athena.py:14
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
WriteCellNoiseToCool.src
src
Definition: WriteCellNoiseToCool.py:513
mergePhysValFiles.start
start
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:13
Token::m_dbID
Guid m_dbID
Database identifier.
Definition: Token.h:115
python.SystemOfUnits.second
float second
Definition: SystemOfUnits.py:135
Token::m_technology
unsigned int m_technology
Technology identifier.
Definition: Token.h:113
Token::less
virtual bool less(const Token &pTok) const
Fast token comparison: operator less.
Definition: Token.cxx:114
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:92
Token
This class provides a token that identifies in a unique way objects on the persistent storage.
Definition: Token.h:21
Token::m_classID
Guid m_classID
Object global identifier.
Definition: Token.h:119
LABEL_OID
constexpr std::string_view LABEL_OID
Definition: Token.cxx:19
Token::m_cntID
std::string m_cntID
Container identifier.
Definition: Token.h:117
m_type
TokenType m_type
the type
Definition: TProperty.cxx:44
Token::OID_t
Definition: Token.h:24
LABEL_TECH
constexpr std::string_view LABEL_TECH
Definition: Token.cxx:18
Token::operator=
Token & operator=(const Token &copy)
No assignment allowed: put prototype to disable bit-wise assignment.
Definition: Token.cxx:89
Token::equal
virtual bool equal(const Token &pTok) const
Fast token comparison: operator equals.
Definition: Token.cxx:97
LABEL_CNT
constexpr std::string_view LABEL_CNT
Definition: Token.cxx:16
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:11
add-xsec-uncert-quadrature-N.label
label
Definition: add-xsec-uncert-quadrature-N.py:104
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
Token::release
int release()
Release token: Decrease reference count and eventually delete.
Definition: Token.cxx:80
Token::m_oid
OID_t m_oid
Persistent object identifier.
Definition: Token.h:121
Token::toString
virtual const std::string toString() const
Retrieve the string representation of the token.
Definition: Token.cxx:134
LABEL_DB
constexpr std::string_view LABEL_DB
Definition: Token.cxx:15
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:16
Token::fromString
Token & fromString(const std::string_view from)
Build from the string representation of a token.
Definition: Token.cxx:147
LABEL_CLID
constexpr std::string_view LABEL_CLID
Definition: Token.cxx:17
Guid
This class provides a encapsulation of a GUID/UUID/CLSID/IID data structure (128 bit number).
Definition: Guid.h:24
Token::~Token
virtual ~Token()
Standard destructor: release all allocated resources.
Definition: Token.cxx:75
Token::m_auxString
std::string m_auxString
Auxiliary string.
Definition: Token.h:125
trigbs_pickEvents.cnt
cnt
Definition: trigbs_pickEvents.py:71
Token::Token
Token()
Standard Constructor.
Definition: Token.cxx:27
DeMoScan.first
bool first
Definition: DeMoScan.py:534
copySelective.source
string source
Definition: copySelective.py:31
calibdata.copy
bool copy
Definition: calibdata.py:26
Token::numInstances
static int numInstances()
expose Token instance counter for debugging
Definition: Token.cxx:24
Token::m_refCount
int m_refCount
Reference count.
Definition: Token.h:111
Token::setData
const Token & setData(Token *pToken) const
Set all the data part of the token.
Definition: Token.cxx:225
Token::set
const Token & set(Token *pToken) const
Set token information.
Definition: Token.cxx:216
Token.h
This file contains the class definition for the Token class (migrated from POOL).
Guid::fromString
constexpr Guid & fromString(std::string_view s)
Automatic conversion from string representation.
Definition: Guid.h:99
Token::key
virtual const std::string key() const
Retrieve token key.
Definition: Token.cxx:206