From 9da4c9bc53b0b2ddf6171757b6d9b3fb705642ac Mon Sep 17 00:00:00 2001 From: kunitoki Date: Sun, 1 Dec 2024 09:47:32 +0100 Subject: [PATCH] Make Identifier usable from associative containers (#27) --- modules/juce_core/text/juce_Identifier.h | 16 ++++++++++++++++ tests/juce_core/juce_Identifier.cpp | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/modules/juce_core/text/juce_Identifier.h b/modules/juce_core/text/juce_Identifier.h index 96af6116..519897e2 100644 --- a/modules/juce_core/text/juce_Identifier.h +++ b/modules/juce_core/text/juce_Identifier.h @@ -147,3 +147,19 @@ class JUCE_API Identifier final }; } // namespace juce + +#ifndef DOXYGEN +namespace std +{ + +template <> +struct hash +{ + size_t operator() (const juce::Identifier& identifier) const noexcept + { + return static_cast (*reinterpret_cast (identifier.getCharPointer().getAddress())); + } +}; + +} // namespace std +#endif diff --git a/tests/juce_core/juce_Identifier.cpp b/tests/juce_core/juce_Identifier.cpp index 9caa8aa8..712c3896 100644 --- a/tests/juce_core/juce_Identifier.cpp +++ b/tests/juce_core/juce_Identifier.cpp @@ -23,6 +23,8 @@ #include +#include + using namespace juce; TEST (Identifier, DefaultConstructorCreatesNullIdentifier) @@ -110,3 +112,13 @@ TEST (Identifier, ConversionToCharPointer) auto ptr = id.getCharPointer(); EXPECT_STREQ (ptr.getAddress(), "pointer"); } + +TEST (Identifier, UseInAssociativeContainers) +{ + std::unordered_map 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")); +}