]> git.mxchange.org Git - simgear.git/commitdiff
cppbind: add from_nasal_helper to convert Nasal ghosts to C++ shared pointer
authorThomas Geymayer <tomgey@gmail.com>
Tue, 19 Mar 2013 17:36:55 +0000 (18:36 +0100)
committerThomas Geymayer <tomgey@gmail.com>
Tue, 19 Mar 2013 17:36:55 +0000 (18:36 +0100)
simgear/nasal/cppbind/Ghost.hxx
simgear/nasal/cppbind/cppbind_test.cxx

index 7354644a544e3959ffdeb66d4c84e4238f0ee438..77c0f2870c53b2e5adc3b29e99af49f293e065e2 100644 (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_ */
index 876f3c45e17d080e4ab8620c501ada66b8008d08..9fa925344507799a0f960fc84b45dd996b4e5fb9 100644 (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...
 
   //----------------------------------------------------------------------------