lua-users home
lua-l archive

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


Documentation: http://wwhiz.com/LuaPlus/LuaPlusCallDispatcher.html
Download: http://wwhiz.com/LuaPlus/LuaPlusCallDispatcher100.zip

LuaPlus contains many useful Lua enhancements.  In the past, the advanced
callback dispatching to C++ functions provided by LuaPlus was only available
as part of the LuaPlus distribution.  This release marks the first version
of the LuaPlus Callback Dispatcher that works directly with the original Lua
5 code base.

Callback Dispatching

Typical C-style Lua callbacks come in the form:

int Callback(lua_State* state)

Callbacks must be static or global.  They can not be C++ class member
functions.  For C++ users, this approach is limiting as the programmer has
to write the non-member callback and then dispatch through to the C++ member
function that actually does the work:

int TheGlobalCallback(lua_State* L)
{
    // Retrieve the class's 'this' pointer, usually through an upvalue, but
possibly as the calling userdata or table.
    MyClass* myClass = // The retrieved pointer
    return myClass->TheMemberCallback(L);
}

int MyClass::TheMemberCallback(lua_State* L)
{
    const char* str = lua_tostring(L, 1);
    printf("%s\n", str);
    return 0;
}

The LuaPlus Callback Dispatcher provides a means whereby C++ member
functions, either virtual or non-virtual, may be called directly.  These
"functor" objects are even capable of calling global and static functions,
just as before.  In the example above, TheMemberCallback() can be registered
and called directly, without the need for TheGlobalCallback to exist.
Additionally, the LuaPlus Callback Dispatcher allows direct calling of C or
C++ functions, without knowing anything about a lua_State.
TheMemberCallback() function above could be represented more naturally:

void MyClass::TheDirectMemberCallback(const char* str)
{
    printf("%s\n", str);
}

In an ideal environment, the callback shim would automatically be generated
for us.  With some C++ template metaprogramming, we are able to accomplish
this with ease.  Best of all, the performance of the call to the either
style of callback function is as high or higher than if C-style
methodologies had been employed to accomplish the same feat.

Finally, the LuaPlus Callback Dispatcher can call the same member functions
with differing 'this' pointers.

Note: This direct registration of C++ functions is based around techniques
presented in LuaBind (http://luabind.sourceforge.net/).  LuaBind provides a
much more powerful function dispatching mechanism, including inheritance for
classes.  It also inherits the baggage of both STL and Boost and runs slower
than the LuaPlus Callback Dispatcher due to its ability to handle class
hierarchies and function overloading.  Depending on your needs, you may find
LuaBind a much more suitable library to use.

Please let me know if there are any issues.

Josh

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.559 / Virus Database: 351 - Release Date: 1/7/2004