lua-users home
lua-l archive

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


Hi Anders,

Let's see some examples... This is how the wrappers looks like:

       SLB::Class< Test >("Unit_006::Test")
            .constructor()
            .set("set", &Test::set)
            .set("get", &Test::get)
            .set("equal", &Test::equal)
            .set("isA", &Test::isA)
            .set("isB", &Test::isB)
            .set("isC", &Test::isC)
            .enumValue("Enum::A", Test::A)
            .enumValue("Enum::B", Test::B)
            .enumValue("Enum::C", Test::C)
        ;

Of course, this is just the simples way to use it. This are more real
examples of use (real code I'm working on):

   SLB::Class<osg::Group, SLB::Instance::SmartPtr<osg::ref_ptr> >("osg::Group")
        .constructor()
        .inherits<osg::Node>()
        .set("addChild", &osg::Group::addChild)
        .set("setChild", &osg::Group::setChild)
        .nonconst_set("getChild", &osg::Group::getChild)
    ;

Here for example we are using a much more complex object, that can not
be copied and uses a smart pointer pattern, as in C++ there are
infinite ways of handling objects I added Policies to the SLB class
definition, so you can choose one of the existing, or create new ones
(existing are: NoCopy, NoCopyNoDestroy, SmartPtr, SmartPtrNoCopy,
SmartPtrSharedCopy....).

It is fundamental to take care of this, as objects will go in and out
from Scripts, we should precisely define how objects are shared, or
not, if they should be copied, or referenced, etc..

I use SLB not only for scripting purposes, also as a effective code
reflection, you can easily use it to construct objects (factories),
send messages to objects, inspect methods, and so on. To support this
you can also set comments and parameters to any "set"

        .set("addChild", &osg::Group::addChild).comment("addChild
method").param("this is a comment to the 1st parameter")

Hybrid classes are another feature, a much more complicated but quite
useful to declare classes in the Script with inheritance from C++
classes, For example (from test_005)

// class definition
class SFH : public SLB::StatefulHybrid<SFH> // means one complete lua
state per instance
{
public:
    SFH();
    ~SFH();
    HYBRID_method_1(calc,int,int); // signature virtual int calc(int p);
protected:
};

// this is the wrapper code

        SLB::Class< SFH, SLB::Instance::NoCopy >("Unit_005::SFH")
            .inherits<SLB::Script>()
            .constructor()
            .set("doString", &SFH::doString)
            .set("calc", &SFH::calc) // virtual function see .hpp
            .hybrid()
        ;

// and now on the Script we can do

function SFH:calc(a)
   return a*2
end

this will declare the method "calc" for any SFH class... but you can
even create "new classes"

function SFH.Type1:calc(b)
   return b*2
end

Here Type1 is like a new type which inherits from SFH...

About using cpp2xml, or doxygen generator, or something... maybe it's
possible, I like doing wrappers by hand they only depend on the
headers (which doesn't change that much...) and you can always tweak
the objects to perform better in the Scripts. This is how scripts
looks like (real example):

SLB.using(SLB.gpuFX) -- "using" namespace as C++

c = Camera()
c:setClearColorBuffer(true)
c:setClearDepthBuffer(true)
c:setLook( Vec3f(0,0,0) )
cs = PerspectiveCamera()
cs:setAspect(8/6)
c:setSettings(cs)

m = Material()
c:addChild(m)
s = Shader.Vertex()
s:setSource [[
attribute vec3 vertex;
attribute vec3 normal;
void main(void)
{
    vec4 p = vec4(vertex,1.0);
    gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix *p;
}
]]
if not m:addShader(s) then error("Vertex Shader") end
...

-- this is an hybrid class
function ScriptedDemo:frame(time)
    c:setPosition( Vec3f(10*math.cos(time/1000)+5,10,10) )
    c:fullRender()
    m.color = Vec4f(math.sin(time/100)*0.5+1,math.sin(time/50)*0.5+1,0,1)
    m.max = math.sin(time/1000)*20+24
end


Sorry for the long, very long mail :)

Cheers,
   Jose-Luis H.



On Tue, Jan 20, 2009 at 7:57 PM, Anders Backman <andersb@cs.umu.se> wrote:
> Ha, there we lured another one out ;-)
> I like what I see, especially when you dont need boost...
> How hard would it be to use cpp2xml to generate this wrapper code?
> Feels like it would be feasible...?
> As far as I know the xml generated was in lqt parsed using lua...
> But it was quite a large number of lines of code...
> This certainly looks like it could work.
> I dont quite understand from this quick look where and how the wrapping
> works.
> In the test directory there are a number of header files, these are parsed
> somehow? Or is there some code Im missing? Like the templetized integration
> of the c++ classes...
> Could you briefly explain how it is all put together?
>
> On Tue, Jan 20, 2009 at 7:45 PM, Jose Luis Hidalgo
> <joseluis.hidalgo@gmail.com> wrote:
>>
>> Hi All,
>>
>>  I just want to add yet another one:
>>
>> SLB: Simple Lua Binder (http://slb.pplux.com)  http://svn.pplux.com/SLB
>>   * templatized, similar to LuaBind but without Boost ( it is just
>> standard C++ )
>>   * supports virtual functions
>>   * supports namespaces
>>   * hybrid classes -> partially implemented in lua
>>   * can do inheritance from script -> using hybrid classes
>>   * can add methods implemented using lua api to extend functionality
>> on the script side.
>>   * very undocumented, ... but I can do better XD
>>   * the author is quite near and friendly, well it's me actually XD
>>
>> I would like to use something like cpp2xml to generate the
>> wrapper-templatized code, but never got to that point, actually I like
>> manually wrapping even some methods I turn to lua api to improve its
>> use, a 1:1 lua version from C++ is not always the best solution.
>>
>> If you want to give it a try, just tell me.
>>
>> Cheers,
>>   Jose-Luis H.
>>
>> On Tue, Jan 20, 2009 at 2:13 PM, Anders Backman <andersb@cs.umu.se> wrote:
>> >
>> > Hi all.
>> > This is probably a wildly discussed topic, but I haven't found a good
>> > compilation of the resources as new ones pops up all over the place, and
>> > others die..
>> > Problem: Given a C++ API I want to export classes, methods, namespaces,
>> > enums to lua. Being able to implement virtual methods in lua, calling
>> > them
>> > from C++/lua transparently...
>> >
>> > This is a the list I know about, correct me if Im wrong, perhaps someone
>> > can
>> > update a list on the lua site?
>> > - LuaBridge
>> >   * Templetized way of exporting C++ classes
>> >   * Require manual labour of explicitly listing all classes, methods
>> > enums
>> > that should be exported.
>> >   *one way only, exporting C++ to lua (no virtual callbacks to lua?)
>> >   * Handles namespaces?
>> > - LuaBind
>> >   * Templetized way of exporting C++ classes
>> >   * Require manual labour of explicitly listing all classes, methods
>> > enums
>> > that should be exported.
>> >   * Full support of virtual methods
>> >   * Seems to handle namespaces?
>> > - tolua
>> >   * dead
>> > - tolua++
>> >   * Parse either modified header files OR cleaned .h (.tolua/.pkg) files
>> >   * Require manual creation of suitable .pkg files
>> >   * Do not support virtual methods in lua
>> >   * Handle namespaces
>> >
>> > - luaQT (right now specialized for QT, but...)
>> >   * Parse .h files of the target API
>> >   * Require you to filter out unwanted classes, but after that its
>> > automatic
>> >   * Does not handle namespaces
>> >
>> > More?
>> > What I really like about the luaQT approach is it has a big gain
>> > exporting
>> > numerous API:s where you want to avoid the manual labour of specifying
>> > each
>> > and every class/method, this is a tedious work and has to be redone each
>> > time you update your dependencies, unless you want to wrap everything
>> > into a
>> > scripting layer which you then export...
>> >
>> > Right now yet another way would be to use cpp2xml (like luaQT) and
>> > generate
>> > cleaned up .pkg files and run those through tolua++. However this would
>> > not
>> > give me support for virtual methods, this would be added to tolua++...
>> >
>> > Looking for quite some feedback because I know this is a hot issue!
>> >
>> > Cheers, Anders
>> >
>> >
>>
>>
>>
>> --
>>  Jose L. Hidalgo Valiño (PpluX)
>>  ---- http://www.pplux.com ----
>
>
>
> --
> __________________________________________
> Anders Backman, CTO  Algoryx Simulation AB
> Uminova Science Park, Box 7973,  SE-907 19
> Umeå,  Sweden
> anders@algoryx.se http://www.algoryx.se
> Cell: +46-70-392 64 67
>



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