The Gaudi Framework  v40r0 (475e45c1)
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>
14 #include <GaudiKernel/StatusCode.h>
15 #include <algorithm>
16 #include <functional>
17 #include <iosfwd>
18 #include <string>
19 #include <vector>
20 
21 namespace Gaudi {
36  public:
38  StringKey( const char* key = "" ) : StringKey{ std::string{ key } } {}
39  //
41  StringKey( std::string key ); // constructor, perform hashing
42  public:
44  const std::string& str() const { return m_str; }
46  operator const std::string&() const { return str(); }
48  bool empty() const { return m_str.empty(); }
50  bool operator!() const { return empty(); }
51 
52  public:
56  friend bool operator==( const StringKey& lhs, const StringKey& rhs ) {
57  return lhs.m_hash == rhs.m_hash && lhs.m_str == rhs.m_str;
58  }
62  friend bool operator==( const StringKey& lhs, const std::string& rhs ) { return lhs.m_str == rhs; }
63  friend bool operator==( const std::string& lhs, const StringKey& rhs ) { return rhs == lhs; }
64  friend bool operator==( const StringKey& lhs, std::string_view rhs ) { return lhs.m_str == rhs; }
65  friend bool operator==( std::string_view lhs, const StringKey& rhs ) { return rhs == lhs; }
66 
67  public: // non-equality
69  friend bool operator!=( const StringKey& lhs, const StringKey& rhs ) { return !( lhs == rhs ); }
71  friend bool operator!=( const StringKey& lhs, std::string_view rhs ) { return !( lhs == rhs ); }
72  friend bool operator!=( const StringKey& lhs, const std::string& rhs ) { return !( lhs == rhs ); }
73  friend bool operator!=( std::string_view lhs, const StringKey& rhs ) { return !( lhs == rhs ); }
74  friend bool operator!=( const std::string& lhs, const StringKey& rhs ) { return !( lhs == rhs ); }
75  // ordering
76  public:
97  friend bool operator<( const StringKey& lhs, const StringKey& rhs ) {
98  return lhs.m_hash == rhs.m_hash ? lhs.m_str < rhs.m_str : lhs.m_hash < rhs.m_hash;
99  }
101  friend bool operator>( const StringKey& lhs, const StringKey& rhs ) { return rhs < lhs; }
103  friend bool operator<=( const StringKey& lhs, const StringKey& rhs ) { return !( lhs > rhs ); }
105  friend bool operator>=( const StringKey& lhs, const StringKey& rhs ) { return !( lhs < rhs ); }
106  // few helper methods for indirect usage, mainly for Python
107  public:
117  std::size_t __hash__() const { return m_hash; }
119  const std::string& __str__() const; // the representation of the object
121  std::string __repr__() const; // the representation of the object
123  bool __eq__( const StringKey& right ) const;
125  bool __eq__( std::string_view right ) const;
127  bool __neq__( const StringKey& right ) const;
129  bool __neq__( const std::string_view right ) const;
130 
131  public:
133  std::string toString() const; // string representation (for properties)
134  private:
136  std::string m_str; // the actual string
138  std::size_t m_hash; // the hash
139  };
150  inline std::size_t hash_value( const Gaudi::StringKey& key ) { return key.__hash__(); }
151 } // namespace Gaudi
152 // interoperability with std::string and const char*
153 inline std::string operator+( const std::string& lhs, const Gaudi::StringKey& rhs ) { return lhs + rhs.str(); }
154 inline std::string operator+( const char* lhs, const Gaudi::StringKey& rhs ) { return lhs + rhs.str(); }
155 inline std::string operator+( const Gaudi::StringKey& lhs, const std::string& rhs ) { return lhs.str() + rhs; }
156 inline std::string operator+( const Gaudi::StringKey& lhs, const char* rhs ) { return lhs.str() + rhs; }
157 // Streaming value -> string
158 namespace 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 // Parsing : string -> value
180 namespace 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 namespace std {
208  template <>
209  struct hash<Gaudi::StringKey> {
210  inline std::size_t operator()( Gaudi::StringKey const& s ) const { return hash_value( s ); }
211  };
212 } // namespace std
Gaudi::StringKey::operator!=
friend bool operator!=(const StringKey &lhs, const std::string &rhs)
Definition: StringKey.h:72
Gaudi::StringKey::m_str
std::string m_str
the actual string:
Definition: StringKey.h:136
Gaudi::StringKey::operator!=
friend bool operator!=(const std::string &lhs, const StringKey &rhs)
Definition: StringKey.h:74
std::hash< Gaudi::StringKey >::operator()
std::size_t operator()(Gaudi::StringKey const &s) const
Definition: StringKey.h:210
GaudiPartProp.decorators.std
std
Definition: decorators.py:32
Gaudi::Parsers::parse
StatusCode parse(GaudiUtils::HashMap< K, V > &result, std::string_view input)
Basic parser for the types of HashMap used in DODBasicMapper.
Definition: DODBasicMapper.cpp:21
Gaudi::StringKey::operator!=
friend bool operator!=(const StringKey &lhs, std::string_view rhs)
non-equality string
Definition: StringKey.h:71
gaudirun.s
string s
Definition: gaudirun.py:346
Gaudi::StringKey::operator==
friend bool operator==(const StringKey &lhs, const std::string &rhs)
equality, without hashing string rely on the native string equality
Definition: StringKey.h:62
GaudiPartProp.decorators.__repr__
__repr__
decorate the vector of properties
Definition: decorators.py:173
Gaudi::StringKey::operator==
friend bool operator==(std::string_view lhs, const StringKey &rhs)
Definition: StringKey.h:65
Gaudi::StringKey::operator<
friend bool operator<(const StringKey &lhs, const StringKey &rhs)
less key It can be used as a key for std::map, e.g.
Definition: StringKey.h:97
Gaudi::StringKey::operator>
friend bool operator>(const StringKey &lhs, const StringKey &rhs)
greater key
Definition: StringKey.h:101
Gaudi::StringKey::operator==
friend bool operator==(const StringKey &lhs, const StringKey &rhs)
equality Key for efficiency reason compare the hash-values first
Definition: StringKey.h:56
StatusCode.h
Gaudi::StringKey::operator==
friend bool operator==(const StringKey &lhs, std::string_view rhs)
Definition: StringKey.h:64
Gaudi::StringKey::m_hash
std::size_t m_hash
the hash:
Definition: StringKey.h:138
Gaudi::StringKey::operator==
friend bool operator==(const std::string &lhs, const StringKey &rhs)
Definition: StringKey.h:63
Gaudi::StringKey
Definition: StringKey.h:35
Gaudi::StringKey::operator<=
friend bool operator<=(const StringKey &lhs, const StringKey &rhs)
less or equal key
Definition: StringKey.h:103
StatusCode
Definition: StatusCode.h:64
Gaudi::StringKey::StringKey
StringKey(const char *key="")
constructor from plain C-string, perform hashing
Definition: StringKey.h:38
Gaudi::StringKey::operator!
bool operator!() const
empty key?
Definition: StringKey.h:50
Gaudi::StringKey::operator>=
friend bool operator>=(const StringKey &lhs, const StringKey &rhs)
greater or equal key
Definition: StringKey.h:105
Gaudi::hash_value
std::size_t hash_value(const Gaudi::StringKey &key)
hash-function: heeded for boost::hash
Definition: StringKey.h:150
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::StringKey::operator!=
friend bool operator!=(const StringKey &lhs, const StringKey &rhs)
non equality Key
Definition: StringKey.h:69
Gaudi::Utils::toString
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:326
GaudiPartProp.decorators.__str__
__str__
decorate the printout for Gaudi::ParticleProperty
Definition: decorators.py:176
Gaudi::StringKey::__hash__
std::size_t __hash__() const
the actual access to the hash
Definition: StringKey.h:117
Kernel.h
Gaudi::Utils::toStream
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
Gaudi::StringKey::str
const std::string & str() const
the actual string
Definition: StringKey.h:44
Gaudi::operator<<
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
Gaudi::StringKey::operator!=
friend bool operator!=(std::string_view lhs, const StringKey &rhs)
Definition: StringKey.h:73
ProduceConsume.key
key
Definition: ProduceConsume.py:84
Gaudi::StringKey::empty
bool empty() const
empty key?
Definition: StringKey.h:48
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:83
operator+
std::string operator+(const std::string &lhs, const Gaudi::StringKey &rhs)
Definition: StringKey.h:153