lua-users home
lua-l archive

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


Hi Ignacio,

thank you for the quick response. I will try the modification, but I have an idea about the solution right now. Thank you again.

Best Regards
Micha

Ignacio Burgueño schrieb:
Michael Bauroth wrote:
Hi,

can eventually someone point me to a short example for how to use __index and __newIndex (eventually in context Lunar) to access C++ methods like properties?


Attached is part of what I use. Take a look at the functions 'thunk_index', 'thunk_newindex' and the place were those are registered.

Basically, you when your table is indexed, you llok for a matching method and if none you look for a matching property. There are some files missing from my example, but you essentially, the code you'd want to rip is here.

Example of use:

class A : public LuaCppBridge::RawObjectWithProperties<A> {
public:
...
[snip]

LCB_ROWP_DECLARE_EXPORTABLE(A);

LCB_DECL_SETGET(value);

}

const LuaCppBridge::RawObjectWithProperties<A>::RegType A::setters[] = {
    LCB_ADD_SET(value),
    {0}
};

const LuaCppBridge::RawObjectWithProperties<A>::RegType A::getters[] = {
    LCB_ADD_GET(value),
    {0}
};


LCB_IMPL_GET(A, value) {
    lua_pushstring(L, "hello world!");
    return 1;
}

LCB_IMPL_SET(A, value) {
    // stack pos #1 is 'self'
    const char* theValue = luaL_checkString(L, 2);
    return 0;
}


Lua code:
 a = A()    -- or A:new()
 print(a.value)
 a.value = "hi!"
 print(a.value)


Hope that helps.

Regards,
Ignacio Burgueño