CMS 3D CMS Logo

Hash.h
Go to the documentation of this file.
1 #ifndef DataFormats_Provenance_Hash_h
2 #define DataFormats_Provenance_Hash_h
3 
4 #include <string>
5 #include <functional>
6 
7 /*----------------------------------------------------------------------
8 
9 Hash:
10 
11  Note: The call to 'fixup' in every member function is a temporary
12  measure for backwards compatibility. It is necessary in every function
13  because Root creates instances of the class *without* using the
14  interface of the class, thus making it insufficient to assure that
15  all constructors make corrected instances.
16 
17 ----------------------------------------------------------------------*/
18 namespace cms {
19  class Digest;
20 }
21 
22 namespace edm {
23 
24  namespace detail {
25  // This string is the 16-byte, non-printable version.
26  std::string const& InvalidHash();
27  }
28 
29  namespace hash_detail {
31  value_type compactForm_(value_type const& hash);
32  void fixup_(value_type& hash);
33  bool isCompactForm_(value_type const& hash);
34  bool isValid_(value_type const& hash);
35  void throwIfIllFormed(value_type const& hash);
36  void toString_(std::string& result, value_type const& hash);
37  void toDigest_(cms::Digest& digest, value_type const& hash);
38  std::ostream& print_(std::ostream& os, value_type const& hash);
39  size_t smallHash_(value_type const& hash);
40  }
41 
42  template <int I>
43  class Hash {
44  public:
46 
47  Hash();
48  explicit Hash(value_type const& v);
49 
50  Hash(Hash<I> const&);
51  Hash<I>& operator=(Hash<I> const& iRHS);
52 
53  Hash(Hash<I>&&) = default;
54  Hash<I>& operator=(Hash<I>&&) = default;
55 
56  void reset();
57 
58  // For now, just check the most basic: a default constructed
59  // ParameterSetID is not valid. This is very crude: we are
60  // assuming that nobody created a ParameterSetID from an empty
61  // string, nor from any string that is not a valid string
62  // representation of an MD5 checksum.
63  bool isValid() const;
64 
65  bool operator<(Hash<I> const& other) const;
66  bool operator>(Hash<I> const& other) const;
67  bool operator==(Hash<I> const& other) const;
68  bool operator!=(Hash<I> const& other) const;
69  std::ostream& print(std::ostream& os) const;
70  void toString(std::string& result) const;
71  void toDigest(cms::Digest& digest) const;
72  void swap(Hash<I>& other);
73 
74  // Return the 16-byte (non-printable) string form.
75  value_type compactForm() const;
76 
77  bool isCompactForm() const;
78 
80  size_t smallHash() const;
81 
82  //Used by ROOT storage
83  // CMS_CLASS_VERSION(11) // This macro is not defined here, so expand it.
84  static short Class_Version() {return 11;}
85 
86  private:
87 
90  void throwIfIllFormed() const;
91 
92  template<typename Op>
93  bool
94  compareUsing(Hash<I> const& iOther, Op op) const {
95  bool meCF = hash_detail::isCompactForm_(hash_);
96  bool otherCF = hash_detail::isCompactForm_(iOther.hash_);
97  if(meCF == otherCF) {
98  return op(this->hash_,iOther.hash_);
99  }
100  //copy constructor will do compact form conversion
101  if(meCF) {
102  Hash<I> temp(iOther);
103  return op(this->hash_,temp.hash_);
104  }
105  Hash<I> temp(*this);
106  return op(temp.hash_,iOther.hash_);
107  }
108 
109  value_type hash_;
110  };
111 
112 
113  //--------------------------------------------------------------------
114  //
115  // Implementation details follow...
116  //--------------------------------------------------------------------
117 
118 
119  template <int I>
120  inline
121  Hash<I>::Hash() : hash_(detail::InvalidHash()) {}
122 
123  template <int I>
124  inline
125  Hash<I>::Hash(typename Hash<I>::value_type const& v) : hash_(v) {
127  }
128 
129  template <int I>
130  inline
131  Hash<I>::Hash(Hash<I> const& iOther) : hash_(iOther.hash_) {
133  }
134 
135  template <int I>
136  inline
137  Hash<I>&
139  hash_ = iRHS.hash_;
141  return *this;
142  }
143 
144  template <int I>
145  inline
146  void
149  }
150 
151  template <int I>
152  inline
153  bool
156  }
157 
158  template <int I>
159  inline
160  bool
162  return this->compareUsing(other, std::less<std::string>());
163  }
164 
165  template <int I>
166  inline
167  bool
169  return this->compareUsing(other, std::greater<std::string>());
170  }
171 
172  template <int I>
173  inline
174  bool
176  return this->compareUsing(other, std::equal_to<std::string>());
177  }
178 
179  template <int I>
180  inline
181  bool
183  return this->compareUsing(other, std::not_equal_to<std::string>());
184  }
185 
186  template <int I>
187  inline
188  std::ostream&
189  Hash<I>::print(std::ostream& os) const {
190  return hash_detail::print_(os, hash_);
191  }
192 
193  template <int I>
194  inline
195  void
197  hash_detail::toString_(result, hash_);
198  }
199 
200  template <int I>
201  inline
202  void
204  hash_detail::toDigest_(digest, hash_);
205  }
206 
207  template <int I>
208  inline
209  void
211  hash_.swap(other.hash_);
212  }
213 
214  template <int I>
215  inline
216  typename Hash<I>::value_type
219  }
220 
221  template<int I>
222  inline
223  size_t
226  }
227 
228  // Note: this template is not declared 'inline' because of the
229  // switch statement.
230 
231  template <int I>
232  inline
233  bool Hash<I>::isCompactForm() const {
235  }
236 
237 
238  // Free swap function
239  template <int I>
240  inline
241  void
243  a.swap(b);
244  }
245 
246  template <int I>
247  inline
248  std::ostream&
249  operator<<(std::ostream& os, Hash<I> const& h) {
250  return h.print(os);
251  }
252 
253 }
254 #endif
bool operator==(Hash< I > const &other) const
Definition: Hash.h:175
value_type hash_
Definition: Hash.h:109
void toDigest_(cms::Digest &digest, value_type const &hash)
Definition: Hash.cc:96
bool isCompactForm_(value_type const &hash)
Definition: Hash.cc:67
bool isValid_(value_type const &hash)
Definition: Hash.cc:72
value_type compactForm() const
Definition: Hash.h:217
void fixup_(value_type &hash)
Definition: Hash.cc:32
std::ostream & print_(std::ostream &os, value_type const &hash)
Definition: Hash.cc:106
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
static short Class_Version()
Definition: Hash.h:84
bool compareUsing(Hash< I > const &iOther, Op op) const
Definition: Hash.h:94
Definition: Hash.h:43
void toString_(std::string &result, value_type const &hash)
Definition: Hash.cc:87
Hash()
Definition: Hash.h:121
bool isCompactForm() const
Definition: Hash.h:233
void swap(Hash< I > &other)
Definition: Hash.h:210
std::ostream & print(std::ostream &os) const
Definition: Hash.h:189
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
bool operator<(Hash< I > const &other) const
Definition: Hash.h:161
size_t smallHash_(value_type const &hash)
Definition: Hash.cc:57
std::string const & InvalidHash()
Definition: Hash.cc:11
void swap(Hash< I > &a, Hash< I > &b)
Definition: Hash.h:242
Hash< I > & operator=(Hash< I > const &iRHS)
Definition: Hash.h:138
std::string value_type
Definition: Hash.h:30
value_type compactForm_(value_type const &hash)
Definition: Hash.cc:19
std::string toString(const char *format,...)
Definition: xdaq_compat.cc:4
bool operator>(View< T > const &, View< T > const &)
Definition: View.h:382
bool operator!=(Hash< I > const &other) const
Definition: Hash.h:182
std::string Hash
Definition: Types.h:45
bool operator==(MD5Result const &a, MD5Result const &b)
Definition: Digest.cc:148
constexpr unsigned int hash(const char *str, int h=0)
Namespace of DDCMS conversion namespace.
bool operator>(Hash< I > const &other) const
Definition: Hash.h:168
size_t smallHash() const
returns a short hash which can be used with hashing containers
Definition: Hash.h:224
double b
Definition: hdecay.h:120
bool operator!=(MD5Result const &a, MD5Result const &b)
Definition: Digest.h:40
void toDigest(cms::Digest &digest) const
Definition: Hash.h:203
HLT enums.
void toString(std::string &result) const
Definition: Hash.h:196
bool isValid() const
Definition: Hash.h:154
double a
Definition: hdecay.h:121
hash_detail::value_type value_type
Definition: Hash.h:45
void throwIfIllFormed(value_type const &hash)
Definition: Hash.cc:77
void reset(double vett[256])
Definition: TPedValues.cc:11
void reset()
Definition: Hash.h:147