lua-users home
lua-l archive

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


Hi,

Is it possible to have multiple inehritance with tolua++ w/o using
syntax like:
myobject.__parent__:mymethod()
?

I made two functions for tolua but haven't tested yet (dont know it it
compiles well).
The first is a function to be stored in object __index metafield
The other is to register this function and register a parent class in
the matatable.

Is that a good solution ?
Is anyone interested ?
Is there others solutions than tolua++ to create binding (I want to talk
about the tolua++ library, not the tolua++ programm which create a c++
file using the library) that handles well inehritance ?

Mildred

-- 
<http://louve.dyndns.org> ou <http://mildred632.free.fr>
Le courrier E-mail est une correspondance privée. Merci de vous y conformer

static int matatable__index(lua_State *L) {
					// STACK: table, key
	// called when failed to get the index
	if(!luaL_getmetafield(L, 1, "tolua2_inehrit_from")){
		// no metatable or no index "tolua2_inehrit_from"
		lua_pushnil(L);		// STACK: table, key, nil
		return 1; // nil
	}
					// STACK: table, key, metafield
	if(!lua_istable(L, -1)){
		// not a table ... error
		lua_pushnil(L);		// STACK: table, key, metafield, nil
		return 1; // nil
	}
	int max = lua_objlen(L, -1);
	int i = 1;
	for(i=1; i<=max; i++){
					// STACK: table, key, metafield
		lua_pushnumber(L, i);	// STACK: table, key, metafield, i
		lua_gettable(L, 1);	// STACK: table, key, metafield, parent
		if(lua_isfunction(L, -1)){
					// STACK: table, key, metafield, parent
					// STACK: table, key, metafield, parentFunc
			lua_pushvalue(L, 1);
					// STACK: table, key, metafield, parentFunc, child
			lua_call(L, 1, 1);
					// STACK: table, key, metafield, parentFunc, parent
			lua_remove(L, -2);
					// STACK: table, key, metafield, parent
		}
		lua_pushvalue(L, 2);	// STACK: table, key, metafield, parent, key
		lua_gettable(L, -2);	// STACK: table, key, metafield, parent, parent_val
		if(!lua_isnil()){
			// the parent_val exists
			return 1; // parent_val
		}
		// the value does not exists
		// clear stack
		lua_pop(L, 2);		// STACK: table, key, metafield
	}
	// no value in parents
	lua_pushnil(L);			// STACK: table, key, metafield, nil
	return 1; // nil
}

TOLUA_API void tolua_inehrit (lua_State *L, lua_CFunction base) {
	// after a tolua_beginmodule
						// STACK: module
	if(!lua_getmetatable(L, -1)){
		// no metatable - create one
		lua_newtable(L)			// STACK: module, table
		lua_setmetatable (L, -2);	// STACK: module
		lua_getmetatable(L, -1)		// STACK: module, mt
	}
						// STACK: module, mt
	if(!luaL_getmetafield(L, -1, "tolua2_inehrit_from")){
		// no index "tolua2_inehrit_from"
		lua_pushstring(L, "tolua2_inehrit_from");
						// STACK: module, mt, key
		lua_newtable(L)			// STACK: module, mt, key, table
		lua_settable(L, -3)		// STACK: module, mt
		luaL_getmetafield(L, -1, "tolua2_inehrit_from")
						// STACK: module, mt, field
	}
						// STACK: module, mt, field
	lua_pushnumber(L, lua_objlen(L, -1)+1);	// STACK: module, mt, field, max
	lua_pushcfunction(L, base);		// STACK: module, mt, field, max, base
	lua_settable(L, -3);			// STACK: module, mt, field
	lua_pop(L, 2);				// STACK: module
}

Attachment: signature.asc
Description: OpenPGP digital signature