From: Thomas Geymayer Date: Tue, 19 Mar 2013 17:36:55 +0000 (+0100) Subject: cppbind: add from_nasal_helper to convert Nasal ghosts to C++ shared pointer X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=971ea8186121a51418f66b11e02bb16517dcce25;p=simgear.git cppbind: add from_nasal_helper to convert Nasal ghosts to C++ shared pointer --- diff --git a/simgear/nasal/cppbind/Ghost.hxx b/simgear/nasal/cppbind/Ghost.hxx index 7354644a..77c0f287 100644 --- a/simgear/nasal/cppbind/Ghost.hxx +++ b/simgear/nasal/cppbind/Ghost.hxx @@ -887,4 +887,19 @@ to_nasal_helper(naContext c, T ptr) return nasal::Ghost::create(c, ptr); } +/** + * Convert nasal ghosts/hashes to shared pointer (of a ghost) + */ +template +typename boost::enable_if< + nasal::internal::has_element_type< + typename nasal::internal::reduced_type::type + >, + T +>::type +from_nasal_helper(naContext c, naRef ref, const T*) +{ + return nasal::Ghost::fromNasal(c, ref); +} + #endif /* SG_NASAL_GHOST_HXX_ */ diff --git a/simgear/nasal/cppbind/cppbind_test.cxx b/simgear/nasal/cppbind/cppbind_test.cxx index 876f3c45..9fa92534 100644 --- a/simgear/nasal/cppbind/cppbind_test.cxx +++ b/simgear/nasal/cppbind/cppbind_test.cxx @@ -52,12 +52,14 @@ struct DoubleDerived: }; typedef boost::shared_ptr BasePtr; +typedef std::vector BaseVec; struct DoubleDerived2: public Derived { const BasePtr& getBase() const{return _base;} BasePtr _base; + BaseVec doSomeBaseWork(const BaseVec& v) { return v; } }; typedef boost::shared_ptr DerivedPtr; @@ -160,7 +162,8 @@ int main(int argc, char* argv[]) .bases(); Ghost::init("DoubleDerived2Ptr") .bases< Ghost >() - .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::fromNasal(c, derived_obj.get_naRef()) == d3 ); + std::vector nasal_objects; + nasal_objects.push_back( Ghost::create(c, d) ); + nasal_objects.push_back( Ghost::create(c, d2) ); + nasal_objects.push_back( Ghost::create(c, d3) ); + naRef obj_vec = to_nasal(c, nasal_objects); + + std::vector objects = from_nasal >(c, obj_vec); + VERIFY( objects[0] == d ); + VERIFY( objects[1] == d2 ); + VERIFY( objects[2] == d3 ); + // TODO actually do something with the ghosts... //----------------------------------------------------------------------------