cppbind: add from_nasal_helper to convert Nasal ghosts to C++ shared pointer

This commit is contained in:
Thomas Geymayer 2013-03-19 18:36:55 +01:00
parent 9e9cc7859c
commit 971ea81861
2 changed files with 30 additions and 1 deletions

View File

@ -887,4 +887,19 @@ to_nasal_helper(naContext c, T 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_ */

View File

@ -52,12 +52,14 @@ struct DoubleDerived:
};
typedef boost::shared_ptr<Base> BasePtr;
typedef std::vector<BasePtr> BaseVec;
struct DoubleDerived2:
public Derived
{
const BasePtr& getBase() const{return _base;}
BasePtr _base;
BaseVec doSomeBaseWork(const BaseVec& v) { return v; }
};
typedef boost::shared_ptr<Derived> DerivedPtr;
@ -160,7 +162,8 @@ int main(int argc, char* argv[])
.bases<DerivedPtr>();
Ghost<DoubleDerived2Ptr>::init("DoubleDerived2Ptr")
.bases< Ghost<DerivedPtr> >()
.member("base", &DoubleDerived2::getBase);
.member("base", &DoubleDerived2::getBase)
.method("doIt", &DoubleDerived2::doSomeBaseWork);
nasal::to_nasal(c, DoubleDerived2Ptr());
@ -220,6 +223,17 @@ int main(int argc, char* argv[])
derived_obj.set("parents", parents2);
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...
//----------------------------------------------------------------------------