cppbind: add from_nasal_helper to convert Nasal ghosts to C++ shared pointer
This commit is contained in:
@@ -887,4 +887,19 @@ to_nasal_helper(naContext c, T ptr)
|
|||||||
return nasal::Ghost<T>::create(c, ptr);
|
return nasal::Ghost<T>::create(c, ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert nasal ghosts/hashes to shared pointer (of a ghost)
|
||||||
|
*/
|
||||||
|
template<class T>
|
||||||
|
typename boost::enable_if<
|
||||||
|
nasal::internal::has_element_type<
|
||||||
|
typename nasal::internal::reduced_type<T>::type
|
||||||
|
>,
|
||||||
|
T
|
||||||
|
>::type
|
||||||
|
from_nasal_helper(naContext c, naRef ref, const T*)
|
||||||
|
{
|
||||||
|
return nasal::Ghost<T>::fromNasal(c, ref);
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* SG_NASAL_GHOST_HXX_ */
|
#endif /* SG_NASAL_GHOST_HXX_ */
|
||||||
|
|||||||
@@ -52,12 +52,14 @@ struct DoubleDerived:
|
|||||||
};
|
};
|
||||||
|
|
||||||
typedef boost::shared_ptr<Base> BasePtr;
|
typedef boost::shared_ptr<Base> BasePtr;
|
||||||
|
typedef std::vector<BasePtr> BaseVec;
|
||||||
|
|
||||||
struct DoubleDerived2:
|
struct DoubleDerived2:
|
||||||
public Derived
|
public Derived
|
||||||
{
|
{
|
||||||
const BasePtr& getBase() const{return _base;}
|
const BasePtr& getBase() const{return _base;}
|
||||||
BasePtr _base;
|
BasePtr _base;
|
||||||
|
BaseVec doSomeBaseWork(const BaseVec& v) { return v; }
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef boost::shared_ptr<Derived> DerivedPtr;
|
typedef boost::shared_ptr<Derived> DerivedPtr;
|
||||||
@@ -160,7 +162,8 @@ int main(int argc, char* argv[])
|
|||||||
.bases<DerivedPtr>();
|
.bases<DerivedPtr>();
|
||||||
Ghost<DoubleDerived2Ptr>::init("DoubleDerived2Ptr")
|
Ghost<DoubleDerived2Ptr>::init("DoubleDerived2Ptr")
|
||||||
.bases< Ghost<DerivedPtr> >()
|
.bases< Ghost<DerivedPtr> >()
|
||||||
.member("base", &DoubleDerived2::getBase);
|
.member("base", &DoubleDerived2::getBase)
|
||||||
|
.method("doIt", &DoubleDerived2::doSomeBaseWork);
|
||||||
|
|
||||||
nasal::to_nasal(c, DoubleDerived2Ptr());
|
nasal::to_nasal(c, DoubleDerived2Ptr());
|
||||||
|
|
||||||
@@ -220,6 +223,17 @@ int main(int argc, char* argv[])
|
|||||||
derived_obj.set("parents", parents2);
|
derived_obj.set("parents", parents2);
|
||||||
VERIFY( Ghost<BasePtr>::fromNasal(c, derived_obj.get_naRef()) == d3 );
|
VERIFY( Ghost<BasePtr>::fromNasal(c, derived_obj.get_naRef()) == d3 );
|
||||||
|
|
||||||
|
std::vector<naRef> nasal_objects;
|
||||||
|
nasal_objects.push_back( Ghost<BasePtr>::create(c, d) );
|
||||||
|
nasal_objects.push_back( Ghost<BasePtr>::create(c, d2) );
|
||||||
|
nasal_objects.push_back( Ghost<BasePtr>::create(c, d3) );
|
||||||
|
naRef obj_vec = to_nasal(c, nasal_objects);
|
||||||
|
|
||||||
|
std::vector<BasePtr> objects = from_nasal<std::vector<BasePtr> >(c, obj_vec);
|
||||||
|
VERIFY( objects[0] == d );
|
||||||
|
VERIFY( objects[1] == d2 );
|
||||||
|
VERIFY( objects[2] == d3 );
|
||||||
|
|
||||||
// TODO actually do something with the ghosts...
|
// TODO actually do something with the ghosts...
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user