Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specialize TfHashAppend for std::shared_ptr and std::unique_ptr #2304

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pxr/base/tf/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <cstring>
#include <string>
#include <map>
#include <memory>
#include <set>
#include <typeindex>
#include <type_traits>
Expand Down Expand Up @@ -142,6 +143,22 @@ TfHashAppend(HashState &h, const T* ptr) {
return h.Append(reinterpret_cast<uintptr_t>(ptr));
}

// Support for hashing std::shared_ptr. When TfHash support for std::hash is
// enabled, this explicit specialization will no longer be necessary.
template <class HashState, class T>
inline void
TfHashAppend(HashState &h, const std::shared_ptr<T>& ptr) {
h.Append(std::hash<std::shared_ptr<T>>{}(ptr));
}

// Support for hashing std::unique_ptr. When TfHash support for std::hash is
// enabled, this explicit specialization will no longer be necessary.
template <class HashState, class T>
inline void
TfHashAppend(HashState &h, const std::unique_ptr<T>& ptr) {
h.Append(std::hash<std::unique_ptr<T>>{}(ptr));
}

// We refuse to hash [const] char *. You're almost certainly trying to hash the
// pointed-to string and this will not do that (it will hash the pointer
// itself). To hash a c-style null terminated string, you can use
Expand Down
6 changes: 6 additions & 0 deletions pxr/base/tf/testenv/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ Test_TfHash()
// Validate support for std::type_index
printf("hash(type_index): %zu\n", h(std::type_index(typeid(int))));

// Validate support for std::shared_ptr
printf("hash(shared_ptr): %zu\n", h(std::make_shared<int>(5)));

// Validate support for std::unique_ptr
printf("hash(unique_ptr): %zu\n", h(std::make_unique<int>(7)));

TfHasher tfh;
//BoostHasher bh;

Expand Down