Loading...
Searching...
No Matches
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
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
18PXR_NAMESPACE_OPEN_SCOPE
19
20#ifndef doxygen
21
22namespace Vt_HashDetail {
23
24// Issue a coding error when we attempt to hash a t.
25VT_API void _IssueUnimplementedHashError(std::type_info const &t);
26
27// A constexpr function that determines hashability.
28template <class T, class = decltype(TfHash()(std::declval<T>()))>
29constexpr bool _IsHashable(long) { return true; }
30template <class T>
31constexpr 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.
36template <class T, class = decltype(TfHash()(std::declval<T>()))>
37inline size_t
38_HashValueImpl(T const &val, long)
39{
40 return TfHash()(val);
41}
42
43template <class T>
44inline size_t
45_HashValueImpl(T const &val, ...)
46{
47 Vt_HashDetail::_IssueUnimplementedHashError(typeid(T));
48 return 0;
49}
50
51} // Vt_HashDetail
52
53#endif // ifndef doxygen
54
57template <class T>
58constexpr bool
60 return Vt_HashDetail::_IsHashable<T>(0);
61}
62
65template <class T>
66size_t VtHashValue(T const &val)
67{
68 return Vt_HashDetail::_HashValueImpl(val, 0);
69}
70
71PXR_NAMESPACE_CLOSE_SCOPE
72
73#endif // PXR_BASE_VT_HASH_H
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:472
constexpr bool VtIsHashable()
A constexpr function that returns true if T is hashable via VtHashValue(), false otherwise.
Definition: hash.h:59
size_t VtHashValue(T const &val)
Compute a hash code for val by invoking TfHash()(val) or when not possible issue a coding error and r...
Definition: hash.h:66