From: Thomas Geymayer Date: Sun, 2 Jun 2013 19:20:47 +0000 (+0200) Subject: Make nasal::Ghost usable with weak_ptr X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=5c8f0cc966576c707ce73ca54d815b2cf56bba0a;p=simgear.git Make nasal::Ghost usable with weak_ptr --- diff --git a/simgear/nasal/cppbind/Ghost.hxx b/simgear/nasal/cppbind/Ghost.hxx index 81165784..c403a307 100644 --- a/simgear/nasal/cppbind/Ghost.hxx +++ b/simgear/nasal/cppbind/Ghost.hxx @@ -35,6 +35,12 @@ #include +template +inline T* get_pointer(boost::weak_ptr const& p) +{ + return p.lock().get(); +} + /** * Bindings between C++ and the Nasal scripting language */ @@ -569,7 +575,7 @@ namespace nasal ++parent ) { pointer ptr = fromNasal(c, *parent); - if( ptr.get() ) + if( get_pointer(ptr) ) return ptr; } } @@ -592,7 +598,7 @@ namespace nasal */ static pointer* createInstance(const pointer& ptr) { - return ptr.get() ? new pointer(ptr) : 0; + return get_pointer(ptr) ? new pointer(ptr) : 0; } static pointer getPtr(void* ptr) @@ -606,14 +612,14 @@ namespace nasal static raw_type* getRawPtr(void* ptr) { if( ptr ) - return static_cast(ptr)->get(); + return get_pointer(*static_cast(ptr)); else return 0; } static raw_type* getRawPtr(const pointer& ptr) { - return ptr.get(); + return get_pointer(ptr); } void addDerived( const internal::GhostMetadata* derived_meta, diff --git a/simgear/nasal/cppbind/cppbind_test.cxx b/simgear/nasal/cppbind/cppbind_test.cxx index ed5cb400..29cb556a 100644 --- a/simgear/nasal/cppbind/cppbind_test.cxx +++ b/simgear/nasal/cppbind/cppbind_test.cxx @@ -5,6 +5,7 @@ #include "NasalString.hxx" #include +#include #include #include @@ -66,6 +67,8 @@ typedef boost::shared_ptr DerivedPtr; typedef boost::shared_ptr DoubleDerivedPtr; typedef boost::shared_ptr DoubleDerived2Ptr; +typedef boost::weak_ptr DerivedWeakPtr; + naRef derivedFreeMember(Derived&, const nasal::CallContext&) { return naNil(); } naRef f_derivedGetX(naContext c, const Derived& d) { @@ -165,6 +168,8 @@ int main(int argc, char* argv[]) .member("base", &DoubleDerived2::getBase) .method("doIt", &DoubleDerived2::doSomeBaseWork); + Ghost::init("DerivedWeakPtr"); + VERIFY( Ghost::isInit() ); nasal::to_nasal(c, DoubleDerived2Ptr());