]> git.mxchange.org Git - simgear.git/commitdiff
Make nasal::Ghost usable with weak_ptr
authorThomas Geymayer <tomgey@gmail.com>
Sun, 2 Jun 2013 19:20:47 +0000 (21:20 +0200)
committerThomas Geymayer <tomgey@gmail.com>
Sun, 2 Jun 2013 19:20:47 +0000 (21:20 +0200)
simgear/nasal/cppbind/Ghost.hxx
simgear/nasal/cppbind/cppbind_test.cxx

index 81165784c2ea01a9acb875584c595c5593868aa2..c403a3077fd0afbf6a777966f13a4f723e76691d 100644 (file)
 
 #include <map>
 
+template<class T>
+inline T* get_pointer(boost::weak_ptr<T> 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<pointer*>(ptr)->get();
+          return get_pointer(*static_cast<pointer*>(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,
index ed5cb400112ce0af6b4cbfc1bbcd45f5d14a0bcc..29cb556a6bbe93749158cb2294bb47e147d38f45 100644 (file)
@@ -5,6 +5,7 @@
 #include "NasalString.hxx"
 
 #include <boost/shared_ptr.hpp>
+#include <boost/weak_ptr.hpp>
 
 #include <cstring>
 #include <iostream>
@@ -66,6 +67,8 @@ typedef boost::shared_ptr<Derived> DerivedPtr;
 typedef boost::shared_ptr<DoubleDerived> DoubleDerivedPtr;
 typedef boost::shared_ptr<DoubleDerived2> DoubleDerived2Ptr;
 
+typedef boost::weak_ptr<Derived> 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<DerivedWeakPtr>::init("DerivedWeakPtr");
+
   VERIFY( Ghost<BasePtr>::isInit() );
   nasal::to_nasal(c, DoubleDerived2Ptr());