On Sat, Sep 26, 2009 at 9:00 AM, Andre Arpin <ar...@kingston.net> wrote: > John Labenski <jlaben...@...> writes: > > Maybe I do not understand the problem but here is my suggestion. > Why not use dynamic cast since you seem to know which class. > C casting is a brute force method.
We only know the class type at runtime, but dynamic_cast<> casts at compile time. > v-table are sometime incompatible from compiler to compiler and even across > compiler versions but using C-casting is not likely to improve portability. I know, but the only way I can think of properly casting the void* pointer from Lua is to add duplicate binding functions that do the casting at compile time. The way I see it is; if it breaks in the future or doesn't work with the Intel compiler, back to the drawing board. > > // TestCasting->cpp > > #include "stdafx.h" > class A { public: int x; }; > class B { public: int y; }; > class AB : public A, public B { public: int z; }; > > void f(void *p) > { > AB* ab = (AB *)p; > A* a = dynamic_cast<A*>(ab); > B* b = dynamic_cast<B*>(ab); This is what we can't do. For example, wxLua_wxItemContainerImmutable_FindString(), line 981 in http://wxlua.cvs.sourceforge.net/viewvc/wxlua/wxLua/modules/wxbind/src/wxcore_controls.cpp?revision=1.20&view=markup 981 static int LUACALL wxLua_wxItemContainerImmutable_FindString(lua_State *L) 982 { 983 // get number of arguments 984 int argCount = lua_gettop(L); 985 // bool bCase = false 986 bool bCase = (argCount >= 3 ? wxlua_getbooleantype(L, 3) : false); 987 // const wxString s 988 const wxString s = wxlua_getwxStringtype(L, 2); 989 // get this 990 wxItemContainerImmutable * self = (wxItemContainerImmutable *)wxluaT_getuserdatatype(L, 1, wxluatype_wxItemContainerImmutable); 991 // call FindString 992 int returns = (self->FindString(s, bCase)); 993 // push the result number 994 lua_pushnumber(L, returns); 995 996 return 1; 997 } The "self" can be a wxComboBox, wxChoice, wxListBox... and the wxItemContainerImmutable class is along the chain of second base classes for all of them. All wxLua userdata objects that wrap C++ objects have a metatable that stores the classes' wxLuaBindClass struct (wxlbind.h) which can contain whatever you want, but remember that this struct is also stored in Lua as a void* pointer. Also, you can't overload C++ functions by return value. The problem is how to replace the line getting the "self" with something that first casts the object on the stack to its original class type (wxComboBox, wxChoice, whatever it may be) before casting it to a wxItemContainerImmutable. Finally, this function/baseclass may be in one library, but a different binding (wxcore and wxadv for example) may have a class with it as it's base class. We don't even know at compile time who uses this as a base class so we can't add a case/if/else statement to properly cast it. In any case, I don't mean to discourage you, but *I* can't figure out another way. The way it's done now has minimal overhead, two extra pointers per class which are NULL unless the class has two or more base classes. There is only a performance penalty when actually calling a function of a second or higher class, but again this trivial as we only have to look through an array with a length equal to the number of classes along the second or higher base class chain, which is only a few items. Regards, John ------------------------------------------------------------------------------ Come build with us! The BlackBerry® Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9-12, 2009. Register now! http://p.sf.net/sfu/devconf _______________________________________________ wxlua-users mailing list wxlua-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/wxlua-users