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 edm {
18 
19  namespace detail {
20  // This string is the 16-byte, non-printable version.
21  std::string const& InvalidHash();
22  }
23 
24  namespace hash_detail {
25  typedef std::string value_type;
27  void fixup_(value_type& hash);
28  bool isCompactForm_(value_type const& hash);
29  bool isValid_(value_type const& hash);
30  void throwIfIllFormed(value_type const& hash);
31  void toString_(std::string& result, value_type const& hash);
32  std::ostream& print_(std::ostream& os, value_type const& hash);
33  }
34 
35  template <int I>
36  class Hash {
37  public:
39 
40  Hash();
41  explicit Hash(value_type const& v);
42 
43  Hash(Hash<I> const&);
44  Hash<I> const& operator=(Hash<I> const& iRHS);
45 
46  void reset();
47 
48  // For now, just check the most basic: a default constructed
49  // ParameterSetID is not valid. This is very crude: we are
50  // assuming that nobody created a ParameterSetID from an empty
51  // string, nor from any string that is not a valid string
52  // representation of an MD5 checksum.
53  bool isValid() const;
54 
55  bool operator<(Hash<I> const& other) const;
56  bool operator>(Hash<I> const& other) const;
57  bool operator==(Hash<I> const& other) const;
58  bool operator!=(Hash<I> const& other) const;
59  std::ostream& print(std::ostream& os) const;
60  void toString(std::string& result) const;
61  void swap(Hash<I>& other);
62 
63  // Return the 16-byte (non-printable) string form.
64  value_type compactForm() const;
65 
66  bool isCompactForm() const;
67 
68  private:
69 
72  void throwIfIllFormed() const;
73 
74  template<typename Op>
75  bool
76  compareUsing(Hash<I> const& iOther, Op op) const {
78  bool otherCF = hash_detail::isCompactForm_(iOther.hash_);
79  if(meCF == otherCF) {
80  return op(this->hash_,iOther.hash_);
81  }
82  //copy constructor will do compact form conversion
83  if(meCF) {
84  Hash<I> temp(iOther);
85  return op(this->hash_,temp.hash_);
86  }
87  Hash<I> temp(*this);
88  return op(temp.hash_,iOther.hash_);
89  }
90 
92  };
93 
94 
95  //--------------------------------------------------------------------
96  //
97  // Implementation details follow...
98  //--------------------------------------------------------------------
99 
100 
101  template <int I>
102  inline
103  Hash<I>::Hash() : hash_(detail::InvalidHash()) {}
104 
105  template <int I>
106  inline
107  Hash<I>::Hash(typename Hash<I>::value_type const& v) : hash_(v) {
109  }
110 
111  template <int I>
112  inline
113  Hash<I>::Hash(Hash<I> const& iOther) : hash_(iOther.hash_) {
115  }
116 
117  template <int I>
118  inline
119  Hash<I> const&
121  hash_ = iRHS.hash_;
122  hash_detail::fixup_(hash_);
123  return *this;
124  }
125 
126  template <int I>
127  inline
128  void
130  hash_ = detail::InvalidHash();
131  }
132 
133  template <int I>
134  inline
135  bool
137  return hash_detail::isValid_(hash_);
138  }
139 
140  template <int I>
141  inline
142  bool
143  Hash<I>::operator<(Hash<I> const& other) const {
144  return this->compareUsing(other, std::less<std::string>());
145  }
146 
147  template <int I>
148  inline
149  bool
150  Hash<I>::operator>(Hash<I> const& other) const {
151  return this->compareUsing(other, std::greater<std::string>());
152  }
153 
154  template <int I>
155  inline
156  bool
157  Hash<I>::operator==(Hash<I> const& other) const {
158  return this->compareUsing(other, std::equal_to<std::string>());
159  }
160 
161  template <int I>
162  inline
163  bool
164  Hash<I>::operator!=(Hash<I> const& other) const {
165  return this->compareUsing(other, std::not_equal_to<std::string>());
166  }
167 
168  template <int I>
169  inline
170  std::ostream&
171  Hash<I>::print(std::ostream& os) const {
172  return hash_detail::print_(os, hash_);
173  }
174 
175  template <int I>
176  inline
177  void
178  Hash<I>::toString(std::string& result) const {
179  hash_detail::toString_(result, hash_);
180  }
181 
182  template <int I>
183  inline
184  void
186  hash_.swap(other.hash_);
187  }
188 
189  template <int I>
190  inline
191  typename Hash<I>::value_type
193  return hash_detail::compactForm_(hash_);
194  }
195 
196  // Note: this template is not declared 'inline' because of the
197  // switch statement.
198 
199  template <int I>
200  inline
201  bool Hash<I>::isCompactForm() const {
202  return hash_detail::isCompactForm_(hash_);
203  }
204 
205 
206  // Free swap function
207  template <int I>
208  inline
209  void
211  a.swap(b);
212  }
213 
214  template <int I>
215  inline
216  std::ostream&
217  operator<<(std::ostream& os, Hash<I> const& h) {
218  return h.print(os);
219  }
220 
221 }
222 #endif
bool operator==(Hash< I > const &other) const
Definition: Hash.h:157
value_type hash_
Definition: Hash.h:91
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:192
void fixup_(value_type &hash)
Definition: Hash.cc:30
std::ostream & print_(std::ostream &os, value_type const &hash)
Definition: Hash.cc:84
bool compareUsing(Hash< I > const &iOther, Op op) const
Definition: Hash.h:76
Definition: Hash.h:36
void toString_(std::string &result, value_type const &hash)
Definition: Hash.cc:75
Hash()
Definition: Hash.h:103
bool isCompactForm() const
Definition: Hash.h:201
void swap(Hash< I > &other)
Definition: Hash.h:185
std::ostream & print(std::ostream &os) const
Definition: Hash.h:171
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
bool operator<(Hash< I > const &other) const
Definition: Hash.h:143
std::string const & InvalidHash()
Definition: Hash.cc:9
std::string value_type
Definition: Hash.h:25
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:164
Hash< I > const & operator=(Hash< I > const &iRHS)
Definition: Hash.h:120
bool operator>(Hash< I > const &other) const
Definition: Hash.h:150
void throwIfIllFormed() const
double b
Definition: hdecay.h:120
void toString(std::string &result) const
Definition: Hash.h:178
bool isValid() const
Definition: Hash.h:136
double a
Definition: hdecay.h:121
hash_detail::value_type value_type
Definition: Hash.h:38
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
void throwIfIllFormed(value_type const &hash)
Definition: Hash.cc:65
void reset()
Definition: Hash.h:129
mathSSE::Vec4< T > v