Commit 473dba97 authored by Artur Grunau's avatar Artur Grunau
Browse files

Make LuaPipeline responsible for creating states

Lua states used to be created externally and passed to LuaPipeline
objects on instantiation. Because they shouldn't be shared anyway, and
due to high state-pipeline coupling, states are now created by Lua
pipelines directly.

References #1
parent b3dba5f4
#include "luapipeline.h"
extern "C" {
#include "lualib.h"
#include "lauxlib.h"
}
namespace campvis {
......@@ -16,16 +21,37 @@ namespace campvis {
}
}
LuaPipeline::LuaPipeline(lua_State* state, DataContainer* dc)
LuaPipeline::LuaPipeline(std::string scriptPath, DataContainer* dc)
: AutoEvaluationPipeline(dc)
, _luaState(state)
, _scriptPath(scriptPath)
{
lua_getglobal(state, "pipeline");
_luaState = luaL_newstate();
// load the libs
luaL_openlibs(_luaState);
// run a Lua script here; true is returned if there were errors
if (luaL_dofile(_luaState, scriptPath.c_str())) {
const char* msg = lua_tostring(_luaState, -1);
if (msg == nullptr)
printf("(error object is not a string)");
else
printf("%s", msg);
lua_pop(_luaState, 1);
}
lua_getglobal(_luaState, "pipeline");
if (!lua_istable(state, -1))
printf("No valid Lua pipeline found (pipeline is %s)\n", lua_typename(state, lua_type(state, -1)));
if (!lua_istable(_luaState, -1))
printf("No valid Lua pipeline found (pipeline is %s)\n", lua_typename(_luaState, lua_type(_luaState, -1)));
lua_pop(_luaState, 1);
}
lua_pop(state, 1);
LuaPipeline::~LuaPipeline() {
lua_close(_luaState);
}
void LuaPipeline::init() {
......
......@@ -11,7 +11,18 @@ extern "C" {
namespace campvis {
class LuaPipeline : public AutoEvaluationPipeline {
public:
LuaPipeline(lua_State* state, DataContainer* dc);
/**
* Create a new Lua pipeline.
*
* \param scriptPath Path to the Lua script defining the pipeline
* \param dc DataContainer containing local working set of data for this pipeline
*/
LuaPipeline(const std::string scriptPath, DataContainer* dc);
/**
* Virtual Destructor
*/
virtual ~LuaPipeline();
/// \see AbstractPipeline::getName()
virtual const std::string getName() const { return getId(); }
......@@ -24,7 +35,8 @@ namespace campvis {
virtual void deinit();
protected:
lua_State* _luaState;
const std::string _scriptPath; ///< path to the Lua script defining the pipeline
lua_State* _luaState; ///< Lua state used to evaluate the pipeline
};
}
......
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include "modules/pipelinefactory.h"
#include "luapipeline.h"
......@@ -13,26 +7,9 @@ using namespace campvis;
int main()
{
lua_State* L = luaL_newstate();
// load the libs
luaL_openlibs(L);
// run a Lua script here; true is returned if there were errors
if (luaL_dofile(L, "script.lua")) {
const char* msg = lua_tostring(L, -1);
if (msg == nullptr)
printf("(error object is not a string)");
else
printf("%s", msg);
lua_pop(L, 1);
}
PipelineFactory& pipelineFactory = PipelineFactory::getRef();
pipelineFactory.registerPipeline<LuaPipeline>([L] (DataContainer* dc) -> AbstractPipeline* {
return new LuaPipeline(L, dc);
pipelineFactory.registerPipeline<LuaPipeline>([] (DataContainer* dc) -> AbstractPipeline* {
return new LuaPipeline("script.lua", dc);
});
DataContainer* dc = new DataContainer("Test Data Container");
......@@ -42,7 +19,5 @@ int main()
printf("\nI am done with Lua in C++.\n");
lua_close(L);
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment