All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hash.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef TF_HASH_H
25 #define TF_HASH_H
26 
29 
30 #include "pxr/pxr.h"
31 #include "pxr/base/tf/tf.h"
32 #include "pxr/base/tf/api.h"
33 #include "pxr/base/arch/hash.h"
34 
35 #include <string>
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
39 class TfAnyWeakPtr;
40 class TfEnum;
41 class TfToken;
42 class TfType;
43 
44 template <class T> class TfWeakPtr;
45 template <class T> class TfRefPtr;
46 class TfRefBase;
47 
48 template <template <class> class X, class T>
49 class TfWeakPtrFacade;
50 
86 class TfHash {
87 private:
88  inline size_t _Mix(size_t val) const {
89  // This is based on Knuth's multiplicative hash for integers. The
90  // constant is the closest prime to the binary expansion of the golden
91  // ratio - 1.
92  return static_cast<size_t>(
93  static_cast<uint64_t>(val) * 11400714819323198549ULL);
94  }
95 
96 public:
97  size_t operator()(const std::string& s) const {
98  return ArchHash(s.c_str(), s.length());
99  }
100 
101  template <class T>
102  size_t operator()(const TfRefPtr<T>& ptr) const {
103  return (*this)(ptr._refBase);
104  }
105 
106  template <template <class> class X, class T>
107  size_t operator()(TfWeakPtrFacade<X, T> const &ptr) const {
108  return (*this)(ptr.GetUniqueIdentifier());
109  }
110 
111  // We don't want to choose the TfAnyWeakPtr overload unless the passed
112  // argument is exactly TfAnyWeakPtr. By making this a function template
113  // that's only enabled for TfAnyWeakPtr, C++ will not perform implicit
114  // conversions (since T is deduced).
115  template <class T, class = typename std::enable_if<
116  std::is_same<T, TfAnyWeakPtr>::value>::type>
117  size_t operator()(const T& ptr) const {
118  return ptr.GetHash();
119  }
120 
121  TF_API size_t operator()(const TfEnum& e) const;
122 
123  TF_API size_t operator()(const TfType& t) const;
124 
125  // We refuse to hash const char*. You're almost certainly trying to
126  // hash the pointed-to string and this will not do that (it will hash
127  // the pointer itself). If you really want to hash the pointer then
128  // use static_cast<const void*>(ptr) or TfHashCharPtr and use
129  // TfHashCString if you want to hash the string.
130  template <class T>
131  size_t operator()(const T* ptr) const {
132  static_assert(!std::is_same<T, char>::value,
133  "Can not hash const char*.");
134  return _Mix((size_t) ptr);
135  }
136 
137  size_t operator()(size_t i) const {
138  return _Mix(i);
139  }
140 
141  // Provide an overload for TfToken to prevent hashing via TfToken's implicit
142  // conversion to std::string.
143  TF_API size_t operator()(const TfToken& t) const;
144 };
145 
146 struct TfHashCharPtr {
147  size_t operator()(const char* ptr) const;
148 };
149 struct TfHashCString {
150  size_t operator()(const char* ptr) const;
151 };
152 struct TfEqualCString {
153  bool operator()(const char* lhs, const char* rhs) const;
154 };
155 
156 PXR_NAMESPACE_CLOSE_SCOPE
157 
158 #endif
ARCH_API uint32_t ArchHash(const char *data, size_t len)
Hash len bytes of data.
An enum class that records both enum type and enum value.
Definition: enum.h:140
Provides the ability to hold an arbitrary TfWeakPtr in a non-type-specific manner in order to observe...
Definition: anyWeakPtr.h:55
Pointer storage with deletion detection.
Definition: hash.h:44
Provides hash function on STL string types and other types.
Definition: hash.h:86
Enable a concrete base class for use with TfRefPtr.
Definition: refBase.h:72
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
TfType represents a dynamic runtime type.
Definition: type.h:70
Reference-counted smart pointer utility class.
Definition: hash.h:45