activemq-cpp-3.6.0
HashCode.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef _DECAF_UTIL_HASHCODE_H_
19 #define _DECAF_UTIL_HASHCODE_H_
20 
21 #include <decaf/util/Config.h>
22 
23 #include <decaf/lang/Pointer.h>
24 #include <decaf/lang/Float.h>
25 #include <decaf/lang/Double.h>
26 
27 #include <string>
28 #include <functional>
29 
30 namespace decaf {
31 namespace util {
32 
33  template<typename T>
35  public:
36 
37  typedef T argument_type;
38  typedef int result_type;
39 
40  virtual ~HashCodeUnaryBase() {}
41  };
42 
49  template<typename T>
51  public:
52 
53  int operator()(T arg) const;
54  };
55 
56  template<typename T>
57  struct HashCode<T*> : public HashCodeUnaryBase<T*> {
58  int operator()(T* arg) const {
59  return reinterpret_cast<int>(arg);
60  }
61  };
62 
63  template<typename T>
65  int operator()(const T& arg) const {
66  return HashCode<const T*>(&arg);
67  }
68  };
69 
70  template<>
71  struct HashCode<bool> : public HashCodeUnaryBase<bool> {
72  int operator()(bool arg) const {
73  return arg ? 1231 : 1237;
74  }
75  };
76 
77  #if defined(HAVE_WCHAR_T)
78  template<>
79  struct HashCode<unsigned char> : public HashCodeUnaryBase<unsigned char> {
80  int operator()(unsigned char arg) const {
81  return (int) arg;
82  }
83  };
84  #endif
85 
86  template<>
87  struct HashCode<char> : public HashCodeUnaryBase<char> {
88  int operator()(char arg) const {
89  return (int) arg;
90  }
91  };
92 
93  template<>
94  struct HashCode<wchar_t> : public HashCodeUnaryBase<wchar_t> {
95  int operator()(wchar_t arg) const {
96  return (int) arg;
97  }
98  };
99 
100  template<>
101  struct HashCode<unsigned short> : public HashCodeUnaryBase<unsigned short> {
102  int operator()(unsigned short arg) const {
103  return (int) arg;
104  }
105  };
106 
107  template<>
108  struct HashCode<short> : public HashCodeUnaryBase<short> {
109  int operator()(short arg) const {
110  return (int) arg;
111  }
112  };
113 
114  template<>
115  struct HashCode<unsigned int> : public HashCodeUnaryBase<unsigned int> {
116  int operator()(unsigned int arg) const {
117  return (int) arg;
118  }
119  };
120 
121  template<>
122  struct HashCode<int> : public HashCodeUnaryBase<int> {
123  int operator()(int arg) const {
124  return arg;
125  }
126  };
127 
128  template<>
129  struct HashCode<unsigned long long> : public HashCodeUnaryBase<unsigned long long> {
130  int operator()(unsigned long long arg) const {
131  return (int) (arg ^ (arg >> 32));
132  }
133  };
134 
135  template<>
136  struct HashCode<long long> : public HashCodeUnaryBase<long long> {
137  int operator()(long long arg) const {
138  return (int) (arg ^ ((unsigned long long) arg >> 32));
139  }
140  };
141 
142  template<>
143  struct HashCode<float> : public HashCodeUnaryBase<float> {
144  int operator()(float arg) const {
146  }
147  };
148 
149  template<>
150  struct HashCode<double> : public HashCodeUnaryBase<double> {
151  int operator()(double arg) const {
152  long long value = decaf::lang::Double::doubleToLongBits(arg);
153  return (int) (value ^ ((unsigned long long) value >> 32));
154  }
155  };
156 
157  template<>
158  struct HashCode<std::string> : public HashCodeUnaryBase<const std::string&> {
159  int operator()(const std::string& arg) const {
160  int h = 0;
161  if (h == 0 && arg.length() > 0) {
162  std::string::const_iterator iter = arg.begin();
163  for (; iter != arg.end(); ++iter) {
164  h = 31 * h + (*iter);
165  }
166  }
167  return h;
168  }
169  };
170 
171  template<typename T>
172  struct HashCode< decaf::lang::Pointer<T> > : public HashCodeUnaryBase<decaf::lang::Pointer<T> > {
174  if (arg != NULL) {
175  return HashCode<T>()(*arg);
176  }
177  return 0;
178  }
179  };
180 
181 }}
182 
183 #endif /* _DECAF_UTIL_HASHCODE_H_ */