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"
25 #include <simgear/debug/logstream.hxx>
27 #include <boost/bind.hpp>
28 #include <boost/call_traits.hpp>
29 #include <boost/function.hpp>
30 #include <boost/lambda/lambda.hpp>
31 #include <boost/mpl/has_xxx.hpp>
32 #include <boost/preprocessor/iteration/iterate.hpp>
33 #include <boost/shared_ptr.hpp>
34 #include <boost/utility/enable_if.hpp>
39 inline T* get_pointer(boost::weak_ptr<T> const& p)
41 return p.lock().get();
45 * Bindings between C++ and the Nasal scripting language
53 * Metadata for Ghost object types
59 * Add a nasal base class to the ghost. Will be available in the ghosts
62 void addNasalBase(const naRef& parent)
64 assert( naIsHash(parent) );
65 _parents.push_back(parent);
68 bool isBaseOf(naGhostType* ghost_type) const
70 if( ghost_type == &_ghost_type )
73 for( DerivedList::const_iterator derived = _derived_classes.begin();
74 derived != _derived_classes.end();
77 if( (*derived)->isBaseOf(ghost_type) )
86 typedef std::vector<const GhostMetadata*> DerivedList;
88 const std::string _name;
89 naGhostType _ghost_type;
90 DerivedList _derived_classes;
91 std::vector<naRef> _parents;
93 explicit GhostMetadata(const std::string& name):
99 void addDerived(const GhostMetadata* derived)
102 _derived_classes.push_back(derived);
108 "Ghost::addDerived: " <<_ghost_type.name << " -> " << derived->_name
112 naRef getParents(naContext c)
114 return nasal::to_nasal(c, _parents);
119 * Hold callable method and convert to Nasal function if required.
124 virtual ~MethodHolder() {}
125 virtual naRef get_naRef(naContext c) = 0;
128 BOOST_MPL_HAS_XXX_TRAIT_DEF(element_type)
133 typedef typename boost::remove_cv<
134 typename boost::remove_reference<T>::type
138 template<class T1, class T2>
139 struct reduced_is_same:
140 public boost::is_same<typename reduced_type<T1>::type, T2>
145 * Class for exposing C++ objects to Nasal
148 * // Example class to be exposed to Nasal
156 * void doSomethingElse(const nasal::CallContext& ctx);
158 * typedef boost::shared_ptr<MyClass> MyClassPtr;
160 * std::string myOtherFreeMember(int num);
162 * void exposeClasses()
164 * // Register a nasal ghost type for MyClass. This needs to be done only
165 * // once before creating the first ghost instance. The exposed class needs
166 * // to be wrapped inside a shared pointer, eg. boost::shared_ptr.
167 * Ghost<MyClassPtr>::init("MyClass")
168 * // Members can be exposed by getters and setters
169 * .member("x", &MyClass::getX, &MyClass::setX)
170 * // For readonly variables only pass a getter
171 * .member("x_readonly", &MyClass::getX)
172 * // It is also possible to expose writeonly members
173 * .member("x_writeonly", &MyClass::setX)
174 * // Methods can be nearly anything callable and accepting a reference
175 * // to an instance of the class type. (member functions, free functions
176 * // and anything else bindable using boost::function and boost::bind)
177 * .method("myMember", &MyClass::myMember)
178 * .method("doSomething", &MyClass::doSomethingElse)
179 * .method("other", &myOtherFreeMember);
185 public internal::GhostMetadata
187 BOOST_STATIC_ASSERT( internal::has_element_type<T>::value );
190 typedef typename T::element_type raw_type;
192 typedef naRef (raw_type::*member_func_t)(const CallContext&);
193 typedef naRef (*free_func_t)(raw_type&, const CallContext&);
194 typedef boost::function<naRef(naContext, raw_type&)> getter_t;
195 typedef boost::function<void(naContext, raw_type&, naRef)> setter_t;
196 typedef boost::function<naRef(raw_type&, const CallContext&)> method_t;
197 typedef boost::shared_ptr<internal::MethodHolder> MethodHolderPtr;
200 public internal::MethodHolder
207 explicit MethodHolder(const method_t& method):
212 virtual naRef get_naRef(naContext c)
214 if( naIsNil(_naRef) )
216 _naRef = naNewFunc(c, naNewCCodeU(c, &MethodHolder::call, this));
226 static naRef call( naContext c,
232 MethodHolder* holder = static_cast<MethodHolder*>(user_data);
234 naRuntimeError(c, "invalid method holder!");
236 return holder->_method
238 requireObject(c, me),
239 CallContext(c, argc, args)
245 * A ghost member. Can consist either of getter and/or setter functions
246 * for exposing a data variable or a single callable function.
253 member_t( const getter_t& getter,
254 const setter_t& setter,
255 const MethodHolderPtr& func = MethodHolderPtr() ):
261 explicit member_t(const MethodHolderPtr& func):
267 MethodHolderPtr func;
270 typedef std::map<std::string, member_t> MemberMap;
273 * Register a new ghost type.
275 * @note Only intialize each ghost type once!
277 * @param name Descriptive name of the ghost type.
279 static Ghost& init(const std::string& name)
281 assert( !getSingletonPtr() );
283 getSingletonHolder().reset( new Ghost(name) );
284 return *getSingletonPtr();
288 * Check whether ghost type has already been initialized.
292 return getSingletonPtr();
296 * Register a base class for this ghost. The base class needs to be
297 * registers on its own before it can be used as base class.
299 * @tparam BaseGhost Type of base class already wrapped into Ghost class
303 * Ghost<MyBasePtr>::init("MyBase");
304 * Ghost<MyClassPtr>::init("MyClass")
305 * .bases<Ghost<MyBasePtr> >();
308 template<class BaseGhost>
309 typename boost::enable_if
311 boost::is_base_of<GhostMetadata, BaseGhost>,
318 boost::is_base_of<typename BaseGhost::raw_type, raw_type>::value
321 BaseGhost* base = BaseGhost::getSingletonPtr();
325 // Both ways of retrieving the address of a static member function
326 // should be legal but not all compilers know this.
327 // g++-4.4.7+ has been tested to work with both versions
328 #if defined(GCC_VERSION) && GCC_VERSION < 40407
329 // The old version of g++ used on Jenkins (16.11.2012) only compiles
331 &getTypeFor<BaseGhost>
333 // VS (2008, 2010, ... ?) only allow this version.
334 &Ghost::getTypeFor<BaseGhost>
338 // Replace any getter that is not available in the current class.
339 // TODO check if this is the correct behavior of function overriding
340 for( typename BaseGhost::MemberMap::const_iterator member =
341 base->_members.begin();
342 member != base->_members.end();
345 if( _members.find(member->first) == _members.end() )
346 _members[member->first] = member_t
348 member->second.getter,
349 member->second.setter,
358 * Register a base class for this ghost. The base class needs to be
359 * registers on its own before it can be used as base class.
361 * @tparam Base Type of base class (Base as used in Ghost<BasePtr>)
364 * Ghost<MyBasePtr>::init("MyBase");
365 * Ghost<MyClassPtr>::init("MyClass")
366 * .bases<MyBasePtr>();
370 typename boost::disable_if
372 boost::is_base_of<GhostMetadata, Base>,
379 boost::is_base_of<typename Ghost<Base>::raw_type, raw_type>::value
382 return bases< Ghost<Base> >();
386 * Register an existing Nasal class/hash as base class for this ghost.
388 * @param parent Nasal hash/class
390 Ghost& bases(const naRef& parent)
392 addNasalBase(parent);
397 * Register a member variable by passing a getter and/or setter method.
399 * @param field Name of member
400 * @param getter Getter for variable
401 * @param setter Setter for variable (Pass 0 to prevent write access)
404 template<class Ret, class Param>
405 Ghost& member( const std::string& field,
406 Ret (raw_type::*getter)() const,
407 void (raw_type::*setter)(Param) )
409 return member(field, to_getter(getter), to_setter(setter));
413 * Register a read only member variable.
415 * @param field Name of member
416 * @param getter Getter for variable
419 Ghost& member( const std::string& field,
420 Ret (raw_type::*getter)() const )
422 return member(field, to_getter(getter), setter_t());
426 * Register a write only member variable.
428 * @param field Name of member
429 * @param setter Setter for variable
432 Ghost& member( const std::string& field,
433 void (raw_type::*setter)(Var) )
435 return member(field, getter_t(), to_setter(setter));
439 * Register a member variable by passing a getter and/or setter method.
441 * @param field Name of member
442 * @param getter Getter for variable
443 * @param setter Setter for variable (Pass empty to prevent write access)
446 Ghost& member( const std::string& field,
447 const getter_t& getter,
448 const setter_t& setter = setter_t() )
450 if( !getter.empty() || !setter.empty() )
451 _members[field] = member_t(getter, setter);
457 "Member '" << field << "' requires a getter or setter"
463 * Register anything that accepts an object instance and a
464 * nasal::CallContext and returns naRef as method.
470 * naRef myMethod(const nasal::CallContext& ctx);
473 * Ghost<MyClassPtr>::init("Test")
474 * .method("myMethod", &MyClass::myMethod);
477 Ghost& method(const std::string& name, const method_t& func)
479 _members[name].func.reset( new MethodHolder(func) );
484 * Register anything that accepts an object instance and a
485 * nasal::CallContext whith automatic conversion of the return type to
490 * void doIt(const MyClass& c, const nasal::CallContext& ctx);
492 * Ghost<MyClassPtr>::init("Test")
493 * .method("doIt", &doIt);
499 const std::string& name,
500 const boost::function<Ret (raw_type&, const CallContext&)>& func
503 return method(name, boost::bind(method_invoker<Ret>, func, _1, _2));
506 #define BOOST_PP_ITERATION_LIMITS (0, 9)
507 #define BOOST_PP_FILENAME_1 <simgear/nasal/cppbind/detail/functor_templates.hxx>
508 #include BOOST_PP_ITERATE()
510 // TODO use variadic template when supporting C++11
511 // TODO check if default constructor exists
512 // static naRef create( naContext c )
514 // return makeGhost(c, createInstance());
518 * Create a Nasal instance of this ghost.
520 * @param c Active Nasal context
521 * @param a1 Parameter used for creating new instance
524 static naRef create( naContext c, const A1& a1 )
526 return makeGhost(c, createInstance(a1));
530 * Nasal callback for creating a new instance of this ghost.
532 static naRef f_create(naContext c, naRef me, int argc, naRef* args)
537 static bool isBaseOf(naGhostType* ghost_type)
542 return getSingletonPtr()->GhostMetadata::isBaseOf(ghost_type);
545 static bool isBaseOf(naRef obj)
547 return isBaseOf( naGhost_type(obj) );
551 * Convert Nasal object to C++ object. To get a valid object the passed
552 * Nasal objects has to be derived class of the target class (Either
553 * derived in C++ or in Nasal using a 'parents' vector)
555 static pointer fromNasal(naContext c, naRef me)
557 // Check if it's a ghost and if it can be converted
558 if( isBaseOf( naGhost_type(me) ) )
559 return getPtr( naGhost_ptr(me) );
561 // Now if it is derived from a ghost (hash with ghost in parent vector)
562 else if( naIsHash(me) )
564 naRef na_parents = naHash_cget(me, const_cast<char*>("parents"));
565 if( !naIsVector(na_parents) )
567 SG_LOG(SG_NASAL, SG_DEBUG, "Missing 'parents' vector for ghost");
571 typedef std::vector<naRef> naRefs;
572 naRefs parents = from_nasal<naRefs>(c, na_parents);
573 for( naRefs::const_iterator parent = parents.begin();
574 parent != parents.end();
577 pointer ptr = fromNasal(c, *parent);
578 if( get_pointer(ptr) )
591 typedef naGhostType* (*type_checker_t)(const raw_type*);
592 typedef std::vector<type_checker_t> DerivedList;
593 DerivedList _derived_types;
596 * Create a shared pointer on the heap to handle the reference counting
597 * for the passed shared pointer while it is used in Nasal space.
599 static pointer* createInstance(const pointer& ptr)
601 return get_pointer(ptr) ? new pointer(ptr) : 0;
604 static pointer getPtr(void* ptr)
607 return *static_cast<pointer*>(ptr);
612 static raw_type* getRawPtr(void* ptr)
615 return get_pointer(*static_cast<pointer*>(ptr));
620 static raw_type* getRawPtr(const pointer& ptr)
622 return get_pointer(ptr);
625 void addDerived( const internal::GhostMetadata* derived_meta,
626 const type_checker_t& derived_info )
628 GhostMetadata::addDerived(derived_meta);
629 _derived_types.push_back(derived_info);
632 template<class BaseGhost>
634 typename boost::enable_if
635 < boost::is_polymorphic<typename BaseGhost::raw_type>,
638 getTypeFor(const typename BaseGhost::raw_type* base)
640 // Check first if passed pointer can by converted to instance of class
643 < typename BaseGhost::raw_type,
644 typename Ghost::raw_type
646 && dynamic_cast<const typename Ghost::raw_type*>(base) != base )
649 // Now check if we can further downcast to one of our derived classes.
650 for( typename DerivedList::reverse_iterator
651 derived = getSingletonPtr()->_derived_types.rbegin();
652 derived != getSingletonPtr()->_derived_types.rend();
655 naGhostType* ghost_type =
656 (*derived)( static_cast<const typename Ghost::raw_type*>(base) );
661 // If base is not an instance of any derived class, this class has to
662 // be the dynamic type.
663 return &getSingletonPtr()->_ghost_type;
666 template<class BaseGhost>
668 typename boost::disable_if
669 < boost::is_polymorphic<typename BaseGhost::raw_type>,
672 getTypeFor(const typename BaseGhost::raw_type* base)
674 // For non polymorphic classes there is no possibility to get the actual
675 // dynamic type, therefore we can only use its static type.
676 return &BaseGhost::getSingletonPtr()->_ghost_type;
679 static Ghost* getSingletonPtr()
681 return getSingletonHolder().get();
684 static raw_type& requireObject(naContext c, naRef me)
686 raw_type* obj = getRawPtr( fromNasal(c, me) );
687 naGhostType* ghost_type = naGhost_type(me);
693 "method called on object of wrong type: is '%s' expected '%s'",
694 ghost_type ? ghost_type->name : "unknown",
695 getSingletonPtr()->_ghost_type.name
702 getter_t to_getter(Ret (raw_type::*getter)() const)
704 typedef typename boost::call_traits<Ret>::param_type param_type;
705 naRef(*to_nasal_)(naContext, param_type) = &to_nasal;
707 // Getter signature: naRef(naContext, raw_type&)
712 boost::bind(getter, _2)
716 template<class Param>
717 setter_t to_setter(void (raw_type::*setter)(Param))
719 // Setter signature: void(naContext, raw_type&, naRef)
724 boost::bind(from_nasal_ptr<Param>::get(), _1, _3)
730 * Invoke a method which returns a value and convert it to Nasal.
734 typename boost::disable_if<boost::is_void<Ret>, naRef>::type
737 const boost::function<Ret (raw_type&, const CallContext&)>& func,
739 const CallContext& ctx
742 return (*to_nasal_ptr<Ret>::get())(ctx.c, func(obj, ctx));
746 * Invoke a method which returns void and "convert" it to nil.
750 typename boost::enable_if<boost::is_void<Ret>, naRef>::type
753 const boost::function<Ret (raw_type&, const CallContext&)>& func,
755 const CallContext& ctx
763 * Extract argument by index from nasal::CallContext and convert to given
768 typename boost::disable_if<
769 internal::reduced_is_same<Arg, CallContext>,
770 typename from_nasal_ptr<Arg>::return_type
772 arg_from_nasal(const CallContext& ctx, size_t index)
774 return ctx.requireArg<Arg>(index);
778 * Specialization to pass through nasal::CallContext.
782 typename boost::enable_if<
783 internal::reduced_is_same<Arg, CallContext>,
784 typename from_nasal_ptr<Arg>::return_type
786 arg_from_nasal(const CallContext& ctx, size_t)
788 // Either const CallContext& or CallContext, non-const reference
789 // does not make sense.
790 BOOST_STATIC_ASSERT( (!boost::is_same<Arg, CallContext&>::value) );
794 typedef std::auto_ptr<Ghost> GhostPtr;
797 explicit Ghost(const std::string& name):
798 GhostMetadata( name )
800 _ghost_type.destroy = &destroyGhost;
801 _ghost_type.name = _name.c_str();
802 _ghost_type.get_member = &getMember;
803 _ghost_type.set_member = &setMember;
806 static GhostPtr& getSingletonHolder()
808 static GhostPtr instance;
812 static naRef makeGhost(naContext c, void *ptr)
816 // We are wrapping shared pointers to already existing objects which
817 // will then be hold be a new shared pointer. We therefore have to
818 // check for the dynamic type of the object as it might differ from
819 // the passed static type.
820 naGhostType* ghost_type = getTypeFor<Ghost>( getRawPtr(ptr) );
823 return naNewGhost2(c, ghost_type, ptr);
830 static void destroyGhost(void *ptr)
832 delete static_cast<pointer*>(ptr);
836 * Callback for retrieving a ghost member.
838 static const char* getMember(naContext c, void* g, naRef key, naRef* out)
840 const std::string key_str = nasal::from_nasal<std::string>(c, key);
841 if( key_str == "parents" )
843 if( getSingletonPtr()->_parents.empty() )
846 *out = getSingletonPtr()->getParents(c);
850 typename MemberMap::iterator member =
851 getSingletonPtr()->_members.find(key_str);
853 if( member == getSingletonPtr()->_members.end() )
856 if( member->second.func )
857 *out = member->second.func->get_naRef(c);
858 else if( !member->second.getter.empty() )
859 *out = member->second.getter(c, *getRawPtr(g));
861 return "Read-protected member";
867 * Callback for writing to a ghost member.
869 static void setMember(naContext c, void* g, naRef field, naRef val)
871 const std::string key = nasal::from_nasal<std::string>(c, field);
872 typename MemberMap::iterator member =
873 getSingletonPtr()->_members.find(key);
875 if( member == getSingletonPtr()->_members.end() )
876 naRuntimeError(c, "ghost: No such member: %s", key.c_str());
877 else if( member->second.setter.empty() )
878 naRuntimeError(c, "ghost: Write protected member: %s", key.c_str());
879 else if( member->second.func )
880 naRuntimeError(c, "ghost: Write to function: %s", key.c_str());
882 member->second.setter(c, *getRawPtr(g), val);
889 * Convert every shared pointer to a ghost.
891 // Needs to be outside any namespace to mark ADL work
893 typename boost::enable_if<
894 nasal::internal::has_element_type<
895 typename nasal::internal::reduced_type<T>::type
899 to_nasal_helper(naContext c, T ptr)
901 return nasal::Ghost<T>::create(c, ptr);
905 * Convert nasal ghosts/hashes to shared pointer (of a ghost)
908 typename boost::enable_if<
909 nasal::internal::has_element_type<
910 typename nasal::internal::reduced_type<T>::type
914 from_nasal_helper(naContext c, naRef ref, const T*)
916 return nasal::Ghost<T>::fromNasal(c, ref);
919 #endif /* SG_NASAL_GHOST_HXX_ */