The Gaudi Framework  master (bb95dfce)
Loading...
Searching...
No Matches
StringKey.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11#pragma once
12
13#include <GaudiKernel/Kernel.h>
15#include <iosfwd>
16#include <map>
17#include <string>
18#include <string_view>
19#include <vector>
20
21namespace Gaudi {
22
30 using is_transparent = void;
31 size_t operator()( std::string_view s ) const { return std::hash<std::string_view>{}( s ); }
32 };
33
68 public:
72 StringKey( const char* key ) : StringKey( std::string_view{ key } ) {}
74 StringKey( std::string_view key ) : m_hash{ StringKeyHash()( key ) }, m_str( key ) {}
76 StringKey( std::string key ) : m_hash{ StringKeyHash()( key ) }, m_str( std::move( key ) ) {}
77
79 const std::string& str() const { return m_str; }
81 operator const std::string&() const { return m_str; }
83 operator std::string_view() const { return m_str; }
85 bool empty() const { return m_str.empty(); }
87 bool operator!() const { return empty(); }
88
90 bool operator==( const StringKey& ) const = default;
92 auto operator<=>( const StringKey& ) const = default;
93
95 bool operator==( const char* rhs ) const { return m_str == rhs; }
96 bool operator==( std::string_view rhs ) const { return m_str == rhs; }
97 bool operator==( const std::string& rhs ) const { return m_str == rhs; }
98
108 std::size_t __hash__() const { return m_hash; }
110 const std::string& __str__() const; // the representation of the object
112 std::string __repr__() const; // the representation of the object
114 bool __eq__( const StringKey& right ) const;
116 bool __eq__( std::string_view right ) const;
118 bool __neq__( const StringKey& right ) const;
120 bool __neq__( const std::string_view right ) const;
121
123 std::string toString() const; // string representation (for properties)
124
125 // interoperability with std::string and const char*
126 friend std::string operator+( const std::string& lhs, const Gaudi::StringKey& rhs ) { return lhs + rhs.str(); }
127 friend std::string operator+( const char* lhs, const Gaudi::StringKey& rhs ) { return lhs + rhs.str(); }
128 friend std::string operator+( const Gaudi::StringKey& lhs, const std::string& rhs ) { return lhs.str() + rhs; }
129 friend std::string operator+( const Gaudi::StringKey& lhs, const char* rhs ) { return lhs.str() + rhs; }
130
131 private:
132 // !!!
133 // Do not change the order of these two members. The compiler-generated
134 // comparison operators will first compare the hash (fast) and then the full string.
135 // !!!
136
138 std::size_t m_hash;
139
141 std::string m_str;
142 };
143
154 inline std::size_t hash_value( const Gaudi::StringKey& key ) { return key.__hash__(); }
155} // namespace Gaudi
156
157// Streaming value -> string
158namespace Gaudi {
159 namespace Utils {
170 GAUDI_API std::ostream& toStream( const Gaudi::StringKey& key, std::ostream& s );
171 } // namespace Utils
177 inline std::ostream& operator<<( std::ostream& o, const Gaudi::StringKey& key ) { return o << key.str(); }
178} // namespace Gaudi
179
180// Parsing : string -> value
181namespace Gaudi {
182 namespace Parsers {
192 GAUDI_API StatusCode parse( Gaudi::StringKey& result, std::string_view input );
202 GAUDI_API StatusCode parse( std::vector<Gaudi::StringKey>& result, std::string_view input );
203
204 // Required for IAlgorithm::AlgResources_t
205 GAUDI_API StatusCode parse( std::map<Gaudi::StringKey, unsigned int>& result, std::string_view input );
206 GAUDI_API StatusCode parse( std::map<Gaudi::StringKey, unsigned int, std::less<>>& result, std::string_view input );
207 } // namespace Parsers
208} // namespace Gaudi
209
210namespace std {
214 template <>
215 struct hash<Gaudi::StringKey> {
216 std::size_t operator()( Gaudi::StringKey const& s ) const { return hash_value( s ); }
217 };
218} // namespace std
#define GAUDI_API
Definition Kernel.h:49
Helper class for efficient "key" access for strings.
Definition StringKey.h:67
friend std::string operator+(const std::string &lhs, const Gaudi::StringKey &rhs)
Definition StringKey.h:126
const std::string & str() const
the actual string
Definition StringKey.h:79
std::size_t __hash__() const
the actual access to the hash
Definition StringKey.h:108
friend std::string operator+(const Gaudi::StringKey &lhs, const char *rhs)
Definition StringKey.h:129
std::string m_str
the actual string
Definition StringKey.h:141
bool operator==(const char *rhs) const
Comparison to other string-like types.
Definition StringKey.h:95
bool operator==(std::string_view rhs) const
Definition StringKey.h:96
bool operator==(const std::string &rhs) const
Definition StringKey.h:97
std::size_t m_hash
the hash
Definition StringKey.h:138
operator std::string_view() const
implicit cast to std::string_view
Definition StringKey.h:83
bool operator!() const
empty key?
Definition StringKey.h:87
StringKey(const char *key)
constructor from plain C-string, perform hashing
Definition StringKey.h:72
bool operator==(const StringKey &) const =default
Compiler generated equality operator first comparing hash, then string (see below).
StringKey(std::string key)
constructor from std::string, perform hashing
Definition StringKey.h:76
auto operator<=>(const StringKey &) const =default
Compiler generated spaceship operator first comparing hash, then string (see below).
friend std::string operator+(const Gaudi::StringKey &lhs, const std::string &rhs)
Definition StringKey.h:128
bool empty() const
empty key?
Definition StringKey.h:85
StringKey(std::string_view key)
constructor from std::string_view, perform hashing
Definition StringKey.h:74
StringKey()
Default constructor from empty string.
Definition StringKey.h:70
friend std::string operator+(const char *lhs, const Gaudi::StringKey &rhs)
Definition StringKey.h:127
StatusCode parse(GaudiUtils::HashMap< K, V > &result, std::string_view input)
Basic parser for the types of HashMap used in DODBasicMapper.
std::ostream & toStream(ITERATOR first, ITERATOR last, std::ostream &s, const std::string &open, const std::string &close, const std::string &delim)
the helper function to print the sequence
Definition ToStream.h:307
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
std::ostream & operator<<(std::ostream &o, const Gaudi::StringKey &key)
printout of the object reply on the native printout for the string
Definition StringKey.h:177
std::size_t hash_value(const Gaudi::StringKey &key)
hash-function: heeded for boost::hash
Definition StringKey.h:154
STL namespace.
Transparent hash operator for heterogeneous lookups.
Definition StringKey.h:29
size_t operator()(std::string_view s) const
Definition StringKey.h:31
std::size_t operator()(Gaudi::StringKey const &s) const
Definition StringKey.h:216