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 "from_nasal.hxx"
24 #include "to_nasal.hxx"
26 #include <simgear/debug/logstream.hxx>
28 #include <boost/bind.hpp>
29 #include <boost/call_traits.hpp>
30 #include <boost/function.hpp>
31 #include <boost/lambda/lambda.hpp>
32 #include <boost/mpl/has_xxx.hpp>
33 #include <boost/preprocessor/iteration/iterate.hpp>
34 #include <boost/shared_ptr.hpp>
35 #include <boost/utility/enable_if.hpp>
40 * Bindings between C++ and the Nasal scripting language
48 * Metadata for Ghost object types
54 * Add a nasal base class to the ghost. Will be available in the ghosts
57 void addNasalBase(const naRef& parent)
59 assert( naIsHash(parent) );
60 _parents.push_back(parent);
63 bool isBaseOf(naGhostType* ghost_type) const
65 if( ghost_type == &_ghost_type )
68 for( DerivedList::const_iterator derived = _derived_classes.begin();
69 derived != _derived_classes.end();
72 if( (*derived)->isBaseOf(ghost_type) )
81 typedef std::vector<const GhostMetadata*> DerivedList;
83 const std::string _name;
84 naGhostType _ghost_type;
85 DerivedList _derived_classes;
86 std::vector<naRef> _parents;
88 explicit GhostMetadata(const std::string& name):
94 void addDerived(const GhostMetadata* derived)
97 _derived_classes.push_back(derived);
103 "Ghost::addDerived: " <<_ghost_type.name << " -> " << derived->_name
107 naRef getParents(naContext c)
109 return nasal::to_nasal(c, _parents);
114 * Hold callable method and convert to Nasal function if required.
119 virtual ~MethodHolder() {}
120 virtual naRef get_naRef(naContext c) = 0;
123 BOOST_MPL_HAS_XXX_TRAIT_DEF(element_type)
125 template<class T1, class T2>
126 struct reduced_is_same:
127 public boost::is_same<
128 typename boost::remove_cv<
129 typename boost::remove_reference<T1>::type
137 * Context passed to a function/method being called from Nasal
141 CallContext(naContext c, size_t argc, naRef* args):
147 bool isNumeric(size_t index) const
149 return (index < argc && naIsNum(args[index]));
152 bool isString(size_t index) const
154 return (index < argc && naIsString(args[index]));
157 bool isHash(size_t index) const
159 return (index < argc && naIsHash(args[index]));
162 bool isVector(size_t index) const
164 return (index < argc && naIsVector(args[index]));
167 bool isGhost(size_t index) const
169 return (index < argc && naIsGhost(args[index]));
172 void popFront(size_t num = 1)
181 void popBack(size_t num = 1)
190 * Get the argument with given index if it exists. Otherwise returns the
191 * passed default value.
193 * @tparam T Type of argument (converted using ::from_nasal)
194 * @param index Index of requested argument
195 * @param def Default value returned if too few arguments available
198 typename from_nasal_ptr<T>::return_type
199 getArg(size_t index, const T& def = T()) const
204 return from_nasal<T>(args[index]);
208 * Get the argument with given index. Raises a Nasal runtime error if there
209 * are to few arguments available.
212 typename from_nasal_ptr<T>::return_type
213 requireArg(size_t index) const
216 naRuntimeError(c, "Missing required arg #%d", index);
218 return from_nasal<T>(args[index]);
222 naRef to_nasal(T arg) const
224 return nasal::to_nasal(c, arg);
228 typename from_nasal_ptr<T>::return_type
229 from_nasal(naRef ref) const
231 return (*from_nasal_ptr<T>::get())(c, ref);
240 * Class for exposing C++ objects to Nasal
243 * // Example class to be exposed to Nasal
251 * void doSomethingElse(const nasal::CallContext& ctx);
253 * typedef boost::shared_ptr<MyClass> MyClassPtr;
255 * std::string myOtherFreeMember(int num);
257 * void exposeClasses()
259 * // Register a nasal ghost type for MyClass. This needs to be done only
260 * // once before creating the first ghost instance. The exposed class needs
261 * // to be wrapped inside a shared pointer, eg. boost::shared_ptr.
262 * Ghost<MyClassPtr>::init("MyClass")
263 * // Members can be exposed by getters and setters
264 * .member("x", &MyClass::getX, &MyClass::setX)
265 * // For readonly variables only pass a getter
266 * .member("x_readonly", &MyClass::getX)
267 * // It is also possible to expose writeonly members
268 * .member("x_writeonly", &MyClass::setX)
269 * // Methods can be nearly anything callable and accepting a reference
270 * // to an instance of the class type. (member functions, free functions
271 * // and anything else bindable using boost::function and boost::bind)
272 * .method("myMember", &MyClass::myMember)
273 * .method("doSomething", &MyClass::doSomethingElse)
274 * .method("other", &myOtherFreeMember);
280 public internal::GhostMetadata
282 BOOST_STATIC_ASSERT( internal::has_element_type<T>::value );
285 typedef typename T::element_type raw_type;
287 typedef naRef (raw_type::*member_func_t)(const CallContext&);
288 typedef naRef (*free_func_t)(raw_type&, const CallContext&);
289 typedef boost::function<naRef(naContext, raw_type&)> getter_t;
290 typedef boost::function<void(naContext, raw_type&, naRef)> setter_t;
291 typedef boost::function<naRef(raw_type&, const CallContext&)> method_t;
292 typedef boost::shared_ptr<internal::MethodHolder> MethodHolderPtr;
295 public internal::MethodHolder
302 explicit MethodHolder(const method_t& method):
307 virtual naRef get_naRef(naContext c)
309 if( naIsNil(_naRef) )
311 _naRef = naNewFunc(c, naNewCCodeU(c, &MethodHolder::call, this));
321 static naRef call( naContext c,
327 MethodHolder* holder = static_cast<MethodHolder*>(user_data);
329 naRuntimeError(c, "invalid method holder!");
331 return holder->_method
333 requireObject(c, me),
334 CallContext(c, argc, args)
340 * A ghost member. Can consist either of getter and/or setter functions
341 * for exposing a data variable or a single callable function.
348 member_t( const getter_t& getter,
349 const setter_t& setter,
350 const MethodHolderPtr& func = MethodHolderPtr() ):
356 explicit member_t(const MethodHolderPtr& func):
362 MethodHolderPtr func;
365 typedef std::map<std::string, member_t> MemberMap;
368 * Register a new ghost type.
370 * @note Only intialize each ghost type once!
372 * @param name Descriptive name of the ghost type.
374 static Ghost& init(const std::string& name)
376 assert( !getSingletonPtr() );
378 getSingletonHolder().reset( new Ghost(name) );
379 return *getSingletonPtr();
383 * Register a base class for this ghost. The base class needs to be
384 * registers on its own before it can be used as base class.
386 * @tparam BaseGhost Type of base class already wrapped into Ghost class
390 * Ghost<MyBasePtr>::init("MyBase");
391 * Ghost<MyClassPtr>::init("MyClass")
392 * .bases<Ghost<MyBasePtr> >();
395 template<class BaseGhost>
396 typename boost::enable_if
398 boost::is_base_of<GhostMetadata, BaseGhost>,
405 boost::is_base_of<typename BaseGhost::raw_type, raw_type>::value
408 BaseGhost* base = BaseGhost::getSingletonPtr();
412 // Both ways of retrieving the address of a static member function
413 // should be legal but not all compilers know this.
414 // g++-4.4.7+ has been tested to work with both versions
415 #if defined(GCC_VERSION) && GCC_VERSION < 40407
416 // The old version of g++ used on Jenkins (16.11.2012) only compiles
418 &getTypeFor<BaseGhost>
420 // VS (2008, 2010, ... ?) only allow this version.
421 &Ghost::getTypeFor<BaseGhost>
425 // Replace any getter that is not available in the current class.
426 // TODO check if this is the correct behavior of function overriding
427 for( typename BaseGhost::MemberMap::const_iterator member =
428 base->_members.begin();
429 member != base->_members.end();
432 if( _members.find(member->first) == _members.end() )
433 _members[member->first] = member_t
435 member->second.getter,
436 member->second.setter,
445 * Register a base class for this ghost. The base class needs to be
446 * registers on its own before it can be used as base class.
448 * @tparam Base Type of base class (Base as used in Ghost<BasePtr>)
451 * Ghost<MyBasePtr>::init("MyBase");
452 * Ghost<MyClassPtr>::init("MyClass")
453 * .bases<MyBasePtr>();
457 typename boost::disable_if
459 boost::is_base_of<GhostMetadata, Base>,
466 boost::is_base_of<typename Ghost<Base>::raw_type, raw_type>::value
469 return bases< Ghost<Base> >();
473 * Register an existing Nasal class/hash as base class for this ghost.
475 * @param parent Nasal hash/class
477 Ghost& bases(const naRef& parent)
479 addNasalBase(parent);
484 * Register a member variable by passing a getter and/or setter method.
486 * @param field Name of member
487 * @param getter Getter for variable
488 * @param setter Setter for variable (Pass 0 to prevent write access)
491 template<class Ret, class Param>
492 Ghost& member( const std::string& field,
493 Ret (raw_type::*getter)() const,
494 void (raw_type::*setter)(Param) )
496 return member(field, to_getter(getter), to_setter(setter));
500 * Register a read only member variable.
502 * @param field Name of member
503 * @param getter Getter for variable
506 Ghost& member( const std::string& field,
507 Ret (raw_type::*getter)() const )
509 return member(field, to_getter(getter), setter_t());
513 * Register a write only member variable.
515 * @param field Name of member
516 * @param setter Setter for variable
519 Ghost& member( const std::string& field,
520 void (raw_type::*setter)(Var) )
522 return member(field, getter_t(), to_setter(setter));
526 * Register a member variable by passing a getter and/or setter method.
528 * @param field Name of member
529 * @param getter Getter for variable
530 * @param setter Setter for variable (Pass empty to prevent write access)
533 Ghost& member( const std::string& field,
534 const getter_t& getter,
535 const setter_t& setter = setter_t() )
537 if( !getter.empty() || !setter.empty() )
538 _members[field] = member_t(getter, setter);
544 "Member '" << field << "' requires a getter or setter"
550 * Register anything that accepts an object instance and a
551 * nasal::CallContext and returns naRef as method.
557 * naRef myMethod(const nasal::CallContext& ctx);
560 * Ghost<MyClassPtr>::init("Test")
561 * .method("myMethod", &MyClass::myMethod);
564 Ghost& method(const std::string& name, const method_t& func)
566 _members[name].func.reset( new MethodHolder(func) );
571 * Register anything that accepts an object instance and a
572 * nasal::CallContext whith automatic conversion of the return type to
577 * void doIt(const MyClass& c, const nasal::CallContext& ctx);
579 * Ghost<MyClassPtr>::init("Test")
580 * .method("doIt", &doIt);
586 const std::string& name,
587 const boost::function<Ret (raw_type&, const CallContext&)>& func
590 return method(name, boost::bind(method_invoker<Ret>, func, _1, _2));
593 #define BOOST_PP_ITERATION_LIMITS (0, 9)
594 #define BOOST_PP_FILENAME_1 <simgear/nasal/cppbind/detail/functor_templates.hxx>
595 #include BOOST_PP_ITERATE()
597 // TODO use variadic template when supporting C++11
598 // TODO check if default constructor exists
599 // static naRef create( naContext c )
601 // return makeGhost(c, createInstance());
605 * Create a Nasal instance of this ghost.
607 * @param c Active Nasal context
608 * @param a1 Parameter used for creating new instance
611 static naRef create( naContext c, const A1& a1 )
613 return makeGhost(c, createInstance(a1));
617 * Nasal callback for creating a new instance of this ghost.
619 static naRef f_create(naContext c, naRef me, int argc, naRef* args)
624 static bool isBaseOf(naGhostType* ghost_type)
629 return getSingletonPtr()->GhostMetadata::isBaseOf(ghost_type);
632 static bool isBaseOf(naRef obj)
634 return isBaseOf( naGhost_type(obj) );
638 * Convert Nasal object to C++ object. To get a valid object the passed
639 * Nasal objects has to be derived class of the target class (Either
640 * derived in C++ or in Nasal using a 'parents' vector)
642 static pointer fromNasal(naContext c, naRef me)
644 // Check if it's a ghost and if it can be converted
645 if( isBaseOf( naGhost_type(me) ) )
646 return getPtr( naGhost_ptr(me) );
648 // Now if it is derived from a ghost (hash with ghost in parent vector)
649 else if( naIsHash(me) )
651 naRef na_parents = naHash_cget(me, const_cast<char*>("parents"));
652 if( !naIsVector(na_parents) )
654 SG_LOG(SG_NASAL, SG_DEBUG, "Missing 'parents' vector for ghost");
658 typedef std::vector<naRef> naRefs;
659 naRefs parents = from_nasal<naRefs>(c, na_parents);
660 for( naRefs::const_iterator parent = parents.begin();
661 parent != parents.end();
664 pointer ptr = fromNasal(c, *parent);
678 typedef naGhostType* (*type_checker_t)(const raw_type*);
679 typedef std::vector<type_checker_t> DerivedList;
680 DerivedList _derived_types;
683 * Create a shared pointer on the heap to handle the reference counting
684 * for the passed shared pointer while it is used in Nasal space.
686 static pointer* createInstance(const pointer& ptr)
688 return ptr ? new pointer(ptr) : 0;
691 static pointer getPtr(void* ptr)
694 return *static_cast<pointer*>(ptr);
699 static raw_type* getRawPtr(void* ptr)
702 return static_cast<pointer*>(ptr)->get();
707 static raw_type* getRawPtr(const pointer& ptr)
712 void addDerived( const internal::GhostMetadata* derived_meta,
713 const type_checker_t& derived_info )
715 GhostMetadata::addDerived(derived_meta);
716 _derived_types.push_back(derived_info);
719 template<class BaseGhost>
721 typename boost::enable_if
722 < boost::is_polymorphic<typename BaseGhost::raw_type>,
725 getTypeFor(const typename BaseGhost::raw_type* base)
727 // Check first if passed pointer can by converted to instance of class
730 < typename BaseGhost::raw_type,
731 typename Ghost::raw_type
733 && dynamic_cast<const typename Ghost::raw_type*>(base) != base )
736 // Now check if we can further downcast to one of our derived classes.
737 for( typename DerivedList::reverse_iterator
738 derived = getSingletonPtr()->_derived_types.rbegin();
739 derived != getSingletonPtr()->_derived_types.rend();
742 naGhostType* ghost_type =
743 (*derived)( static_cast<const typename Ghost::raw_type*>(base) );
748 // If base is not an instance of any derived class, this class has to
749 // be the dynamic type.
750 return &getSingletonPtr()->_ghost_type;
753 template<class BaseGhost>
755 typename boost::disable_if
756 < boost::is_polymorphic<typename BaseGhost::raw_type>,
759 getTypeFor(const typename BaseGhost::raw_type* base)
761 // For non polymorphic classes there is no possibility to get the actual
762 // dynamic type, therefore we can only use its static type.
763 return &BaseGhost::getSingletonPtr()->_ghost_type;
766 static Ghost* getSingletonPtr()
768 return getSingletonHolder().get();
771 static raw_type& requireObject(naContext c, naRef me)
773 raw_type* obj = getRawPtr( fromNasal(c, me) );
774 naGhostType* ghost_type = naGhost_type(me);
780 "method called on object of wrong type: is '%s' expected '%s'",
781 ghost_type ? ghost_type->name : "unknown",
782 getSingletonPtr()->_ghost_type.name
789 getter_t to_getter(Ret (raw_type::*getter)() const)
791 typedef typename boost::call_traits<Ret>::param_type param_type;
792 naRef(*to_nasal_)(naContext, param_type) = &to_nasal;
794 // Getter signature: naRef(naContext, raw_type&)
799 boost::bind(getter, _2)
803 template<class Param>
804 setter_t to_setter(void (raw_type::*setter)(Param))
806 // Setter signature: void(naContext, raw_type&, naRef)
811 boost::bind(from_nasal_ptr<Param>::get(), _1, _3)
817 * Invoke a method which returns a value and convert it to Nasal.
821 typename boost::disable_if<boost::is_void<Ret>, naRef>::type
824 const boost::function<Ret (raw_type&, const CallContext&)>& func,
826 const CallContext& ctx
829 return (*to_nasal_ptr<Ret>::get())(ctx.c, func(obj, ctx));
833 * Invoke a method which returns void and "convert" it to nil.
837 typename boost::enable_if<boost::is_void<Ret>, naRef>::type
840 const boost::function<Ret (raw_type&, const CallContext&)>& func,
842 const CallContext& ctx
850 * Extract argument by index from nasal::CallContext and convert to given
855 typename boost::disable_if<
856 internal::reduced_is_same<Arg, CallContext>,
857 typename from_nasal_ptr<Arg>::return_type
859 arg_from_nasal(const CallContext& ctx, size_t index)
861 return ctx.requireArg<Arg>(index);
865 * Specialization to pass through nasal::CallContext.
869 typename boost::enable_if<
870 internal::reduced_is_same<Arg, CallContext>,
871 typename from_nasal_ptr<Arg>::return_type
873 arg_from_nasal(const CallContext& ctx, size_t)
875 // Either const CallContext& or CallContext, non-const reference
876 // does not make sense.
877 BOOST_STATIC_ASSERT( (!boost::is_same<Arg, CallContext&>::value) );
881 typedef std::auto_ptr<Ghost> GhostPtr;
884 explicit Ghost(const std::string& name):
885 GhostMetadata( name )
887 _ghost_type.destroy = &destroyGhost;
888 _ghost_type.name = _name.c_str();
889 _ghost_type.get_member = &getMember;
890 _ghost_type.set_member = &setMember;
893 static GhostPtr& getSingletonHolder()
895 static GhostPtr instance;
899 static naRef makeGhost(naContext c, void *ptr)
903 // We are wrapping shared pointers to already existing objects which
904 // will then be hold be a new shared pointer. We therefore have to
905 // check for the dynamic type of the object as it might differ from
906 // the passed static type.
907 naGhostType* ghost_type = getTypeFor<Ghost>( getRawPtr(ptr) );
910 return naNewGhost2(c, ghost_type, ptr);
917 static void destroyGhost(void *ptr)
919 delete static_cast<pointer*>(ptr);
923 * Callback for retrieving a ghost member.
925 static const char* getMember(naContext c, void* g, naRef key, naRef* out)
927 const std::string key_str = nasal::from_nasal<std::string>(c, key);
928 if( key_str == "parents" )
930 if( getSingletonPtr()->_parents.empty() )
933 *out = getSingletonPtr()->getParents(c);
937 typename MemberMap::iterator member =
938 getSingletonPtr()->_members.find(key_str);
940 if( member == getSingletonPtr()->_members.end() )
943 if( member->second.func )
944 *out = member->second.func->get_naRef(c);
945 else if( !member->second.getter.empty() )
946 *out = member->second.getter(c, *getRawPtr(g));
948 return "Read-protected member";
954 * Callback for writing to a ghost member.
956 static void setMember(naContext c, void* g, naRef field, naRef val)
958 const std::string key = nasal::from_nasal<std::string>(c, field);
959 typename MemberMap::iterator member =
960 getSingletonPtr()->_members.find(key);
962 if( member == getSingletonPtr()->_members.end() )
963 naRuntimeError(c, "ghost: No such member: %s", key.c_str());
964 else if( member->second.setter.empty() )
965 naRuntimeError(c, "ghost: Write protected member: %s", key.c_str());
966 else if( member->second.func )
967 naRuntimeError(c, "ghost: Write to function: %s", key.c_str());
969 member->second.setter(c, *getRawPtr(g), val);
975 #endif /* SG_NASAL_GHOST_HXX_ */