|
|
|
|
@ -47,39 +47,34 @@ each call to ResourceManager::get() will call load() implementation unless the
|
|
|
|
|
resource is already loaded (or loading is in progress). Note that resources |
|
|
|
|
requested before the loader was added are not be affected by the loader. |
|
|
|
|
|
|
|
|
|
Subclassing is done by implementing at least load() function. The loading can |
|
|
|
|
Subclassing is done by implementing at least doLoad() function. The loading can |
|
|
|
|
be done synchronously or asynchronously (i.e., in another thread). The base |
|
|
|
|
implementation provides interface to ResourceManager and manages loading |
|
|
|
|
progress (which is then available through functions requestedCount(), |
|
|
|
|
loadedCount() and notFoundCount()). You shouldn't access the ResourceManager |
|
|
|
|
directly when loading the data. |
|
|
|
|
|
|
|
|
|
Your load() implementation must call the base implementation at the beginning |
|
|
|
|
so ResourceManager is informed about loading state. Then, after your resources |
|
|
|
|
are loaded, call set() to pass them to ResourceManager or call setNotFound() |
|
|
|
|
to indicate that the resource was not found. |
|
|
|
|
In your doLoad() implementation, after your resources are loaded, call set() to |
|
|
|
|
pass them to ResourceManager or call setNotFound() to indicate that the |
|
|
|
|
resource was not found. |
|
|
|
|
|
|
|
|
|
You can also implement name() to provide meaningful names for resource keys. |
|
|
|
|
|
|
|
|
|
Example implementation for synchronous mesh loader: |
|
|
|
|
@code |
|
|
|
|
class MeshResourceLoader: public AbstractResourceLoader<Mesh> { |
|
|
|
|
public: |
|
|
|
|
void load(ResourceKey key) { |
|
|
|
|
// Indicate that loading has begun
|
|
|
|
|
AbstractResourceLoader<Mesh>::load(key); |
|
|
|
|
|
|
|
|
|
// Load the mesh...
|
|
|
|
|
|
|
|
|
|
// Not found
|
|
|
|
|
if(!found) { |
|
|
|
|
setNotFound(key); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
void doLoad(ResourceKey key) override { |
|
|
|
|
// Load the mesh...
|
|
|
|
|
|
|
|
|
|
// Found, pass it to resource manager
|
|
|
|
|
set(key, mesh, state, policy); |
|
|
|
|
// Not found
|
|
|
|
|
if(!found) { |
|
|
|
|
setNotFound(key); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Found, pass it to resource manager
|
|
|
|
|
set(key, mesh, state, policy); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
@endcode |
|
|
|
|
|
|
|
|
|
@ -132,9 +127,9 @@ template<class T> class AbstractResourceLoader {
|
|
|
|
|
* @brief %Resource name corresponding to given key |
|
|
|
|
* |
|
|
|
|
* If no such resource exists or the resource name is not available, |
|
|
|
|
* returns empty string. Default implementation returns empty string. |
|
|
|
|
* returns empty string. |
|
|
|
|
*/ |
|
|
|
|
virtual std::string name(ResourceKey key) const; |
|
|
|
|
std::string name(ResourceKey key) const { return doName(key); } |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Request resource to be loaded |
|
|
|
|
@ -144,12 +139,10 @@ template<class T> class AbstractResourceLoader {
|
|
|
|
|
* requested features is incremented. Depending on implementation the |
|
|
|
|
* resource might be loaded synchronously or asynchronously. |
|
|
|
|
* |
|
|
|
|
* See class documentation for reimplementation guide. |
|
|
|
|
* |
|
|
|
|
* @see ResourceManager::state(), requestedCount(), notFoundCount(), |
|
|
|
|
* loadedCount() |
|
|
|
|
*/ |
|
|
|
|
virtual void load(ResourceKey key) = 0; |
|
|
|
|
void load(ResourceKey key); |
|
|
|
|
|
|
|
|
|
protected: |
|
|
|
|
/**
|
|
|
|
|
@ -172,6 +165,25 @@ template<class T> class AbstractResourceLoader {
|
|
|
|
|
*/ |
|
|
|
|
void setNotFound(ResourceKey key); |
|
|
|
|
|
|
|
|
|
#ifndef DOXYGEN_GENERATING_OUTPUT |
|
|
|
|
private: |
|
|
|
|
#else |
|
|
|
|
protected: |
|
|
|
|
#endif |
|
|
|
|
/**
|
|
|
|
|
* @brief Implementation for name() |
|
|
|
|
* |
|
|
|
|
* Default implementation returns empty string. |
|
|
|
|
*/ |
|
|
|
|
virtual std::string doName(ResourceKey key) const; |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Implementation for load() |
|
|
|
|
* |
|
|
|
|
* See class documentation for reimplementation guide. |
|
|
|
|
*/ |
|
|
|
|
virtual void doLoad(ResourceKey key) = 0; |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
Implementation::ResourceManagerData<T>* manager; |
|
|
|
|
std::size_t _requestedCount; |
|
|
|
|
@ -183,12 +195,14 @@ template<class T> AbstractResourceLoader<T>::~AbstractResourceLoader() {
|
|
|
|
|
if(manager) manager->_loader = nullptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template<class T> std::string AbstractResourceLoader<T>::name(ResourceKey) const { return {}; } |
|
|
|
|
template<class T> std::string AbstractResourceLoader<T>::doName(ResourceKey) const { return {}; } |
|
|
|
|
|
|
|
|
|
template<class T> void AbstractResourceLoader<T>::load(ResourceKey key) { |
|
|
|
|
++_requestedCount; |
|
|
|
|
/** @todo What policy for loading resources? */ |
|
|
|
|
manager->set(key, nullptr, ResourceDataState::Loading, ResourcePolicy::Resident); |
|
|
|
|
|
|
|
|
|
doLoad(key); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template<class T> void AbstractResourceLoader<T>::set(ResourceKey key, T* data, ResourceDataState state, ResourcePolicy policy) { |
|
|
|
|
|