diff options
author | fluxgen <fluxgen> | 2006-02-18 10:12:10 (GMT) |
---|---|---|
committer | fluxgen <fluxgen> | 2006-02-18 10:12:10 (GMT) |
commit | d23eeeaa4e1ed5adb76d97eb47dd0b88b6dfed3f (patch) | |
tree | 74205e15fb35c7dc0030ea42dd24b3bafde7b8f0 /src | |
parent | c21f0d4a74625c65a0bf0ef2593fa0c596b701a7 (diff) | |
download | fluxbox_pavel-d23eeeaa4e1ed5adb76d97eb47dd0b88b6dfed3f.zip fluxbox_pavel-d23eeeaa4e1ed5adb76d97eb47dd0b88b6dfed3f.tar.bz2 |
Added template getResource(string) to ResourceManager.
It finds and returns reference to resource on success,
else throws ResourceException.
Diffstat (limited to 'src')
-rw-r--r-- | src/FbTk/Resource.hh | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh index c48b6ca..95f649e 100644 --- a/src/FbTk/Resource.hh +++ b/src/FbTk/Resource.hh | |||
@@ -32,12 +32,24 @@ | |||
32 | 32 | ||
33 | #include <X11/Xlib.h> | 33 | #include <X11/Xlib.h> |
34 | #include <X11/Xresource.h> | 34 | #include <X11/Xresource.h> |
35 | #include <exception> | ||
36 | #include <typeinfo> | ||
35 | #include "XrmDatabaseHelper.hh" | 37 | #include "XrmDatabaseHelper.hh" |
36 | 38 | ||
37 | namespace FbTk { | 39 | namespace FbTk { |
38 | 40 | ||
39 | class XrmDatabaseHelper; | 41 | class XrmDatabaseHelper; |
40 | 42 | ||
43 | class ResourceException: public std::exception { | ||
44 | public: | ||
45 | ResourceException(const std::string &err): | ||
46 | m_str(err) { }; | ||
47 | ~ResourceException() throw() { } | ||
48 | const char *what() const throw () { return m_str.c_str(); } | ||
49 | private: | ||
50 | std::string m_str; | ||
51 | }; | ||
52 | |||
41 | /// Base class for resources, this is only used in ResourceManager | 53 | /// Base class for resources, this is only used in ResourceManager |
42 | class Resource_base:private FbTk::NotCopyable | 54 | class Resource_base:private FbTk::NotCopyable |
43 | { | 55 | { |
@@ -100,9 +112,18 @@ public: | |||
100 | 112 | ||
101 | Resource_base *findResource(const std::string &resourcename); | 113 | Resource_base *findResource(const std::string &resourcename); |
102 | const Resource_base *findResource(const std::string &resourcename) const; | 114 | const Resource_base *findResource(const std::string &resourcename) const; |
115 | |||
103 | std::string resourceValue(const std::string &resourcename) const; | 116 | std::string resourceValue(const std::string &resourcename) const; |
104 | void setResourceValue(const std::string &resourcename, const std::string &value); | 117 | void setResourceValue(const std::string &resourcename, const std::string &value); |
105 | 118 | ||
119 | /** | ||
120 | * Will search and cast the resource to Resource<Type>, | ||
121 | * it will throw exception if it fails | ||
122 | * @return reference to resource type | ||
123 | */ | ||
124 | template <typename ResourceType> | ||
125 | Resource<ResourceType> &getResource(const std::string &resource); | ||
126 | |||
106 | // this marks the database as "in use" and will avoid reloading | 127 | // this marks the database as "in use" and will avoid reloading |
107 | // resources unless it is zero. | 128 | // resources unless it is zero. |
108 | // It returns this resource manager. Useful for passing to | 129 | // It returns this resource manager. Useful for passing to |
@@ -208,6 +229,25 @@ void ResourceManager::addResource(Resource<T> &r) { | |||
208 | } | 229 | } |
209 | 230 | ||
210 | 231 | ||
232 | template <typename ResourceType> | ||
233 | Resource<ResourceType> &ResourceManager::getResource(const std::string &resname) { | ||
234 | Resource_base *res = findResource(resname); | ||
235 | if (res == 0) { | ||
236 | throw ResourceException("Could not find resource \"" + | ||
237 | resname + "\""); | ||
238 | } | ||
239 | |||
240 | Resource<ResourceType> *res_type = | ||
241 | dynamic_cast<Resource<ResourceType> *>(res); | ||
242 | if (res_type == 0) { | ||
243 | throw ResourceException("Could not convert resource \"" + | ||
244 | resname + | ||
245 | "\""); | ||
246 | } | ||
247 | |||
248 | return *res_type; | ||
249 | } | ||
250 | |||
211 | } // end namespace FbTk | 251 | } // end namespace FbTk |
212 | 252 | ||
213 | #endif // FBTK_RESOURCE_HH | 253 | #endif // FBTK_RESOURCE_HH |