]> git.mxchange.org Git - simgear.git/blob - simgear/misc/ResourceManager.hxx
Fixed mingw build.
[simgear.git] / simgear / misc / ResourceManager.hxx
1 // ResourceManager.hxx -- 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
21 #ifndef SG_RESOURCE_MANAGER_HXX
22 #define SG_RESOURCE_MANAGER_HXX
23
24 #include <vector>
25
26 #include <simgear/misc/sg_path.hxx>
27
28 namespace simgear
29 {
30
31 class ResourceProvider;
32
33 /**
34  * singleton management of resources
35  */
36 class ResourceManager
37 {
38 public:
39     typedef enum {
40       PRIORITY_DEFAULT = 0,
41       PRIORITY_FALLBACK = -100,
42       PRIORITY_NORMAL = 100,
43       PRIORITY_HIGH = 1000
44     } Priority;
45
46     static ResourceManager* instance();    
47     
48     /**
49      * add a simple fixed resource location, to resolve against
50      */
51     void addBasePath(const SGPath& aPath, Priority aPriority = PRIORITY_DEFAULT);
52     
53     /**
54      *
55      */
56     void addProvider(ResourceProvider* aProvider);
57     
58     /**
59      * given a resource name (or path), find the appropriate real resource
60      * path.
61      * @param aContext an optional current location to resolve relative names
62      *   against (e.g a current directory)
63      */
64     SGPath findPath(const std::string& aResource, SGPath aContext = SGPath());
65     
66 private:
67     ResourceManager();
68     
69     typedef std::vector<ResourceProvider*> ProviderVec;
70     ProviderVec _providers;
71 };      
72     
73 class ResourceProvider
74 {
75 public:
76     virtual SGPath resolve(const std::string& aResource, SGPath& aContext) const = 0;
77     
78     virtual int priority() const
79     {
80       return _priority;
81     }
82     
83 protected:
84     ResourceProvider(int aPriority) :
85       _priority(aPriority)
86     {}
87     
88     int _priority;
89 };
90     
91 } // of simgear namespace
92
93 #endif // of header guard