Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Is there a better way to do this? (http://www.velocityreviews.com/forums/t621133-is-there-a-better-way-to-do-this.html)

Jim Langston 06-19-2008 10:41 AM

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



joseph cook 06-19-2008 10:46 AM

Re: Is there a better way to do this?
 
On Jun 19, 6:41*am, "Jim Langston" <tazmas...@rocketmail.com> 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

Fei Liu 06-19-2008 01:28 PM

Re: Is there a better way to do this?
 
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

Jim Langston 06-20-2008 12:04 AM

Re: Is there a better way to do this?
 

"Fei Liu" <fei.liu@gmail.com> wrote in message
news:485A5EE6.4090407@gmail.com...
> 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.



ManicQin 06-20-2008 02:51 AM

Re: Is there a better way to do this?
 
On Jun 19, 3:41*am, "Jim Langston" <tazmas...@rocketmail.com> 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"

;)

Road.Tang 06-20-2008 08:44 AM

Re: Is there a better way to do this?
 
On Jun 20, 10:51 am, ManicQin <Manic...@gmail.com> wrote:
> On Jun 19, 3:41 am, "Jim Langston" <tazmas...@rocketmail.com> 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

Jim Langston 06-20-2008 02:55 PM

Re: Is there a better way to do this?
 
"Jim Langston" <tazmaster@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?

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



James Kanze 06-20-2008 06:51 PM

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& 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:james.kanze@gmail.com
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

Jim Langston 06-21-2008 12:29 PM

Re: Is there a better way to do this?
 
"James Kanze" <james.kanze@gmail.com> wrote in message
news:ebfc1b1f-1ad3-421c-81c8-891d06bb1835@56g2000hsm.googlegroups.com...
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" );


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




All times are GMT. The time now is 02:22 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.