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     ~ResourceManager();
40 
41     typedef enum {
42       PRIORITY_DEFAULT = 0,
43       PRIORITY_FALLBACK = -100,
44       PRIORITY_NORMAL = 100,
45       PRIORITY_HIGH = 1000
46     } Priority;
47 
48     static ResourceManager* instance();
49 
50     static bool haveInstance();
51 
52     static void reset();
53 
54     /**
55      * add a simple fixed resource location, to resolve against
56      */
57     void addBasePath(const SGPath& aPath, Priority aPriority = PRIORITY_DEFAULT);
58 
59     /**
60      *
61      */
62     void addProvider(ResourceProvider* aProvider);
63 
64     void removeProvider(ResourceProvider* aProvider);
65 
66     /**
67      * given a resource name (or path), find the appropriate real resource
68      * path.
69      * @param aContext an optional current location to resolve relative names
70      *   against (e.g a current directory)
71      */
72     SGPath findPath(const std::string& aResource, SGPath aContext = SGPath());
73 
74 private:
75     ResourceManager();
76 
77     typedef std::vector<ResourceProvider*> ProviderVec;
78     ProviderVec _providers;
79 };
80 
81 class ResourceProvider
82 {
83 public:
84     virtual SGPath resolve(const std::string& aResource, SGPath& aContext) const = 0;
85 
86     virtual ~ResourceProvider();
87 
priority() const88     virtual ResourceManager::Priority priority() const
89     {
90       return _priority;
91     }
92 
93 protected:
ResourceProvider(ResourceManager::Priority aPriority)94     ResourceProvider(ResourceManager::Priority aPriority) :
95       _priority(aPriority)
96     {}
97 
98     ResourceManager::Priority _priority = ResourceManager::PRIORITY_DEFAULT;
99 };
100 
101 } // of simgear namespace
102 
103 #endif // of header guard
104