All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
hash.h
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
16PXR_NAMESPACE_OPEN_SCOPE
17
18namespace Vt_HashDetail {
19
20// Issue a coding error when we attempt to hash a t.
21VT_API void _IssueUnimplementedHashError(std::type_info const &t);
22
23// A constexpr function that determines hashability.
24template <class T, class = decltype(TfHash()(std::declval<T>()))>
25constexpr bool _IsHashable(long) { return true; }
26template <class T>
27constexpr 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.
32template <class T, class = decltype(TfHash()(std::declval<T>()))>
33inline size_t
34_HashValueImpl(T const &val, long)
35{
36 return TfHash()(val);
37}
38
39template <class T>
40inline size_t
41_HashValueImpl(T const &val, ...)
42{
43 Vt_HashDetail::_IssueUnimplementedHashError(typeid(T));
44 return 0;
45}
46
47} // Vt_HashDetail
48
49
52template <class T>
53constexpr bool
54VtIsHashable() {
55 return Vt_HashDetail::_IsHashable<T>(0);
56}
57
60template <class T>
61size_t VtHashValue(T const &val)
62{
63 return Vt_HashDetail::_HashValueImpl(val, 0);
64}
65
66PXR_NAMESPACE_CLOSE_SCOPE
67
68#endif // PXR_BASE_VT_HASH_H
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:460