The Gaudi Framework  master (fcd9f667)
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 <compare>
16#include <iosfwd>
17#include <map>
18#include <string>
19#include <string_view>
20#include <vector>
21
22namespace Gaudi {
23
31 using is_transparent = void;
32 size_t operator()( std::string_view s ) const { return std::hash<std::string_view>{}( s ); }
33 };
34
69 public:
73 StringKey( const char* key ) : StringKey( std::string_view{ key } ) {}
75 StringKey( std::string_view key ) : m_hash{ StringKeyHash()( key ) }, m_str( key ) {}
77 StringKey( std::string key ) : m_hash{ StringKeyHash()( key ) }, m_str( std::move( key ) ) {}
78
80 const std::string& str() const { return m_str; }
82 operator const std::string&() const { return m_str; }
84 operator std::string_view() const { return m_str; }
86 bool empty() const { return m_str.empty(); }
88 bool operator!() const { return empty(); }
89
91 bool operator==( const StringKey& ) const = default;
93 auto operator<=>( const StringKey& ) const = default;
94
96 bool operator==( const char* rhs ) const { return m_str == rhs; }
97 bool operator==( std::string_view rhs ) const { return m_str == rhs; }
98 bool operator==( const std::string& rhs ) const { return m_str == rhs; }
99
100 auto operator<=>( const char* rhs ) const { return m_str <=> rhs; }
101 auto operator<=>( std::string_view rhs ) const { return m_str <=> rhs; }
102 auto operator<=>( const std::string& rhs ) const { return m_str <=> rhs; }
103
113 std::size_t __hash__() const { return m_hash; }
115 const std::string& __str__() const; // the representation of the object
117 std::string __repr__() const; // the representation of the object
119 bool __eq__( const StringKey& right ) const;
121 bool __eq__( std::string_view right ) const;
123 bool __neq__( const StringKey& right ) const;
125 bool __neq__( const std::string_view right ) const;
126
128 std::string toString() const; // string representation (for properties)
129
130 // interoperability with std::string and const char*
131 friend std::string operator+( const std::string& lhs, const Gaudi::StringKey& rhs ) { return lhs + rhs.str(); }
132 friend std::string operator+( const char* lhs, const Gaudi::StringKey& rhs ) { return lhs + rhs.str(); }
133 friend std::string operator+( const Gaudi::StringKey& lhs, const std::string& rhs ) { return lhs.str() + rhs; }
134 friend std::string operator+( const Gaudi::StringKey& lhs, const char* rhs ) { return lhs.str() + rhs; }
135
136 private:
137 // !!!
138 // Do not change the order of these two members. The compiler-generated
139 // comparison operators will first compare the hash (fast) and then the full string.
140 // !!!
141
143 std::size_t m_hash;
144
146 std::string m_str;
147 };
148
159 inline std::size_t hash_value( const Gaudi::StringKey& key ) { return key.__hash__(); }
160} // namespace Gaudi
161
162// Streaming value -> string
163namespace Gaudi {
164 namespace Utils {
175 GAUDI_API std::ostream& toStream( const Gaudi::StringKey& key, std::ostream& s );
176 } // namespace Utils
182 inline std::ostream& operator<<( std::ostream& o, const Gaudi::StringKey& key ) { return o << key.str(); }
183} // namespace Gaudi
184
185// Parsing : string -> value
186namespace Gaudi {
187 namespace Parsers {
197 GAUDI_API StatusCode parse( Gaudi::StringKey& result, std::string_view input );
207 GAUDI_API StatusCode parse( std::vector<Gaudi::StringKey>& result, std::string_view input );
208
209 // Required for IAlgorithm::AlgResources_t
210 GAUDI_API StatusCode parse( std::map<Gaudi::StringKey, unsigned int>& result, std::string_view input );
211 GAUDI_API StatusCode parse( std::map<Gaudi::StringKey, unsigned int, std::less<>>& result, std::string_view input );
212 } // namespace Parsers
213} // namespace Gaudi
214
215namespace std {
219 template <>
220 struct hash<Gaudi::StringKey> {
221 std::size_t operator()( Gaudi::StringKey const& s ) const { return hash_value( s ); }
222 };
223} // namespace std
#define GAUDI_API
Definition Kernel.h:49
Helper class for efficient "key" access for strings.
Definition StringKey.h:68
auto operator<=>(const std::string &rhs) const
Definition StringKey.h:102
friend std::string operator+(const std::string &lhs, const Gaudi::StringKey &rhs)
Definition StringKey.h:131
const std::string & str() const
the actual string
Definition StringKey.h:80
std::size_t __hash__() const
the actual access to the hash
Definition StringKey.h:113
friend std::string operator+(const Gaudi::StringKey &lhs, const char *rhs)
Definition StringKey.h:134
std::string m_str
the actual string
Definition StringKey.h:146
bool operator==(const char *rhs) const
Comparison to other string-like types.
Definition StringKey.h:96
bool operator==(std::string_view rhs) const
Definition StringKey.h:97
bool operator==(const std::string &rhs) const
Definition StringKey.h:98
auto operator<=>(const char *rhs) const
Definition StringKey.h:100
std::size_t m_hash
the hash
Definition StringKey.h:143
operator std::string_view() const
implicit cast to std::string_view
Definition StringKey.h:84
bool operator!() const
empty key?
Definition StringKey.h:88
StringKey(const char *key)
constructor from plain C-string, perform hashing
Definition StringKey.h:73
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:77
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:133
auto operator<=>(std::string_view rhs) const
Definition StringKey.h:101
bool empty() const
empty key?
Definition StringKey.h:86
StringKey(std::string_view key)
constructor from std::string_view, perform hashing
Definition StringKey.h:75
StringKey()
Default constructor from empty string.
Definition StringKey.h:71
friend std::string operator+(const char *lhs, const Gaudi::StringKey &rhs)
Definition StringKey.h:132
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:182
std::size_t hash_value(const Gaudi::StringKey &key)
hash-function: heeded for boost::hash
Definition StringKey.h:159
STL namespace.
Transparent hash operator for heterogeneous lookups.
Definition StringKey.h:30
size_t operator()(std::string_view s) const
Definition StringKey.h:32
std::size_t operator()(Gaudi::StringKey const &s) const
Definition StringKey.h:221