]> git.mxchange.org Git - simgear.git/blob - simgear/misc/ResourceManager.cxx
Fixed mingw build.
[simgear.git] / simgear / misc / ResourceManager.cxx
1 // ResourceManager.cxx -- manage finding resources by names/paths
2 // Copyright (C) 2010 James Turner
3 //
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.
8 //
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.
13 //
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.
17 //
18 // $Id$
19
20 #include <simgear_config.h>
21
22 #include <simgear/misc/ResourceManager.hxx>
23
24 namespace simgear
25 {
26     
27 static ResourceManager* static_manager = NULL;
28
29 ResourceManager::ResourceManager()
30 {
31     
32 }
33
34 ResourceManager* ResourceManager::instance()
35 {
36     if (!static_manager) {
37         static_manager = new ResourceManager();
38     }
39     
40     return static_manager;
41 }    
42
43 /**
44  * trivial provider using a fixed base path
45  */
46 class BasePathProvider : public ResourceProvider
47 {
48 public:
49     BasePathProvider(const SGPath& aBase, int aPriority) :
50         ResourceProvider(aPriority),
51         _base(aBase)
52     {
53         
54     }
55     
56     virtual SGPath resolve(const std::string& aResource, SGPath&) const
57     {
58         SGPath p(_base, aResource);
59         return p.exists() ? p : SGPath();
60     }
61 private:
62     SGPath _base;  
63 };
64
65 void ResourceManager::addBasePath(const SGPath& aPath, Priority aPriority)
66 {
67     addProvider(new BasePathProvider(aPath, aPriority));
68 }
69
70 void ResourceManager::addProvider(ResourceProvider* aProvider)
71 {
72     ProviderVec::iterator it = _providers.begin();
73     for (; it != _providers.end(); ++it) {
74       if (aProvider->priority() > (*it)->priority()) {
75         _providers.insert(it, aProvider);
76         return;
77       }
78     }
79     
80     // fell out of the iteration, goes to the end of the vec
81     _providers.push_back(aProvider);
82 }
83
84 SGPath ResourceManager::findPath(const std::string& aResource, SGPath aContext)
85 {
86     if (!aContext.isNull()) {
87         SGPath r(aContext, aResource);
88         if (r.exists()) {
89             return r;
90         }
91     }
92     
93     ProviderVec::iterator it = _providers.begin();
94     for (; it != _providers.end(); ++it) {
95       SGPath path = (*it)->resolve(aResource, aContext);
96       if (!path.isNull()) {
97         return path;
98       }
99     }
100     
101     return SGPath();
102 }
103
104 } // of namespace simgear