Is there a better way to do this?
I am trying to write an interface to a LUA compiler in C++. I know there
are some out there, I downloaded 5 or 6 and couldn't get any to work
correctly (they would compile in DevC++, not MSVC++, wouldn't work with LUA
library I had, etc...) so I'm having to write my own.
Right now I'm working on trying to simply run a lua file with parameters.
Steps are not that difficult.
1. Load the lua file
2. Push the parameters using functions
3. Execute the code.
Well I'm trying to make this simple, and optimally I'd like something like:
Lua L;
L.Execute( "MyFile.lua", "parm1", "parm2", "parm3" );
Of course the number of the paramters is not fixed, but va_arg is a
nightmare and it is suggested not to do it in C++. So far I've come close
with:
LuaExecute( L, "MyFile.lua")( "parm1" )( "parm2" )( "parm3" );
which is kinda ugly and could use some improvments. Any suggestions?
Output is:
Here would load :Test
Here (or in operator() would store:a, b, c,
Here would execute
which is what I want. Would just like to know a way to do a bit of a nicer
syntax.
#include <iostream>
#include <vector>
#include <string>
class Lua
{
// stub
};
class Foo
{
public:
Foo( Lua& L, const std::string& name ): L( L )
{
std::cout << "Here would load :" << name << "\n";
}
Foo& operator()( const std::string& Value )
{
Data.push_back( Value );
return *this;
}
~Foo()
{
std::cout << "Here (or in operator() would store:";
for ( std::vector<std::string>::iterator it = Data.begin(); it !=
Data.end(); ++it )
std::cout << *it << ", ";
std::cout << "\n";
std::cout << "Here would execute\n";
}
private:
Lua& L;
std::vector<std::string> Data;
};
int main()
{
Lua L;
Foo( L, "Test" )("a")("b")("c");
}