The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
StringKey.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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 <string>
17#include <string_view>
18#include <vector>
19
20namespace Gaudi {
21
29 using is_transparent = void;
30 size_t operator()( std::string_view s ) const { return std::hash<std::string_view>{}( s ); }
31 };
32
67 public:
71 StringKey( const char* key ) : StringKey( std::string_view{ key } ) {}
73 StringKey( std::string_view key ) : m_hash{ StringKeyHash()( key ) }, m_str( key ) {}
75 StringKey( std::string key ) : m_hash{ StringKeyHash()( key ) }, m_str( std::move( key ) ) {}
76
78 const std::string& str() const { return m_str; }
80 operator const std::string&() const { return m_str; }
82 operator std::string_view() const { return m_str; }
84 bool empty() const { return m_str.empty(); }
86 bool operator!() const { return empty(); }
87
89 bool operator==( const StringKey& ) const = default;
91 auto operator<=>( const StringKey& ) const = default;
92
94 bool operator==( const char* rhs ) const { return m_str == rhs; }
95 bool operator==( std::string_view rhs ) const { return m_str == rhs; }
96 bool operator==( const std::string& rhs ) const { return m_str == rhs; }
97
107 std::size_t __hash__() const { return m_hash; }
109 const std::string& __str__() const; // the representation of the object
111 std::string __repr__() const; // the representation of the object
113 bool __eq__( const StringKey& right ) const;
115 bool __eq__( std::string_view right ) const;
117 bool __neq__( const StringKey& right ) const;
119 bool __neq__( const std::string_view right ) const;
120
122 std::string toString() const; // string representation (for properties)
123
124 // interoperability with std::string and const char*
125 friend std::string operator+( const std::string& lhs, const Gaudi::StringKey& rhs ) { return lhs + rhs.str(); }
126 friend std::string operator+( const char* lhs, const Gaudi::StringKey& rhs ) { return lhs + rhs.str(); }
127 friend std::string operator+( const Gaudi::StringKey& lhs, const std::string& rhs ) { return lhs.str() + rhs; }
128 friend std::string operator+( const Gaudi::StringKey& lhs, const char* rhs ) { return lhs.str() + rhs; }
129
130 private:
131 // !!!
132 // Do not change the order of these two members. The compiler-generated
133 // comparison operators will first compare the hash (fast) and then the full string.
134 // !!!
135
137 std::size_t m_hash;
138
140 std::string m_str;
141 };
142
153 inline std::size_t hash_value( const Gaudi::StringKey& key ) { return key.__hash__(); }
154} // namespace Gaudi
155
156// Streaming value -> string
157namespace Gaudi {
158 namespace Utils {
169 GAUDI_API std::ostream& toStream( const Gaudi::StringKey& key, std::ostream& s );
170 } // namespace Utils
176 inline std::ostream& operator<<( std::ostream& o, const Gaudi::StringKey& key ) { return o << key.str(); }
177} // namespace Gaudi
178
179// Parsing : string -> value
180namespace Gaudi {
181 namespace Parsers {
191 GAUDI_API StatusCode parse( Gaudi::StringKey& result, std::string_view input );
201 GAUDI_API StatusCode parse( std::vector<Gaudi::StringKey>& result, std::string_view input );
202 } // namespace Parsers
203} // namespace Gaudi
204
205namespace std {
209 template <>
210 struct hash<Gaudi::StringKey> {
211 std::size_t operator()( Gaudi::StringKey const& s ) const { return hash_value( s ); }
212 };
213} // namespace std
#define GAUDI_API
Definition Kernel.h:49
Helper class for efficient "key" access for strings.
Definition StringKey.h:66
friend std::string operator+(const std::string &lhs, const Gaudi::StringKey &rhs)
Definition StringKey.h:125
const std::string & str() const
the actual string
Definition StringKey.h:78
std::size_t __hash__() const
the actual access to the hash
Definition StringKey.h:107
friend std::string operator+(const Gaudi::StringKey &lhs, const char *rhs)
Definition StringKey.h:128
std::string m_str
the actual string
Definition StringKey.h:140
bool operator==(const char *rhs) const
Comparison to other string-like types.
Definition StringKey.h:94
bool operator==(std::string_view rhs) const
Definition StringKey.h:95
bool operator==(const std::string &rhs) const
Definition StringKey.h:96
std::size_t m_hash
the hash
Definition StringKey.h:137
operator std::string_view() const
implicit cast to std::string_view
Definition StringKey.h:82
bool operator!() const
empty key?
Definition StringKey.h:86
StringKey(const char *key)
constructor from plain C-string, perform hashing
Definition StringKey.h:71
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:75
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:127
bool empty() const
empty key?
Definition StringKey.h:84
StringKey(std::string_view key)
constructor from std::string_view, perform hashing
Definition StringKey.h:73
StringKey()
Default constructor from empty string.
Definition StringKey.h:69
friend std::string operator+(const char *lhs, const Gaudi::StringKey &rhs)
Definition StringKey.h:126
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:304
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:176
std::size_t hash_value(const Gaudi::StringKey &key)
hash-function: heeded for boost::hash
Definition StringKey.h:153
STL namespace.
Transparent hash operator for heterogeneous lookups.
Definition StringKey.h:28
size_t operator()(std::string_view s) const
Definition StringKey.h:30
std::size_t operator()(Gaudi::StringKey const &s) const
Definition StringKey.h:211