CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
6 /*----------------------------------------------------------------------
7 
8 Hash:
9 
10  Note: The call to 'fixup' in every member function is a temporary
11  measure for backwards compatibility. It is necessary in every function
12  because Root creates instances of the class *without* using the
13  interface of the class, thus making it insufficient to assure that
14  all constructors make corrected instances.
15 
16 ----------------------------------------------------------------------*/
17 namespace cms {
18  class Digest;
19 }
20 
21 namespace edm {
22 
23  namespace detail {
24  // This string is the 16-byte, non-printable version.
25  std::string const& InvalidHash();
26  }
27 
28  namespace hash_detail {
29  typedef std::string value_type;
31  void fixup_(value_type& hash);
32  bool isCompactForm_(value_type const& hash);
33  bool isValid_(value_type const& hash);
34  void throwIfIllFormed(value_type const& hash);
35  void toString_(std::string& result, value_type const& hash);
36  void toDigest_(cms::Digest& digest, value_type const& hash);
37  std::ostream& print_(std::ostream& os, value_type const& hash);
38  }
39 
40  template <int I>
41  class Hash {
42  public:
44 
45  Hash();
46  explicit Hash(value_type const& v);
47 
48  Hash(Hash<I> const&);
49  Hash<I>& operator=(Hash<I> const& iRHS);
50 
51  void reset();
52 
53  // For now, just check the most basic: a default constructed
54  // ParameterSetID is not valid. This is very crude: we are
55  // assuming that nobody created a ParameterSetID from an empty
56  // string, nor from any string that is not a valid string
57  // representation of an MD5 checksum.
58  bool isValid() const;
59 
60  bool operator<(Hash<I> const& other) const;
61  bool operator>(Hash<I> const& other) const;
62  bool operator==(Hash<I> const& other) const;
63  bool operator!=(Hash<I> const& other) const;
64  std::ostream& print(std::ostream& os) const;
65  void toString(std::string& result) const;
66  void toDigest(cms::Digest& digest) const;
67  void swap(Hash<I>& other);
68 
69  // Return the 16-byte (non-printable) string form.
70  value_type compactForm() const;
71 
72  bool isCompactForm() const;
73 
74  //Used by ROOT storage
75  // CMS_CLASS_VERSION(10) // This macro is not defined here, so expand it.
76  static short Class_Version() {return 10;}
77 
78  private:
79 
82  void throwIfIllFormed() const;
83 
84  template<typename Op>
85  bool
86  compareUsing(Hash<I> const& iOther, Op op) const {
88  bool otherCF = hash_detail::isCompactForm_(iOther.hash_);
89  if(meCF == otherCF) {
90  return op(this->hash_,iOther.hash_);
91  }
92  //copy constructor will do compact form conversion
93  if(meCF) {
94  Hash<I> temp(iOther);
95  return op(this->hash_,temp.hash_);
96  }
97  Hash<I> temp(*this);
98  return op(temp.hash_,iOther.hash_);
99  }
100 
102  };
103 
104 
105  //--------------------------------------------------------------------
106  //
107  // Implementation details follow...
108  //--------------------------------------------------------------------
109 
110 
111  template <int I>
112  inline
113  Hash<I>::Hash() : hash_(detail::InvalidHash()) {}
114 
115  template <int I>
116  inline
117  Hash<I>::Hash(typename Hash<I>::value_type const& v) : hash_(v) {
119  }
120 
121  template <int I>
122  inline
123  Hash<I>::Hash(Hash<I> const& iOther) : hash_(iOther.hash_) {
125  }
126 
127  template <int I>
128  inline
129  Hash<I>&
131  hash_ = iRHS.hash_;
132  hash_detail::fixup_(hash_);
133  return *this;
134  }
135 
136  template <int I>
137  inline
138  void
140  hash_ = detail::InvalidHash();
141  }
142 
143  template <int I>
144  inline
145  bool
147  return hash_detail::isValid_(hash_);
148  }
149 
150  template <int I>
151  inline
152  bool
153  Hash<I>::operator<(Hash<I> const& other) const {
154  return this->compareUsing(other, std::less<std::string>());
155  }
156 
157  template <int I>
158  inline
159  bool
160  Hash<I>::operator>(Hash<I> const& other) const {
161  return this->compareUsing(other, std::greater<std::string>());
162  }
163 
164  template <int I>
165  inline
166  bool
167  Hash<I>::operator==(Hash<I> const& other) const {
168  return this->compareUsing(other, std::equal_to<std::string>());
169  }
170 
171  template <int I>
172  inline
173  bool
174  Hash<I>::operator!=(Hash<I> const& other) const {
175  return this->compareUsing(other, std::not_equal_to<std::string>());
176  }
177 
178  template <int I>
179  inline
180  std::ostream&
181  Hash<I>::print(std::ostream& os) const {
182  return hash_detail::print_(os, hash_);
183  }
184 
185  template <int I>
186  inline
187  void
188  Hash<I>::toString(std::string& result) const {
189  hash_detail::toString_(result, hash_);
190  }
191 
192  template <int I>
193  inline
194  void
196  hash_detail::toDigest_(digest, hash_);
197  }
198 
199  template <int I>
200  inline
201  void
203  hash_.swap(other.hash_);
204  }
205 
206  template <int I>
207  inline
208  typename Hash<I>::value_type
210  return hash_detail::compactForm_(hash_);
211  }
212 
213  // Note: this template is not declared 'inline' because of the
214  // switch statement.
215 
216  template <int I>
217  inline
218  bool Hash<I>::isCompactForm() const {
219  return hash_detail::isCompactForm_(hash_);
220  }
221 
222 
223  // Free swap function
224  template <int I>
225  inline
226  void
228  a.swap(b);
229  }
230 
231  template <int I>
232  inline
233  std::ostream&
234  operator<<(std::ostream& os, Hash<I> const& h) {
235  return h.print(os);
236  }
237 
238 }
239 #endif
bool operator==(Hash< I > const &other) const
Definition: Hash.h:167
value_type hash_
Definition: Hash.h:101
void toDigest_(cms::Digest &digest, value_type const &hash)
Definition: Hash.cc:84
bool isCompactForm_(value_type const &hash)
Definition: Hash.cc:55
bool isValid_(value_type const &hash)
Definition: Hash.cc:60
value_type compactForm() const
Definition: Hash.h:209
void fixup_(value_type &hash)
Definition: Hash.cc:30
std::ostream & print_(std::ostream &os, value_type const &hash)
Definition: Hash.cc:94
static short Class_Version()
Definition: Hash.h:76
bool compareUsing(Hash< I > const &iOther, Op op) const
Definition: Hash.h:86
Definition: Hash.h:41
void toString_(std::string &result, value_type const &hash)
Definition: Hash.cc:75
Hash()
Definition: Hash.h:113
bool isCompactForm() const
Definition: Hash.h:218
void swap(Hash< I > &other)
Definition: Hash.h:202
std::ostream & print(std::ostream &os) const
Definition: Hash.h:181
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
bool operator<(Hash< I > const &other) const
Definition: Hash.h:153
std::string const & InvalidHash()
Definition: Hash.cc:9
Hash< I > & operator=(Hash< I > const &iRHS)
Definition: Hash.h:130
std::string value_type
Definition: Hash.h:29
value_type compactForm_(value_type const &hash)
Definition: Hash.cc:17
tuple result
Definition: query.py:137
bool operator!=(Hash< I > const &other) const
Definition: Hash.h:174
bool operator>(Hash< I > const &other) const
Definition: Hash.h:160
void throwIfIllFormed() const
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
double b
Definition: hdecay.h:120
void toDigest(cms::Digest &digest) const
Definition: Hash.h:195
void toString(std::string &result) const
Definition: Hash.h:188
bool isValid() const
Definition: Hash.h:146
double a
Definition: hdecay.h:121
hash_detail::value_type value_type
Definition: Hash.h:43
void throwIfIllFormed(value_type const &hash)
Definition: Hash.cc:65
void reset()
Definition: Hash.h:139
mathSSE::Vec4< T > v