13
0

refactor lua header includes

This commit is contained in:
Robin Gareus 2016-03-23 13:46:57 +01:00
parent b031d762c9
commit 829da7eb33
4 changed files with 86 additions and 11 deletions

View File

@ -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_

View 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&);

View 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)
{
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;
assert (lsi);
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)
{

View 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