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 inline T* get_pointer(SGWeakPtr<T> const& p)
50 return p.lock().get();
54 inline T* get_pointer(osg::observer_ptr<T> const& p)
61 // Both ways of retrieving the address of a static member function
62 // should be legal but not all compilers know this.
63 // g++-4.4.7+ has been tested to work with both versions
64 #if defined(SG_GCC_VERSION) && SG_GCC_VERSION < 40407
65 // The old version of g++ used on Jenkins (16.11.2012) only compiles
67 # define SG_GET_TEMPLATE_MEMBER(type, member) &member
69 // VS (2008, 2010, ... ?) only allow this version.
70 # define SG_GET_TEMPLATE_MEMBER(type, member) &type::member
74 * Bindings between C++ and the Nasal scripting language
81 * Metadata for Ghost object types
87 * Add a nasal base class to the ghost. Will be available in the ghosts
90 void addNasalBase(const naRef& parent)
92 assert( naIsHash(parent) );
93 _parents.push_back(parent);
96 bool isBaseOf(naGhostType* ghost_type, bool& is_weak) const
98 if( ghost_type == _ghost_type_strong_ptr )
104 if( ghost_type == _ghost_type_weak_ptr )
110 for( DerivedList::const_iterator derived = _derived_classes.begin();
111 derived != _derived_classes.end();
114 if( (*derived)->isBaseOf(ghost_type, is_weak) )
123 typedef std::vector<const GhostMetadata*> DerivedList;
125 const std::string _name_strong,
127 const naGhostType *_ghost_type_strong_ptr,
128 *_ghost_type_weak_ptr;
129 DerivedList _derived_classes;
130 std::vector<naRef> _parents;
132 GhostMetadata( const std::string& name,
133 const naGhostType* ghost_type_strong,
134 const naGhostType* ghost_type_weak ):
136 _name_weak(name + " (weak ref)"),
137 _ghost_type_strong_ptr(ghost_type_strong),
138 _ghost_type_weak_ptr(ghost_type_weak)
143 void addDerived(const GhostMetadata* derived)
146 _derived_classes.push_back(derived);
152 "Ghost::addDerived: " << _name_strong << " -> "
153 << derived->_name_strong
157 naRef getParents(naContext c)
159 return nasal::to_nasal(c, _parents);
164 * Hold callable method and convert to Nasal function if required.
167 public SGWeakReferenced
170 virtual ~MethodHolder() {}
172 naRef get_naRef(naContext c)
175 _obj.reset(createNasalObject(c));
176 return _obj.get_naRef();
182 virtual naRef createNasalObject(naContext c) = 0;
185 BOOST_MPL_HAS_XXX_TRAIT_DEF(element_type)
190 typedef typename boost::remove_cv<
191 typename boost::remove_reference<T>::type
195 template<class T1, class T2>
196 struct reduced_is_same:
197 public boost::is_same<typename reduced_type<T1>::type, T2>
201 typedef SGSharedPtr<internal::MethodHolder> MethodHolderPtr;
202 typedef SGWeakPtr<internal::MethodHolder> MethodHolderWeakPtr;
205 * Class for exposing C++ objects to Nasal
208 * // Example class to be exposed to Nasal
216 * void doSomethingElse(const nasal::CallContext& ctx);
218 * typedef boost::shared_ptr<MyClass> MyClassPtr;
220 * std::string myOtherFreeMember(int num);
222 * void exposeClasses()
224 * // Register a nasal ghost type for MyClass. This needs to be done only
225 * // once before creating the first ghost instance. The exposed class needs
226 * // to be wrapped inside a shared pointer, eg. boost::shared_ptr.
227 * Ghost<MyClassPtr>::init("MyClass")
228 * // Members can be exposed by getters and setters
229 * .member("x", &MyClass::getX, &MyClass::setX)
230 * // For readonly variables only pass a getter
231 * .member("x_readonly", &MyClass::getX)
232 * // It is also possible to expose writeonly members
233 * .member("x_writeonly", &MyClass::setX)
234 * // Methods can be nearly anything callable and accepting a reference
235 * // to an instance of the class type. (member functions, free functions
236 * // and anything else bindable using boost::function and boost::bind)
237 * .method("myMember", &MyClass::myMember)
238 * .method("doSomething", &MyClass::doSomethingElse)
239 * .method("other", &myOtherFreeMember);
245 public internal::GhostMetadata
247 // Shared pointer required for Ghost (no weak pointer!)
248 BOOST_STATIC_ASSERT((shared_ptr_traits<T>::is_strong::value));
251 typedef typename T::element_type raw_type;
252 typedef typename shared_ptr_traits<T>::strong_ref strong_ref;
253 typedef typename shared_ptr_traits<T>::weak_ref weak_ref;
254 typedef naRef (raw_type::*member_func_t)(const CallContext&);
255 typedef naRef (*free_func_t)(raw_type&, const CallContext&);
256 typedef boost::function<naRef(raw_type&, naContext)> getter_t;
257 typedef boost::function<void( raw_type&, naContext, naRef)> setter_t;
258 typedef boost::function<naRef(raw_type&, const CallContext&)> method_t;
259 typedef boost::function<bool( raw_type&,
262 naRef& )> fallback_getter_t;
263 typedef boost::function<bool( raw_type&,
266 naRef )> fallback_setter_t;
269 public internal::MethodHolder
272 explicit MethodHolder(const method_t& method):
278 typedef SGSharedPtr<MethodHolder> SharedPtr;
279 typedef SGWeakPtr<MethodHolder> WeakPtr;
283 virtual naRef createNasalObject(naContext c)
295 static void destroyHolder(void* user_data)
297 delete static_cast<WeakPtr*>(user_data);
300 static naRef call( naContext c,
306 WeakPtr* holder_weak = static_cast<WeakPtr*>(user_data);
308 naRuntimeError(c, "invalid method holder!");
312 SharedPtr holder = holder_weak->lock();
314 throw std::runtime_error("holder has expired");
316 return holder->_method
318 requireObject(c, me),
319 CallContext(c, argc, args)
322 catch(const std::exception& ex)
324 naRuntimeError(c, "Fatal error in method call: %s", ex.what());
328 naRuntimeError(c, "Unknown exception in method call.");
336 * A ghost member. Can consist either of getter and/or setter functions
337 * for exposing a data variable or a single callable function.
344 member_t( const getter_t& getter,
345 const setter_t& setter,
346 const MethodHolderPtr& func = MethodHolderPtr() ):
352 explicit member_t(const MethodHolderPtr& func):
358 MethodHolderPtr func;
361 typedef std::map<std::string, member_t> MemberMap;
364 * Register a new ghost type.
366 * @note Only intialize each ghost type once!
368 * @param name Descriptive name of the ghost type.
370 static Ghost& init(const std::string& name)
372 getSingletonHolder().reset( new Ghost(name) );
373 return *getSingletonPtr();
377 * Check whether ghost type has already been initialized.
381 return getSingletonPtr();
385 * Register a base class for this ghost. The base class needs to be
386 * registers on its own before it can be used as base class.
388 * @tparam BaseGhost Type of base class already wrapped into Ghost class
392 * Ghost<MyBasePtr>::init("MyBase");
393 * Ghost<MyClassPtr>::init("MyClass")
394 * .bases<Ghost<MyBasePtr> >();
397 template<class BaseGhost>
398 typename boost::enable_if
400 boost::is_base_of<GhostMetadata, BaseGhost>,
407 boost::is_base_of<typename BaseGhost::raw_type, raw_type>::value
410 BaseGhost* base = BaseGhost::getSingletonPtr();
413 SG_GET_TEMPLATE_MEMBER(Ghost, getTypeFor<BaseGhost>)
416 // Replace any getter that is not available in the current class.
417 // TODO check if this is the correct behavior of function overriding
418 for( typename BaseGhost::MemberMap::const_iterator member =
419 base->_members.begin();
420 member != base->_members.end();
423 if( _members.find(member->first) == _members.end() )
424 _members[member->first] = member_t
426 member->second.getter,
427 member->second.setter,
432 if( !_fallback_setter )
433 _fallback_setter = base->_fallback_setter;
434 if( !_fallback_getter )
435 _fallback_getter = base->_fallback_getter;
441 * Register a base class for this ghost. The base class needs to be
442 * registers on its own before it can be used as base class.
444 * @tparam Base Type of base class (Base as used in Ghost<BasePtr>)
447 * Ghost<MyBasePtr>::init("MyBase");
448 * Ghost<MyClassPtr>::init("MyClass")
449 * .bases<MyBasePtr>();
453 typename boost::disable_if
455 boost::is_base_of<GhostMetadata, Base>,
462 boost::is_base_of<typename Ghost<Base>::raw_type, raw_type>::value
465 return bases< Ghost<Base> >();
469 * Register an existing Nasal class/hash as base class for this ghost.
471 * @param parent Nasal hash/class
473 Ghost& bases(const naRef& parent)
475 addNasalBase(parent);
480 * Register a member variable by passing a getter and/or setter method.
482 * @param field Name of member
483 * @param getter Getter for variable
484 * @param setter Setter for variable (Pass 0 to prevent write access)
487 template<class Ret, class Param>
488 Ghost& member( const std::string& field,
489 Ret (raw_type::*getter)() const,
490 void (raw_type::*setter)(Param) )
492 return member(field, to_getter(getter), to_setter(setter));
495 template<class Param>
496 Ghost& member( const std::string& field,
497 const getter_t& getter,
498 void (raw_type::*setter)(Param) )
500 return member(field, getter, to_setter(setter));
504 Ghost& member( const std::string& field,
505 Ret (raw_type::*getter)() const,
506 const setter_t& setter )
508 return member(field, to_getter(getter), setter);
512 * Register a read only member variable.
514 * @param field Name of member
515 * @param getter Getter for variable
518 Ghost& member( const std::string& field,
519 Ret (raw_type::*getter)() const )
521 return member(field, to_getter(getter), setter_t());
524 Ghost& member( const std::string& field,
525 naRef (*getter)(const raw_type&, naContext) )
527 return member(field, getter_t(getter), setter_t());
530 Ghost& member( const std::string& field,
531 naRef (*getter)(raw_type&, naContext) )
533 return member(field, getter_t(getter), setter_t());
537 * Register a write only member variable.
539 * @param field Name of member
540 * @param setter Setter for variable
543 Ghost& member( const std::string& field,
544 void (raw_type::*setter)(Var) )
546 return member(field, getter_t(), to_setter(setter));
549 Ghost& member( const std::string& field,
550 void (*setter)(raw_type&, naContext, naRef) )
552 return member(field, getter_t(), setter_t(setter));
555 Ghost& member( const std::string& field,
556 const setter_t& setter )
558 return member(field, getter_t(), setter);
562 * Register a member variable by passing a getter and/or setter method.
564 * @param field Name of member
565 * @param getter Getter for variable
566 * @param setter Setter for variable (Pass empty to prevent write access)
569 Ghost& member( const std::string& field,
570 const getter_t& getter,
571 const setter_t& setter = setter_t() )
573 if( !getter.empty() || !setter.empty() )
574 _members[field] = member_t(getter, setter);
580 "Member '" << field << "' requires a getter or setter"
586 * Register a function which is called upon retrieving an unknown member
589 Ghost& _get(const fallback_getter_t& getter)
591 _fallback_getter = getter;
596 * Register a function which is called upon retrieving an unknown member
597 * of this ghost, and convert it to the given @a Param type.
599 template<class Param>
600 Ghost& _get( const boost::function<bool ( raw_type&,
604 return _get(boost::bind(
605 convert_param_invoker<Param>, getter, _1, _2, _3, _4
610 * Register a method which is called upon retrieving an unknown member of
617 * bool getMember( const std::string& key,
618 * std::string& value_out ) const;
621 * Ghost<MyClassPtr>::init("Test")
622 * ._get(&MyClass::getMember);
625 template<class Param>
626 Ghost& _get(bool (raw_type::*getter)(const std::string&, Param&) const)
629 boost::function<bool (raw_type&, const std::string&, Param&)>(getter)
634 * Register a method which is called upon retrieving an unknown member of
641 * bool getMember( naContext c,
642 * const std::string& key,
643 * naRef& value_out );
646 * Ghost<MyClassPtr>::init("Test")
647 * ._get(&MyClass::getMember);
650 Ghost& _get(bool (raw_type::*getter)( naContext,
654 return _get( fallback_getter_t(getter) );
658 * Register a function which is called upon setting an unknown member of
661 Ghost& _set(const fallback_setter_t& setter)
663 _fallback_setter = setter;
668 * Register a function which is called upon setting an unknown member of
669 * this ghost, and convert it to the given @a Param type.
671 template<class Param>
672 Ghost& _set(const boost::function<bool ( raw_type&,
676 // Setter signature: bool( raw_type&,
678 // const std::string&,
680 return _set(boost::bind(
684 boost::bind(from_nasal_ptr<Param>::get(), _2, _4)
689 * Register a method which is called upon setting an unknown member of
696 * bool setMember( const std::string& key,
697 * const std::string& value );
700 * Ghost<MyClassPtr>::init("Test")
701 * ._set(&MyClass::setMember);
704 template<class Param>
705 Ghost& _set(bool (raw_type::*setter)(const std::string&, Param))
708 boost::function<bool (raw_type&, const std::string&, Param)>(setter)
713 * Register a method which is called upon setting an unknown member of
720 * bool setMember( naContext c,
721 * const std::string& key,
725 * Ghost<MyClassPtr>::init("Test")
726 * ._set(&MyClass::setMember);
729 Ghost& _set(bool (raw_type::*setter)( naContext,
733 return _set( fallback_setter_t(setter) );
737 * Register anything that accepts an object instance and a
738 * nasal::CallContext and returns naRef as method.
744 * naRef myMethod(const nasal::CallContext& ctx);
747 * Ghost<MyClassPtr>::init("Test")
748 * .method("myMethod", &MyClass::myMethod);
751 Ghost& method(const std::string& name, const method_t& func)
753 _members[name].func = new MethodHolder(func);
758 * Register anything that accepts an object instance and a
759 * nasal::CallContext whith automatic conversion of the return type to
764 * void doIt(const MyClass& c, const nasal::CallContext& ctx);
766 * Ghost<MyClassPtr>::init("Test")
767 * .method("doIt", &doIt);
773 const std::string& name,
774 const boost::function<Ret (raw_type&, const CallContext&)>& func
777 return method(name, boost::bind(method_invoker<Ret>, func, _1, _2));
780 // Build dependency for CMake, gcc, etc.
781 #define SG_DONT_DO_ANYTHING
782 # include <simgear/nasal/cppbind/detail/functor_templates.hxx>
783 #undef SG_DONT_DO_ANYTHING
785 #define BOOST_PP_ITERATION_LIMITS (0, 9)
786 #define BOOST_PP_FILENAME_1 <simgear/nasal/cppbind/detail/functor_templates.hxx>
787 #include BOOST_PP_ITERATE()
790 * Create a shared pointer on the heap to handle the reference counting
791 * for the passed shared pointer while it is used in Nasal space.
793 template<class RefType>
795 typename boost::enable_if_c<
796 boost::is_same<RefType, strong_ref>::value
797 || boost::is_same<RefType, weak_ref>::value,
800 makeGhost(naContext c, RefType const& ref_ptr)
802 strong_ref ref(ref_ptr);
803 raw_type* ptr = get_pointer(ref_ptr);
807 // We are wrapping shared pointers to already existing objects which
808 // will then be hold be a new shared pointer. We therefore have to
809 // check for the dynamic type of the object as it might differ from
810 // the passed static type.
811 naGhostType* ghost_type =
812 getTypeFor<Ghost>(ptr, shared_ptr_traits<RefType>::is_strong::value);
817 return naNewGhost2(c, ghost_type, new RefType(ref_ptr));
821 * Convert Nasal object to C++ object. To get a valid object the passed
822 * Nasal objects has to be derived class of the target class (Either
823 * derived in C++ or in Nasal using a 'parents' vector)
825 template<class RefType>
827 typename boost::enable_if_c<
828 boost::is_same<RefType, strong_ref>::value
829 || boost::is_same<RefType, weak_ref>::value,
832 fromNasal(naContext c, naRef me)
834 bool is_weak = false;
836 // Check if it's a ghost and if it can be converted
837 if( isBaseOf(naGhost_type(me), is_weak) )
839 void* ghost = naGhost_ptr(me);
840 return is_weak ? getPtr<RefType, true>(ghost)
841 : getPtr<RefType, false>(ghost);
844 // Now if it is derived from a ghost (hash with ghost in parent vector)
845 else if( naIsHash(me) )
847 naRef na_parents = naHash_cget(me, const_cast<char*>("parents"));
848 if( !naIsVector(na_parents) )
851 typedef std::vector<naRef> naRefs;
852 naRefs parents = from_nasal<naRefs>(c, na_parents);
853 for( naRefs::const_iterator parent = parents.begin();
854 parent != parents.end();
857 RefType ptr = fromNasal<RefType>(c, *parent);
858 if( get_pointer(ptr) )
866 static bool isBaseOf(naRef obj)
869 return isBaseOf(naGhost_type(obj), is_weak);
877 static naGhostType _ghost_type_strong, //!< Stored as shared pointer
878 _ghost_type_weak; //!< Stored as weak shared pointer
880 typedef naGhostType* (*type_checker_t)(const raw_type*, bool);
881 typedef std::vector<type_checker_t> DerivedList;
882 DerivedList _derived_types;
884 static bool isBaseOf(naGhostType* ghost_type, bool& is_weak)
886 if( !ghost_type || !getSingletonPtr() )
889 return getSingletonPtr()->GhostMetadata::isBaseOf(ghost_type, is_weak);
892 static bool isBaseOf(naRef obj, bool& is_weak)
894 return isBaseOf(naGhost_type(obj), is_weak);
897 template<class RefPtr, bool is_weak>
899 typename boost::enable_if_c<
906 return RefPtr(*static_cast<strong_ref*>(ptr));
911 template<class RefPtr, bool is_weak>
913 typename boost::enable_if_c<
914 is_weak && supports_weak_ref<T>::value,
920 return RefPtr(*static_cast<weak_ref*>(ptr));
925 template<class RefPtr, bool is_weak>
927 typename boost::enable_if_c<
928 is_weak && !supports_weak_ref<T>::value,
936 void addDerived( const internal::GhostMetadata* derived_meta,
937 type_checker_t derived_type_checker )
939 GhostMetadata::addDerived(derived_meta);
940 _derived_types.push_back(derived_type_checker);
943 template<class BaseGhost>
945 typename boost::enable_if
946 < boost::is_polymorphic<typename BaseGhost::raw_type>,
949 getTypeFor(const typename BaseGhost::raw_type* base, bool strong)
951 // Check first if passed pointer can by converted to instance of class
954 < typename BaseGhost::raw_type,
955 typename Ghost::raw_type
957 && dynamic_cast<const typename Ghost::raw_type*>(base) != base )
960 if( !getSingletonPtr() )
966 "Ghost::getTypeFor: can not get type for unregistered ghost"
971 // Now check if we can further downcast to one of our derived classes.
972 for( typename DerivedList::reverse_iterator
973 derived = getSingletonPtr()->_derived_types.rbegin();
974 derived != getSingletonPtr()->_derived_types.rend();
977 naGhostType* ghost_type =
979 static_cast<const typename Ghost::raw_type*>(base),
987 // If base is not an instance of any derived class, this class has to
988 // be the dynamic type.
990 ? &_ghost_type_strong
994 template<class BaseGhost>
996 typename boost::disable_if
997 < boost::is_polymorphic<typename BaseGhost::raw_type>,
1000 getTypeFor(const typename BaseGhost::raw_type* base, bool strong)
1002 // For non polymorphic classes there is no possibility to get the actual
1003 // dynamic type, therefore we can only use its static type.
1005 ? &BaseGhost::_ghost_type_strong
1006 : &BaseGhost::_ghost_type_weak;
1009 static Ghost* getSingletonPtr()
1011 return getSingletonHolder().get();
1014 static raw_type& requireObject(naContext c, naRef me)
1016 raw_type* obj = get_pointer( fromNasal<strong_ref>(c, me) );
1020 naGhostType* ghost_type = naGhost_type(me);
1024 "method called on object of wrong type: is '%s' expected '%s'",
1025 naIsNil(me) ? "nil" : (ghost_type ? ghost_type->name : "unknown"),
1026 _ghost_type_strong.name
1034 getter_t to_getter(Ret (raw_type::*getter)() const)
1036 typedef typename boost::call_traits<Ret>::param_type param_type;
1037 naRef(*to_nasal_)(naContext, param_type) = &to_nasal;
1039 // Getter signature: naRef(raw_type&, naContext)
1044 boost::bind(getter, _1)
1048 template<class Param>
1049 setter_t to_setter(void (raw_type::*setter)(Param))
1051 // Setter signature: void(raw_type&, naContext, naRef)
1056 boost::bind(from_nasal_ptr<Param>::get(), _2, _3)
1061 * Invoke a method which writes the converted parameter to a reference
1063 template<class Param>
1065 bool convert_param_invoker
1067 const boost::function<bool ( raw_type&,
1072 const std::string& key,
1077 if( !func(obj, key, p) )
1080 out = to_nasal(c, p);
1085 * Invoke a method which returns a value and convert it to Nasal.
1089 typename boost::disable_if<boost::is_void<Ret>, naRef>::type
1092 const boost::function<Ret (raw_type&, const CallContext&)>& func,
1094 const CallContext& ctx
1097 return (*to_nasal_ptr<Ret>::get())(ctx.c, func(obj, ctx));
1101 * Invoke a method which returns void and "convert" it to nil.
1105 typename boost::enable_if<boost::is_void<Ret>, naRef>::type
1108 const boost::function<Ret (raw_type&, const CallContext&)>& func,
1110 const CallContext& ctx
1118 * Extract argument by index from nasal::CallContext and convert to given
1123 typename boost::disable_if<
1124 internal::reduced_is_same<Arg, CallContext>,
1125 typename from_nasal_ptr<Arg>::return_type
1127 arg_from_nasal(const CallContext& ctx, size_t index)
1129 return ctx.requireArg<Arg>(index);
1133 * Specialization to pass through nasal::CallContext.
1137 typename boost::enable_if<
1138 internal::reduced_is_same<Arg, CallContext>,
1139 typename from_nasal_ptr<Arg>::return_type
1141 arg_from_nasal(const CallContext& ctx, size_t)
1143 // Either const CallContext& or CallContext, non-const reference
1144 // does not make sense.
1145 BOOST_STATIC_ASSERT( (!boost::is_same<Arg, CallContext&>::value) );
1149 typedef std::auto_ptr<Ghost> GhostPtr;
1151 fallback_getter_t _fallback_getter;
1152 fallback_setter_t _fallback_setter;
1154 explicit Ghost(const std::string& name):
1155 GhostMetadata( name,
1156 &_ghost_type_strong,
1157 supports_weak_ref<T>::value ? &_ghost_type_weak : NULL )
1159 _ghost_type_strong.destroy =
1160 SG_GET_TEMPLATE_MEMBER(Ghost, destroy<strong_ref>);
1161 _ghost_type_strong.name = _name_strong.c_str();
1162 _ghost_type_strong.get_member =
1163 SG_GET_TEMPLATE_MEMBER(Ghost, getMember<false>);
1164 _ghost_type_strong.set_member =
1165 SG_GET_TEMPLATE_MEMBER(Ghost, setMember<false>);
1167 _ghost_type_weak.destroy =
1168 SG_GET_TEMPLATE_MEMBER(Ghost, destroy<weak_ref>);
1169 _ghost_type_weak.name = _name_weak.c_str();
1171 if( supports_weak_ref<T>::value )
1173 _ghost_type_weak.get_member =
1174 SG_GET_TEMPLATE_MEMBER(Ghost, getMember<true>);
1175 _ghost_type_weak.set_member =
1176 SG_GET_TEMPLATE_MEMBER(Ghost, setMember<true>);
1180 _ghost_type_weak.get_member = 0;
1181 _ghost_type_weak.set_member = 0;
1185 static GhostPtr& getSingletonHolder()
1187 static GhostPtr instance;
1191 template<class Type>
1192 static void destroy(void *ptr)
1194 delete static_cast<Type*>(ptr);
1198 * Callback for retrieving a ghost member.
1200 static const char* getMember( naContext c,
1205 const std::string key_str = nasal::from_nasal<std::string>(c, key);
1206 // TODO merge instance parents with static class parents
1207 // if( key_str == "parents" )
1209 // if( getSingletonPtr()->_parents.empty() )
1212 // *out = getSingletonPtr()->getParents(c);
1216 typename MemberMap::iterator member =
1217 getSingletonPtr()->_members.find(key_str);
1219 if( member == getSingletonPtr()->_members.end() )
1221 fallback_getter_t fallback_get = getSingletonPtr()->_fallback_getter;
1223 || !fallback_get(obj, c, key_str, *out) )
1226 else if( member->second.func )
1227 *out = member->second.func->get_naRef(c);
1228 else if( !member->second.getter.empty() )
1229 *out = member->second.getter(obj, c);
1231 return "Read-protected member";
1236 template<bool is_weak>
1237 static const char* getMember( naContext c,
1242 strong_ref const& ptr = getPtr<strong_ref, is_weak>(ghost);
1243 return getMember(c, *get_pointer(ptr), key, out);
1247 * Callback for writing to a ghost member.
1249 static void setMember( naContext c,
1254 const std::string key = nasal::from_nasal<std::string>(c, field);
1255 const MemberMap& members = getSingletonPtr()->_members;
1257 typename MemberMap::const_iterator member = members.find(key);
1258 if( member == members.end() )
1260 fallback_setter_t fallback_set = getSingletonPtr()->_fallback_setter;
1262 naRuntimeError(c, "ghost: No such member: %s", key.c_str());
1263 else if( !fallback_set(obj, c, key, val) )
1264 naRuntimeError(c, "ghost: Failed to write (_set: %s)", key.c_str());
1266 else if( member->second.setter.empty() )
1267 naRuntimeError(c, "ghost: Write protected member: %s", key.c_str());
1268 else if( member->second.func )
1269 naRuntimeError(c, "ghost: Write to function: %s", key.c_str());
1271 member->second.setter(obj, c, val);
1274 template<bool is_weak>
1275 static void setMember( naContext c,
1280 strong_ref const& ptr = getPtr<strong_ref, is_weak>(ghost);
1281 return setMember(c, *get_pointer(ptr), field, val);
1286 naGhostType Ghost<T>::_ghost_type_strong;
1288 naGhostType Ghost<T>::_ghost_type_weak;
1290 } // namespace nasal
1292 // Needs to be outside any namespace to make ADL work
1294 * Convert every shared pointer to a ghost.
1297 typename boost::enable_if<
1298 nasal::internal::has_element_type<
1299 typename nasal::internal::reduced_type<T>::type
1303 to_nasal_helper(naContext c, T ptr)
1305 typedef typename nasal::shared_ptr_traits<T>::strong_ref strong_ref;
1306 return nasal::Ghost<strong_ref>::makeGhost(c, ptr);
1310 * Convert nasal ghosts/hashes to shared pointer (of a ghost).
1313 typename boost::enable_if<
1314 nasal::internal::has_element_type<
1315 typename nasal::internal::reduced_type<T>::type
1319 from_nasal_helper(naContext c, naRef ref, const T*)
1321 typedef typename nasal::shared_ptr_traits<T>::strong_ref strong_ref;
1322 return nasal::Ghost<strong_ref>::template fromNasal<T>(c, ref);
1326 * Convert any pointer to a SGReference based object to a ghost.
1329 typename boost::enable_if_c<
1330 boost::is_base_of<SGReferenced, T>::value
1331 || boost::is_base_of<SGWeakReferenced, T>::value,
1334 to_nasal_helper(naContext c, T* ptr)
1336 return nasal::Ghost<SGSharedPtr<T> >::makeGhost(c, SGSharedPtr<T>(ptr));
1340 * Convert nasal ghosts/hashes to pointer (of a SGReference based ghost).
1343 typename boost::enable_if_c<
1346 typename boost::remove_pointer<T>::type
1348 || boost::is_base_of<
1350 typename boost::remove_pointer<T>::type
1354 from_nasal_helper(naContext c, naRef ref, const T*)
1356 typedef SGSharedPtr<typename boost::remove_pointer<T>::type> TypeRef;
1357 return nasal::Ghost<TypeRef>::template fromNasal<TypeRef>(c, ref).release();
1360 #endif /* SG_NASAL_GHOST_HXX_ */