lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi All,

   We always like reinventing the wheel... I did it once, and still
working on it since then, my solution was SLB (Simple Lua binder) ,
now it's hosted at code.google.com, I've been using it for a long time
but I never find time to document it all (its main drawback) but you
can have a loot at the tests to get an idea. For example:

                SLB::Class<Animal, SLB::Instance::NoCopy >("Unit_004::Animal")
                        .set("makeSound", &Animal::makeSound)
                ;

                SLB::Class<Duck>("Unit_004::Duck")
                        .constructor<bool>()
                        .set("canFly", &Duck::canFly)
                        .inherits<Animal>()
                ;

                SLB::Class<Dog>("Unit_004::Dog")
                        .constructor<bool>()
                        .set("bites", &Dog::bites)
                        .inherits<Animal>()
                ;

The project is here:
http://code.google.com/p/slb/

and some tests:
http://code.google.com/p/slb/source/browse/#hg/tests/src

Int addition to be able of using C++ code, you can use hybrid classes
(C++ classes with some methods implemented in Lua):

     class HClass : public SLB::Hybrid<HClass>
        {
        public:
                HClass();

                /* int get() { return LCall<int>("get"); } */
                HYBRID_method_0(get,int);

                /* void calc(int a, int b) { return LCall<void, int,
int>("calc"); } */
                HYBRID_method_2(calc,void,int,int);

                HYBRID_const_method_0(update,void);

....
 in the script:

function HClass:calc(a,b)
        result = a+b
end

-- create one instance
v = HClass()

-- another function:
function HClass:get()
        return result
end

v:calc(6,7)

(result variable is stored in a private table per instance, so you can
extend classes attributes)


Cheers,
   JL

On Wed, Dec 9, 2009 at 1:05 PM, liam mail <liam.list@googlemail.com> wrote:
>
>
> 2009/12/9 Carsten Fuchs <CarstenFuchs@t-online.de>
>>
>> Hi Martin,
>>
>> Martin C. Martin schrieb:
>>>
>>> What libraries would someone recommend for binding Lua to parts of a
>>> large, existing C++ code base?  It needs to work in both Windows and Linux.
>>>  I'm looking at LuaPlus (I know, it's a lot more than just a C++ binding),
>>> but perhaps that's not the place to start?
>>
>> When I started binding Lua to my C++ code in the Cafu game engine
>> <http://www.cafu.de>, my goals were to keep everything as straightforward
>>  and with as few additional layers of code as possible, and thus, although I
>> looked into them, I never used any of the available C++-binding libraries,
>> but just the techniques explained in the PiL2 book.
>>
>> While this route certainly takes longer in order to see the first results
>> and in fact may seem cumbersome in a sense (yes, you start thinking about
>> putting the resulting code into a library of your own... which in a sense
>> means re-inventing the wheel), I still found the results worthwhile: My code
>> is very short, clean, and I fully understand all the details both on the Lua
>> and on the C++ side.  :-)  (And I thoroughly learned Lua and its C API this
>> way.)
>>
>> So my recommendation is to start with the PiL2 book, and only turn to the
>> libraries if you cannot or don't want to take the "hand-made" approach.
>>
>> Best regards,
>> Carsten
>>
>>
>>
>> --
>>     Cafu - The Game and Graphics Engine for
>> multiplayer, cross-platform, real-time 3D Action
>>            Learn more at www.cafu.de
>
> It depends on what you want from the library which one you should try. For
> example as you want it to be cross platform that rules out a couple of them.
> Do you want it to use vanilla Lua or are you OK with it making adjustment to
> the Lua core, remembering that many addons and debuggers for Lua with a
> changed core may not work. Then there seems to be some with are no longer in
> development and also do you want to preform the binding yourself or do you
> want it to be automatic and the library parses your input files?
>
> If you are thinking of going down the reinvent the wheel route the there is
> a comparision of the different methods in IIRC Game Programming Gems 6, also
> some details to consider in an article in Lua Programming Gems. Personally I
> would say do not invest the time in reinventing the wheel and instead use a
> try and tested library, yet if everyone did this there would not be so many
> options (I hold my hand up :) ).
>
> Sorry but if I were to recommend a library it would be a biased answer so I
> will leave it at that.
> Liam
>



-- 
  Jose L. Hidalgo Valiño (PpluX)
  ---- http://www.pplux.com ----