Browse Source

Make virtual AbstractResourceLoader functions private.

Removes the need to call AbstractResourceLoader::load() from
reimplementation.
pull/277/head
Vladimír Vondruš 13 years ago
parent
commit
77dca430c3
  1. 64
      src/AbstractResourceLoader.h
  2. 38
      src/Test/ResourceManagerTest.cpp

64
src/AbstractResourceLoader.h

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

38
src/Test/ResourceManagerTest.cpp

@ -56,18 +56,6 @@ class Data {
typedef Magnum::ResourceManager<Int, Data> ResourceManager; typedef Magnum::ResourceManager<Int, Data> ResourceManager;
class IntResourceLoader: public AbstractResourceLoader<Int> {
public:
void load(ResourceKey key) override {
AbstractResourceLoader<Int>::load(key);
}
void load() {
set("hello", new Int(773), ResourceDataState::Final, ResourcePolicy::Resident);
setNotFound("world");
}
};
size_t Data::count = 0; size_t Data::count = 0;
ResourceManagerTest::ResourceManagerTest() { ResourceManagerTest::ResourceManagerTest() {
@ -239,18 +227,30 @@ void ResourceManagerTest::manualPolicy() {
} }
void ResourceManagerTest::loader() { void ResourceManagerTest::loader() {
ResourceManager rm; class IntResourceLoader: public AbstractResourceLoader<Int> {
IntResourceLoader loader; public:
rm.setLoader(&loader);
Resource<Data> data = rm.get<Data>("data"); void load() {
Resource<Int> hello = rm.get<Int>("hello"); set("hello", new Int(773), ResourceDataState::Final, ResourcePolicy::Resident);
Resource<Int> world = rm.get<Int>("world"); setNotFound("world");
}
private:
void doLoad(ResourceKey) override {}
};
auto rm = new ResourceManager;
auto loader = new IntResourceLoader;
rm->setLoader(loader);
Resource<Data> data = rm->get<Data>("data");
Resource<Int> hello = rm->get<Int>("hello");
Resource<Int> world = rm->get<Int>("world");
CORRADE_COMPARE(data.state(), ResourceState::NotLoaded); CORRADE_COMPARE(data.state(), ResourceState::NotLoaded);
CORRADE_COMPARE(hello.state(), ResourceState::Loading); CORRADE_COMPARE(hello.state(), ResourceState::Loading);
CORRADE_COMPARE(world.state(), ResourceState::Loading); CORRADE_COMPARE(world.state(), ResourceState::Loading);
loader.load(); loader->load();
CORRADE_COMPARE(hello.state(), ResourceState::Final); CORRADE_COMPARE(hello.state(), ResourceState::Final);
CORRADE_COMPARE(*hello, 773); CORRADE_COMPARE(*hello, 773);
CORRADE_COMPARE(world.state(), ResourceState::NotFound); CORRADE_COMPARE(world.state(), ResourceState::NotFound);

Loading…
Cancel
Save