ATLAS Offline Software
SGVersionedKey.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <cstdio>
6 #include <cstdlib>
7 #include <cstring>
8 #include <iostream>
9 #include <stdexcept>
10 #include <cassert>
11 #include "SGTools/SGVersionedKey.h"
12 using namespace SG;
13 using namespace std;
14 
16 
17 bool
18 VersionedKey::isVersionedKey(const char * vkey) {
19  return (vkey[0]==separator() && vkey[3]==separator());
20 }
21 
22 bool
23 VersionedKey::isVersionedKey(const std::string& vkey) {
24  return isVersionedKey(vkey.c_str());
25 }
26 
27 bool
28 VersionedKey::isAuto(const std::string& vkey) {
29  return (vkey.compare(0,4, VersionedKey::autoVS())==0);
30 }
31 
32 VersionedKey::VersionedKey(const char* key, unsigned char version) {
33  encode(std::string(key), version);
34 }
35 VersionedKey::VersionedKey(const std::string& key, unsigned char version) {
36  encode(key, version);
37 }
38 VersionedKey::VersionedKey(const std::string& versionedKey) {
39  copyVK(versionedKey);
40 }
41 VersionedKey::VersionedKey(const char* versionedKey) {
42  copyVK(std::string(versionedKey));
43 }
44 
45 void VersionedKey::decode(std::string& outKey, unsigned char& version) const {
46  outKey = this->key();
47  version = (unsigned char)atoi(m_versionKey.substr(1,2).c_str());
48  assert(version <= 99);
49 }
50 
51 void VersionedKey::encode(const std::string& inKey, unsigned char version) {
52  assert(version <= 99);
53  char vers[6];
54  snprintf(vers, 6, versionFormatString(), version);
55  m_versionKey = vers + inKey;
56  m_baseKey = inKey;
57 }
58 
59 void VersionedKey::copyVK(const std::string& inKey) {
60  if (isVersionedKey(inKey)) {
61  m_versionKey = inKey;
62  m_baseKey = m_versionKey.substr(4);
63  } else {
64  encode(inKey, 0); //FIXME should autoincrement
65  }
66 }
67 unsigned char VersionedKey::version() const {
68  return (unsigned char) std::stoul(m_versionKey.substr(1,2), nullptr, 0);
69 }
71 const std::string& VersionedKey::key() const {
72  return m_baseKey;
73 }
74 
75 bool
76 VersionedKey::sameKey(const VersionedKey& vkey) const {
77  return (this->key() == vkey.key());
78 }
79 
80 bool
81 VersionedKey::sameKey(const std::string& baseKey) const {
82  return (this->key() == baseKey);
83 }
84 
85 bool
86 VersionedKey::sameKey(const char* baseKey) const {
87  return (this->key() == std::string(baseKey));
88 }
89 
91 bool operator < (const SG::VersionedKey& lhs, const SG::VersionedKey& rhs) {
92  std::string lhskey;
93  unsigned char lhsVersion(0);
94  lhs.decode(lhskey, lhsVersion);
95  unsigned char rhsVersion(0);
96  std::string rhskey;
97  rhs.decode(rhskey, rhsVersion);
98  int keyCompare(strcmp(lhskey.c_str(), rhskey.c_str()));
99  return ( ( keyCompare < 0) ||
100  ( (keyCompare == 0) && (lhsVersion < rhsVersion) ) ) ;
101 
102 }
SG::VersionedKey::isVersionedKey
static bool isVersionedKey(const char *)
quickly determine whether a string has the right format to be a VK
Definition: SGVersionedKey.cxx:18
SG::VersionedKey::VersionedKey
VersionedKey()
default constructor (invalid state, do not use)
Definition: SGVersionedKey.h:89
SG::VersionedKey::isAuto
bool isAuto() const
Definition: SGVersionedKey.h:84
SG::VersionedKey::decode
void decode(std::string &outKey, unsigned char &version) const
sets outKey to point to base key, and version to encoded version (0 is taken to mean default version)...
Definition: SGVersionedKey.cxx:45
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
SG::VersionedKey::version
unsigned char version() const
Definition: SGVersionedKey.cxx:67
xAOD::char
char
Definition: TrigDecision_v1.cxx:38
operator<
bool operator<(const SG::VersionedKey &lhs, const SG::VersionedKey &rhs)
sort according to highest key version
Definition: SGVersionedKey.cxx:91
SGVersionedKey.h
SG::VersionedKey::autoVS
static const char * autoVS()
Definition: SGVersionedKey.h:100
SG::VersionedKey
a StoreGateSvc key with a version number. Notice that StoreGate does not order multiple instances of ...
Definition: SGVersionedKey.h:31
SG::VersionedKey::encode
void encode(const std::string &inKey, unsigned char version)
Definition: SGVersionedKey.cxx:51
SG::VersionedKey::key
const std::string & key() const
Definition: SGVersionedKey.cxx:71
SG::VersionedKey::~VersionedKey
~VersionedKey()
Definition: SGVersionedKey.cxx:15
get_generator_info.version
version
Definition: get_generator_info.py:33
SG::VersionedKey::sameKey
bool sameKey(const VersionedKey &vkey) const
compare base keys
Definition: SGVersionedKey.cxx:76
SG::VersionedKey::copyVK
void copyVK(const std::string &inKey)
Definition: SGVersionedKey.cxx:59
CxxUtils::atoi
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
Definition: Control/CxxUtils/Root/StringUtils.cxx:85
python.PerfMonSerializer.encode
def encode(data, use_base64=True)
Definition: PerfMonSerializer.py:375
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37