The Gaudi Framework  v29r0 (ff2e7097)
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 <algorithm>
9 #include <functional>
10 #include <iosfwd>
11 #include <string>
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( 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:
56  // ========================================================================
60  bool operator==( const StringKey& o ) const { return m_hash == o.m_hash && m_str == o.m_str; }
64  bool operator==( const std::string& o ) const { return m_str == o; }
65  // ========================================================================
66  public: // non-equality
67  // ========================================================================
69  bool operator!=( const StringKey& o ) const { return !( *this == o ); }
71  bool operator!=( const std::string& o ) const { return !( *this == o ); }
72  // ========================================================================
73  // ordering
74  // ========================================================================
75  public:
96  bool operator<( const StringKey& o ) const { return m_hash == o.m_hash ? m_str < o.m_str : m_hash < o.m_hash; }
98  bool operator>( const StringKey& o ) const { return o < *this; }
100  bool operator<=( const StringKey& o ) const { return !( *this > o ); }
102  bool operator>=( const StringKey& o ) const { return !( *this < o ); }
103  // ========================================================================
104  // few helper methods for indirect usage, mainly for Python
105  // ========================================================================
106  public:
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==( const Gaudi::StringKey& key1, const char ( &key2 )[N] )
151  {
152  return key1.str().size() == N && std::equal( key2, key2 + N, key1.str().begin() );
153  }
154  // ==========================================================================
159  template <unsigned int N>
160  inline bool operator!=( const Gaudi::StringKey& key1, const char ( &key2 )[N] )
161  {
162  return !( key1 == key2 );
163  }
164  // ==========================================================================
169  inline bool operator==( const std::string& key1, const Gaudi::StringKey& key2 ) { return key2 == key1; }
170  // ==========================================================================
175  template <unsigned int N>
176  inline bool operator==( const char ( &key1 )[N], const Gaudi::StringKey& key2 )
177  {
178  return key2 == key1;
179  }
180  // ==========================================================================
185  inline bool operator!=( const std::string& key1, const Gaudi::StringKey& key2 ) { return key2 != key1; }
186  // ==========================================================================
191  template <unsigned int N>
192  inline bool operator!=( const char ( &key1 )[N], const Gaudi::StringKey& key2 )
193  {
194  return key2 != key1;
195  }
196  // ==========================================================================
207  inline std::size_t hash_value( const Gaudi::StringKey& key ) { return key.__hash__(); }
208  // ==========================================================================
209 } // end of namespace Gaudi
210 // interoperability with std::string and const char*
211 inline std::string operator+( const std::string& lhs, const Gaudi::StringKey& rhs ) { return lhs + rhs.str(); }
212 inline std::string operator+( const char* lhs, const Gaudi::StringKey& rhs ) { return lhs + rhs.str(); }
213 inline std::string operator+( const Gaudi::StringKey& lhs, const std::string& rhs ) { return lhs.str() + rhs; }
214 inline std::string operator+( const Gaudi::StringKey& lhs, const char* rhs ) { return lhs.str() + rhs; }
215 // ============================================================================
216 // Streaming value -> string
217 // ============================================================================
218 namespace Gaudi
219 {
220  // ==========================================================================
221  namespace Utils
222  {
223  // ========================================================================
235  // ========================================================================
236  } // end of namespace Gaudi::Utils
237  // ==========================================================================
243  inline std::ostream& operator<<( std::ostream& o, const Gaudi::StringKey& key ) { return o << key.str(); }
244  // ==========================================================================
245 } // end of namespace Gaudi
246 // ============================================================================
247 // Parsing : string -> value
248 // ============================================================================
249 namespace Gaudi
250 {
251  // ==========================================================================
252  namespace Parsers
253  {
254  // ========================================================================
264  GAUDI_API StatusCode parse( Gaudi::StringKey& result, const std::string& input );
265  // ========================================================================
276  // ========================================================================
277  } // end of namespace Gaudi::Parsers
278  // ==========================================================================
279 } // end of namespace Gaudi
280 // ============================================================================
281 namespace std
282 {
286  template <>
287  struct hash<Gaudi::StringKey> {
288  inline std::size_t operator()( Gaudi::StringKey const& s ) const { return hash_value( s ); }
289  };
290 }
291 // ============================================================================
292 #endif // GAUDIKERNEL_STRINGKEY_H
293 // ============================================================================
bool operator>=(const StringKey &o) const
greater or equal key
Definition: StringKey.h:102
GAUDI_API std::ostream & operator<<(std::ostream &o, const Gaudi::Histo1DDef &histo)
the streamer operator for class Gaudi::Histo1DDef
Definition: HistoDef.cpp:94
bool operator!=(const T &v, const Property< TP, V, H > &p)
delegate (value != property) to property operator!=
Definition: Property.h:680
GAUDI_API std::ostream & toStream(const Gaudi::StringKey &key, std::ostream &s)
send the object to stream (needed to use it as property)
Definition: StringKey.cpp:63
bool operator==(const T &v, const Property< TP, V, H > &p)
delegate (value == property) to property operator==
Definition: Property.h:673
bool operator!() const
empty key?
Definition: StringKey.h:53
bool operator<(const StringKey &o) const
less key It can be used as a key for std::map, e.g.
Definition: StringKey.h:96
bool operator<=(const StringKey &o) const
less or equal key
Definition: StringKey.h:100
STL namespace.
std::size_t operator()(Gaudi::StringKey const &s) const
Definition: StringKey.h:288
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
STL class.
bool operator!=(const std::string &o) const
non-equality string
Definition: StringKey.h:71
bool operator==(const std::string &o) const
equality, without hashing string rely on the native string equality
Definition: StringKey.h:64
std::size_t hash_value(const Gaudi::StringKey &key)
hash-function: heeded for boost::hash
Definition: StringKey.h:207
int N
Definition: IOTest.py:101
std::string m_str
the actual string:
Definition: StringKey.h:139
GAUDI_API StatusCode parse(std::vector< Gaudi::StringKey > &result, const std::string &input)
parse the vector of keys from the string
Definition: StringKey.cpp:93
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
T size(T...args)
bool operator!=(const StringKey &o) const
non equality Key
Definition: StringKey.h:69
T begin(T...args)
string s
Definition: gaudirun.py:253
bool operator==(const StringKey &o) const
equality Key for efficiency reason compare the hash-values first
Definition: StringKey.h:60
bool operator>(const StringKey &o) const
greater key
Definition: StringKey.h:98
std::size_t __hash__() const
the actual access to the hash
Definition: StringKey.h:116
#define GAUDI_API
Definition: Kernel.h:110
STL class.
T equal(T...args)
const std::string & str() const
the actual string
Definition: StringKey.h:47
Helper functions to set/get the application return code.
Definition: __init__.py:1
std::string toString(const Type &)
decltype(std::declval< TP >()+std::declval< T >()) operator+(const T &v, const Property< TP, V, H > &p)
implemantation of (value + property)
Definition: Property.h:687