aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-07-08 13:35:42 (GMT)
committerPavel Labath <pavelo@centrum.sk>2011-11-01 09:57:21 (GMT)
commitdbc29ae953ab73b9cdd2a13dbe17a1eb0a8c863a (patch)
tree0ad775057cef0499f1e02d0946ecb2ac52cf3eed /src/FbTk
parentb3bd3805961c05776c7f398ce98082effebd8ca2 (diff)
downloadfluxbox_paul-dbc29ae953ab73b9cdd2a13dbe17a1eb0a8c863a.zip
fluxbox_paul-dbc29ae953ab73b9cdd2a13dbe17a1eb0a8c863a.tar.bz2
use Lua::registerInitFunction to simplify some parts of code
Diffstat (limited to 'src/FbTk')
-rw-r--r--src/FbTk/LResource.cc47
-rw-r--r--src/FbTk/LResource.hh11
2 files changed, 26 insertions, 32 deletions
diff --git a/src/FbTk/LResource.cc b/src/FbTk/LResource.cc
index b91f0d8..6671b89 100644
--- a/src/FbTk/LResource.cc
+++ b/src/FbTk/LResource.cc
@@ -24,8 +24,8 @@
24 24
25#include "LResource.hh" 25#include "LResource.hh"
26 26
27#include "LuaUtil.hh"
27#include "Resource.hh" 28#include "Resource.hh"
28#include "Luamm.hh"
29 29
30extern const char LResourceHelper[]; 30extern const char LResourceHelper[];
31extern const unsigned int LResourceHelper_size; 31extern const unsigned int LResourceHelper_size;
@@ -61,30 +61,32 @@ namespace {
61 61
62 return 0; 62 return 0;
63 } 63 }
64}
65 64
66void LResourceManager::initState(lua::state &l) { 65 void initState(Lua &l) {
67 l.checkstack(6); 66 l.checkstack(6);
68 lua::stack_sentry s(l); 67 lua::stack_sentry s(l);
69 68
70 l.loadstring(LResourceHelper, LResourceHelper_size); 69 l.loadstring(LResourceHelper, LResourceHelper_size);
71 l.pushfunction(&readResource); 70 l.pushfunction(&readResource);
72 l.pushfunction(&writeResource); 71 l.pushfunction(&writeResource);
73 l.newtable(); {
74 l.newtable(); { 72 l.newtable(); {
75 l.pushvalue(-2); 73 l.newtable(); {
76 l.setfield(-2, "__metatable"); 74 l.pushvalue(-2);
77 } l.setfield(lua::REGISTRYINDEX, resource_metatable); 75 l.setfield(-2, "__metatable");
76 } l.setfield(lua::REGISTRYINDEX, resource_metatable);
77 }
78 l.call(3, 3);
79 l.setfield(lua::REGISTRYINDEX, dump_resources);
80 l.setfield(lua::REGISTRYINDEX, register_resource);
81 l.setfield(lua::REGISTRYINDEX, make_root);
78 } 82 }
79 l.call(3, 3); 83
80 l.setfield(lua::REGISTRYINDEX, dump_resources); 84 Lua::RegisterInitFunction register_init_state(&initState);
81 l.setfield(lua::REGISTRYINDEX, register_resource); 85
82 l.setfield(lua::REGISTRYINDEX, make_root); 86} // anonymous namespace
83}
84 87
85void LResourceManager::convert(ResourceManager &old, const std::string &new_file) { 88void LResourceManager::convert(ResourceManager &old, const std::string &new_file) {
86 lua::state l; 89 Lua l;
87 initState(l);
88 90
89 LResourceManager new_rm(old.root(), l); 91 LResourceManager new_rm(old.root(), l);
90 for(ResourceList::const_iterator i = old.begin(); i != old.end(); ++i) { 92 for(ResourceList::const_iterator i = old.begin(); i != old.end(); ++i) {
@@ -98,17 +100,12 @@ void LResourceManager::convert(ResourceManager &old, const std::string &new_file
98 new_rm.save(new_file.c_str(), NULL); 100 new_rm.save(new_file.c_str(), NULL);
99} 101}
100 102
101LResourceManager::LResourceManager(const std::string &root, lua::state &l) 103LResourceManager::LResourceManager(const std::string &root, Lua &l)
102 : ResourceManager_base(root), m_l(&l) { 104 : ResourceManager_base(root), m_l(&l) {
103 l.checkstack(2); 105 l.checkstack(2);
104 lua::stack_sentry s(l); 106 lua::stack_sentry s(l);
105 107
106 l.getfield(lua::REGISTRYINDEX, make_root); 108 l.getfield(lua::REGISTRYINDEX, make_root);
107 if(l.isnil(-1)) {
108 l.pop();
109 initState(l);
110 l.getfield(lua::REGISTRYINDEX, make_root);
111 }
112 l.pushstring(root); 109 l.pushstring(root);
113 l.call(1, 0); 110 l.call(1, 0);
114} 111}
diff --git a/src/FbTk/LResource.hh b/src/FbTk/LResource.hh
index 1125707..1567269 100644
--- a/src/FbTk/LResource.hh
+++ b/src/FbTk/LResource.hh
@@ -29,25 +29,22 @@
29 29
30#include "Resource.hh" 30#include "Resource.hh"
31 31
32namespace lua {
33 class state;
34}
35
36namespace FbTk { 32namespace FbTk {
37 33
34class Lua;
35
38class LResourceManager: public ResourceManager_base { 36class LResourceManager: public ResourceManager_base {
39public: 37public:
40 static void initState(lua::state &l);
41 static void convert(ResourceManager &old, const std::string &new_file); 38 static void convert(ResourceManager &old, const std::string &new_file);
42 39
43 LResourceManager(const std::string &root, lua::state &l); 40 LResourceManager(const std::string &root, Lua &l);
44 virtual bool save(const char *filename, const char *); 41 virtual bool save(const char *filename, const char *);
45 virtual void addResource(Resource_base &r); 42 virtual void addResource(Resource_base &r);
46 virtual void removeResource(Resource_base &r); 43 virtual void removeResource(Resource_base &r);
47 44
48private: 45private:
49 46
50 lua::state *m_l; 47 Lua *m_l;
51}; 48};
52 49
53} // end namespace FbTk 50} // end namespace FbTk