Browse Source

Ensure Resource operator== doesn't work with different types.

Otherwise it could in some circumstances compare the actual
resource values giving false positives.
pull/349/head
Vladimír Vondruš 7 years ago
parent
commit
f89e935f7d
  1. 6
      src/Magnum/Resource.h
  2. 14
      src/Magnum/Test/ResourceManagerTest.cpp

6
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<class V, class W> bool operator==(const Resource<V, W>&) const = delete;
/** @brief Non-equality comparison */
bool operator!=(const Resource<T, U>& other) const {
return !operator==(other);
}
/** @brief Non-equality comparison with other types is explicitly disallowed */
template<class V, class W> bool operator!=(const Resource<V, W>&) const = delete;
/** @brief Resource key */
ResourceKey key() const { return _key; }

14
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<Int, Float> rm2;
rm2.set(resKeyA, 1);
rm2.set(resKeyA, 1.0f);
Resource<Int> resAOther = rm2.get<Int>(resKeyA);
Resource<Float> resADifferentType = rm2.get<Float>(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() {

Loading…
Cancel
Save