Skip to content
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
16 changes: 16 additions & 0 deletions modules/juce_core/text/juce_Identifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,19 @@ class JUCE_API Identifier final
};

} // namespace juce

#ifndef DOXYGEN
namespace std
{

template <>
struct hash<juce::Identifier>
{
size_t operator() (const juce::Identifier& identifier) const noexcept
{
return static_cast<size_t> (*reinterpret_cast<uintptr_t*> (identifier.getCharPointer().getAddress()));
}
};

} // namespace std
#endif
12 changes: 12 additions & 0 deletions tests/juce_core/juce_Identifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include <juce_core/juce_core.h>

#include <unordered_map>

using namespace juce;

TEST (Identifier, DefaultConstructorCreatesNullIdentifier)
Expand Down Expand Up @@ -110,3 +112,13 @@ TEST (Identifier, ConversionToCharPointer)
auto ptr = id.getCharPointer();
EXPECT_STREQ (ptr.getAddress(), "pointer");
}

TEST (Identifier, UseInAssociativeContainers)
{
std::unordered_map<Identifier, Identifier> ids;
ids[Identifier("test1")] = Identifier("test2");

ASSERT_TRUE(ids.find(Identifier("test1")) != ids.end());
EXPECT_EQ(ids.find(Identifier("test1"))->first, Identifier("test1"));
EXPECT_EQ(ids.find(Identifier("test1"))->second, Identifier("test2"));
}