Re: Is there a better way to do this?
On Jun 20, 4:55 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
"Jim Langston" <tazmas...@rocketmail.com> wrote in message
news:vJq6k.3$Vq.2@newsfe05.lga...
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?
Ugly is in the eye of the beholder. There's always things like:
Lua L ;
L.execute( "MyFile.lua" ) << "parm1" ...
Just make Lua::execute take the name of the program, and return
a "magic" object which supports any binary operator which
pleases you (and those who have to read your code). The magic
object collects the arguments, and calls the actual execution
function with the arguments (in a vector, or whatever) in its
destructor.
To make this work, of course, you'll either need move semantics
(coming to a compiler near you one of these days), or you'll
need to simulate them. You might want to take a look at how I
handled the problem with my OutputStreamWrapper in the code at
my site: basically the object you return contains a
boost::shared_ptr to the actual object, and forwards all
requests to it, or you implement something similar---the idea is
that all copies be idempotent, and that the "real" destructor is
only executed when the destructor of the last copy is called.
[SNIP]
For now I've decided to use this ugly to code, easy to use kludge:
int ExecuteFile( const std::string& FileName, const std::string& Parm=
1 =
"\xFF", const std::string& Parm2 = "\xFF",
const std::string& Parm3 = "\xFF", const std::string& Parm4 =
"\xFF", const std::string& Parm5 = "\xFF")
{
int ReturnCode = luaL_loadfile( L, FileName.c_str() );
if ( ReturnCode )
{
std::cerr << "Error loading file: " << FileName << "\n";
return ReturnCode;
}
int ParmCount = 0;
if ( Parm1 != "\xFF" )
{
lua_pushstring( L, Parm1.c_str() );
++ParmCount;
}
if ( Parm2 != "\xFF" )
{
lua_pushstring( L, Parm2.c_str() );
++ParmCount;
}
if ( Parm3 != "\xFF" )
{
lua_pushstring( L, Parm3.c_str() );
++ParmCount;
}
if ( Parm4 != "\xFF" )
{
lua_pushstring( L, Parm4.c_str() );
++ParmCount;
}
if ( Parm5 != "\xFF" )
{
lua_pushstring( L, Parm5.c_str() );
++ParmCount;
}
ReturnCode = lua_pcall(L, ParmCount, LUA_MULTRET, 0);
if ( ReturnCode )
std::cerr << "Error executing file: " << FileName << "\n";
return ReturnCode;
}
If in the future I get an error because I'm trying to use 6
parameters, I'll just modify the code to add Parm6
I'm not happy with this, but it works.
If you're willing to do that:
int
executeFile( std::string const& filename,
std::vector< std::string > const& parameters ) ;
int
executeFile( std::string const& filename,
std::string const& parameter1 )
{
std::vector< Parameter > args ;
args.push_back( parameter1 ) ;
return executeFile( filename, args ) ;
}
int
executeFile( std::string const& filename,
std::string const& parameter1
std::string const& parameter2 )
{
std::vector< Parameter > args ;
args.push_back( parameter1 ) ;
args.push_back( parameter2 ) ;
return executeFile( filename, args ) ;
}
and so on. (It wouldn't surprise me if some clever person
figured out some sort of TMP trick to generate this
automatically. Otherwise, it would be pretty trivial to write
an AWK script to generate all of the functions but the first;
which would probably be easier for any maintenance programmer to
understand. Whatever: you certainly don't want to write it out
by hand.)
Just one thing that I'm wondering about, however: where are the
arguments coming from. If it's from user input, it might be
just as easy to get them as an std::vector< std::string > to
begin with.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34