aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/Resource.hh
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-06-11 11:00:45 (GMT)
committerPavel Labath <pavelo@centrum.sk>2011-06-15 23:12:25 (GMT)
commitcd9df3b8144c4c41f7b57f0075fbb98ef7561296 (patch)
tree10ffc1692e4afbcb2c74b9db8c8a44b434ef62ca /src/FbTk/Resource.hh
parent787c7a994d4328a9c90a037dd464f8007351b0c0 (diff)
downloadfluxbox_pavel-cd9df3b8144c4c41f7b57f0075fbb98ef7561296.zip
fluxbox_pavel-cd9df3b8144c4c41f7b57f0075fbb98ef7561296.tar.bz2
Simplify FbTk::Resource template class
by outsourcing the conversion from string/lua to the specific type (and back) to a separate class. This change touches a lot of files because the interface of FbTk::Resource changed slightly. However, the changes are minor.
Diffstat (limited to 'src/FbTk/Resource.hh')
-rw-r--r--src/FbTk/Resource.hh60
1 files changed, 36 insertions, 24 deletions
diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh
index 1c53bff..fb05426 100644
--- a/src/FbTk/Resource.hh
+++ b/src/FbTk/Resource.hh
@@ -31,12 +31,9 @@
31 31
32#include <exception> 32#include <exception>
33#include <typeinfo> 33#include <typeinfo>
34#include "ResTraits.hh"
34#include "XrmDatabaseHelper.hh" 35#include "XrmDatabaseHelper.hh"
35 36
36namespace lua {
37 class state;
38}
39
40namespace FbTk { 37namespace FbTk {
41 38
42class ResourceException: public std::exception { 39class ResourceException: public std::exception {
@@ -82,7 +79,7 @@ private:
82 std::string m_altname; ///< alternative name 79 std::string m_altname; ///< alternative name
83}; 80};
84 81
85template <typename T> 82template <typename T, typename Traits>
86class Resource; 83class Resource;
87 84
88class ResourceManager_base 85class ResourceManager_base
@@ -119,8 +116,8 @@ public:
119 * it will throw exception if it fails 116 * it will throw exception if it fails
120 * @return reference to resource type 117 * @return reference to resource type
121 */ 118 */
122 template <typename ResourceType> 119 template <typename ResourceType, typename Traits>
123 Resource<ResourceType> &getResource(const std::string &resource); 120 Resource<ResourceType, Traits> &getResource(const std::string &resource);
124}; 121};
125 122
126class ResourceManager: public ResourceManager_base 123class ResourceManager: public ResourceManager_base
@@ -189,15 +186,10 @@ private:
189 186
190/// Real resource class 187/// Real resource class
191/** 188/**
192 * usage: Resource<int> someresource(resourcemanager, 10, "someresourcename", "somealternativename"); 189 * usage: Resource<int, IntTraits<int> > someresource(resourcemanager, 10, "someresourcename", "somealternativename");
193 * and then implement setFromString and getString 190 * If there is no traits class for your type, you have to implement one.
194 * example:
195 * template <>
196 * void Resource<int>::setFromString(const char *str) {
197 * *(*this) = atoi(str);
198 * }
199 */ 191 */
200template <typename T> 192template <typename T, typename Traits>
201class Resource:public Resource_base, public Accessor<T> { 193class Resource:public Resource_base, public Accessor<T> {
202public: 194public:
203 typedef T Type; 195 typedef T Type;
@@ -211,14 +203,30 @@ public:
211 203
212 void setDefaultValue() { m_value = m_defaultval; } 204 void setDefaultValue() { m_value = m_defaultval; }
213 /// sets resource from string, specialized, must be implemented 205 /// sets resource from string, specialized, must be implemented
214 void setFromString(const char *strval); 206 void setFromString(const char *strval) {
207 try {
208 m_value = Traits::fromString(strval);
209 }
210 catch(ConversionError &e) {
211 std::cerr << name() << ": " << e.what() << std::endl;
212 setDefaultValue();
213 }
214 }
215 Accessor<T> &operator =(const T& newvalue) { m_value = newvalue; return *this;} 215 Accessor<T> &operator =(const T& newvalue) { m_value = newvalue; return *this;}
216 /// specialized, must be implemented 216 /// specialized, must be implemented
217 /// @return string value of resource 217 /// @return string value of resource
218 std::string getString() const; 218 std::string getString() const { return Traits::toString(m_value); }
219 219
220 virtual void setFromLua(lua::state &l); 220 virtual void setFromLua(lua::state &l) {
221 virtual void pushToLua(lua::state &l) const; 221 try {
222 m_value = Traits::fromLua(l);
223 }
224 catch(ConversionError &e) {
225 std::cerr << name() << ": " << e.what() << std::endl;
226 setDefaultValue();
227 }
228 }
229 virtual void pushToLua(lua::state &l) const { Traits::toLua(m_value, l); }
222 230
223 operator T() const { return m_value; } 231 operator T() const { return m_value; }
224 T& get() { return m_value; } 232 T& get() { return m_value; }
@@ -232,17 +240,16 @@ private:
232}; 240};
233 241
234 242
235 243template <typename ResourceType, typename Traits>
236template <typename ResourceType> 244Resource<ResourceType, Traits> &ResourceManager_base::getResource(const std::string &resname) {
237Resource<ResourceType> &ResourceManager_base::getResource(const std::string &resname) {
238 Resource_base *res = findResource(resname); 245 Resource_base *res = findResource(resname);
239 if (res == 0) { 246 if (res == 0) {
240 throw ResourceException("Could not find resource \"" + 247 throw ResourceException("Could not find resource \"" +
241 resname + "\""); 248 resname + "\"");
242 } 249 }
243 250
244 Resource<ResourceType> *res_type = 251 Resource<ResourceType, Traits> *res_type =
245 dynamic_cast<Resource<ResourceType> *>(res); 252 dynamic_cast<Resource<ResourceType, Traits> *>(res);
246 if (res_type == 0) { 253 if (res_type == 0) {
247 throw ResourceException("Could not convert resource \"" + 254 throw ResourceException("Could not convert resource \"" +
248 resname + 255 resname +
@@ -252,6 +259,11 @@ Resource<ResourceType> &ResourceManager_base::getResource(const std::string &res
252 return *res_type; 259 return *res_type;
253} 260}
254 261
262typedef Resource<bool, BoolTraits> BoolResource;
263typedef Resource<int, IntTraits<int> > IntResource;
264typedef Resource<unsigned int, IntTraits<unsigned int> > UIntResource;
265typedef Resource<std::string, StringTraits> StringResource;
266
255} // end namespace FbTk 267} // end namespace FbTk
256 268
257#endif // FBTK_RESOURCE_HH 269#endif // FBTK_RESOURCE_HH