From 429ba7977a56e9cadf8f745d0f8908a189a830da Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Fri, 8 Jul 2011 15:35:42 +0200 Subject: use Lua::registerInitFunction to simplify some parts of code --- src/FbCommands.cc | 19 ++++++++++--------- src/FbTk/LResource.cc | 47 ++++++++++++++++++++++------------------------- src/FbTk/LResource.hh | 11 ++++------- src/MenuCreator.cc | 1 + src/Screen.cc | 2 +- src/fluxbox.cc | 3 ++- src/fluxbox.hh | 4 ++-- 7 files changed, 42 insertions(+), 45 deletions(-) diff --git a/src/FbCommands.cc b/src/FbCommands.cc index 0d7c1df..c74cce6 100644 --- a/src/FbCommands.cc +++ b/src/FbCommands.cc @@ -31,7 +31,7 @@ #include "MenuCreator.hh" #include "FbTk/I18n.hh" -#include "FbTk/Luamm.hh" +#include "FbTk/LuaUtil.hh" #include "FbTk/Theme.hh" #include "FbTk/Menu.hh" #include "FbTk/CommandParser.hh" @@ -547,6 +547,15 @@ REGISTER_UNTRUSTED_COMMAND_WITH_ARGS(lua, LuaCmd, void); namespace { const char LuaCmds[] = "FbCommands::LuaCmd"; + + void initLuaCmds(FbTk::Lua &l) { + l.checkstack(1); + + l.newtable(); + l.rawsetfield(lua::REGISTRYINDEX, LuaCmds); + } + + FbTk::Lua::RegisterInitFunction register_init_lua_cmds(&initLuaCmds); } LuaCmd::LuaCmd(const std::string &chunk) { @@ -571,14 +580,6 @@ void LuaCmd::init(lua::state &l) { l.checkstack(2); l.rawgetfield(lua::REGISTRYINDEX, LuaCmds); { - if(l.isnil(-1)) { - l.pop(); - l.newtable(); - - l.pushvalue(-1); - l.rawsetfield(lua::REGISTRYINDEX, LuaCmds); - } - l.pushvalue(-2); m_ref = l.ref(-2); } l.pop(); 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 @@ #include "LResource.hh" +#include "LuaUtil.hh" #include "Resource.hh" -#include "Luamm.hh" extern const char LResourceHelper[]; extern const unsigned int LResourceHelper_size; @@ -61,30 +61,32 @@ namespace { return 0; } -} -void LResourceManager::initState(lua::state &l) { - l.checkstack(6); - lua::stack_sentry s(l); + void initState(Lua &l) { + l.checkstack(6); + lua::stack_sentry s(l); - l.loadstring(LResourceHelper, LResourceHelper_size); - l.pushfunction(&readResource); - l.pushfunction(&writeResource); - l.newtable(); { + l.loadstring(LResourceHelper, LResourceHelper_size); + l.pushfunction(&readResource); + l.pushfunction(&writeResource); l.newtable(); { - l.pushvalue(-2); - l.setfield(-2, "__metatable"); - } l.setfield(lua::REGISTRYINDEX, resource_metatable); + l.newtable(); { + l.pushvalue(-2); + l.setfield(-2, "__metatable"); + } l.setfield(lua::REGISTRYINDEX, resource_metatable); + } + l.call(3, 3); + l.setfield(lua::REGISTRYINDEX, dump_resources); + l.setfield(lua::REGISTRYINDEX, register_resource); + l.setfield(lua::REGISTRYINDEX, make_root); } - l.call(3, 3); - l.setfield(lua::REGISTRYINDEX, dump_resources); - l.setfield(lua::REGISTRYINDEX, register_resource); - l.setfield(lua::REGISTRYINDEX, make_root); -} + + Lua::RegisterInitFunction register_init_state(&initState); + +} // anonymous namespace void LResourceManager::convert(ResourceManager &old, const std::string &new_file) { - lua::state l; - initState(l); + Lua l; LResourceManager new_rm(old.root(), l); 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 new_rm.save(new_file.c_str(), NULL); } -LResourceManager::LResourceManager(const std::string &root, lua::state &l) +LResourceManager::LResourceManager(const std::string &root, Lua &l) : ResourceManager_base(root), m_l(&l) { l.checkstack(2); lua::stack_sentry s(l); l.getfield(lua::REGISTRYINDEX, make_root); - if(l.isnil(-1)) { - l.pop(); - initState(l); - l.getfield(lua::REGISTRYINDEX, make_root); - } l.pushstring(root); l.call(1, 0); } 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 @@ #include "Resource.hh" -namespace lua { - class state; -} - namespace FbTk { +class Lua; + class LResourceManager: public ResourceManager_base { public: - static void initState(lua::state &l); static void convert(ResourceManager &old, const std::string &new_file); - LResourceManager(const std::string &root, lua::state &l); + LResourceManager(const std::string &root, Lua &l); virtual bool save(const char *filename, const char *); virtual void addResource(Resource_base &r); virtual void removeResource(Resource_base &r); private: - lua::state *m_l; + Lua *m_l; }; } // end namespace FbTk diff --git a/src/MenuCreator.cc b/src/MenuCreator.cc index ea45487..406d9e5 100644 --- a/src/MenuCreator.cc +++ b/src/MenuCreator.cc @@ -43,6 +43,7 @@ #include "RootCmdMenuItem.hh" #include "FbTk/I18n.hh" +#include "FbTk/LuaUtil.hh" #include "FbTk/MultiButtonMenuItem.hh" #include "FbTk/BoolMenuItem.hh" #include "FbTk/RefCount.hh" diff --git a/src/Screen.cc b/src/Screen.cc index f57c5ad..cb86c73 100644 --- a/src/Screen.cc +++ b/src/Screen.cc @@ -73,7 +73,7 @@ #include "FbTk/STLUtil.hh" #include "FbTk/KeyUtil.hh" #include "FbTk/Util.hh" -#include "FbTk/Luamm.hh" +#include "FbTk/LuaUtil.hh" //use GNU extensions #ifndef _GNU_SOURCE diff --git a/src/fluxbox.cc b/src/fluxbox.cc index 88038c9..7c2147c 100644 --- a/src/fluxbox.cc +++ b/src/fluxbox.cc @@ -43,6 +43,7 @@ #include "FbTk/FileUtil.hh" #include "FbTk/ImageControl.hh" #include "FbTk/EventManager.hh" +#include "FbTk/LuaUtil.hh" #include "FbTk/StringUtil.hh" #include "FbTk/Util.hh" #include "FbTk/LResource.hh" @@ -232,7 +233,7 @@ Fluxbox::Fluxbox(int argc, char **argv, const std::string& dpy_name, const std::string& rc_path, const std::string& rc_filename, bool xsync) : FbTk::App(dpy_name.c_str()), - m_l(new lua::state()), + m_l(new Lua), m_fbatoms(FbAtoms::instance()), m_resourcemanager("session", *m_l), diff --git a/src/fluxbox.hh b/src/fluxbox.hh index e027660..c241fb7 100644 --- a/src/fluxbox.hh +++ b/src/fluxbox.hh @@ -173,7 +173,7 @@ public: /// get screen from number BScreen *findScreen(int num); - lua::state &lua() { return *m_l; } + FbTk::Lua& lua() { return *m_l; } typedef std::list ScreenList; const ScreenList screenList() const { return m_screen_list; } @@ -226,7 +226,7 @@ private: /// Called when a window layer changes void windowLayerChanged(FluxboxWindow &win); - std::auto_ptr m_l; + std::auto_ptr m_l; std::auto_ptr m_fbatoms; FbTk::LResourceManager m_resourcemanager; -- cgit v0.11.2