summaryrefslogtreecommitdiff
path: root/libs/lua
diff options
context:
space:
mode:
authorRobin Gareus <robin@gareus.org>2016-06-01 13:59:31 +0200
committerRobin Gareus <robin@gareus.org>2016-06-01 13:59:31 +0200
commit67083d65e4f3f3792cb29540222fc226edb4b44c (patch)
tree6405cc915dec3d4328480357dbd244be4e06b0b6 /libs/lua
parent79245a296b73c4ad49bc9b5a204cdda120e1ab90 (diff)
add lua/C++ dynamic_cast<>
Diffstat (limited to 'libs/lua')
-rw-r--r--libs/lua/LuaBridge/detail/CFunctions.h23
-rw-r--r--libs/lua/LuaBridge/detail/Namespace.h16
2 files changed, 39 insertions, 0 deletions
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 <class T, class R>
+ struct CastClass
+ {
+ static int f (lua_State* L)
+ {
+ T * const t = Userdata::get <T> (L, 1, false );
+ Stack <R*>::push (L, dynamic_cast<R*>(t));
+ return 1;
+ }
+ };
+
+ template <class T, class R>
+ struct CastConstClass
+ {
+ static int f (lua_State* L)
+ {
+ T const* const t = Userdata::get <T> (L, 1, true);
+ Stack <R const*>::push (L, dynamic_cast<R const*>(t));
+ return 1;
+ }
+ };
+
+
template <class T>
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 U>
+ Class <T>& 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 <T, U>::f, 0);
+ rawsetfield (L, -3, name); // class table
+
+ lua_pushcclosure (L, &CFunc::CastConstClass <T, U>::f, 0);
+ rawsetfield (L, -4, name); // const table
+ return *this;
+ }
+
};
/** C Array to/from table */