The Gaudi Framework  v33r0 (d5ea422b)
StringKey.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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 #ifndef GAUDIKERNEL_STRINGKEY_H
12 #define GAUDIKERNEL_STRINGKEY_H 1
13 // ============================================================================
14 // Include files
15 // ============================================================================
16 // STD & STL
17 // ============================================================================
18 #include <algorithm>
19 #include <functional>
20 #include <iosfwd>
21 #include <string>
22 #include <vector>
23 // ============================================================================
24 // GaudiKernel
25 // ============================================================================
26 #include "GaudiKernel/Kernel.h"
27 #include "GaudiKernel/StatusCode.h"
28 // ============================================================================
29 namespace Gaudi {
30  // ==========================================================================
45  public:
46  // ========================================================================
48  StringKey( const char* key = "" ); // constructor, perform hashing
50  StringKey( std::string key ); // constructor, perform hashing
51  // ========================================================================
52  public:
53  // ========================================================================
55  const std::string& str() const { return m_str; }
57  operator const std::string&() const { return str(); }
59  bool empty() const { return m_str.empty(); }
61  bool operator!() const { return empty(); }
62  // ========================================================================
63  public:
64  // ========================================================================
68  bool operator==( const StringKey& o ) const { return m_hash == o.m_hash && m_str == o.m_str; }
72  bool operator==( const std::string& o ) const { return m_str == o; }
73  // ========================================================================
74  public: // non-equality
75  // ========================================================================
77  bool operator!=( const StringKey& o ) const { return !( *this == o ); }
79  bool operator!=( const std::string& o ) const { return !( *this == o ); }
80  // ========================================================================
81  // ordering
82  // ========================================================================
83  public:
104  bool operator<( const StringKey& o ) const { return m_hash == o.m_hash ? m_str < o.m_str : m_hash < o.m_hash; }
106  bool operator>( const StringKey& o ) const { return o < *this; }
108  bool operator<=( const StringKey& o ) const { return !( *this > o ); }
110  bool operator>=( const StringKey& o ) const { return !( *this < o ); }
111  // ========================================================================
112  // few helper methods for indirect usage, mainly for Python
113  // ========================================================================
114  public:
124  std::size_t __hash__() const { return m_hash; }
125  // ========================================================================
127  std::string __str__() const; // the representation of the object
129  std::string __repr__() const; // the representation of the object
131  bool __eq__( const StringKey& right ) const;
133  bool __eq__( const std::string& right ) const;
135  bool __neq__( const StringKey& right ) const;
137  bool __neq__( const std::string& right ) const;
138  // ========================================================================
139  public:
140  // ========================================================================
142  std::string toString() const; // string representation (for properties)
143  // ========================================================================
144  private:
145  // ========================================================================
147  std::string m_str; // the actual string
149  std::size_t m_hash; // the hash
150  // ========================================================================
151  };
152  // ==========================================================================
157  template <unsigned int N>
158  inline bool operator==( const Gaudi::StringKey& key1, const char ( &key2 )[N] ) {
159  return key1.str().size() == N && std::equal( key2, key2 + N, key1.str().begin() );
160  }
161  // ==========================================================================
166  template <unsigned int N>
167  inline bool operator!=( const Gaudi::StringKey& key1, const char ( &key2 )[N] ) {
168  return !( key1 == key2 );
169  }
170  // ==========================================================================
175  inline bool operator==( const std::string& key1, const Gaudi::StringKey& key2 ) { return key2 == key1; }
176  // ==========================================================================
181  template <unsigned int N>
182  inline bool operator==( const char ( &key1 )[N], const Gaudi::StringKey& key2 ) {
183  return key2 == key1;
184  }
185  // ==========================================================================
190  inline bool operator!=( const std::string& key1, const Gaudi::StringKey& key2 ) { return key2 != key1; }
191  // ==========================================================================
196  template <unsigned int N>
197  inline bool operator!=( const char ( &key1 )[N], const Gaudi::StringKey& key2 ) {
198  return key2 != key1;
199  }
200  // ==========================================================================
211  inline std::size_t hash_value( const Gaudi::StringKey& key ) { return key.__hash__(); }
212  // ==========================================================================
213 } // end of namespace Gaudi
214 // interoperability with std::string and const char*
215 inline std::string operator+( const std::string& lhs, const Gaudi::StringKey& rhs ) { return lhs + rhs.str(); }
216 inline std::string operator+( const char* lhs, const Gaudi::StringKey& rhs ) { return lhs + rhs.str(); }
217 inline std::string operator+( const Gaudi::StringKey& lhs, const std::string& rhs ) { return lhs.str() + rhs; }
218 inline std::string operator+( const Gaudi::StringKey& lhs, const char* rhs ) { return lhs.str() + rhs; }
219 // ============================================================================
220 // Streaming value -> string
221 // ============================================================================
222 namespace Gaudi {
223  // ==========================================================================
224  namespace Utils {
225  // ========================================================================
237  // ========================================================================
238  } // namespace Utils
239  // ==========================================================================
245  inline std::ostream& operator<<( std::ostream& o, const Gaudi::StringKey& key ) { return o << key.str(); }
246  // ==========================================================================
247 } // end of namespace Gaudi
248 // ============================================================================
249 // Parsing : string -> value
250 // ============================================================================
251 namespace Gaudi {
252  // ==========================================================================
253  namespace Parsers {
254  // ========================================================================
264  GAUDI_API StatusCode parse( Gaudi::StringKey& result, const std::string& input );
265  // ========================================================================
276  // ========================================================================
277  } // namespace Parsers
278  // ==========================================================================
279 } // end of namespace Gaudi
280 // ============================================================================
281 namespace std {
285  template <>
286  struct hash<Gaudi::StringKey> {
287  inline std::size_t operator()( Gaudi::StringKey const& s ) const { return hash_value( s ); }
288  };
289 } // namespace std
290 // ============================================================================
291 #endif // GAUDIKERNEL_STRINGKEY_H
bool operator!=(const T &v, const Property< TP, V, H > &p)
delegate (value != property) to property operator!=
Definition: Property.h:730
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:291
bool operator==(const T &v, const Property< TP, V, H > &p)
delegate (value == property) to property operator==
Definition: Property.h:724
bool operator<=(const StringKey &o) const
less or equal key
Definition: StringKey.h:108
const std::string & str() const
the actual string
Definition: StringKey.h:55
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:341
bool operator!=(const std::string &o) const
non-equality string
Definition: StringKey.h:79
bool operator>=(const StringKey &o) const
greater or equal key
Definition: StringKey.h:110
STL namespace.
StatusCode parse(GaudiUtils::HashMap< K, V > &result, const std::string &input)
Basic parser for the types of HashMap used in DODBasicMapper.
bool operator>(const StringKey &o) const
greater key
Definition: StringKey.h:106
std::size_t m_hash
the hash:
Definition: StringKey.h:149
The helper class to represent the efficient "key" for access.
Definition: StringKey.h:44
bool operator<(const StringKey &o) const
less key It can be used as a key for std::map, e.g.
Definition: StringKey.h:104
STL class.
bool operator==(const StringKey &o) const
equality Key for efficiency reason compare the hash-values first
Definition: StringKey.h:68
bool operator!=(const StringKey &o) const
non equality Key
Definition: StringKey.h:77
std::size_t hash_value(const Gaudi::StringKey &key)
hash-function: heeded for boost::hash
Definition: StringKey.h:211
int N
Definition: IOTest.py:110
std::string m_str
the actual string:
Definition: StringKey.h:147
std::size_t __hash__() const
the actual access to the hash
Definition: StringKey.h:124
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
std::string operator+(const std::string &lhs, const Gaudi::StringKey &rhs)
Definition: StringKey.h:215
std::size_t operator()(Gaudi::StringKey const &s) const
Definition: StringKey.h:287
bool empty() const
empty key?
Definition: StringKey.h:59
T size(T... args)
T begin(T... args)
bool operator==(const std::string &o) const
equality, without hashing string rely on the native string equality
Definition: StringKey.h:72
string s
Definition: gaudirun.py:328
bool operator!() const
empty key?
Definition: StringKey.h:61
#define GAUDI_API
Definition: Kernel.h:81
STL class.
T equal(T... args)
Header file for std:chrono::duration-based Counters.
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:245