Browse Source

Made Resource nothrow-movable.

Otherwise it can't be used in growable Arrays (and std::vector copies it
instead of moving when reallocating).
pull/415/head
Vladimír Vondruš 6 years ago
parent
commit
eda984c9c9
  1. 2
      doc/changelog.dox
  2. 31
      src/Magnum/Resource.h
  3. 3
      src/Magnum/Test/ResourceManagerTest.cpp

2
doc/changelog.dox

@ -105,6 +105,8 @@ See also:
- The @ref PixelFormat and @ref CompressedPixelFormat enums can now be saved - The @ref PixelFormat and @ref CompressedPixelFormat enums can now be saved
and retrieved from @ref Corrade::Utility::Configuration / and retrieved from @ref Corrade::Utility::Configuration /
@ref Corrade::Utility::Arguments @ref Corrade::Utility::Arguments
- @ref Resource is now nothrow-movable and thus can be used with growable
@ref Containers::Array instances
@subsubsection changelog-latest-changes-gl GL library @subsubsection changelog-latest-changes-gl GL library

31
src/Magnum/Resource.h

@ -135,10 +135,7 @@ class Resource {
} }
/** @brief Move constructor */ /** @brief Move constructor */
Resource(Resource<T, U>&& other): manager(other.manager), _key(other._key), lastCheck(other.lastCheck), _state(other._state), data(other.data) { Resource(Resource<T, U>&& other) noexcept;
/** @brief Make other's state well-defined */
other.manager = nullptr;
}
/** @brief Destructor */ /** @brief Destructor */
~Resource() { ~Resource() {
@ -149,7 +146,7 @@ class Resource {
Resource<T, U>& operator=(const Resource<T, U>& other); Resource<T, U>& operator=(const Resource<T, U>& other);
/** @brief Move assignment */ /** @brief Move assignment */
Resource<T, U>& operator=(Resource<T, U>&& other); Resource<T, U>& operator=(Resource<T, U>&& other) noexcept;
/** @brief Equality comparison */ /** @brief Equality comparison */
bool operator==(const Resource<T, U>& other) const { bool operator==(const Resource<T, U>& other) const {
@ -260,17 +257,21 @@ template<class T, class U> Resource<T, U>& Resource<T, U>::operator=(const Resou
return *this; return *this;
} }
template<class T, class U> Resource<T, U>& Resource<T, U>::operator=(Resource<T, U>&& other) { template<class T, class U> Resource<T, U>::Resource(Resource<T, U>&& other) noexcept: manager(other.manager), _key(other._key), lastCheck(other.lastCheck), _state(other._state), data(other.data) {
/** @todo Just swap the values */
if(manager) manager->decrementReferenceCount(_key);
manager = other.manager;
_key = other._key;
lastCheck = other.lastCheck;
_state = other._state;
data = other.data;
other.manager = nullptr; other.manager = nullptr;
other._key = {};
other.lastCheck = 0;
other._state = ResourceState::Final;
other.data = nullptr;
}
template<class T, class U> Resource<T, U>& Resource<T, U>::operator=(Resource<T, U>&& other) noexcept {
using std::swap;
swap(manager, other.manager);
swap(_key, other._key);
swap(lastCheck, other.lastCheck);
swap(_state, other._state);
swap(data, other.data);
return *this; return *this;
} }

3
src/Magnum/Test/ResourceManagerTest.cpp

@ -157,6 +157,9 @@ void ResourceManagerTest::constructResourceMove() {
CORRADE_COMPARE(c.state(), ResourceState::Final); CORRADE_COMPARE(c.state(), ResourceState::Final);
CORRADE_COMPARE(*c, 6432); CORRADE_COMPARE(*c, 6432);
CORRADE_COMPARE(rm.referenceCount<Int>("thing"), 1); CORRADE_COMPARE(rm.referenceCount<Int>("thing"), 1);
CORRADE_VERIFY(std::is_nothrow_move_constructible<Resource<Int>>::value);
CORRADE_VERIFY(std::is_nothrow_move_assignable<Resource<Int>>::value);
} }
void ResourceManagerTest::compare() { void ResourceManagerTest::compare() {

Loading…
Cancel
Save