Is there a better way to do this?

J

Jim Langston

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");
}
 
J

joseph cook

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:


va_arg is a "nightmare" but this is a perfect use for it I believe.
You are trying to re-write va_arg essentially, but I would give it a
try anyway. Assuming that all your parameters are POD type.

Joe Cook
 
F

Fei Liu

Jim said:
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");
}
are you trying to write a simple interpreter? check out boost::regex
that allows you to tokenize string input and facilitate token
interpretation.

F
 
J

Jim Langston

Fei Liu said:
are you trying to write a simple interpreter? check out boost::regex that
allows you to tokenize string input and facilitate token interpretation.

No. LUA is an interpreter. I just want to pass a variable number of
parameters. For my case string will work.
 
M

ManicQin

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.

Sorry for being totaly off topic here but i qoute the introduction of
Lua programing languge

"Like most names, it should be written in lower case with an initial
capital, that is, "Lua". Please do not write it as "LUA", which is
both ugly and confusing"

;)
 
R

Road.Tang

Sorry for being totaly off topic here but i qoute the introduction of
Lua programing languge

"Like most names, it should be written in lower case with an initial
capital, that is, "Lua". Please do not write it as "LUA", which is
both ugly and confusing"

;)

IMO, va_args is not bad for you problem.

anyway, if you doesn't like that, since it would be not safe.
how about to write a Args class

L.Execute( "MyFile.lua", Args("parm1 parm2 parm3" ));
or
L.Execute(Args("MyFile.lua parm1 parm2 parm3" ));

-roadt
 
J

Jim Langston

Jim Langston said:
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?
[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& Parm1 =
"\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.
 
J

James Kanze

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.
For now I've decided to use this ugly to code, easy to use kludge:

int ExecuteFile( const std::string& FileName, const std::string& Parm1 =
"\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.
 
J

Jim Langston

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" );
[SNIP]

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.

Well, the parameters will be coming from code itself, interfacing program
objects with Lua code. So there will be code such as:
L.ExecuteFile( MeetFile, Object1, Object2 );
L.ExecuteFile( InitializeFile, Object );
etc..

I'm building the framework right now to be used however it winds up being
used basically. Just trying to get the interface easy to use so using it
won't be a pita.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top