Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is there a better way to do this?

Reply
Thread Tools

Is there a better way to do this?

 
 
Jim Langston
Guest
Posts: n/a
 
      06-19-2008
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");
}


 
Reply With Quote
 
 
 
 
joseph cook
Guest
Posts: n/a
 
      06-19-2008
On Jun 19, 6:41*am, "Jim Langston" <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
 
 
 
Fei Liu
Guest
Posts: n/a
 
      06-19-2008
Jim Langston wrote:
> 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
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      06-20-2008

"Fei Liu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Jim Langston wrote:
>> 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.


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


 
Reply With Quote
 
ManicQin
Guest
Posts: n/a
 
      06-20-2008
On Jun 19, 3:41*am, "Jim Langston" <(E-Mail Removed)> wrote:
> 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"


 
Reply With Quote
 
Road.Tang
Guest
Posts: n/a
 
      06-20-2008
On Jun 20, 10:51 am, ManicQin <(E-Mail Removed)> wrote:
> On Jun 19, 3:41 am, "Jim Langston" <(E-Mail Removed)> wrote:
>
> > 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"
>
>


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
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      06-20-2008
"Jim Langston" <(E-Mail Removed)> wrote in message
news:vJq6k.3$(E-Mail Removed)...
>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.


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-20-2008
On Jun 20, 4:55 pm, "Jim Langston" <(E-Mail Removed)> wrote:
> "Jim Langston" <(E-Mail Removed)> wrote in message


> news:vJq6k.3$(E-Mail Removed)...


> >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& 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.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      06-21-2008
"James Kanze" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On Jun 20, 4:55 pm, "Jim Langston" <(E-Mail Removed)> wrote:
> "Jim Langston" <(E-Mail Removed)> wrote in message


> news:vJq6k.3$(E-Mail Removed)...


> >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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Parsing Binary Structures; Is there a better way / What is your way? Paul Rubin Python 5 08-06-2009 08:06 AM
Are there better ADSL modem routers out there? Bypass UK VOIP 28 01-12-2007 12:41 AM
Is splint really better than lint? Is there a better tool than splint? Peter Bencsik C Programming 2 09-21-2006 10:02 PM
Build a Better Blair (like Build a Better Bush, only better) Kenny Computer Support 0 05-06-2005 04:50 AM
There MUST be a better way! dwa ASP .Net 5 01-29-2004 02:07 AM



Advertisments