Make nasal::Ghost usable with weak_ptr

This commit is contained in:
Thomas Geymayer
2013-06-02 21:20:47 +02:00
parent 2a6c50c893
commit 5c8f0cc966
2 changed files with 15 additions and 4 deletions
+10 -4
View File
@@ -35,6 +35,12 @@
#include <map> #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 * Bindings between C++ and the Nasal scripting language
*/ */
@@ -569,7 +575,7 @@ namespace nasal
++parent ) ++parent )
{ {
pointer ptr = fromNasal(c, *parent); pointer ptr = fromNasal(c, *parent);
if( ptr.get() ) if( get_pointer(ptr) )
return ptr; return ptr;
} }
} }
@@ -592,7 +598,7 @@ namespace nasal
*/ */
static pointer* createInstance(const pointer& ptr) 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) static pointer getPtr(void* ptr)
@@ -606,14 +612,14 @@ namespace nasal
static raw_type* getRawPtr(void* ptr) static raw_type* getRawPtr(void* ptr)
{ {
if( ptr ) if( ptr )
return static_cast<pointer*>(ptr)->get(); return get_pointer(*static_cast<pointer*>(ptr));
else else
return 0; return 0;
} }
static raw_type* getRawPtr(const pointer& ptr) static raw_type* getRawPtr(const pointer& ptr)
{ {
return ptr.get(); return get_pointer(ptr);
} }
void addDerived( const internal::GhostMetadata* derived_meta, void addDerived( const internal::GhostMetadata* derived_meta,
+5
View File
@@ -5,6 +5,7 @@
#include "NasalString.hxx" #include "NasalString.hxx"
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
@@ -66,6 +67,8 @@ typedef boost::shared_ptr<Derived> DerivedPtr;
typedef boost::shared_ptr<DoubleDerived> DoubleDerivedPtr; typedef boost::shared_ptr<DoubleDerived> DoubleDerivedPtr;
typedef boost::shared_ptr<DoubleDerived2> DoubleDerived2Ptr; typedef boost::shared_ptr<DoubleDerived2> DoubleDerived2Ptr;
typedef boost::weak_ptr<Derived> DerivedWeakPtr;
naRef derivedFreeMember(Derived&, const nasal::CallContext&) { return naNil(); } naRef derivedFreeMember(Derived&, const nasal::CallContext&) { return naNil(); }
naRef f_derivedGetX(naContext c, const Derived& d) naRef f_derivedGetX(naContext c, const Derived& d)
{ {
@@ -165,6 +168,8 @@ int main(int argc, char* argv[])
.member("base", &DoubleDerived2::getBase) .member("base", &DoubleDerived2::getBase)
.method("doIt", &DoubleDerived2::doSomeBaseWork); .method("doIt", &DoubleDerived2::doSomeBaseWork);
Ghost<DerivedWeakPtr>::init("DerivedWeakPtr");
VERIFY( Ghost<BasePtr>::isInit() ); VERIFY( Ghost<BasePtr>::isInit() );
nasal::to_nasal(c, DoubleDerived2Ptr()); nasal::to_nasal(c, DoubleDerived2Ptr());