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