Resource manager clean-up code

This commit is contained in:
James Turner 2018-05-28 21:57:31 +02:00
parent dc2f142480
commit 7dfe705717
2 changed files with 38 additions and 8 deletions

View File

@ -20,11 +20,17 @@
#include <simgear_config.h> #include <simgear_config.h>
#include <simgear/misc/ResourceManager.hxx> #include <simgear/misc/ResourceManager.hxx>
#include <simgear/debug/logstream.hxx>
namespace simgear namespace simgear
{ {
static ResourceManager* static_manager = NULL; static ResourceManager* static_manager = nullptr;
ResourceProvider::~ResourceProvider()
{
// pin to this compilation unit
}
ResourceManager::ResourceManager() ResourceManager::ResourceManager()
{ {
@ -40,13 +46,20 @@ ResourceManager* ResourceManager::instance()
return static_manager; return static_manager;
} }
ResourceManager::~ResourceManager()
{
assert(this == static_manager);
static_manager = nullptr;
std::for_each(_providers.begin(), _providers.end(),
[](ResourceProvider* p) { delete p; });
}
/** /**
* trivial provider using a fixed base path * trivial provider using a fixed base path
*/ */
class BasePathProvider : public ResourceProvider class BasePathProvider : public ResourceProvider
{ {
public: public:
BasePathProvider(const SGPath& aBase, int aPriority) : BasePathProvider(const SGPath& aBase, ResourceManager::Priority aPriority) :
ResourceProvider(aPriority), ResourceProvider(aPriority),
_base(aBase) _base(aBase)
{ {
@ -69,6 +82,8 @@ void ResourceManager::addBasePath(const SGPath& aPath, Priority aPriority)
void ResourceManager::addProvider(ResourceProvider* aProvider) void ResourceManager::addProvider(ResourceProvider* aProvider)
{ {
assert(aProvider);
ProviderVec::iterator it = _providers.begin(); ProviderVec::iterator it = _providers.begin();
for (; it != _providers.end(); ++it) { for (; it != _providers.end(); ++it) {
if (aProvider->priority() > (*it)->priority()) { if (aProvider->priority() > (*it)->priority()) {
@ -81,6 +96,16 @@ void ResourceManager::addProvider(ResourceProvider* aProvider)
_providers.push_back(aProvider); _providers.push_back(aProvider);
} }
void ResourceManager::removeProvider(ResourceProvider* aProvider)
{
assert(aProvider);
auto it = std::find(_providers.begin(), _providers.end(), aProvider);
if (it == _providers.end()) {
SG_LOG(SG_GENERAL, SG_DEV_ALERT, "unknown provider doing remove");
return;
}
}
SGPath ResourceManager::findPath(const std::string& aResource, SGPath aContext) SGPath ResourceManager::findPath(const std::string& aResource, SGPath aContext)
{ {
if (!aContext.isNull()) { if (!aContext.isNull()) {
@ -90,9 +115,8 @@ SGPath ResourceManager::findPath(const std::string& aResource, SGPath aContext)
} }
} }
ProviderVec::iterator it = _providers.begin(); for (auto provider : _providers) {
for (; it != _providers.end(); ++it) { SGPath path = provider->resolve(aResource, aContext);
SGPath path = (*it)->resolve(aResource, aContext);
if (!path.isNull()) { if (!path.isNull()) {
return path; return path;
} }

View File

@ -36,6 +36,8 @@ class ResourceProvider;
class ResourceManager class ResourceManager
{ {
public: public:
~ResourceManager();
typedef enum { typedef enum {
PRIORITY_DEFAULT = 0, PRIORITY_DEFAULT = 0,
PRIORITY_FALLBACK = -100, PRIORITY_FALLBACK = -100,
@ -55,6 +57,8 @@ public:
*/ */
void addProvider(ResourceProvider* aProvider); void addProvider(ResourceProvider* aProvider);
void removeProvider(ResourceProvider* aProvider);
/** /**
* given a resource name (or path), find the appropriate real resource * given a resource name (or path), find the appropriate real resource
* path. * path.
@ -75,17 +79,19 @@ class ResourceProvider
public: public:
virtual SGPath resolve(const std::string& aResource, SGPath& aContext) const = 0; virtual SGPath resolve(const std::string& aResource, SGPath& aContext) const = 0;
virtual int priority() const virtual ~ResourceProvider();
virtual ResourceManager::Priority priority() const
{ {
return _priority; return _priority;
} }
protected: protected:
ResourceProvider(int aPriority) : ResourceProvider(ResourceManager::Priority aPriority) :
_priority(aPriority) _priority(aPriority)
{} {}
int _priority; ResourceManager::Priority _priority = ResourceManager::PRIORITY_DEFAULT;
}; };
} // of simgear namespace } // of simgear namespace