diff options
author | Pavel Labath <pavelo@centrum.sk> | 2011-07-08 13:35:42 (GMT) |
---|---|---|
committer | Pavel Labath <pavelo@centrum.sk> | 2011-07-08 13:36:58 (GMT) |
commit | 429ba7977a56e9cadf8f745d0f8908a189a830da (patch) | |
tree | 36b5b7d834fc5f5988d14f51fe75159977765047 /src/FbTk | |
parent | 613df819d690dbe8cdec97457bcf538e980093ae (diff) | |
download | fluxbox_pavel-429ba7977a56e9cadf8f745d0f8908a189a830da.zip fluxbox_pavel-429ba7977a56e9cadf8f745d0f8908a189a830da.tar.bz2 |
use Lua::registerInitFunction to simplify some parts of code
Diffstat (limited to 'src/FbTk')
-rw-r--r-- | src/FbTk/LResource.cc | 47 | ||||
-rw-r--r-- | src/FbTk/LResource.hh | 11 |
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 | ||
30 | extern const char LResourceHelper[]; | 30 | extern const char LResourceHelper[]; |
31 | extern const unsigned int LResourceHelper_size; | 31 | extern const unsigned int LResourceHelper_size; |
@@ -61,30 +61,32 @@ namespace { | |||
61 | 61 | ||
62 | return 0; | 62 | return 0; |
63 | } | 63 | } |
64 | } | ||
65 | 64 | ||
66 | void 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 | ||
85 | void LResourceManager::convert(ResourceManager &old, const std::string &new_file) { | 88 | void 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 | ||
101 | LResourceManager::LResourceManager(const std::string &root, lua::state &l) | 103 | LResourceManager::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 | ||
32 | namespace lua { | ||
33 | class state; | ||
34 | } | ||
35 | |||
36 | namespace FbTk { | 32 | namespace FbTk { |
37 | 33 | ||
34 | class Lua; | ||
35 | |||
38 | class LResourceManager: public ResourceManager_base { | 36 | class LResourceManager: public ResourceManager_base { |
39 | public: | 37 | public: |
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 | ||
48 | private: | 45 | private: |
49 | 46 | ||
50 | lua::state *m_l; | 47 | Lua *m_l; |
51 | }; | 48 | }; |
52 | 49 | ||
53 | } // end namespace FbTk | 50 | } // end namespace FbTk |