diff options
author | Pavel Labath <pavelo@centrum.sk> | 2011-06-05 12:27:13 (GMT) |
---|---|---|
committer | Pavel Labath <pavelo@centrum.sk> | 2011-06-15 23:07:49 (GMT) |
commit | de1e05cec6b26f6a7f398a9ea64f38fa0943f9c0 (patch) | |
tree | 6c5cb813d5cf4bad3c620fb42a2fb4236b9f9ec1 /src/FbTk/Resource.hh | |
parent | f7981d4a85985b4862af0036b7728f8543706374 (diff) | |
download | fluxbox_pavel-de1e05cec6b26f6a7f398a9ea64f38fa0943f9c0.zip fluxbox_pavel-de1e05cec6b26f6a7f398a9ea64f38fa0943f9c0.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.hh | 78 |
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 | ||
36 | namespace lua { | ||
37 | class state; | ||
38 | } | ||
39 | |||
36 | namespace FbTk { | 40 | namespace FbTk { |
37 | 41 | ||
38 | class ResourceException: public std::exception { | 42 | class 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 | |||
65 | protected: | 75 | protected: |
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: | |||
75 | template <typename T> | 85 | template <typename T> |
76 | class Resource; | 86 | class Resource; |
77 | 87 | ||
78 | class ResourceManager | 88 | class ResourceManager_base |
79 | { | 89 | { |
80 | public: | 90 | public: |
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 | |||
126 | class ResourceManager: public ResourceManager_base | ||
127 | { | ||
128 | public: | ||
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> | |||
164 | class Resource:public Resource_base, public Accessor<T> { | 201 | class Resource:public Resource_base, public Accessor<T> { |
165 | public: | 202 | public: |
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; } |
189 | private: | 229 | private: |
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 | ||
196 | template <typename ResourceType> | 236 | template <typename ResourceType> |
197 | Resource<ResourceType> &ResourceManager::getResource(const std::string &resname) { | 237 | Resource<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 \"" + |