From d23eeeaa4e1ed5adb76d97eb47dd0b88b6dfed3f Mon Sep 17 00:00:00 2001 From: fluxgen Date: Sat, 18 Feb 2006 10:12:10 +0000 Subject: Added template getResource(string) to ResourceManager. It finds and returns reference to resource on success, else throws ResourceException. --- src/FbTk/Resource.hh | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) 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 @@ #include #include +#include +#include #include "XrmDatabaseHelper.hh" namespace FbTk { class XrmDatabaseHelper; +class ResourceException: public std::exception { +public: + ResourceException(const std::string &err): + m_str(err) { }; + ~ResourceException() throw() { } + const char *what() const throw () { return m_str.c_str(); } +private: + std::string m_str; +}; + /// Base class for resources, this is only used in ResourceManager class Resource_base:private FbTk::NotCopyable { @@ -100,9 +112,18 @@ public: Resource_base *findResource(const std::string &resourcename); const Resource_base *findResource(const std::string &resourcename) const; + std::string resourceValue(const std::string &resourcename) const; void setResourceValue(const std::string &resourcename, const std::string &value); + /** + * Will search and cast the resource to Resource, + * it will throw exception if it fails + * @return reference to resource type + */ + template + Resource &getResource(const std::string &resource); + // this marks the database as "in use" and will avoid reloading // resources unless it is zero. // It returns this resource manager. Useful for passing to @@ -208,6 +229,25 @@ void ResourceManager::addResource(Resource &r) { } +template +Resource &ResourceManager::getResource(const std::string &resname) { + Resource_base *res = findResource(resname); + if (res == 0) { + throw ResourceException("Could not find resource \"" + + resname + "\""); + } + + Resource *res_type = + dynamic_cast *>(res); + if (res_type == 0) { + throw ResourceException("Could not convert resource \"" + + resname + + "\""); + } + + return *res_type; +} + } // end namespace FbTk #endif // FBTK_RESOURCE_HH -- cgit v0.11.2