Browse Source

Make it possible to pass Containers::Pointer to ResourceManager APIs.

Also cleaned up the test from naked new. This still needs to be reworked
to not do manual memory management inside (and then deprecate the raw
pointer versions), but for now this has to suffice.
pull/317/head
Vladimír Vondruš 7 years ago
parent
commit
2d04b22aba
  1. 3
      doc/changelog.dox
  2. 10
      src/Magnum/AbstractResourceLoader.h
  3. 23
      src/Magnum/ResourceManager.h
  4. 64
      src/Magnum/Test/ResourceManagerTest.cpp

3
doc/changelog.dox

@ -58,6 +58,9 @@ See also:
@subsection changelog-latest-changes Changes and improvements @subsection changelog-latest-changes Changes and improvements
- The @ref ResourceManager class now accepts also
@ref Corrade::Containers::Pointer instances in addition to raw pointers
@subsubsection changelog-latest-changes-audio Audio library @subsubsection changelog-latest-changes-audio Audio library
- The @ref Audio::AnyImporter "AnyAudioImporter" plugin now correctly - The @ref Audio::AnyImporter "AnyAudioImporter" plugin now correctly

10
src/Magnum/AbstractResourceLoader.h

@ -164,6 +164,11 @@ template<class T> class AbstractResourceLoader {
*/ */
void set(ResourceKey key, T* data, ResourceDataState state, ResourcePolicy policy); void set(ResourceKey key, T* data, ResourceDataState state, ResourcePolicy policy);
/** @overload */
void set(ResourceKey key, Containers::Pointer<T> data, ResourceDataState state, ResourcePolicy policy) {
return set(key, data.release(), state, policy);
}
/** @overload */ /** @overload */
template<class U> void set(ResourceKey key, U&& data, ResourceDataState state, ResourcePolicy policy) { template<class U> void set(ResourceKey key, U&& data, ResourceDataState state, ResourcePolicy policy) {
set(key, new typename std::decay<U>::type(std::forward<U>(data)), state, policy); set(key, new typename std::decay<U>::type(std::forward<U>(data)), state, policy);
@ -179,6 +184,11 @@ template<class T> class AbstractResourceLoader {
set(key, data, ResourceDataState::Final, ResourcePolicy::Resident); set(key, data, ResourceDataState::Final, ResourcePolicy::Resident);
} }
/** @overload */
void set(ResourceKey key, Containers::Pointer<T> data) {
set(key, data.release());
}
/** @overload */ /** @overload */
template<class U> void set(ResourceKey key, U&& data) { template<class U> void set(ResourceKey key, U&& data) {
set(key, new typename std::decay<U>::type(std::forward<U>(data))); set(key, new typename std::decay<U>::type(std::forward<U>(data)));

23
src/Magnum/ResourceManager.h

@ -30,6 +30,7 @@
*/ */
#include <unordered_map> #include <unordered_map>
#include <Corrade/Containers/Pointer.h>
#include "Magnum/Resource.h" #include "Magnum/Resource.h"
@ -345,6 +346,12 @@ template<class... Types> class ResourceManager: private Implementation::Resource
return *this; return *this;
} }
/** @overload */
template<class T> ResourceManager<Types...>& set(ResourceKey key, Containers::Pointer<T>&& data, ResourceDataState state, ResourcePolicy policy) {
set(key, data.release(), state, policy);
return *this;
}
/** @overload */ /** @overload */
template<class U> ResourceManager<Types...>& set(ResourceKey key, U&& data, ResourceDataState state, ResourcePolicy policy) { template<class U> ResourceManager<Types...>& set(ResourceKey key, U&& data, ResourceDataState state, ResourcePolicy policy) {
return set(key, new typename std::decay<U>::type(std::forward<U>(data)), state, policy); return set(key, new typename std::decay<U>::type(std::forward<U>(data)), state, policy);
@ -361,6 +368,11 @@ template<class... Types> class ResourceManager: private Implementation::Resource
return set(key, data, ResourceDataState::Final, ResourcePolicy::Resident); return set(key, data, ResourceDataState::Final, ResourcePolicy::Resident);
} }
/** @overload */
template<class T> ResourceManager<Types...>& set(ResourceKey key, Containers::Pointer<T>&& data) {
return set(key, data.release());
}
/** @overload */ /** @overload */
template<class U> ResourceManager<Types...>& set(ResourceKey key, U&& data) { template<class U> ResourceManager<Types...>& set(ResourceKey key, U&& data) {
return set(key, new typename std::decay<U>::type(std::forward<U>(data))); return set(key, new typename std::decay<U>::type(std::forward<U>(data)));
@ -385,6 +397,12 @@ template<class... Types> class ResourceManager: private Implementation::Resource
return *this; return *this;
} }
/** @overload */
template<class T> ResourceManager<Types...>& setFallback(Containers::Pointer<T>&& data) {
setFallback(data.release());
return *this;
}
/** @overload */ /** @overload */
template<class U> ResourceManager<Types...>& setFallback(U&& data) { template<class U> ResourceManager<Types...>& setFallback(U&& data) {
return setFallback(new typename std::decay<U>::type(std::forward<U>(data))); return setFallback(new typename std::decay<U>::type(std::forward<U>(data)));
@ -457,6 +475,11 @@ template<class... Types> class ResourceManager: private Implementation::Resource
return *this; return *this;
} }
/** @overload */
template<class T> ResourceManager<Types...>& setLoader(Containers::Pointer<AbstractResourceLoader<T>>&& loader) {
return setLoader(loader.release());
}
private: private:
template<class FirstType, class ...NextTypes> void freeInternal(Implementation::ResourceTypePack<FirstType, NextTypes...>) { template<class FirstType, class ...NextTypes> void freeInternal(Implementation::ResourceTypePack<FirstType, NextTypes...>) {
free<FirstType>(); free<FirstType>();

64
src/Magnum/Test/ResourceManagerTest.cpp

@ -101,7 +101,7 @@ void ResourceManagerTest::state() {
void ResourceManagerTest::stateFallback() { void ResourceManagerTest::stateFallback() {
{ {
ResourceManager rm; ResourceManager rm;
rm.setFallback(new Data); rm.setFallback(Containers::pointer<Data>());
Resource<Data> data = rm.get<Data>("data"); Resource<Data> data = rm.get<Data>("data");
CORRADE_VERIFY(data); CORRADE_VERIFY(data);
@ -174,15 +174,15 @@ void ResourceManagerTest::basic() {
} }
void ResourceManagerTest::residentPolicy() { void ResourceManagerTest::residentPolicy() {
ResourceManager* rm = new ResourceManager; {
ResourceManager rm;
rm->set("blah", new Data, ResourceDataState::Mutable, ResourcePolicy::Resident);
CORRADE_COMPARE(Data::count, 1);
rm->free(); rm.set("blah", Containers::pointer<Data>(), ResourceDataState::Mutable, ResourcePolicy::Resident);
CORRADE_COMPARE(Data::count, 1); CORRADE_COMPARE(Data::count, 1);
delete rm; rm.free();
CORRADE_COMPARE(Data::count, 1);
}
CORRADE_COMPARE(Data::count, 0); CORRADE_COMPARE(Data::count, 0);
} }
@ -192,7 +192,7 @@ void ResourceManagerTest::referenceCountedPolicy() {
ResourceKey dataRefCountKey("dataRefCount"); ResourceKey dataRefCountKey("dataRefCount");
/* Resource is deleted after all references are removed */ /* Resource is deleted after all references are removed */
rm.set(dataRefCountKey, new Data, ResourceDataState::Final, ResourcePolicy::ReferenceCounted); rm.set(dataRefCountKey, Containers::pointer<Data>(), ResourceDataState::Final, ResourcePolicy::ReferenceCounted);
CORRADE_COMPARE(rm.count<Data>(), 1); CORRADE_COMPARE(rm.count<Data>(), 1);
{ {
Resource<Data> data = rm.get<Data>(dataRefCountKey); Resource<Data> data = rm.get<Data>(dataRefCountKey);
@ -205,7 +205,7 @@ void ResourceManagerTest::referenceCountedPolicy() {
/* Reference counted resources which were not used once will stay loaded /* Reference counted resources which were not used once will stay loaded
until free() is called */ until free() is called */
rm.set(dataRefCountKey, new Data, ResourceDataState::Final, ResourcePolicy::ReferenceCounted); rm.set(dataRefCountKey, Containers::pointer<Data>(), ResourceDataState::Final, ResourcePolicy::ReferenceCounted);
CORRADE_COMPARE(rm.count<Data>(), 1); CORRADE_COMPARE(rm.count<Data>(), 1);
CORRADE_COMPARE(rm.state<Data>(dataRefCountKey), ResourceState::Final); CORRADE_COMPARE(rm.state<Data>(dataRefCountKey), ResourceState::Final);
CORRADE_COMPARE(rm.referenceCount<Data>(dataRefCountKey), 0); CORRADE_COMPARE(rm.referenceCount<Data>(dataRefCountKey), 0);
@ -223,7 +223,7 @@ void ResourceManagerTest::manualPolicy() {
/* Manual free */ /* Manual free */
{ {
rm.set(dataKey, new Data, ResourceDataState::Mutable, ResourcePolicy::Manual); rm.set(dataKey, Containers::pointer<Data>(), ResourceDataState::Mutable, ResourcePolicy::Manual);
Resource<Data> data = rm.get<Data>(dataKey); Resource<Data> data = rm.get<Data>(dataKey);
rm.free(); rm.free();
} }
@ -234,21 +234,21 @@ void ResourceManagerTest::manualPolicy() {
CORRADE_COMPARE(rm.count<Data>(), 0); CORRADE_COMPARE(rm.count<Data>(), 0);
CORRADE_COMPARE(Data::count, 0); CORRADE_COMPARE(Data::count, 0);
rm.set(dataKey, new Data, ResourceDataState::Mutable, ResourcePolicy::Manual); rm.set(dataKey, Containers::pointer<Data>(), ResourceDataState::Mutable, ResourcePolicy::Manual);
CORRADE_COMPARE(rm.count<Data>(), 1); CORRADE_COMPARE(rm.count<Data>(), 1);
CORRADE_COMPARE(Data::count, 1); CORRADE_COMPARE(Data::count, 1);
} }
void ResourceManagerTest::defaults() { void ResourceManagerTest::defaults() {
ResourceManager rm; ResourceManager rm;
rm.set("data", new Data); rm.set("data", Containers::pointer<Data>());
CORRADE_COMPARE(rm.state<Data>("data"), ResourceState::Final); CORRADE_COMPARE(rm.state<Data>("data"), ResourceState::Final);
} }
void ResourceManagerTest::clear() { void ResourceManagerTest::clear() {
ResourceManager rm; ResourceManager rm;
rm.set("blah", new Data); rm.set("blah", Containers::pointer<Data>());
CORRADE_COMPARE(Data::count, 1); CORRADE_COMPARE(Data::count, 1);
rm.free(); rm.free();
@ -281,7 +281,7 @@ void ResourceManagerTest::loader() {
IntResourceLoader(): resource(ResourceManager::instance().get<Data>("data")) {} IntResourceLoader(): resource(ResourceManager::instance().get<Data>("data")) {}
void load() { void load() {
set("hello", 773, ResourceDataState::Final, ResourcePolicy::Resident); set("hello", Containers::pointer<Int>(773), ResourceDataState::Final, ResourcePolicy::Resident);
setNotFound("world"); setNotFound("world");
} }
@ -298,38 +298,38 @@ void ResourceManagerTest::loader() {
Resource<Data> resource; Resource<Data> resource;
}; };
auto rm = new ResourceManager;
auto loader = new IntResourceLoader;
rm->setLoader(loader);
{ {
Resource<Data> data = rm->get<Data>("data"); ResourceManager rm;
Resource<Int> hello = rm->get<Int>("hello"); Containers::Pointer<IntResourceLoader> loaderPtr{Containers::InPlaceInit};
Resource<Int> world = rm->get<Int>("world"); IntResourceLoader& loader = *loaderPtr;
rm.setLoader<Int>(std::move(loaderPtr));
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);
CORRADE_COMPARE(loader->requestedCount(), 2); CORRADE_COMPARE(loader.requestedCount(), 2);
CORRADE_COMPARE(loader->loadedCount(), 0); CORRADE_COMPARE(loader.loadedCount(), 0);
CORRADE_COMPARE(loader->notFoundCount(), 0); CORRADE_COMPARE(loader.notFoundCount(), 0);
CORRADE_COMPARE(loader->name(ResourceKey("hello")), "hello"); CORRADE_COMPARE(loader.name(ResourceKey("hello")), "hello");
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);
CORRADE_COMPARE(loader->requestedCount(), 2); CORRADE_COMPARE(loader.requestedCount(), 2);
CORRADE_COMPARE(loader->loadedCount(), 1); CORRADE_COMPARE(loader.loadedCount(), 1);
CORRADE_COMPARE(loader->notFoundCount(), 1); CORRADE_COMPARE(loader.notFoundCount(), 1);
/* Verify that the loader is deleted at proper time */ /* Verify that the loader is deleted at proper time */
rm->set("data", new Data); rm.set("data", Containers::pointer<Data>());
CORRADE_COMPARE(Data::count, 1); CORRADE_COMPARE(Data::count, 1);
} }
delete rm;
CORRADE_COMPARE(Data::count, 0); CORRADE_COMPARE(Data::count, 0);
} }

Loading…
Cancel
Save