aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk')
-rw-r--r--src/FbTk/Resource.hh40
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
37namespace FbTk { 39namespace FbTk {
38 40
39class XrmDatabaseHelper; 41class XrmDatabaseHelper;
40 42
43class ResourceException: public std::exception {
44public:
45 ResourceException(const std::string &err):
46 m_str(err) { };
47 ~ResourceException() throw() { }
48 const char *what() const throw () { return m_str.c_str(); }
49private:
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
42class Resource_base:private FbTk::NotCopyable 54class 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
232template <typename ResourceType>
233Resource<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