diff options
author | Pavel Labath <pavelo@centrum.sk> | 2011-06-11 11:00:45 (GMT) |
---|---|---|
committer | Pavel Labath <pavelo@centrum.sk> | 2011-06-15 23:12:25 (GMT) |
commit | cd9df3b8144c4c41f7b57f0075fbb98ef7561296 (patch) | |
tree | 10ffc1692e4afbcb2c74b9db8c8a44b434ef62ca /src/FbTk/Resource.hh | |
parent | 787c7a994d4328a9c90a037dd464f8007351b0c0 (diff) | |
download | fluxbox_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.hh | 60 |
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 | ||
36 | namespace lua { | ||
37 | class state; | ||
38 | } | ||
39 | |||
40 | namespace FbTk { | 37 | namespace FbTk { |
41 | 38 | ||
42 | class ResourceException: public std::exception { | 39 | class 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 | ||
85 | template <typename T> | 82 | template <typename T, typename Traits> |
86 | class Resource; | 83 | class Resource; |
87 | 84 | ||
88 | class ResourceManager_base | 85 | class 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 | ||
126 | class ResourceManager: public ResourceManager_base | 123 | class 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 | */ |
200 | template <typename T> | 192 | template <typename T, typename Traits> |
201 | class Resource:public Resource_base, public Accessor<T> { | 193 | class Resource:public Resource_base, public Accessor<T> { |
202 | public: | 194 | public: |
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 | 243 | template <typename ResourceType, typename Traits> | |
236 | template <typename ResourceType> | 244 | Resource<ResourceType, Traits> &ResourceManager_base::getResource(const std::string &resname) { |
237 | Resource<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 | ||
262 | typedef Resource<bool, BoolTraits> BoolResource; | ||
263 | typedef Resource<int, IntTraits<int> > IntResource; | ||
264 | typedef Resource<unsigned int, IntTraits<unsigned int> > UIntResource; | ||
265 | typedef 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 |