Skip to content

Commit

Permalink
Make Identifier usable from associative containers (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki authored Dec 1, 2024
1 parent fba9f76 commit 9da4c9b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
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"));
}

0 comments on commit 9da4c9b

Please sign in to comment.