lua-users home
lua-l archive

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


Hi

I need some advice on how to make typecasts in Lua.

I'm using tolua++ together with tolua_base.h and virtual_method_hooks.lua as shown in
"Implementing Virtual Methods With Toluapp "

Below is description of what I have done and what my problem exactly is.

C++
---------------
class IEvent
{
protected:
 IEvent() {}
} ;

class KeyEvent : public IEvent
{
public:
 KeyEvent( int someKey ) ;
 int GetKey() const ;
}  ;

class IEventLister
{
public:
 virtual void OnEvent( const IEvent& ev ) const ;
} ;

PKG
---------------
$include "tolua_base.h"
class IEvent
{
protected:
 IEvent() ;
} ;

class KeyEvent : public IEvent
{
public:
 KeyEvent( int someKey ) ;
 int GetKey() const ;
} :

class IEventLister
{
public:
 virtual void OnEvent( const IEvent& ev ) const ;
} ;

LUA
---------------------------
KeyListener = {}
KeyListener.__index = KeyListener
function KeyListener:OnEvent(ev)
--- Coming here ev is a IEvent
--- wish to do something like  KeyEvent k = (KeyEvent)ev ;
end

function KeyListener:new()
local t = {}
setmetatable(t,KeyListener)
local w = Lua__IEventListener:new()
tolua.setpeer(w,t)
w:tolua__set_instance(w)
return w
end
....
k = KeyListener:new()
-- after this i register the keylisterer to
-- a class calling OnEvent with KeyEvent's
-- when keys are pressed
...

------------------------------------------------------------------
When running the code I come tho the k:OnEvent method
when keys are pressed, so that part is ok.

My problem is that the event object in the OnEvent method
is a IEvent object and needs to be typecasted to a KeyEvent
in order to access the GetKey funtion.

I have tried with tolua.cast and some other things with no success.
Is it possible to cast the 'ev' to a KeyEvent in Lua?

Sorry for the long mail
and thanks for you time

~
Roland Strålberg
www.rstralberg.com
rstralberg@msn.com

Arbete/Work
Microbit 2.0 AB
www.microbit.se
roland.stralberg@microbit.se