ATLAS Offline Software
Loading...
Searching...
No Matches
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>
12using namespace SG;
13using namespace std;
14
16
17bool
18VersionedKey::isVersionedKey(const char * vkey) {
19 return (vkey[0]==separator() && vkey[3]==separator());
20}
21
22bool
23VersionedKey::isVersionedKey(const std::string& vkey) {
24 return isVersionedKey(vkey.c_str());
25}
26
27bool
28VersionedKey::isAuto(const std::string& vkey) {
29 return (vkey.compare(0,4, VersionedKey::autoVS())==0);
30}
31
32VersionedKey::VersionedKey(const char* key, unsigned char version) {
33 encode(std::string(key), version);
34}
35VersionedKey::VersionedKey(const std::string& key, unsigned char version) {
37}
38VersionedKey::VersionedKey(const std::string& versionedKey) {
39 copyVK(versionedKey);
40}
41VersionedKey::VersionedKey(const char* versionedKey) {
42 copyVK(std::string(versionedKey));
43}
44
45void 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
51void 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
59void 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}
67unsigned char VersionedKey::version() const {
68 return (unsigned char) std::stoul(m_versionKey.substr(1,2), nullptr, 0);
69}
70
71const std::string& VersionedKey::key() const {
72 return m_baseKey;
73}
74
75bool
77 return (this->key() == vkey.key());
78}
79
80bool
81VersionedKey::sameKey(const std::string& baseKey) const {
82 return (this->key() == baseKey);
83}
84
85bool
86VersionedKey::sameKey(const char* baseKey) const {
87 return (this->key() == std::string(baseKey));
88}
89
91bool 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}
bool operator<(const DataVector< T > &a, const DataVector< T > &b)
Vector ordering relation.
defines a StoreGateSvc key with a version number
a StoreGateSvc key with a version number.
bool sameKey(const VersionedKey &vkey) const
compare base keys
VersionedKey(const char *key, unsigned char version)
version must be [0,98], 0 is the default version
unsigned char version() const
bool isAuto() const
static bool isVersionedKey(const char *)
quickly determine whether a string has the right format to be a VK
void copyVK(const std::string &inKey)
static const char * autoVS()
static char separator()
VersionedKey()
default constructor (invalid state, do not use)
void encode(const std::string &inKey, unsigned char version)
std::string m_baseKey
static const char * versionFormatString()
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)...
std::string m_versionKey
the encoded version/key.
const std::string & key() const
Forward declaration.
STL namespace.