HDK
 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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_VT_HASH_H
8 #define PXR_BASE_VT_HASH_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/base/vt/api.h"
12 #include "pxr/base/tf/hash.h"
13 #include <typeinfo>
14 #include <utility>
15 
17 
18 namespace Vt_HashDetail {
19 
20 // Issue a coding error when we attempt to hash a t.
21 VT_API void _IssueUnimplementedHashError(std::type_info const &t);
22 
23 // A constexpr function that determines hashability.
24 template <class T, class = decltype(TfHash()(std::declval<T>()))>
25 constexpr bool _IsHashable(long) { return true; }
26 template <class T>
27 constexpr bool _IsHashable(...) { return false; }
28 
29 // Hash implementations -- We're using an overload resolution ordering trick
30 // here (long vs ...) so that we pick TfHash() if possible, otherwise
31 // we issue a runtime error.
32 template <class T, class = decltype(TfHash()(std::declval<T>()))>
33 inline size_t
34 _HashValueImpl(T const &val, long)
35 {
36  return TfHash()(val);
37 }
38 
39 template <class T>
40 inline size_t
41 _HashValueImpl(T const &val, ...)
42 {
44  return 0;
45 }
46 
47 } // Vt_HashDetail
48 
49 
50 /// A constexpr function that returns true if T is hashable via VtHashValue,
51 /// false otherwise. This is true if we can invoke TfHash()() on a T instance.
52 template <class T>
53 constexpr bool
55  return Vt_HashDetail::_IsHashable<T>(0);
56 }
57 
58 /// Compute a hash code for \p val by invoking TfHash()(val) or when not
59 /// possible issue a coding error and return 0.
60 template <class T>
61 size_t VtHashValue(T const &val)
62 {
63  return Vt_HashDetail::_HashValueImpl(val, 0);
64 }
65 
67 
68 #endif // PXR_BASE_VT_HASH_H
constexpr bool _IsHashable(long)
Definition: hash.h:25
#define VT_API
Definition: api.h:23
Definition: hash.h:472
size_t _HashValueImpl(T const &val, long)
Definition: hash.h:34
constexpr bool VtIsHashable()
Definition: hash.h:54
GLdouble t
Definition: glad.h:2397
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
size_t VtHashValue(T const &val)
Definition: hash.h:61
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
VT_API void _IssueUnimplementedHashError(std::type_info const &t)