1 // ResourceManager.cxx -- manage finding resources by names/paths
2 // Copyright (C) 2010 James Turner
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Library General Public
6 // License as published by the Free Software Foundation; either
7 // version 2 of the License, or (at your option) any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Library General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <simgear_config.h>
22 #include <simgear/misc/ResourceManager.hxx>
27 static ResourceManager* static_manager = NULL;
29 ResourceManager::ResourceManager()
34 ResourceManager* ResourceManager::instance()
36 if (!static_manager) {
37 static_manager = new ResourceManager();
40 return static_manager;
44 * trivial provider using a fixed base path
46 class BasePathProvider : public ResourceProvider
49 BasePathProvider(const SGPath& aBase, int aPriority) :
50 ResourceProvider(aPriority),
56 virtual SGPath resolve(const std::string& aResource, SGPath&) const
58 SGPath p(_base, aResource);
59 return p.exists() ? p : SGPath();
65 void ResourceManager::addBasePath(const SGPath& aPath, Priority aPriority)
67 addProvider(new BasePathProvider(aPath, aPriority));
70 void ResourceManager::addProvider(ResourceProvider* aProvider)
72 ProviderVec::iterator it = _providers.begin();
73 for (; it != _providers.end(); ++it) {
74 if (aProvider->priority() > (*it)->priority()) {
75 _providers.insert(it, aProvider);
80 // fell out of the iteration, goes to the end of the vec
81 _providers.push_back(aProvider);
84 SGPath ResourceManager::findPath(const std::string& aResource, SGPath aContext)
86 if (!aContext.isNull()) {
87 SGPath r(aContext, aResource);
93 ProviderVec::iterator it = _providers.begin();
94 for (; it != _providers.end(); ++it) {
95 SGPath path = (*it)->resolve(aResource, aContext);
104 } // of namespace simgear