From 1d7c144967b9cd24f4fd53270c340a8accc61cab Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Fri, 26 Aug 2016 18:25:15 +0200 Subject: [PATCH] add "sameinstance()" lua binding for all shared/weak ptrs --- libs/lua/LuaBridge/detail/CFunctions.h | 33 +++++++++++++++++++++++++- libs/lua/LuaBridge/detail/Namespace.h | 18 +++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/libs/lua/LuaBridge/detail/CFunctions.h b/libs/lua/LuaBridge/detail/CFunctions.h index 9f1712995a..fafad5d0a7 100644 --- a/libs/lua/LuaBridge/detail/CFunctions.h +++ b/libs/lua/LuaBridge/detail/CFunctions.h @@ -401,7 +401,6 @@ struct CFunc } }; - template struct PtrNullCheck { @@ -430,6 +429,38 @@ struct CFunc } }; + template + struct PtrEqualCheck + { + static int f (lua_State* L) + { + boost::shared_ptr t0 = luabridge::Stack >::get (L, 1); + boost::shared_ptr t1 = luabridge::Stack >::get (L, 2); + Stack ::push (L, t0 == t1); + return 1; + } + }; + + template + struct WPtrEqualCheck + { + static int f (lua_State* L) + { + bool rv = false; + boost::weak_ptr tw0 = luabridge::Stack >::get (L, 1); + boost::weak_ptr tw1 = luabridge::Stack >::get (L, 2); + boost::shared_ptr const t0 = tw0.lock(); + boost::shared_ptr const t1 = tw1.lock(); + if (t0 && t1) { + T* const tt0 = t0.get(); + T* const tt1 = t1.get(); + rv = (tt0 == tt1); + } + Stack ::push (L, rv); + return 1; + } + }; + template ::ReturnType> struct CallMemberWPtr diff --git a/libs/lua/LuaBridge/detail/Namespace.h b/libs/lua/LuaBridge/detail/Namespace.h index 07963f47db..7774b9f873 100644 --- a/libs/lua/LuaBridge/detail/Namespace.h +++ b/libs/lua/LuaBridge/detail/Namespace.h @@ -1298,6 +1298,21 @@ private: return *this; } + WSPtrClass & addEqualCheck () + { + PRINTDOC("Member Function", _name << "sameinstance", std::string("bool"), std::string("void (*)(" + type_name () + ")")) + set_weak_class (); + assert (lua_istable (L, -1)); + lua_pushcclosure (L, &CFunc::WPtrEqualCheck ::f, 0); + rawsetfield (L, -3, "isnil"); // class table + + set_shared_class (); + assert (lua_istable (L, -1)); + lua_pushcclosure (L, &CFunc::PtrEqualCheck ::f, 0); + rawsetfield (L, -3, "isnil"); // class table + + return *this; + } Namespace endClass () { @@ -1652,7 +1667,8 @@ public: WSPtrClass beginWSPtrClass (char const* name) { return WSPtrClass (name, this) - .addNullCheck(); + .addNullCheck() + .addEqualCheck(); } //---------------------------------------------------------------------------- -- 2.30.2