2 /// Expose C++ objects to Nasal as ghosts
4 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Library General Public
8 // License as published by the Free Software Foundation; either
9 // version 2 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Library General Public License for more details.
16 // You should have received a copy of the GNU Library General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #ifndef SG_NASAL_GHOST_HXX_
21 #define SG_NASAL_GHOST_HXX_
23 #include "NasalCallContext.hxx"
24 #include "NasalObjectHolder.hxx"
26 #include <simgear/debug/logstream.hxx>
27 #include <simgear/structure/SGWeakReferenced.hxx>
28 #include <simgear/structure/SGWeakPtr.hxx>
30 #include <boost/bind.hpp>
31 #include <boost/call_traits.hpp>
32 #include <boost/function.hpp>
33 #include <boost/lambda/lambda.hpp>
34 #include <boost/mpl/has_xxx.hpp>
35 #include <boost/preprocessor/iteration/iterate.hpp>
36 #include <boost/shared_ptr.hpp>
37 #include <boost/utility/enable_if.hpp>
42 inline T* get_pointer(boost::weak_ptr<T> const& p)
44 return p.lock().get();
48 * Bindings between C++ and the Nasal scripting language
56 * Metadata for Ghost object types
62 * Add a nasal base class to the ghost. Will be available in the ghosts
65 void addNasalBase(const naRef& parent)
67 assert( naIsHash(parent) );
68 _parents.push_back(parent);
71 bool isBaseOf(naGhostType* ghost_type) const
73 if( ghost_type == _ghost_type_ptr )
76 for( DerivedList::const_iterator derived = _derived_classes.begin();
77 derived != _derived_classes.end();
80 if( (*derived)->isBaseOf(ghost_type) )
89 typedef std::vector<const GhostMetadata*> DerivedList;
91 const std::string _name;
92 const naGhostType *_ghost_type_ptr;
93 DerivedList _derived_classes;
94 std::vector<naRef> _parents;
96 GhostMetadata( const std::string& name,
97 const naGhostType* ghost_type ):
99 _ghost_type_ptr(ghost_type)
104 void addDerived(const GhostMetadata* derived)
107 _derived_classes.push_back(derived);
113 "Ghost::addDerived: " << _name << " -> " << derived->_name
117 naRef getParents(naContext c)
119 return nasal::to_nasal(c, _parents);
124 * Hold callable method and convert to Nasal function if required.
127 public SGWeakReferenced
130 virtual ~MethodHolder() {}
132 naRef get_naRef(naContext c)
135 _obj.reset(createNasalObject(c));
136 return _obj.get_naRef();
142 virtual naRef createNasalObject(naContext c) = 0;
145 BOOST_MPL_HAS_XXX_TRAIT_DEF(element_type)
150 typedef typename boost::remove_cv<
151 typename boost::remove_reference<T>::type
155 template<class T1, class T2>
156 struct reduced_is_same:
157 public boost::is_same<typename reduced_type<T1>::type, T2>
161 typedef SGSharedPtr<internal::MethodHolder> MethodHolderPtr;
162 typedef SGWeakPtr<internal::MethodHolder> MethodHolderWeakPtr;
165 * Class for exposing C++ objects to Nasal
168 * // Example class to be exposed to Nasal
176 * void doSomethingElse(const nasal::CallContext& ctx);
178 * typedef boost::shared_ptr<MyClass> MyClassPtr;
180 * std::string myOtherFreeMember(int num);
182 * void exposeClasses()
184 * // Register a nasal ghost type for MyClass. This needs to be done only
185 * // once before creating the first ghost instance. The exposed class needs
186 * // to be wrapped inside a shared pointer, eg. boost::shared_ptr.
187 * Ghost<MyClassPtr>::init("MyClass")
188 * // Members can be exposed by getters and setters
189 * .member("x", &MyClass::getX, &MyClass::setX)
190 * // For readonly variables only pass a getter
191 * .member("x_readonly", &MyClass::getX)
192 * // It is also possible to expose writeonly members
193 * .member("x_writeonly", &MyClass::setX)
194 * // Methods can be nearly anything callable and accepting a reference
195 * // to an instance of the class type. (member functions, free functions
196 * // and anything else bindable using boost::function and boost::bind)
197 * .method("myMember", &MyClass::myMember)
198 * .method("doSomething", &MyClass::doSomethingElse)
199 * .method("other", &myOtherFreeMember);
205 public internal::GhostMetadata
207 BOOST_STATIC_ASSERT( internal::has_element_type<T>::value );
210 typedef typename T::element_type raw_type;
212 typedef naRef (raw_type::*member_func_t)(const CallContext&);
213 typedef naRef (*free_func_t)(raw_type&, const CallContext&);
214 typedef boost::function<naRef(naContext, raw_type&)> getter_t;
215 typedef boost::function<void(naContext, raw_type&, naRef)> setter_t;
216 typedef boost::function<naRef(raw_type&, const CallContext&)> method_t;
219 public internal::MethodHolder
222 explicit MethodHolder(const method_t& method):
228 typedef SGSharedPtr<MethodHolder> SharedPtr;
229 typedef SGWeakPtr<MethodHolder> WeakPtr;
233 virtual naRef createNasalObject(naContext c)
245 static void destroyHolder(void* user_data)
247 delete static_cast<WeakPtr*>(user_data);
250 static naRef call( naContext c,
256 WeakPtr* holder_weak = static_cast<WeakPtr*>(user_data);
258 naRuntimeError(c, "invalid method holder!");
262 SharedPtr holder = holder_weak->lock();
264 throw std::runtime_error("holder has expired");
266 return holder->_method
268 requireObject(c, me),
269 CallContext(c, argc, args)
272 catch(const std::exception& ex)
274 naRuntimeError(c, "Fatal error in method call: %s", ex.what());
278 naRuntimeError(c, "Unknown exception in method call.");
286 * A ghost member. Can consist either of getter and/or setter functions
287 * for exposing a data variable or a single callable function.
294 member_t( const getter_t& getter,
295 const setter_t& setter,
296 const MethodHolderPtr& func = MethodHolderPtr() ):
302 explicit member_t(const MethodHolderPtr& func):
308 MethodHolderPtr func;
311 typedef std::map<std::string, member_t> MemberMap;
314 * Register a new ghost type.
316 * @note Only intialize each ghost type once!
318 * @param name Descriptive name of the ghost type.
320 static Ghost& init(const std::string& name)
322 getSingletonHolder().reset( new Ghost(name) );
323 return *getSingletonPtr();
327 * Check whether ghost type has already been initialized.
331 return getSingletonPtr();
335 * Register a base class for this ghost. The base class needs to be
336 * registers on its own before it can be used as base class.
338 * @tparam BaseGhost Type of base class already wrapped into Ghost class
342 * Ghost<MyBasePtr>::init("MyBase");
343 * Ghost<MyClassPtr>::init("MyClass")
344 * .bases<Ghost<MyBasePtr> >();
347 template<class BaseGhost>
348 typename boost::enable_if
350 boost::is_base_of<GhostMetadata, BaseGhost>,
357 boost::is_base_of<typename BaseGhost::raw_type, raw_type>::value
360 BaseGhost* base = BaseGhost::getSingletonPtr();
364 // Both ways of retrieving the address of a static member function
365 // should be legal but not all compilers know this.
366 // g++-4.4.7+ has been tested to work with both versions
367 #if defined(SG_GCC_VERSION) && SG_GCC_VERSION < 40407
368 // The old version of g++ used on Jenkins (16.11.2012) only compiles
370 &getTypeFor<BaseGhost>
372 // VS (2008, 2010, ... ?) only allow this version.
373 &Ghost::getTypeFor<BaseGhost>
377 // Replace any getter that is not available in the current class.
378 // TODO check if this is the correct behavior of function overriding
379 for( typename BaseGhost::MemberMap::const_iterator member =
380 base->_members.begin();
381 member != base->_members.end();
384 if( _members.find(member->first) == _members.end() )
385 _members[member->first] = member_t
387 member->second.getter,
388 member->second.setter,
397 * Register a base class for this ghost. The base class needs to be
398 * registers on its own before it can be used as base class.
400 * @tparam Base Type of base class (Base as used in Ghost<BasePtr>)
403 * Ghost<MyBasePtr>::init("MyBase");
404 * Ghost<MyClassPtr>::init("MyClass")
405 * .bases<MyBasePtr>();
409 typename boost::disable_if
411 boost::is_base_of<GhostMetadata, Base>,
418 boost::is_base_of<typename Ghost<Base>::raw_type, raw_type>::value
421 return bases< Ghost<Base> >();
425 * Register an existing Nasal class/hash as base class for this ghost.
427 * @param parent Nasal hash/class
429 Ghost& bases(const naRef& parent)
431 addNasalBase(parent);
436 * Register a member variable by passing a getter and/or setter method.
438 * @param field Name of member
439 * @param getter Getter for variable
440 * @param setter Setter for variable (Pass 0 to prevent write access)
443 template<class Ret, class Param>
444 Ghost& member( const std::string& field,
445 Ret (raw_type::*getter)() const,
446 void (raw_type::*setter)(Param) )
448 return member(field, to_getter(getter), to_setter(setter));
452 * Register a read only member variable.
454 * @param field Name of member
455 * @param getter Getter for variable
458 Ghost& member( const std::string& field,
459 Ret (raw_type::*getter)() const )
461 return member(field, to_getter(getter), setter_t());
465 * Register a write only member variable.
467 * @param field Name of member
468 * @param setter Setter for variable
471 Ghost& member( const std::string& field,
472 void (raw_type::*setter)(Var) )
474 return member(field, getter_t(), to_setter(setter));
478 * Register a member variable by passing a getter and/or setter method.
480 * @param field Name of member
481 * @param getter Getter for variable
482 * @param setter Setter for variable (Pass empty to prevent write access)
485 Ghost& member( const std::string& field,
486 const getter_t& getter,
487 const setter_t& setter = setter_t() )
489 if( !getter.empty() || !setter.empty() )
490 _members[field] = member_t(getter, setter);
496 "Member '" << field << "' requires a getter or setter"
502 * Register anything that accepts an object instance and a
503 * nasal::CallContext and returns naRef as method.
509 * naRef myMethod(const nasal::CallContext& ctx);
512 * Ghost<MyClassPtr>::init("Test")
513 * .method("myMethod", &MyClass::myMethod);
516 Ghost& method(const std::string& name, const method_t& func)
518 _members[name].func = new MethodHolder(func);
523 * Register anything that accepts an object instance and a
524 * nasal::CallContext whith automatic conversion of the return type to
529 * void doIt(const MyClass& c, const nasal::CallContext& ctx);
531 * Ghost<MyClassPtr>::init("Test")
532 * .method("doIt", &doIt);
538 const std::string& name,
539 const boost::function<Ret (raw_type&, const CallContext&)>& func
542 return method(name, boost::bind(method_invoker<Ret>, func, _1, _2));
545 #define BOOST_PP_ITERATION_LIMITS (0, 9)
546 #define BOOST_PP_FILENAME_1 <simgear/nasal/cppbind/detail/functor_templates.hxx>
547 #include BOOST_PP_ITERATE()
549 // TODO use variadic template when supporting C++11
550 // TODO check if default constructor exists
551 // static naRef create( naContext c )
553 // return makeGhost(c, createInstance());
557 * Create a Nasal instance of this ghost.
559 * @param c Active Nasal context
560 * @param a1 Parameter used for creating new instance
563 static naRef create( naContext c, const A1& a1 )
565 return makeGhost(c, createInstance(a1));
569 * Nasal callback for creating a new instance of this ghost.
571 static naRef f_create(naContext c, naRef me, int argc, naRef* args)
576 static bool isBaseOf(naGhostType* ghost_type)
581 return getSingletonPtr()->GhostMetadata::isBaseOf(ghost_type);
584 static bool isBaseOf(naRef obj)
586 return isBaseOf( naGhost_type(obj) );
590 * Convert Nasal object to C++ object. To get a valid object the passed
591 * Nasal objects has to be derived class of the target class (Either
592 * derived in C++ or in Nasal using a 'parents' vector)
594 static pointer fromNasal(naContext c, naRef me)
596 // Check if it's a ghost and if it can be converted
597 if( isBaseOf( naGhost_type(me) ) )
598 return getPtr( naGhost_ptr(me) );
600 // Now if it is derived from a ghost (hash with ghost in parent vector)
601 else if( naIsHash(me) )
603 naRef na_parents = naHash_cget(me, const_cast<char*>("parents"));
604 if( !naIsVector(na_parents) )
606 SG_LOG(SG_NASAL, SG_DEBUG, "Missing 'parents' vector for ghost");
610 typedef std::vector<naRef> naRefs;
611 naRefs parents = from_nasal<naRefs>(c, na_parents);
612 for( naRefs::const_iterator parent = parents.begin();
613 parent != parents.end();
616 pointer ptr = fromNasal(c, *parent);
617 if( get_pointer(ptr) )
630 static naGhostType _ghost_type;
632 typedef naGhostType* (*type_checker_t)(const raw_type*);
633 typedef std::vector<type_checker_t> DerivedList;
634 DerivedList _derived_types;
637 * Create a shared pointer on the heap to handle the reference counting
638 * for the passed shared pointer while it is used in Nasal space.
640 static pointer* createInstance(const pointer& ptr)
642 return get_pointer(ptr) ? new pointer(ptr) : 0;
645 static pointer getPtr(void* ptr)
648 return *static_cast<pointer*>(ptr);
653 static raw_type* getRawPtr(void* ptr)
656 return get_pointer(*static_cast<pointer*>(ptr));
661 static raw_type* getRawPtr(const pointer& ptr)
663 return get_pointer(ptr);
666 void addDerived( const internal::GhostMetadata* derived_meta,
667 const type_checker_t& derived_info )
669 GhostMetadata::addDerived(derived_meta);
670 _derived_types.push_back(derived_info);
673 template<class BaseGhost>
675 typename boost::enable_if
676 < boost::is_polymorphic<typename BaseGhost::raw_type>,
679 getTypeFor(const typename BaseGhost::raw_type* base)
681 // Check first if passed pointer can by converted to instance of class
684 < typename BaseGhost::raw_type,
685 typename Ghost::raw_type
687 && dynamic_cast<const typename Ghost::raw_type*>(base) != base )
690 // Now check if we can further downcast to one of our derived classes.
691 for( typename DerivedList::reverse_iterator
692 derived = getSingletonPtr()->_derived_types.rbegin();
693 derived != getSingletonPtr()->_derived_types.rend();
696 naGhostType* ghost_type =
697 (*derived)( static_cast<const typename Ghost::raw_type*>(base) );
702 // If base is not an instance of any derived class, this class has to
703 // be the dynamic type.
707 template<class BaseGhost>
709 typename boost::disable_if
710 < boost::is_polymorphic<typename BaseGhost::raw_type>,
713 getTypeFor(const typename BaseGhost::raw_type* base)
715 // For non polymorphic classes there is no possibility to get the actual
716 // dynamic type, therefore we can only use its static type.
717 return &BaseGhost::_ghost_type;
720 static Ghost* getSingletonPtr()
722 return getSingletonHolder().get();
725 static raw_type& requireObject(naContext c, naRef me)
727 raw_type* obj = getRawPtr( fromNasal(c, me) );
728 naGhostType* ghost_type = naGhost_type(me);
734 "method called on object of wrong type: is '%s' expected '%s'",
735 ghost_type ? ghost_type->name : "unknown",
743 getter_t to_getter(Ret (raw_type::*getter)() const)
745 typedef typename boost::call_traits<Ret>::param_type param_type;
746 naRef(*to_nasal_)(naContext, param_type) = &to_nasal;
748 // Getter signature: naRef(naContext, raw_type&)
753 boost::bind(getter, _2)
757 template<class Param>
758 setter_t to_setter(void (raw_type::*setter)(Param))
760 // Setter signature: void(naContext, raw_type&, naRef)
765 boost::bind(from_nasal_ptr<Param>::get(), _1, _3)
771 * Invoke a method which returns a value and convert it to Nasal.
775 typename boost::disable_if<boost::is_void<Ret>, naRef>::type
778 const boost::function<Ret (raw_type&, const CallContext&)>& func,
780 const CallContext& ctx
783 return (*to_nasal_ptr<Ret>::get())(ctx.c, func(obj, ctx));
787 * Invoke a method which returns void and "convert" it to nil.
791 typename boost::enable_if<boost::is_void<Ret>, naRef>::type
794 const boost::function<Ret (raw_type&, const CallContext&)>& func,
796 const CallContext& ctx
804 * Extract argument by index from nasal::CallContext and convert to given
809 typename boost::disable_if<
810 internal::reduced_is_same<Arg, CallContext>,
811 typename from_nasal_ptr<Arg>::return_type
813 arg_from_nasal(const CallContext& ctx, size_t index)
815 return ctx.requireArg<Arg>(index);
819 * Specialization to pass through nasal::CallContext.
823 typename boost::enable_if<
824 internal::reduced_is_same<Arg, CallContext>,
825 typename from_nasal_ptr<Arg>::return_type
827 arg_from_nasal(const CallContext& ctx, size_t)
829 // Either const CallContext& or CallContext, non-const reference
830 // does not make sense.
831 BOOST_STATIC_ASSERT( (!boost::is_same<Arg, CallContext&>::value) );
835 typedef std::auto_ptr<Ghost> GhostPtr;
838 explicit Ghost(const std::string& name):
839 GhostMetadata(name, &_ghost_type)
841 _ghost_type.destroy = &destroyGhost;
842 _ghost_type.name = _name.c_str();
843 _ghost_type.get_member = &getMember;
844 _ghost_type.set_member = &setMember;
847 static GhostPtr& getSingletonHolder()
849 static GhostPtr instance;
853 static naRef makeGhost(naContext c, void *ptr)
857 // We are wrapping shared pointers to already existing objects which
858 // will then be hold be a new shared pointer. We therefore have to
859 // check for the dynamic type of the object as it might differ from
860 // the passed static type.
861 naGhostType* ghost_type = getTypeFor<Ghost>( getRawPtr(ptr) );
864 return naNewGhost2(c, ghost_type, ptr);
871 static void destroyGhost(void *ptr)
873 delete static_cast<pointer*>(ptr);
877 * Callback for retrieving a ghost member.
879 static const char* getMember(naContext c, void* g, naRef key, naRef* out)
881 const std::string key_str = nasal::from_nasal<std::string>(c, key);
882 if( key_str == "parents" )
884 if( getSingletonPtr()->_parents.empty() )
887 *out = getSingletonPtr()->getParents(c);
891 typename MemberMap::iterator member =
892 getSingletonPtr()->_members.find(key_str);
894 if( member == getSingletonPtr()->_members.end() )
897 if( member->second.func )
898 *out = member->second.func->get_naRef(c);
899 else if( !member->second.getter.empty() )
900 *out = member->second.getter(c, *getRawPtr(g));
902 return "Read-protected member";
908 * Callback for writing to a ghost member.
910 static void setMember(naContext c, void* g, naRef field, naRef val)
912 const std::string key = nasal::from_nasal<std::string>(c, field);
913 typename MemberMap::iterator member =
914 getSingletonPtr()->_members.find(key);
916 if( member == getSingletonPtr()->_members.end() )
917 naRuntimeError(c, "ghost: No such member: %s", key.c_str());
918 else if( member->second.setter.empty() )
919 naRuntimeError(c, "ghost: Write protected member: %s", key.c_str());
920 else if( member->second.func )
921 naRuntimeError(c, "ghost: Write to function: %s", key.c_str());
923 member->second.setter(c, *getRawPtr(g), val);
928 naGhostType Ghost<T>::_ghost_type;
932 // Needs to be outside any namespace to make ADL work
934 * Convert every shared pointer to a ghost.
937 typename boost::enable_if<
938 nasal::internal::has_element_type<
939 typename nasal::internal::reduced_type<T>::type
943 to_nasal_helper(naContext c, T ptr)
945 return nasal::Ghost<T>::create(c, ptr);
949 * Convert nasal ghosts/hashes to shared pointer (of a ghost).
952 typename boost::enable_if<
953 nasal::internal::has_element_type<
954 typename nasal::internal::reduced_type<T>::type
958 from_nasal_helper(naContext c, naRef ref, const T*)
960 return nasal::Ghost<T>::fromNasal(c, ref);
964 * Convert any pointer to a SGReference based object to a ghost.
967 typename boost::enable_if<boost::is_base_of<SGReferenced, T>, naRef>::type
968 to_nasal_helper(naContext c, T* ptr)
970 return nasal::Ghost<SGSharedPtr<T> >::create(c, SGSharedPtr<T>(ptr));
974 * Convert nasal ghosts/hashes to pointer (of a SGReference based ghost).
977 typename boost::enable_if<
978 boost::is_base_of<SGReferenced, typename boost::remove_pointer<T>::type>,
981 from_nasal_helper(naContext c, naRef ref, const T*)
983 typedef SGSharedPtr<typename boost::remove_pointer<T>::type> TypeRef;
984 return nasal::Ghost<TypeRef>::fromNasal(c, ref).release();
987 #endif /* SG_NASAL_GHOST_HXX_ */