Is there a better way to do this?

Discussion in 'C++' started by Jim Langston, Jun 19, 2008.

  1. Jim Langston

    Jim Langston Guest

    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");
    }
     
    Jim Langston, Jun 19, 2008
    #1
    1. Advertising

  2. Jim Langston

    joseph cook Guest

    On Jun 19, 6:41 am, "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:



    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
     
    joseph cook, Jun 19, 2008
    #2
    1. Advertising

  3. Jim Langston

    Fei Liu Guest

    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
     
    Fei Liu, Jun 19, 2008
    #3
  4. Jim Langston

    Jim Langston Guest

    "Fei Liu" <> wrote in message
    news:...
    > 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.
     
    Jim Langston, Jun 20, 2008
    #4
  5. Jim Langston

    ManicQin Guest

    On Jun 19, 3:41 am, "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.


    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"

    ;)
     
    ManicQin, Jun 20, 2008
    #5
  6. Jim Langston

    Road.Tang Guest

    On Jun 20, 10:51 am, ManicQin <> wrote:
    > On Jun 19, 3:41 am, "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.

    >
    > 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
     
    Road.Tang, Jun 20, 2008
    #6
  7. Jim Langston

    Jim Langston Guest

    "Jim Langston" <> wrote in message
    news:vJq6k.3$...
    >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.
     
    Jim Langston, Jun 20, 2008
    #7
  8. Jim Langston

    James Kanze Guest

    On Jun 20, 4:55 pm, "Jim Langston" <> wrote:
    > "Jim Langston" <> wrote in message


    > news:vJq6k.3$...


    > >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:
    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
     
    James Kanze, Jun 20, 2008
    #8
  9. Jim Langston

    Jim Langston Guest

    "James Kanze" <> wrote in message
    news:...
    On Jun 20, 4:55 pm, "Jim Langston" <> wrote:
    > "Jim Langston" <> wrote in message


    > news:vJq6k.3$...


    > >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.
     
    Jim Langston, Jun 21, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dwa

    There MUST be a better way!

    dwa, Jan 28, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    445
    Marauderz
    Jan 29, 2004
  2. Guadala Harry

    Nested DIVs - is there a better way?

    Guadala Harry, Oct 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    502
    Guadala Harry
    Oct 26, 2004
  3. Peter Bencsik
    Replies:
    2
    Views:
    878
  4. Paul Rubin
    Replies:
    5
    Views:
    440
    Hendrik van Rooyen
    Aug 6, 2009
  5. Replies:
    2
    Views:
    68
    Mark H Harris
    May 13, 2014
Loading...

Share This Page