From 67083d65e4f3f3792cb29540222fc226edb4b44c Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Wed, 1 Jun 2016 13:59:31 +0200 Subject: add lua/C++ dynamic_cast<> --- libs/lua/LuaBridge/detail/CFunctions.h | 23 +++++++++++++++++++++++ libs/lua/LuaBridge/detail/Namespace.h | 16 ++++++++++++++++ 2 files changed, 39 insertions(+) (limited to 'libs/lua') diff --git a/libs/lua/LuaBridge/detail/CFunctions.h b/libs/lua/LuaBridge/detail/CFunctions.h index 15bc7022b3..9f1712995a 100644 --- a/libs/lua/LuaBridge/detail/CFunctions.h +++ b/libs/lua/LuaBridge/detail/CFunctions.h @@ -379,6 +379,29 @@ struct CFunc }; + template + struct CastClass + { + static int f (lua_State* L) + { + T * const t = Userdata::get (L, 1, false ); + Stack ::push (L, dynamic_cast(t)); + return 1; + } + }; + + template + struct CastConstClass + { + static int f (lua_State* L) + { + T const* const t = Userdata::get (L, 1, true); + Stack ::push (L, dynamic_cast(t)); + return 1; + } + }; + + template struct PtrNullCheck { diff --git a/libs/lua/LuaBridge/detail/Namespace.h b/libs/lua/LuaBridge/detail/Namespace.h index 590f25be4a..79a0786d40 100644 --- a/libs/lua/LuaBridge/detail/Namespace.h +++ b/libs/lua/LuaBridge/detail/Namespace.h @@ -1048,6 +1048,22 @@ private: return *this; } + template + Class & addCast (char const* name) + { + PRINTDOC("Cast", _name << name, + type_name< U >(), + type_name< U >() << " (" << type_name< T >() << "::*)()") + + assert (lua_istable (L, -1)); + lua_pushcclosure (L, &CFunc::CastClass ::f, 0); + rawsetfield (L, -3, name); // class table + + lua_pushcclosure (L, &CFunc::CastConstClass ::f, 0); + rawsetfield (L, -4, name); // const table + return *this; + } + }; /** C Array to/from table */ -- cgit v1.2.3