aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/Resource.hh
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-06-05 12:27:13 (GMT)
committerPavel Labath <pavelo@centrum.sk>2011-11-01 09:52:46 (GMT)
commitfc21fd822e527dbd918c6565e0e2cea82b0466f4 (patch)
treeb658bf437fafb80d39986c77caebcaf608761964 /src/FbTk/Resource.hh
parente3fabc4cd47ac5479bca421a4d7b79c0c2d9e120 (diff)
downloadfluxbox_pavel-fc21fd822e527dbd918c6565e0e2cea82b0466f4.zip
fluxbox_pavel-fc21fd822e527dbd918c6565e0e2cea82b0466f4.tar.bz2
A rough version of resource implementation in lua
I added a new class, LResourceManager, which should handle loading and saving of resources like the old ResourceManager, only it does that with the help of lua. I moved the common features of the two managers (interface + a few functions) to a common base class ResourceManager_base. I augmented the Resource_base class with two new functions (setFromLua and pushToLua) which are used by the lua RM instead of getString and setFromString. Parts of the new RM are written in lua. To avoid loading scripts from a file at runtime I decided to link compiled lua code straight into the executable. For this purpose I created a small script which converts a binary file into a declaration of a C array of bytes.
Diffstat (limited to 'src/FbTk/Resource.hh')
-rw-r--r--src/FbTk/Resource.hh78
1 files changed, 59 insertions, 19 deletions
diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh
index 55afe60..1c53bff 100644
--- a/src/FbTk/Resource.hh
+++ b/src/FbTk/Resource.hh
@@ -33,6 +33,10 @@
33#include <typeinfo> 33#include <typeinfo>
34#include "XrmDatabaseHelper.hh" 34#include "XrmDatabaseHelper.hh"
35 35
36namespace lua {
37 class state;
38}
39
36namespace FbTk { 40namespace FbTk {
37 41
38class ResourceException: public std::exception { 42class ResourceException: public std::exception {
@@ -62,6 +66,12 @@ public:
62 /// get name of this resource 66 /// get name of this resource
63 const std::string& name() const { return m_name; } 67 const std::string& name() const { return m_name; }
64 68
69 // Sets the resource value using the value on top of lua stack. Pops the value.
70 virtual void setFromLua(lua::state &l) = 0;
71
72 // pushes the value of the resource on the stack
73 virtual void pushToLua(lua::state &l) const = 0;
74
65protected: 75protected:
66 Resource_base(const std::string &name, const std::string &altname): 76 Resource_base(const std::string &name, const std::string &altname):
67 m_name(name), m_altname(altname) 77 m_name(name), m_altname(altname)
@@ -75,40 +85,31 @@ private:
75template <typename T> 85template <typename T>
76class Resource; 86class Resource;
77 87
78class ResourceManager 88class ResourceManager_base
79{ 89{
80public: 90public:
81 typedef std::list<Resource_base *> ResourceList; 91 typedef std::list<Resource_base *> ResourceList;
82 92
83 // lock specifies if the database should be opened with one level locked 93 virtual ~ResourceManager_base();
84 // (useful for constructing inside initial set of constructors)
85 ResourceManager(const char *filename, bool lock_db);
86 virtual ~ResourceManager();
87
88 /// Load all resources registered to this class
89 /// @return true on success
90 virtual bool load(const char *filename);
91 94
92 /// Save all resouces registered to this class 95 /// Save all resouces registered to this class
93 /// @return true on success 96 /// @return true on success
94 virtual bool save(const char *filename, const char *mergefilename=0); 97 virtual bool save(const char *filename, const char *mergefilename=0) = 0;
95 98
96 99
97 100
98 /// Add resource to list, only used in Resource<T> 101 /// Add resource to list, only used in Resource<T>
99 void addResource(Resource_base &r); 102 virtual void addResource(Resource_base &r) = 0;
100 103
101 /// Remove a specific resource, only used in Resource<T> 104 /// Remove a specific resource, only used in Resource<T>
102 void removeResource(Resource_base &r) { 105 virtual void removeResource(Resource_base &r) = 0;
103 m_resourcelist.remove(&r);
104 }
105 106
106 /// searches for the resource with the resourcename 107 /// searches for the resource with the resourcename
107 /// @return pointer to resource base on success, else 0. 108 /// @return pointer to resource base on success, else 0.
108 Resource_base *findResource(const std::string &resourcename); 109 virtual Resource_base *findResource(const std::string &resourcename) = 0;
109 /// searches for the resource with the resourcename 110 /// searches for the resource with the resourcename
110 /// @return pointer to resource base on success, else 0. 111 /// @return pointer to resource base on success, else 0.
111 const Resource_base *findResource(const std::string &resourcename) const; 112 virtual const Resource_base *findResource(const std::string &resourcename) const = 0;
112 113
113 std::string resourceValue(const std::string &resourcename) const; 114 std::string resourceValue(const std::string &resourcename) const;
114 void setResourceValue(const std::string &resourcename, const std::string &value); 115 void setResourceValue(const std::string &resourcename, const std::string &value);
@@ -120,6 +121,42 @@ public:
120 */ 121 */
121 template <typename ResourceType> 122 template <typename ResourceType>
122 Resource<ResourceType> &getResource(const std::string &resource); 123 Resource<ResourceType> &getResource(const std::string &resource);
124};
125
126class ResourceManager: public ResourceManager_base
127{
128public:
129 typedef std::list<Resource_base *> ResourceList;
130
131 // lock specifies if the database should be opened with one level locked
132 // (useful for constructing inside initial set of constructors)
133 ResourceManager(const char *filename, bool lock_db);
134 virtual ~ResourceManager();
135
136 /// Load all resources registered to this class
137 /// @return true on success
138 virtual bool load(const char *filename);
139
140 /// Save all resouces registered to this class
141 /// @return true on success
142 virtual bool save(const char *filename, const char *mergefilename=0);
143
144
145
146 /// Add resource to list, only used in Resource<T>
147 virtual void addResource(Resource_base &r);
148
149 /// Remove a specific resource, only used in Resource<T>
150 virtual void removeResource(Resource_base &r) {
151 m_resourcelist.remove(&r);
152 }
153
154 /// searches for the resource with the resourcename
155 /// @return pointer to resource base on success, else 0.
156 virtual Resource_base *findResource(const std::string &resourcename);
157 /// searches for the resource with the resourcename
158 /// @return pointer to resource base on success, else 0.
159 virtual const Resource_base *findResource(const std::string &resourcename) const;
123 160
124 // this marks the database as "in use" and will avoid reloading 161 // this marks the database as "in use" and will avoid reloading
125 // resources unless it is zero. 162 // resources unless it is zero.
@@ -164,7 +201,7 @@ template <typename T>
164class Resource:public Resource_base, public Accessor<T> { 201class Resource:public Resource_base, public Accessor<T> {
165public: 202public:
166 typedef T Type; 203 typedef T Type;
167 Resource(ResourceManager &rm, T val, const std::string &name, const std::string &altname): 204 Resource(ResourceManager_base &rm, T val, const std::string &name, const std::string &altname):
168 Resource_base(name, altname), m_value(val), m_defaultval(val), m_rm(rm) { 205 Resource_base(name, altname), m_value(val), m_defaultval(val), m_rm(rm) {
169 m_rm.addResource(*this); // add this to resource handler 206 m_rm.addResource(*this); // add this to resource handler
170 } 207 }
@@ -180,6 +217,9 @@ public:
180 /// @return string value of resource 217 /// @return string value of resource
181 std::string getString() const; 218 std::string getString() const;
182 219
220 virtual void setFromLua(lua::state &l);
221 virtual void pushToLua(lua::state &l) const;
222
183 operator T() const { return m_value; } 223 operator T() const { return m_value; }
184 T& get() { return m_value; } 224 T& get() { return m_value; }
185 T& operator*() { return m_value; } 225 T& operator*() { return m_value; }
@@ -188,13 +228,13 @@ public:
188 const T *operator->() const { return &m_value; } 228 const T *operator->() const { return &m_value; }
189private: 229private:
190 T m_value, m_defaultval; 230 T m_value, m_defaultval;
191 ResourceManager &m_rm; 231 ResourceManager_base &m_rm;
192}; 232};
193 233
194 234
195 235
196template <typename ResourceType> 236template <typename ResourceType>
197Resource<ResourceType> &ResourceManager::getResource(const std::string &resname) { 237Resource<ResourceType> &ResourceManager_base::getResource(const std::string &resname) {
198 Resource_base *res = findResource(resname); 238 Resource_base *res = findResource(resname);
199 if (res == 0) { 239 if (res == 0) {
200 throw ResourceException("Could not find resource \"" + 240 throw ResourceException("Could not find resource \"" +