Browse Source

Added method for getting key of given Resource.

pull/279/head
Vladimír Vondruš 14 years ago
parent
commit
934d04a76b
  1. 29
      src/ResourceManager.h

29
src/ResourceManager.h

@ -266,40 +266,40 @@ template<class T, class U = T> class Resource {
inline Resource(): manager(nullptr), lastCheck(0), _state(ResourceState::Final), data(nullptr) {} inline Resource(): manager(nullptr), lastCheck(0), _state(ResourceState::Final), data(nullptr) {}
/** @brief Copy constructor */ /** @brief Copy constructor */
inline Resource(const Resource<T, U>& other): manager(other.manager), key(other.key), lastCheck(other.lastCheck), _state(other._state), data(other.data) { inline 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 */
inline Resource(Resource<T, U>&& other): manager(other.manager), key(other.key), lastCheck(other.lastCheck), _state(other._state), data(other.data) { inline Resource(Resource<T, U>&& other): manager(other.manager), _key(other._key), lastCheck(other.lastCheck), _state(other._state), data(other.data) {
other.manager = nullptr; other.manager = nullptr;
} }
/** @brief Destructor */ /** @brief Destructor */
inline ~Resource() { inline ~Resource() {
if(manager) manager->decrementReferenceCount(key); if(manager) manager->decrementReferenceCount(_key);
} }
/** @brief Assignment operator */ /** @brief Assignment operator */
Resource<T, U>& operator=(const Resource<T, U>& other) { 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;
} }
/** @brief Assignment move operator */ /** @brief Assignment move operator */
Resource<T, U>& operator=(Resource<T, U>&& other) { Resource<T, U>& operator=(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;
@ -308,6 +308,9 @@ template<class T, class U = T> class Resource {
return *this; return *this;
} }
/** @brief Resource key */
inline ResourceKey key() const { return _key; }
/** /**
* @brief %Resource state * @brief %Resource state
* *
@ -343,7 +346,7 @@ template<class T, class U = T> class Resource {
} }
private: private:
inline Resource(Implementation::ResourceManagerData<T>* manager, ResourceKey key): manager(manager), key(key), lastCheck(0), _state(ResourceState::NotLoaded), data(nullptr) { inline Resource(Implementation::ResourceManagerData<T>* manager, ResourceKey key): manager(manager), _key(key), lastCheck(0), _state(ResourceState::NotLoaded), data(nullptr) {
manager->incrementReferenceCount(key); manager->incrementReferenceCount(key);
} }
@ -355,7 +358,7 @@ template<class T, class U = T> class Resource {
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 */
@ -368,7 +371,7 @@ template<class T, class U = T> class Resource {
} }
Implementation::ResourceManagerData<T>* manager; Implementation::ResourceManagerData<T>* manager;
ResourceKey key; ResourceKey _key;
size_t lastCheck; size_t lastCheck;
ResourceState _state; ResourceState _state;
T* data; T* data;
@ -423,7 +426,7 @@ Resource<Mesh> cube(manager->get<Mesh>("cube"));
if(!cube) { if(!cube) {
Mesh* mesh = new Mesh; Mesh* mesh = new Mesh;
// ... // ...
manager->set("cube", mesh, ResourceDataState::Final, ResourcePolicy::Resident); manager->set(cube.key(), mesh, ResourceDataState::Final, ResourcePolicy::Resident);
} }
@endcode @endcode
- Using the resource data. - Using the resource data.

Loading…
Cancel
Save