From f89e935f7d311b0e7dd4100639b77bf5e04dd03e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 6 Jun 2019 18:21:12 +0200 Subject: [PATCH] Ensure Resource operator== doesn't work with different types. Otherwise it could in some circumstances compare the actual resource values giving false positives. --- src/Magnum/Resource.h | 6 ++++++ src/Magnum/Test/ResourceManagerTest.cpp | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Magnum/Resource.h b/src/Magnum/Resource.h index 4f6790d9c..f3f936646 100644 --- a/src/Magnum/Resource.h +++ b/src/Magnum/Resource.h @@ -156,11 +156,17 @@ class Resource { return manager == other.manager && _key == other._key; } + /** @brief Equality comparison with other types is explicitly disallowed */ + template bool operator==(const Resource&) const = delete; + /** @brief Non-equality comparison */ bool operator!=(const Resource& other) const { return !operator==(other); } + /** @brief Non-equality comparison with other types is explicitly disallowed */ + template bool operator!=(const Resource&) const = delete; + /** @brief Resource key */ ResourceKey key() const { return _key; } diff --git a/src/Magnum/Test/ResourceManagerTest.cpp b/src/Magnum/Test/ResourceManagerTest.cpp index c325b4095..acc23710c 100644 --- a/src/Magnum/Test/ResourceManagerTest.cpp +++ b/src/Magnum/Test/ResourceManagerTest.cpp @@ -98,6 +98,20 @@ void ResourceManagerTest::compare() { CORRADE_VERIFY(resA1 == resA1); CORRADE_VERIFY(resA1 == resA2); CORRADE_VERIFY(resA1 != resB); + + Magnum::ResourceManager rm2; + rm2.set(resKeyA, 1); + rm2.set(resKeyA, 1.0f); + + Resource resAOther = rm2.get(resKeyA); + Resource resADifferentType = rm2.get(resKeyA); + /* Verify it checks for manager equality as well */ + CORRADE_VERIFY(resA1 != resAOther); + /* If the comparison operator wouldn't be deleted, the implicit conversion + would kick in and then 1.0f == 1.0f, which is wrong. With the deleted + operator this doesn't compile. */ + //CORRADE_VERIFY(resA1 != resADifferentType); + //CORRADE_VERIFY(!(resA1 == resADifferentType)); } void ResourceManagerTest::state() {