All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
StringKey.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_STRINGKEY_H
2 #define GAUDIKERNEL_STRINGKEY_H 1
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 // STD & STL
7 // ============================================================================
8 #include <iosfwd>
9 #include <string>
10 #include <functional>
11 #include <algorithm>
12 #include <vector>
13 // ============================================================================
14 // GaudiKernel
15 // ============================================================================
16 #include "GaudiKernel/Kernel.h"
17 #include "GaudiKernel/StatusCode.h"
18 // ============================================================================
19 namespace Gaudi
20 {
21  // ==========================================================================
36  {
37  public:
38  // ========================================================================
40  StringKey ( const char* key = "" ) ; // constructor, perform hashing
42  StringKey ( const std::string& key ) ; // constructor, perform hashing
43  // ========================================================================
44  public:
45  // ========================================================================
47  const std::string& str() const { return m_str ; }
49  operator const std::string&() const { return str() ; }
51  bool empty () const { return m_str.empty() ; }
53  bool operator!() const { return empty() ; }
54  // ========================================================================
55  public: // equality
56  // ========================================================================
60  bool operator == ( const StringKey& o ) const
61  { return m_hash == o.m_hash && m_str == o.m_str ; }
65  bool operator == ( const std::string& o ) const { return m_str == o ; }
66  // ========================================================================
67  public: // non-equality
68  // ========================================================================
70  bool operator!=( const StringKey& o ) const { return !(*this==o) ; }
72  bool operator!=( const std::string& o ) const { return !(*this==o) ; }
73  // ========================================================================
74  public: // ordering
75  // ========================================================================
96  bool operator < ( const StringKey& o ) const
97  { return m_hash == o.m_hash ? m_str < o.m_str : m_hash < o.m_hash ; }
99  bool operator > ( const StringKey& o ) const { return o < *this ; }
101  bool operator <=( const StringKey& o ) const { return !(*this > o ) ; }
103  bool operator >=( const StringKey& o ) const { return !(*this < o ) ; }
104  // ========================================================================
105  public: // few helper methods for indirect usage, mainly for Python
106  // ========================================================================
116  std::size_t __hash__ () const { return m_hash ; }
117  // ========================================================================
119  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__ ( const std::string& right ) const ;
127  bool __neq__ ( const StringKey& right ) const ;
129  bool __neq__ ( const std::string& right ) const ;
130  // ========================================================================
131  public:
132  // ========================================================================
134  std::string toString () const ; // string representation (for properties)
135  // ========================================================================
136  private:
137  // ========================================================================
139  std::string m_str ; // the actual string
141  std::size_t m_hash ; // the hash
142  // ========================================================================
143  };
144  // ==========================================================================
149  template <unsigned int N>
150  inline bool operator==
151  ( const Gaudi::StringKey& key1 ,
152  const char (&key2)[N] )
153  {
154  return
155  key1.str().size() == N &&
156  std::equal ( key2 , key2 + N , key1.str().begin() ) ;
157  }
158  // ==========================================================================
163  template <unsigned int N>
164  inline bool operator!=
165  ( const Gaudi::StringKey& key1 ,
166  const char (&key2)[N] )
167  { return ! ( key1 == key2 ) ; }
168  // ==========================================================================
173  inline bool operator==
174  ( const std::string& key1 ,
175  const Gaudi::StringKey& key2 )
176  { return key2 == key1 ; }
177  // ==========================================================================
182  template <unsigned int N>
183  inline bool operator==
184  ( const char (&key1 )[N] ,
185  const Gaudi::StringKey& key2 )
186  { return key2 == key1 ; }
187  // ==========================================================================
192  inline bool operator!=
193  ( const std::string& key1 ,
194  const Gaudi::StringKey& key2 )
195  { return key2 != key1 ; }
196  // ==========================================================================
201  template <unsigned int N>
202  inline bool operator!=
203  ( const char (&key1)[N] ,
204  const Gaudi::StringKey& key2 ) { return key2 != key1 ; }
205  // ==========================================================================
216  inline std::size_t hash_value ( const Gaudi::StringKey& key )
217  { return key.__hash__ () ; }
218  // ==========================================================================
219 } // end of namespace Gaudi
220 // ============================================================================
221 // Streaming value -> string
222 // ============================================================================
223 namespace Gaudi
224 {
225  // ==========================================================================
226  namespace Utils
227  {
228  // ========================================================================
239  GAUDI_API std::ostream& toStream
240  ( const Gaudi::StringKey& key ,
241  std::ostream& s ) ;
242  // ========================================================================
243  } // end of namespace Gaudi::Utils
244  // ==========================================================================
250  inline std::ostream& operator<<
251  ( std::ostream& o ,
252  const Gaudi::StringKey& key ) { return o << key.str() ; }
253  // ==========================================================================
254 } // end of namespace Gaudi
255 // ============================================================================
256 // Parsing : string -> value
257 // ============================================================================
258 namespace Gaudi
259 {
260  // ==========================================================================
261  namespace Parsers
262  {
263  // ========================================================================
274  ( Gaudi::StringKey& result ,
275  const std::string& input ) ;
276  // ========================================================================
287  ( std::vector<Gaudi::StringKey>& result ,
288  const std::string& input ) ;
289  // ========================================================================
290  } // end of namespace Gaudi::Parsers
291  // ==========================================================================
292 } // end of namespace Gaudi
293 // ============================================================================
294 #if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
295 namespace std {
299  template <>
300  struct hash<Gaudi::StringKey> {
301  inline std::size_t operator()(Gaudi::StringKey const& s) const {
302  return hash_value(s);
303  }
304  };
305 }
306 #endif
307 // ============================================================================
308 #endif // GAUDIKERNEL_STRINGKEY_H
309 // ============================================================================
bool operator==(const Gaudi::StringKey &key1, const char(&key2)[N])
equality operator with C-arrays
Definition: StringKey.h:151
bool operator!() const
empty key?
Definition: StringKey.h:53
bool operator<=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.icpp:237
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:367
bool operator>=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.icpp:245
StatusCode parse(GaudiUtils::HashMap< K, V > &result, const std::string &input)
Basic parser for the types of HashMap used in DODBasicMapper.
std::size_t m_hash
the hash:
Definition: StringKey.h:141
The helper class to represent the efficient "key" for access.
Definition: StringKey.h:35
bool empty() const
empty key?
Definition: StringKey.h:51
bool operator!=(const std::string &o) const
non-equality string
Definition: StringKey.h:72
std::size_t hash_value(const Gaudi::StringKey &key)
hash-function: heeded for boost::hash
Definition: StringKey.h:216
int N
Definition: IOTest.py:90
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:341
std::string m_str
the actual string:
Definition: StringKey.h:139
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
bool operator>(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.icpp:241
bool operator!=(const StringKey &o) const
non equality Key
Definition: StringKey.h:70
string s
Definition: gaudirun.py:210
This is a number of static methods for bootstrapping the Gaudi framework.
Definition: Bootstrap.h:14
bool operator<(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.icpp:233
std::size_t __hash__() const
the actual access to the hash
Definition: StringKey.h:116
#define GAUDI_API
Definition: Kernel.h:108
const std::string & str() const
the actual string
Definition: StringKey.h:47