Browse Source

Deprecate ResourceManager::instance().

If I would have done this a year ago, I could have it removed by now.
Well. Gotta look forward to 2020, then.
pull/364/head
Vladimír Vondruš 7 years ago
parent
commit
0f7e1e8fad
  1. 3
      doc/changelog.dox
  2. 2
      doc/snippets/Magnum.cpp
  3. 23
      src/Magnum/DebugTools/ResourceManager.cpp
  4. 7
      src/Magnum/DebugTools/ResourceManager.h
  5. 10
      src/Magnum/ResourceManager.h
  6. 2
      src/Magnum/ResourceManager.hpp
  7. 23
      src/Magnum/Test/CMakeLists.txt
  8. 3
      src/Magnum/Test/ResourceManagerLocalInstanceTest.cpp
  9. 2
      src/Magnum/Test/ResourceManagerLocalInstanceTestLib.cpp
  10. 4
      src/Magnum/Test/ResourceManagerTest.cpp

3
doc/changelog.dox

@ -585,6 +585,9 @@ See also:
- Passing @cpp nullptr @ce to @ref ImageView constructors is deprecated and - Passing @cpp nullptr @ce to @ref ImageView constructors is deprecated and
will print a warning at runtime. Use a constructor without the @p data will print a warning at runtime. Use a constructor without the @p data
parameter instead. parameter instead.
- The @cpp ResourceManager::instance() @ce singleton is deprecated as it
makes some use cases harder than they should be. Make your own singleton
or explicitly pass a @ref ResourceManager reference around instead.
- @ref Platform::BasicScreen::application() now returns a reference instead - @ref Platform::BasicScreen::application() now returns a reference instead
of a pointer and together with @ref Platform::BasicScreen::redraw() checks of a pointer and together with @ref Platform::BasicScreen::redraw() checks
that the screen is actually added to the application instead of returning that the screen is actually added to the application instead of returning

2
doc/snippets/Magnum.cpp

@ -233,7 +233,7 @@ struct MyShader: GL::AbstractShaderProgram {
void bindTexture(GL::Texture2D&) {} void bindTexture(GL::Texture2D&) {}
}; };
/* [ResourceManager-fill] */ /* [ResourceManager-fill] */
MyResourceManager& manager = MyResourceManager::instance(); MyResourceManager manager;
Resource<GL::Texture2D> texture{manager.get<GL::Texture2D>("texture")}; Resource<GL::Texture2D> texture{manager.get<GL::Texture2D>("texture")};
Resource<GL::AbstractShaderProgram, MyShader> shader = Resource<GL::AbstractShaderProgram, MyShader> shader =
manager.get<GL::AbstractShaderProgram, MyShader>("shader"); manager.get<GL::AbstractShaderProgram, MyShader>("shader");

23
src/Magnum/DebugTools/ResourceManager.cpp

@ -41,11 +41,32 @@ namespace Implementation {
namespace DebugTools { namespace DebugTools {
namespace {
#ifdef CORRADE_BUILD_MULTITHREADED
CORRADE_THREAD_LOCAL
#endif
ResourceManager* resourceManagerInstance = nullptr;
}
ResourceManager& ResourceManager::instance() {
CORRADE_ASSERT(resourceManagerInstance,
"DebugTools::ResourceManager::instance(): no instance exists",
*resourceManagerInstance);
return *resourceManagerInstance;
}
ResourceManager::ResourceManager() { ResourceManager::ResourceManager() {
CORRADE_ASSERT(!resourceManagerInstance,
"DebugTools::ResourceManager: another instance is already created", );
resourceManagerInstance = this;
setFallback(new ForceRendererOptions); setFallback(new ForceRendererOptions);
setFallback(new ObjectRendererOptions); setFallback(new ObjectRendererOptions);
} }
ResourceManager::~ResourceManager() = default; ResourceManager::~ResourceManager() {
CORRADE_INTERNAL_ASSERT(resourceManagerInstance == this);
resourceManagerInstance = nullptr;
}
}} }}

7
src/Magnum/DebugTools/ResourceManager.h

@ -65,6 +65,13 @@ information.
class MAGNUM_DEBUGTOOLS_EXPORT ResourceManager: public Magnum::ResourceManager<Magnum::Implementation::ResourceManagerLocalInstance, GL::AbstractShaderProgram, GL::Buffer, GL::Mesh, GL::MeshView, DebugTools::ForceRendererOptions, DebugTools::ObjectRendererOptions> class MAGNUM_DEBUGTOOLS_EXPORT ResourceManager: public Magnum::ResourceManager<Magnum::Implementation::ResourceManagerLocalInstance, GL::AbstractShaderProgram, GL::Buffer, GL::Mesh, GL::MeshView, DebugTools::ForceRendererOptions, DebugTools::ObjectRendererOptions>
{ {
public: public:
/**
* @brief Global instance
*
* Assumes that the instance exists.
*/
static ResourceManager& instance();
explicit ResourceManager(); explicit ResourceManager();
~ResourceManager(); ~ResourceManager();
}; };

10
src/Magnum/ResourceManager.h

