lua-users home
lua-l archive

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


Hi !
  I'm about to release "yet another library" to do simple bindings,
some like lunar, some others like luabind, similar to boost-python,
and so on. I know there are lots of libraries that target the same
problem, and probably, in similar ways... but If been working on this
for long time, I use it a lot, and maybe it could be useful for other
people. It is called "SLB" ( Simple Lua Binder ) and looks like this:

   SLB::Class<osg::Vec3>("osg::Vec3")
       .constructor<float,float,float>()
       .set("length", &osg::Vec3::length)
       .set("normalize", &osg::Vec3::normalize)
   ;
   SLB::Class<osg::Group, SLB::Instance::SmartPtr<osg::ref_ptr> >("osg::Group")
       .constructor()
       .inherits<osg::Node>()
       .set("addChild", &osg::Group::addChild)
   ;

Until here, it is another library, but I've found quite interesting
your proposal, and seems plausible. What I would try to do will be
something like this:

class MyPureCppClass : public SLB::HibridClass
{
public:
   // normal C++ methods...
   int method1();
   float method2(float);
   inline float method3(float a1, float a2, int a3)
          { return SLB::deferred_call<int>("method3", this, a1,a2,a3); }
};

This class should inherit from a known class that could host a lua
state to execute the lua code, also this class should have a method to
load the lua code for "method3".  If the class is also wrapped using
SLB, the class  could be accessible inside lua (of course) so it will
need also:

   SLB::Class<MyPureCppClass>("MyPureCppClass")
       .constructor()
       .set("method1", &MyPureCppClass::method1)
       .set("method2", &MyPureCppClass::method2)
       .set("method3", &MyPureCppClass::method3)
   ;

The only thing left will be internal attributes, you could write
methods to access them... but they need to be "public" because
bindings are only capable of access public methods.

The main issue is to create a lua_State for each instance to protect
possible simultaneous access from several threads, if this is not a
problem it is safe to use only one lua_state for all instances.

Anyway, I will probably need something like this to make some
"callbacks" in my code... so  it will be implemented somehow in SLB in
the very near future.

Cheers,
   Jose L.


2007/5/16, E. Wing <ewmailing@gmail.com>:
I'm looking at the Simple(r)Cpp Bindings on the Wiki, particularly the
Lunar bindings ("Cpp Binding With Lunar" and "Calling Lua From Cpp").
Does somebody have an example that turns these examples inside-out?
The examples mostly show how to take a C++ class (implemented in C++)
and how to use it in Lua. I'm curious about how to have that same C++
public interface, but implement some of the underlying details in Lua.

So for example, instead of having the C++ methods deposit(double),
withdraw(double), and balance() which use a C++ member variable called
m_balance, what if I wanted to have those C++ methods call Lua
functions and use Lua variables for storage? I'm also curious about
the additional case where a class method is defined in pure C++ and
those Lua functions need to call it (which makes this more
complicated).

Thank you



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