refactor lua header includes
authorRobin Gareus <robin@gareus.org>
Wed, 23 Mar 2016 12:46:57 +0000 (13:46 +0100)
committerRobin Gareus <robin@gareus.org>
Wed, 23 Mar 2016 16:00:10 +0000 (17:00 +0100)
libs/ardour/ardour/lua_script_params.h [new file with mode: 0644]
libs/ardour/ardour/luascripting.h
libs/ardour/luascripting.cc
libs/lua/LuaBridge/LuaBridge.h

diff --git a/libs/ardour/ardour/lua_script_params.h b/libs/ardour/ardour/lua_script_params.h
new file mode 100644 (file)
index 0000000..6b041de
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+#ifndef _ardour_lua_script_params_h_
+#define _ardour_lua_script_params_h_
+
+#include "ardour/luascripting.h"
+#include "LuaBridge/LuaBridge.h"
+
+/* Semantically these are static functions of the LuaScripting class
+ * but are kept separately to minimize header includes.
+ *
+ * LuaScripting itself is a standalone abstraction (not depending on luabridge)
+ * luascripting.h is included by session.h (this file is not).
+ *
+ * The implementation of these functions is in libs/ardour/luascripting.cc
+ */
+namespace ARDOUR { namespace LuaScriptParams {
+
+       LuaScriptParamList script_params (LuaScriptInfoPtr, const std::string &);
+       LuaScriptParamList script_params (const std::string &, const std::string &, bool file=true);
+       void params_to_ref (luabridge::LuaRef *tbl_args, const LuaScriptParamList&);
+       void ref_to_params (LuaScriptParamList&, luabridge::LuaRef *tbl_args);
+
+} } // namespace
+
+#endif // _ardour_lua_script_params_h_
index a5a066b2cd57657690cbd0d217f6755db85fb4d1..d55f13179998c27d3978f8a84c04221938e8502b 100644 (file)
@@ -101,12 +101,6 @@ public:
 
        void refresh ();
        static LuaScriptInfoPtr script_info (const std::string &script ) { return scan_script ("", script); }
-
-       static LuaScriptParamList script_params (LuaScriptInfoPtr, const std::string &);
-       static LuaScriptParamList session_script_params (LuaScriptInfoPtr lsi) {
-               return script_params (lsi, "sess_params");
-       }
-
        static bool try_compile (const std::string&, const LuaScriptParamList&);
        static std::string get_factory_bytecode (const std::string&);
 
index d3768c17d50098d2ed064c8036baeac2d773e8fe..5350ea8f117318b596aa836d04a20290ff37ae90 100644 (file)
@@ -23,6 +23,7 @@
 #include "pbd/compose.h"
 
 #include "ardour/luascripting.h"
+#include "ardour/lua_script_params.h"
 #include "ardour/search_paths.h"
 
 #include "lua/luastate.h"
@@ -275,24 +276,33 @@ LuaScriptInfo::str2type (const std::string& str) {
 }
 
 LuaScriptParamList
-LuaScripting::script_params (LuaScriptInfoPtr lsi, const std::string &fn)
+LuaScriptParams::script_params (LuaScriptInfoPtr lsi, const std::string &pname)
 {
-       LuaScriptParamList rv;
        assert (lsi);
+       return LuaScriptParams::script_params (lsi->path, pname);
+}
+
+LuaScriptParamList
+LuaScriptParams::script_params (const std::string& s, const std::string &pname, bool file)
+{
+       LuaScriptParamList rv;
 
        LuaState lua;
        lua_State* L = lua.getState();
-       lua.Print.connect (&LuaScripting::lua_print);
        lua.do_command ("io = nil;");
        lua.do_command ("function ardour () end");
 
        try {
-               lua.do_file (lsi->path);
+               if (file) {
+                       lua.do_file (s);
+               } else {
+                       lua.do_command (s);
+               }
        } catch (luabridge::LuaException const& e) {
                return rv;
        }
 
-       luabridge::LuaRef lua_params = luabridge::getGlobal (L, fn.c_str());
+       luabridge::LuaRef lua_params = luabridge::getGlobal (L, pname.c_str());
        if (lua_params.isFunction ()) {
                luabridge::LuaRef params = lua_params ();
                if (params.isTable ()) {
@@ -320,6 +330,33 @@ LuaScripting::script_params (LuaScriptInfoPtr lsi, const std::string &fn)
        return rv;
 }
 
+void
+LuaScriptParams::params_to_ref (luabridge::LuaRef *tbl_args, const LuaScriptParamList& args)
+{
+       assert (tbl_args &&  (*tbl_args).isTable ());
+       for (LuaScriptParamList::const_iterator i = args.begin(); i != args.end(); ++i) {
+               if ((*i)->optional && !(*i)->is_set) { continue; }
+               (*tbl_args)[(*i)->name] = (*i)->value;
+       }
+}
+
+void
+LuaScriptParams::ref_to_params (LuaScriptParamList& args, luabridge::LuaRef *tbl_ref)
+{
+       assert (tbl_ref &&  (*tbl_ref).isTable ());
+       for (luabridge::Iterator i (*tbl_ref); !i.isNil (); ++i) {
+               if (!i.key ().isString ()) { assert(0); continue; }
+               std::string name = i.key ().cast<std::string> ();
+               std::string value = i.value ().cast<std::string> ();
+               for (LuaScriptParamList::const_iterator ii = args.begin(); ii != args.end(); ++ii) {
+                       if ((*ii)->name == name) {
+                               (*ii)->value = value;
+                               break;
+                       }
+               }
+       }
+}
+
 bool
 LuaScripting::try_compile (const std::string& script, const LuaScriptParamList& args)
 {
index a7384f68d30dfe0c71b6d0d96d786e7734a1a209..706e77cbfdb7dae7101e8fe83986a40fb1925a3b 100644 (file)
@@ -51,6 +51,8 @@
 #include <boost/type_traits.hpp>
 #include <boost/shared_ptr.hpp>
 
+#include "lua/luastate.h"
+
 #define LUABRIDGE_MAJOR_VERSION 2
 #define LUABRIDGE_MINOR_VERSION 0
 #define LUABRIDGE_VERSION 200