@ -246,12 +246,14 @@ Basic usage is:
template implementation file. */ template implementation file. */
template<class... Types> class ResourceManager: private Implementation::ResourceManagerImplementation<Types>... { template<class... Types> class ResourceManager: private Implementation::ResourceManagerImplementation<Types>... {
public: public:
#ifdef MAGNUM_BUILD_DEPRECATED
/** /**
* @brief Global instance * @brief Global instance
* *
* Assumes that the instance exists. * Assumes that the instance exists.
*/ */
static ResourceManager<Types...>& instance(); static CORRADE_DEPRECATED("implicit ResourceManager singleton is deprecated, make your own or pass a reference around instead") ResourceManager<Types...>& instance();
#endif
/** /**
* @brief Constructor * @brief Constructor
@ -625,23 +627,29 @@ template<class T> inline ResourceManagerData<T>::Data::~Data() {
} }
#ifdef MAGNUM_BUILD_DEPRECATED
template<class ...Types> ResourceManager<Types...>& ResourceManager<Types...>::instance() { template<class ...Types> ResourceManager<Types...>& ResourceManager<Types...>::instance() {
CORRADE_ASSERT(Implementation::ResourceManagerImplementation<Types...>::internalInstance(), CORRADE_ASSERT(Implementation::ResourceManagerImplementation<Types...>::internalInstance(),
"ResourceManager::instance(): no instance exists", "ResourceManager::instance(): no instance exists",
static_cast<ResourceManager<Types...>&>(*Implementation::ResourceManagerImplementation<Types...>::internalInstance())); static_cast<ResourceManager<Types...>&>(*Implementation::ResourceManagerImplementation<Types...>::internalInstance()));
return static_cast<ResourceManager<Types...>&>(*Implementation::ResourceManagerImplementation<Types...>::internalInstance()); return static_cast<ResourceManager<Types...>&>(*Implementation::ResourceManagerImplementation<Types...>::internalInstance());
} }
#endif
template<class ...Types> ResourceManager<Types...>::ResourceManager() { template<class ...Types> ResourceManager<Types...>::ResourceManager() {
#ifdef MAGNUM_BUILD_DEPRECATED
CORRADE_ASSERT(!Implementation::ResourceManagerImplementation<Types...>::internalInstance(), CORRADE_ASSERT(!Implementation::ResourceManagerImplementation<Types...>::internalInstance(),
"ResourceManager::ResourceManager(): another instance is already created", ); "ResourceManager::ResourceManager(): another instance is already created", );
Implementation::ResourceManagerImplementation<Types...>::internalInstance() = this; Implementation::ResourceManagerImplementation<Types...>::internalInstance() = this;
#endif
} }
template<class ...Types> ResourceManager<Types...>::~ResourceManager() { template<class ...Types> ResourceManager<Types...>::~ResourceManager() {
freeLoaders(typename Implementation::ResourceManagerImplementation<Types...>::TypePack{}); freeLoaders(typename Implementation::ResourceManagerImplementation<Types...>::TypePack{});
#ifdef MAGNUM_BUILD_DEPRECATED
CORRADE_INTERNAL_ASSERT(Implementation::ResourceManagerImplementation<Types...>::internalInstance() == this); CORRADE_INTERNAL_ASSERT(Implementation::ResourceManagerImplementation<Types...>::internalInstance() == this);
Implementation::ResourceManagerImplementation<Types...>::internalInstance() = nullptr; Implementation::ResourceManagerImplementation<Types...>::internalInstance() = nullptr;
#endif
} }
} }

2
src/Magnum/ResourceManager.hpp

@ -40,6 +40,7 @@
namespace Magnum { namespace Implementation { namespace Magnum { namespace Implementation {
#ifdef MAGNUM_BUILD_DEPRECATED
template<class ...Types> template<class ...Types>
#ifndef _MSC_VER #ifndef _MSC_VER
CORRADE_VISIBILITY_EXPORT CORRADE_VISIBILITY_EXPORT
@ -48,6 +49,7 @@ ResourceManager<Types...>*& ResourceManagerLocalInstanceImplementation<Types...>
static ResourceManager<Types...>* _instance(nullptr); static ResourceManager<Types...>* _instance(nullptr);
return _instance; return _instance;
} }
#endif
}} }}

23
src/Magnum/Test/CMakeLists.txt

@ -35,13 +35,6 @@ corrade_add_test(ResourceManagerTest ResourceManagerTest.cpp LIBRARIES Magnum)
target_compile_definitions(ResourceManagerTest PRIVATE "CORRADE_GRACEFUL_ASSERT") target_compile_definitions(ResourceManagerTest PRIVATE "CORRADE_GRACEFUL_ASSERT")
corrade_add_test(SamplerTest SamplerTest.cpp LIBRARIES MagnumTestLib) corrade_add_test(SamplerTest SamplerTest.cpp LIBRARIES MagnumTestLib)
add_library(ResourceManagerLocalInstanceTestLib ${SHARED_OR_STATIC} ResourceManagerLocalInstanceTestLib.cpp)
target_link_libraries(ResourceManagerLocalInstanceTestLib Magnum)
if(NOT BUILD_STATIC)
target_compile_definitions(ResourceManagerLocalInstanceTestLib PRIVATE "ResourceManagerLocalInstanceTestLib_EXPORTS")
endif()
corrade_add_test(ResourceManagerLocalInstanceTest ResourceManagerLocalInstanceTest.cpp LIBRARIES Magnum ResourceManagerLocalInstanceTestLib)
corrade_add_test(TagsTest TagsTest.cpp LIBRARIES Magnum) corrade_add_test(TagsTest TagsTest.cpp LIBRARIES Magnum)
set_target_properties( set_target_properties(
@ -52,8 +45,20 @@ set_target_properties(
PixelFormatTest PixelFormatTest
PixelStorageTest PixelStorageTest
ResourceManagerTest ResourceManagerTest
ResourceManagerLocalInstanceTestLib
ResourceManagerLocalInstanceTest
SamplerTest SamplerTest
TagsTest TagsTest
PROPERTIES FOLDER "Magnum/Test") PROPERTIES FOLDER "Magnum/Test")
if(MAGNUM_BUILD_DEPRECATED)
add_library(ResourceManagerLocalInstanceTestLib ${SHARED_OR_STATIC} ResourceManagerLocalInstanceTestLib.cpp)
target_link_libraries(ResourceManagerLocalInstanceTestLib Magnum)
if(NOT BUILD_STATIC)
target_compile_definitions(ResourceManagerLocalInstanceTestLib PRIVATE "ResourceManagerLocalInstanceTestLib_EXPORTS")
endif()
corrade_add_test(ResourceManagerLocalInstanceTest ResourceManagerLocalInstanceTest.cpp LIBRARIES Magnum ResourceManagerLocalInstanceTestLib)
set_target_properties(
ResourceManagerLocalInstanceTestLib
ResourceManagerLocalInstanceTest
PROPERTIES FOLDER "Magnum/Test")
endif()

3
src/Magnum/Test/ResourceManagerLocalInstanceTest.cpp

@ -42,9 +42,10 @@ ResourceManagerLocalInstanceTest::ResourceManagerLocalInstanceTest() {
} }
void ResourceManagerLocalInstanceTest::instance() { void ResourceManagerLocalInstanceTest::instance() {
CORRADE_IGNORE_DEPRECATED_PUSH
ResourceManagerWithLocalInstance::instance().set("another", 13); ResourceManagerWithLocalInstance::instance().set("another", 13);
CORRADE_COMPARE(&manager.staticInstance, &manager.instance()); CORRADE_COMPARE(&manager.staticInstance, &manager.instance());
CORRADE_IGNORE_DEPRECATED_POP
CORRADE_COMPARE(manager.count<Int>(), 2); CORRADE_COMPARE(manager.count<Int>(), 2);
CORRADE_COMPARE(manager.state<Int>("integer"), ResourceState::Final); CORRADE_COMPARE(manager.state<Int>("integer"), ResourceState::Final);
} }

2
src/Magnum/Test/ResourceManagerLocalInstanceTestLib.cpp

@ -35,9 +35,11 @@ namespace Implementation {
namespace Test { namespace Test {
CORRADE_IGNORE_DEPRECATED_PUSH
ResourceManagerWithLocalInstance::ResourceManagerWithLocalInstance(): staticInstance(static_cast<ResourceManagerWithLocalInstance&>(instance())) { ResourceManagerWithLocalInstance::ResourceManagerWithLocalInstance(): staticInstance(static_cast<ResourceManagerWithLocalInstance&>(instance())) {
/* Add some stuff */ /* Add some stuff */
set("integer", 42); set("integer", 42);
} }
CORRADE_IGNORE_DEPRECATED_POP
}} }}

