Browse Source

Prefix internal Resource members with an underscore.

pull/415/head
Vladimír Vondruš 6 years ago
parent
commit
91fc09e3e9
  1. 66
      src/Magnum/Resource.h

66
src/Magnum/Resource.h

@ -127,11 +127,11 @@ class Resource {
* Creates empty resource. Resources are acquired from the manager by * Creates empty resource. Resources are acquired from the manager by
* calling @ref ResourceManager::get(). * calling @ref ResourceManager::get().
*/ */
explicit Resource(): manager(nullptr), lastCheck(0), _state(ResourceState::Final), data(nullptr) {} explicit Resource(): _manager{nullptr}, _lastCheck{0}, _state{ResourceState::Final}, _data{nullptr} {}
/** @brief Copy constructor */ /** @brief Copy constructor */
Resource(const Resource<T, U>& other): manager(other.manager), _key(other._key), lastCheck(other.lastCheck), _state(other._state), data(other.data) { Resource(const Resource<T, U>& other): _manager{other._manager}, _key{other._key}, _lastCheck{other._lastCheck}, _state{other._state}, _data{other._data} {
if(manager) manager->incrementReferenceCount(_key); if(_manager) _manager->incrementReferenceCount(_key);
} }
/** @brief Move constructor */ /** @brief Move constructor */
@ -139,7 +139,7 @@ class Resource {
/** @brief Destructor */ /** @brief Destructor */
~Resource() { ~Resource() {
if(manager) manager->decrementReferenceCount(_key); if(_manager) _manager->decrementReferenceCount(_key);
} }
/** @brief Copy assignment */ /** @brief Copy assignment */
@ -150,7 +150,7 @@ class Resource {
/** @brief Equality comparison */ /** @brief Equality comparison */
bool operator==(const Resource<T, U>& other) const { bool operator==(const Resource<T, U>& other) const {
return manager == other.manager && _key == other._key; return _manager == other._manager && _key == other._key;
} }
/** @brief Equality comparison with other types is explicitly disallowed */ /** @brief Equality comparison with other types is explicitly disallowed */
@ -187,7 +187,7 @@ class Resource {
*/ */
operator bool() { operator bool() {
acquire(); acquire();
return data; return _data;
} }
/** /**
@ -197,7 +197,7 @@ class Resource {
*/ */
operator U*() { operator U*() {
acquire(); acquire();
return static_cast<U*>(data); return static_cast<U*>(_data);
} }
/** /**
@ -209,8 +209,8 @@ class Resource {
*/ */
U& operator*() { U& operator*() {
acquire(); acquire();
CORRADE_ASSERT(data, "Resource: accessing not loaded data with key" << key(), *static_cast<U*>(data)); CORRADE_ASSERT(_data, "Resource: accessing not loaded data with key" << _key, *static_cast<U*>(_data));
return *static_cast<U*>(data); return *static_cast<U*>(_data);
} }
/** /**
@ -222,8 +222,8 @@ class Resource {
*/ */
U* operator->() { U* operator->() {
acquire(); acquire();
CORRADE_ASSERT(data, "Resource: accessing not loaded data with key" << key(), nullptr); CORRADE_ASSERT(_data, "Resource: accessing not loaded data with key" << _key, nullptr);
return static_cast<U*>(data); return static_cast<U*>(_data);
} }
private: private:
@ -231,47 +231,47 @@ class Resource {
friend Implementation::ResourceManagerData<T>; friend Implementation::ResourceManagerData<T>;
#endif #endif
Resource(Implementation::ResourceManagerData<T>* manager, ResourceKey key): manager(manager), _key(key), lastCheck(0), _state(ResourceState::NotLoaded), data(nullptr) { Resource(Implementation::ResourceManagerData<T>* manager, ResourceKey key): _manager{manager}, _key{key}, _lastCheck{0}, _state{ResourceState::NotLoaded}, _data{nullptr} {
manager->incrementReferenceCount(key); manager->incrementReferenceCount(key);
} }
void acquire(); void acquire();
Implementation::ResourceManagerData<T>* manager; Implementation::ResourceManagerData<T>* _manager;
ResourceKey _key; ResourceKey _key;
std::size_t lastCheck; std::size_t _lastCheck;
ResourceState _state; ResourceState _state;
T* data; T* _data;
}; };
template<class T, class U> Resource<T, U>& Resource<T, U>::operator=(const Resource<T, U>& other) { template<class T, class U> Resource<T, U>& Resource<T, U>::operator=(const Resource<T, U>& other) {
if(manager) manager->decrementReferenceCount(_key); if(_manager) _manager->decrementReferenceCount(_key);
manager = other.manager; _manager = other._manager;
_key = other._key; _key = other._key;
lastCheck = other.lastCheck; _lastCheck = other._lastCheck;
_state = other._state; _state = other._state;
data = other.data; _data = other._data;
if(manager) manager->incrementReferenceCount(_key); if(_manager) _manager->incrementReferenceCount(_key);
return *this; return *this;
} }
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) { 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) {
other.manager = nullptr; other._manager = nullptr;
other._key = {}; other._key = {};
other.lastCheck = 0; other._lastCheck = 0;
other._state = ResourceState::Final; other._state = ResourceState::Final;
other.data = nullptr; other._data = nullptr;
} }
template<class T, class U> Resource<T, U>& Resource<T, U>::operator=(Resource<T, U>&& other) noexcept { template<class T, class U> Resource<T, U>& Resource<T, U>::operator=(Resource<T, U>&& other) noexcept {
using std::swap; using std::swap;
swap(manager, other.manager); swap(_manager, other._manager);
swap(_key, other._key); swap(_key, other._key);
swap(lastCheck, other.lastCheck); swap(_lastCheck, other._lastCheck);
swap(_state, other._state); swap(_state, other._state);
swap(data, other.data); swap(_data, other._data);
return *this; return *this;
} }
@ -280,20 +280,20 @@ template<class T, class U> void Resource<T, U>::acquire() {
if(_state == ResourceState::Final) return; if(_state == ResourceState::Final) return;
/* Nothing changed since last check */ /* Nothing changed since last check */
if(manager->lastChange() < lastCheck) return; if(_manager->lastChange() < _lastCheck) return;
/* Acquire new data and save last check time */ /* Acquire new data and save last check time */
const typename Implementation::ResourceManagerData<T>::Data& d = manager->data(_key); const typename Implementation::ResourceManagerData<T>::Data& d = _manager->data(_key);
lastCheck = manager->lastChange(); _lastCheck = _manager->lastChange();
/* Try to get the data */ /* Try to get the data */
data = d.data; _data = d.data;
_state = static_cast<ResourceState>(d.state); _state = static_cast<ResourceState>(d.state);
/* Data are not available */ /* Data are not available */
if(!data) { if(!_data) {
/* Fallback found, add *Fallback to state */ /* Fallback found, add *Fallback to state */
if((data = manager->fallback())) { if((_data = _manager->fallback())) {
if(_state == ResourceState::Loading) if(_state == ResourceState::Loading)
_state = ResourceState::LoadingFallback; _state = ResourceState::LoadingFallback;
else if(_state == ResourceState::NotFound) else if(_state == ResourceState::NotFound)

Loading…
Cancel
Save