ATLAS Offline Software
Token.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #include <cstdio>
8 #include <cstring>
9 #include <climits>
10 #include <atomic>
11 
12 static const char* const fmt_clid = "[CLID=";
13 static const char* const fmt_tech = "[TECH=%08X]";
14 static const char* const fmt_oid = "[OID=%016llX-%016llX]";
15 static const char* const fmt_oid_old = "[OID=%08llX-%08llX]";
16 static const int KEY_MASK = (~0u) << CHAR_BIT;
17 static std::atomic<int> s_numCount { 0 };
18 
19 int Token::numInstances() { return s_numCount; }
20 
22 Token::Token() : m_refCount(1),
23  m_technology(0),
24  m_dbID(Guid::null()),
25  m_classID(Guid::null()),
26  m_oid(OID_t(~0x0LL, ~0x0LL)),
27  m_type(0) {
28  s_numCount++;
29 }
30 
32 Token::Token(const Token& copy) : m_refCount(1),
33  m_technology(copy.m_technology),
34  m_dbID(copy.m_dbID),
35  m_classID(copy.m_classID),
36  m_oid(copy.m_oid),
37  m_type(0) {
38  copy.setData(this);
39  s_numCount++;
40 }
41 
43 Token::Token(const Token* source) : m_refCount(1),
44  m_technology(0),
45  m_dbID(Guid::null()),
46  m_classID(Guid::null()),
47  m_oid(OID_t(~0x0LL, ~0x0LL)),
48  m_type(0) {
49  if (source != 0) {
50  source->setData(this);
51  }
52  s_numCount++;
53 }
54 
56  s_numCount--;
57 }
58 
61  int cnt = --m_refCount;
62  if (0 >= cnt) {
63  delete this;
64  }
65  return cnt;
66 }
67 
70  if (&copy != this) {
71  copy.setData(this);
72  }
73  return *this;
74 }
75 
77 bool Token::equal(const Token& copy) const {
78  if (&copy != this) {
79  if (m_oid.second == copy.m_oid.second) {
80  if (m_classID == copy.m_classID) {
81  if (m_dbID == copy.m_dbID) {
82  if (m_cntID == copy.m_cntID) {
83  return true;
84  }
85  }
86  }
87  }
88  return false;
89  }
90  return true;
91 }
92 
94 bool Token::less(const Token& copy) const {
95  if (&copy != this) {
96  if (m_oid.second < copy.m_oid.second)
97  return true;
98  else if (m_oid.second > copy.m_oid.second)
99  return false;
100  if (!(m_classID == copy.m_classID)) {
101  return (m_classID < copy.m_classID);
102  }
103  if (!(m_dbID == copy.m_dbID)) {
104  return (m_dbID < copy.m_dbID);
105  }
106  int res = m_cntID.compare(copy.m_cntID);
107  if (res != 0) {
108  return (res < 0);
109  }
110  }
111  return false;
112 }
113 
114 const std::string Token::toString() const {
115  char text1[128];
116  int s1 = sprintf(text1, fmt_tech, m_technology);
117  char text2[128];
118  int s2 = sprintf(text2, fmt_oid, m_oid.first, m_oid.second);
119  std::string str = "[DB=";
120  str.reserve(32);
121  str += m_dbID.toString();
122  str += "][CNT=";
123  str += m_cntID;
124  str += "][CLID=";
125  str += m_classID.toString();
126  str += ']';
127  str.append(text1, s1);
128  str.append(text2, s2);
129  str += m_auxString;
130  return str;
131 }
132 
133 Token& Token::fromString(const std::string& source) {
134  m_auxString.clear();
135  for (const char* p1 = source.c_str(); p1; p1 = ::strchr(++p1,'[')) {
136  const char* p2 = ::strchr(p1, '=');
137  const char* p3 = ::strchr(p1, ']');
138  if (p2 != 0 && p3 != 0) {
139  if (::strncmp("[DB=", p1, 4) == 0) {
140  m_dbID.fromString(p1 + 4);
141  } else if (::strncmp("[CNT=", p1, 5) == 0) {
142  m_cntID.assign (p2+1, p3-p2-1);
143  } else if (::strncmp(fmt_oid, p1, 5) == 0) {
144  long long unsigned int first, second;
145  if (::strncmp("]", p1 + 22, 1) == 0) { // 5 + 8(int) + 1(minus) + 8(int) = 22
146  ::sscanf(p1, fmt_oid_old, &first, &second);
147  if (int(first) == ~0x0) first = ~0x0LL;
148  if (int(second) == ~0x0) second = ~0x0LL;
149  } else {
150  ::sscanf(p1, fmt_oid, &first, &second);
151  }
152  m_oid.first = first;
153  m_oid.second = second;
154  } else if (::strncmp(fmt_clid, p1, 6) == 0) {
155  m_classID.fromString(p1 + 6);
156  } else if (::strncmp(fmt_tech, p1, 6) == 0) {
157  ::sscanf(p1, fmt_tech, &m_technology);
158  } else {
159  while (*(p2 + 1) == '[' && p3 && *(++p3) != 0 && *p3 != ']') {
160  p3 = ::strchr(p3, ']');
161  }
162  if (!p3) p3 = source.c_str() + source.size();
163  m_auxString.append (p1, p3-p1);
164  m_auxString += ']';
165  }
166  }
167  }
168  return *this;
169 }
170 
172 const std::string Token::key() const {
173  char text[1024];
174  int s = ::sprintf(text, "][TECH=%08X]", m_technology & KEY_MASK);
175  std::string k = "[DB=";
176  k += m_dbID.toString();
177  k += "][CNT=";
178  k += m_cntID;
179  k += "][CLID=";
180  k += m_classID.toString();
181  k.append(text, s);
182  return k;
183 }
184 
185 const Token& Token::set(Token* pToken) const {
186  pToken->m_technology = m_technology;
187  pToken->m_dbID = m_dbID;
188  pToken->m_cntID = m_cntID;
189  pToken->m_classID = m_classID;
190  pToken->m_oid.first = m_oid.first;
191  return *this;
192 }
193 
194 const Token& Token::setData(Token* pToken) const {
195  this->set(pToken);
196  pToken->m_oid.second = m_oid.second;
197  return *this;
198 }
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
ReadCellNoiseFromCoolCompare.s1
s1
Definition: ReadCellNoiseFromCoolCompare.py:378
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
Token::m_dbID
Guid m_dbID
Database identifier.
Definition: Token.h:113
Guid::toString
const std::string toString() const
Automatic conversion to string representation.
Definition: Guid.cxx:58
Token::m_technology
unsigned int m_technology
Technology identifier.
Definition: Token.h:111
Token::less
virtual bool less(const Token &pTok) const
Fast token comparison: operator less.
Definition: Token.cxx:94
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:83
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:117
ParticleJetTools::p3
Amg::Vector3D p3(const xAOD::TruthVertex *p)
Definition: ParticleJetLabelCommon.cxx:55
Token::m_cntID
std::string m_cntID
Container identifier.
Definition: Token.h:115
m_type
TokenType m_type
the type
Definition: TProperty.cxx:44
Token::OID_t
Definition: Token.h:24
Token::fromString
Token & fromString(const std::string &from)
Build from the string representation of a token.
Definition: Token.cxx:133
Token::operator=
Token & operator=(const Token &copy)
No assignment allowed: put prototype to disable bit-wise assignment.
Definition: Token.cxx:69
Token::equal
virtual bool equal(const Token &pTok) const
Fast token comparison: operator equals.
Definition: Token.cxx:77
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
Token::release
int release()
Release token: Decrease reference count and eventually delete.
Definition: Token.cxx:60
Token::m_oid
OID_t m_oid
Persistent object identifier.
Definition: Token.h:119
Token::toString
virtual const std::string toString() const
Retrieve the string representation of the token.
Definition: Token.cxx:114
Guid
This class provides a encapsulation of a GUID/UUID/CLSID/IID data structure (128 bit number).
Definition: Guid.h:20
Token::~Token
virtual ~Token()
Standard destructor: release all allocated resources.
Definition: Token.cxx:55
python.CaloScaleNoiseConfig.str
str
Definition: CaloScaleNoiseConfig.py:78
Token::m_auxString
std::string m_auxString
Auxiliary string.
Definition: Token.h:123
trigbs_pickEvents.cnt
cnt
Definition: trigbs_pickEvents.py:71
Token::Token
Token()
Standard Constructor.
Definition: Token.cxx:22
DeMoScan.first
bool first
Definition: DeMoScan.py:534
ReadCellNoiseFromCoolCompare.s2
s2
Definition: ReadCellNoiseFromCoolCompare.py:379
Guid::fromString
const Guid & fromString(const std::string &s)
Automatic conversion from string representation.
Definition: Guid.cxx:65
makeTransCanvas.text
text
Definition: makeTransCanvas.py:11
str
Definition: BTagTrackIpAccessor.cxx:11
calibdata.copy
bool copy
Definition: calibdata.py:27
Token::numInstances
static int numInstances()
expose Token instance counter for debugging
Definition: Token.cxx:19
Token::m_refCount
int m_refCount
Reference count.
Definition: Token.h:109
Token::setData
const Token & setData(Token *pToken) const
Set all the data part of the token.
Definition: Token.cxx:194
Token::set
const Token & set(Token *pToken) const
Set token information.
Definition: Token.cxx:185
Token.h
This file contains the class definition for the Token class (migrated from POOL).
fitman.k
k
Definition: fitman.py:528
Token::key
virtual const std::string key() const
Retrieve token key.
Definition: Token.cxx:172