4
src/Magnum/Test/ResourceManagerTest.cpp

@ -316,7 +316,7 @@ void ResourceManagerTest::clearWhileReferenced() {
void ResourceManagerTest::loader() { void ResourceManagerTest::loader() {
class IntResourceLoader: public AbstractResourceLoader<Int> { class IntResourceLoader: public AbstractResourceLoader<Int> {
public: public:
IntResourceLoader(): resource(ResourceManager::instance().get<Data>("data")) {} IntResourceLoader(ResourceManager& instance): resource{instance.get<Data>("data")} {}
void load() { void load() {
set("hello", Containers::pointer<Int>(773), ResourceDataState::Final, ResourcePolicy::Resident); set("hello", Containers::pointer<Int>(773), ResourceDataState::Final, ResourcePolicy::Resident);
@ -338,7 +338,7 @@ void ResourceManagerTest::loader() {
{ {
ResourceManager rm; ResourceManager rm;
Containers::Pointer<IntResourceLoader> loaderPtr{Containers::InPlaceInit}; Containers::Pointer<IntResourceLoader> loaderPtr{Containers::InPlaceInit, rm};
IntResourceLoader& loader = *loaderPtr; IntResourceLoader& loader = *loaderPtr;
rm.setLoader<Int>(std::move(loaderPtr)); rm.setLoader<Int>(std::move(loaderPtr));

Loading…
Cancel
Save