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();
56 inline T* get_pointer(observer_ptr<T> const& p)
65 inline typename boost::enable_if<
74 // Both ways of retrieving the address of a static member function
75 // should be legal but not all compilers know this.
76 // g++-4.4.7+ has been tested to work with both versions
77 #if defined(SG_GCC_VERSION) && SG_GCC_VERSION < 40407
78 // The old version of g++ used on Jenkins (16.11.2012) only compiles
80 # define SG_GET_TEMPLATE_MEMBER(type, member) &member
82 // With old g++ on Jenkins (21.07.2014), ADL for static_pointer_cast does not
84 using boost::static_pointer_cast;
85 using osg::static_pointer_cast;
87 // VS (2008, 2010, ... ?) only allow this version.
88 # define SG_GET_TEMPLATE_MEMBER(type, member) &type::template member
92 * Bindings between C++ and the Nasal scripting language
99 * Metadata for Ghost object types
104 typedef void(*Deleter)(void*);
105 typedef std::vector<std::pair<Deleter, void*> > DestroyList;
107 static DestroyList _destroy_list;
110 * Add a nasal base class to the ghost. Will be available in the ghosts
113 void addNasalBase(const naRef& parent);
115 bool isInstance(naGhostType* ghost_type, bool& is_weak) const;
119 const std::string _name_strong,
121 const naGhostType *_ghost_type_strong_ptr,
122 *_ghost_type_weak_ptr;
123 std::vector<naRef> _parents;
125 GhostMetadata( const std::string& name,
126 const naGhostType* ghost_type_strong,
127 const naGhostType* ghost_type_weak );
129 void addDerived(const GhostMetadata* derived);
131 naRef getParents(naContext c);
135 * Hold callable method and convert to Nasal function if required.
138 public SGWeakReferenced
141 virtual ~MethodHolder() {}
143 naRef get_naRef(naContext c)
146 _obj.reset(createNasalObject(c));
147 return _obj.get_naRef();
153 virtual naRef createNasalObject(naContext c) = 0;
156 BOOST_MPL_HAS_XXX_TRAIT_DEF(element_type)
161 typedef typename boost::remove_cv<
162 typename boost::remove_reference<T>::type
166 template<class T1, class T2>
167 struct reduced_is_same:
168 public boost::is_same<typename reduced_type<T1>::type, T2>
172 /** @brief Destroy all ghost queued for deletion.
174 * This needs can not be done directly during garbage collection, as
175 * destructors may execute Nasal code which requires creating new Nasal
176 * contexts. Creating a Nasal context during garbage collection is not
177 * possible and leads to a dead lock.
179 void ghostProcessDestroyList();
181 typedef SGSharedPtr<internal::MethodHolder> MethodHolderPtr;
182 typedef SGWeakPtr<internal::MethodHolder> MethodHolderWeakPtr;
184 // Dummy template to create shorter and easy to understand compile errors if
185 // trying to wrap the wrong type as a Ghost.
186 template<class T, class Enable = void>
190 BOOST_STATIC_ASSERT(("Ghost can only wrap shared pointer!"
191 && is_strong_ref<T>::value
194 static Ghost& init(const std::string& name);
195 static bool isInit();
199 * Class for exposing C++ objects to Nasal
202 * // Example class to be exposed to Nasal
210 * void doSomethingElse(const nasal::CallContext& ctx);
212 * typedef boost::shared_ptr<MyClass> MyClassPtr;
214 * std::string myOtherFreeMember(int num);
216 * void exposeClasses()
218 * // Register a nasal ghost type for MyClass. This needs to be done only
219 * // once before creating the first ghost instance. The exposed class needs
220 * // to be wrapped inside a shared pointer, eg. boost::shared_ptr.
221 * Ghost<MyClassPtr>::init("MyClass")
222 * // Members can be exposed by getters and setters
223 * .member("x", &MyClass::getX, &MyClass::setX)
224 * // For readonly variables only pass a getter
225 * .member("x_readonly", &MyClass::getX)
226 * // It is also possible to expose writeonly members
227 * .member("x_writeonly", &MyClass::setX)
228 * // Methods can be nearly anything callable and accepting a reference
229 * // to an instance of the class type. (member functions, free functions
230 * // and anything else bindable using boost::function and boost::bind)
231 * .method("myMember", &MyClass::myMember)
232 * .method("doSomething", &MyClass::doSomethingElse)
233 * .method("other", &myOtherFreeMember);
238 class Ghost<T, typename boost::enable_if<is_strong_ref<T> >::type>:
239 public internal::GhostMetadata
241 // Shared pointer required for Ghost (no weak pointer!)
242 BOOST_STATIC_ASSERT((is_strong_ref<T>::value));
245 typedef typename T::element_type raw_type;
246 typedef typename shared_ptr_traits<T>::strong_ref strong_ref;
247 typedef typename shared_ptr_traits<T>::weak_ref weak_ref;
248 typedef naRef (raw_type::*member_func_t)(const CallContext&);
249 typedef naRef (*free_func_t)(raw_type&, const CallContext&);
250 typedef boost::function<naRef(raw_type&, naContext)> getter_t;
251 typedef boost::function<void( raw_type&, naContext, naRef)> setter_t;
252 typedef boost::function<naRef(raw_type&, const CallContext&)> method_t;
253 typedef boost::function<bool( raw_type&,
256 naRef& )> fallback_getter_t;
257 typedef boost::function<bool( raw_type&,
260 naRef )> fallback_setter_t;
263 public internal::MethodHolder
266 explicit MethodHolder(const method_t& method):
272 typedef SGSharedPtr<MethodHolder> SharedPtr;
273 typedef SGWeakPtr<MethodHolder> WeakPtr;
277 virtual naRef createNasalObject(naContext c)
289 static void destroyHolder(void* user_data)
291 delete static_cast<WeakPtr*>(user_data);
294 static naRef call( naContext c,
300 WeakPtr* holder_weak = static_cast<WeakPtr*>(user_data);
302 naRuntimeError(c, "invalid method holder!");
306 SharedPtr holder = holder_weak->lock();
308 throw std::runtime_error("holder has expired");
310 // Keep reference for duration of call to prevent expiring
311 // TODO not needed for strong referenced ghost
312 strong_ref ref = fromNasal(c, me);
315 naGhostType* ghost_type = naGhost_type(me);
319 "method called on object of wrong type: "
320 "is '%s' expected '%s'",
322 : (ghost_type ? ghost_type->name : "unknown"),
323 _ghost_type_strong.name
327 return holder->_method
330 CallContext(c, me, argc, args)
333 catch(const std::exception& ex)
335 naRuntimeError(c, "Fatal error in method call: %s", ex.what());
339 naRuntimeError(c, "Unknown exception in method call.");
347 * A ghost member. Can consist either of getter and/or setter functions
348 * for exposing a data variable or a single callable function.
355 member_t( const getter_t& getter,
356 const setter_t& setter,
357 const MethodHolderPtr& func = MethodHolderPtr() ):
363 explicit member_t(const MethodHolderPtr& func):
369 MethodHolderPtr func;
372 typedef std::map<std::string, member_t> MemberMap;
375 * Register a new ghost type.
377 * @note Only intialize each ghost type once!
379 * @param name Descriptive name of the ghost type.
381 static Ghost& init(const std::string& name)
383 getSingletonHolder().reset( new Ghost(name) );
384 return *getSingletonPtr();
388 * Check whether ghost type has already been initialized.
392 return getSingletonPtr();
396 * Register a base class for this ghost. The base class needs to be
397 * registers on its own before it can be used as base class.
399 * @tparam BaseGhost Type of base class already wrapped into Ghost class
403 * Ghost<MyBasePtr>::init("MyBase");
404 * Ghost<MyClassPtr>::init("MyClass")
405 * .bases<Ghost<MyBasePtr> >();
408 template<class BaseGhost>
409 typename boost::enable_if
411 boost::is_base_of<GhostMetadata, BaseGhost>,
418 boost::is_base_of<typename BaseGhost::raw_type, raw_type>::value
421 typedef typename BaseGhost::strong_ref base_ref;
423 BaseGhost* base = BaseGhost::getSingletonPtr();
426 SG_GET_TEMPLATE_MEMBER(Ghost, toNasal<BaseGhost>),
427 SG_GET_TEMPLATE_MEMBER(Ghost, fromNasalWithCast<base_ref>)
430 // Replace any getter that is not available in the current class.
431 // TODO check if this is the correct behavior of function overriding
432 for( typename BaseGhost::MemberMap::const_iterator member =
433 base->_members.begin();
434 member != base->_members.end();
437 if( _members.find(member->first) == _members.end() )
438 _members[member->first] = member_t
440 member->second.getter,
441 member->second.setter,
446 if( !_fallback_setter )
447 _fallback_setter = base->_fallback_setter;
448 if( !_fallback_getter )
449 _fallback_getter = base->_fallback_getter;
455 * Register a base class for this ghost. The base class needs to be
456 * registers on its own before it can be used as base class.
458 * @tparam Base Type of base class (Base as used in Ghost<BasePtr>)
461 * Ghost<MyBasePtr>::init("MyBase");
462 * Ghost<MyClassPtr>::init("MyClass")
463 * .bases<MyBasePtr>();
467 typename boost::disable_if
469 boost::is_base_of<GhostMetadata, Base>,
476 boost::is_base_of<typename Ghost<Base>::raw_type, raw_type>::value
479 return bases< Ghost<Base> >();
483 * Register an existing Nasal class/hash as base class for this ghost.
485 * @param parent Nasal hash/class
487 Ghost& bases(const naRef& parent)
489 addNasalBase(parent);
494 * Register a member variable by passing a getter and/or setter method.
496 * @param field Name of member
497 * @param getter Getter for variable
498 * @param setter Setter for variable (Pass 0 to prevent write access)
501 template<class Ret, class Param>
502 Ghost& member( const std::string& field,
503 Ret (raw_type::*getter)() const,
504 void (raw_type::*setter)(Param) )
506 return member(field, to_getter(getter), to_setter(setter));
509 template<class Param>
510 Ghost& member( const std::string& field,
511 const getter_t& getter,
512 void (raw_type::*setter)(Param) )
514 return member(field, getter, to_setter(setter));
518 Ghost& member( const std::string& field,
519 Ret (raw_type::*getter)() const,
520 const setter_t& setter )
522 return member(field, to_getter(getter), setter);
526 * Register a read only member variable.
528 * @param field Name of member
529 * @param getter Getter for variable
532 Ghost& member( const std::string& field,
533 Ret (raw_type::*getter)() const )
535 return member(field, to_getter(getter), setter_t());
538 Ghost& member( const std::string& field,
539 naRef (*getter)(const raw_type&, naContext) )
541 return member(field, getter_t(getter), setter_t());
544 Ghost& member( const std::string& field,
545 naRef (*getter)(raw_type&, naContext) )
547 return member(field, getter_t(getter), setter_t());
551 * Register a write only member variable.
553 * @param field Name of member
554 * @param setter Setter for variable
557 Ghost& member( const std::string& field,
558 void (raw_type::*setter)(Var) )
560 return member(field, getter_t(), to_setter(setter));
563 Ghost& member( const std::string& field,
564 void (*setter)(raw_type&, naContext, naRef) )
566 return member(field, getter_t(), setter_t(setter));
569 Ghost& member( const std::string& field,
570 const setter_t& setter )
572 return member(field, getter_t(), setter);
576 * Register a member variable by passing a getter and/or setter method.
578 * @param field Name of member
579 * @param getter Getter for variable
580 * @param setter Setter for variable (Pass empty to prevent write access)
583 Ghost& member( const std::string& field,
584 const getter_t& getter,
585 const setter_t& setter = setter_t() )
587 if( !getter.empty() || !setter.empty() )
588 _members[field] = member_t(getter, setter);
594 "Member '" << field << "' requires a getter or setter"
600 * Register a function which is called upon retrieving an unknown member
603 Ghost& _get(const fallback_getter_t& getter)
605 _fallback_getter = getter;
610 * Register a function which is called upon retrieving an unknown member
611 * of this ghost, and convert it to the given @a Param type.
613 template<class Param>
614 Ghost& _get( const boost::function<bool ( raw_type&,
618 return _get(boost::bind(
619 convert_param_invoker<Param>, getter, _1, _2, _3, _4
624 * Register a method which is called upon retrieving an unknown member of
631 * bool getMember( const std::string& key,
632 * std::string& value_out ) const;
635 * Ghost<MyClassPtr>::init("Test")
636 * ._get(&MyClass::getMember);
639 template<class Param>
640 Ghost& _get(bool (raw_type::*getter)(const std::string&, Param&) const)
643 boost::function<bool (raw_type&, const std::string&, Param&)>(getter)
648 * Register a method which is called upon retrieving an unknown member of
655 * bool getMember( naContext c,
656 * const std::string& key,
657 * naRef& value_out );
660 * Ghost<MyClassPtr>::init("Test")
661 * ._get(&MyClass::getMember);
664 Ghost& _get(bool (raw_type::*getter)( naContext,
668 return _get( fallback_getter_t(getter) );
672 * Register a function which is called upon setting an unknown member of
675 Ghost& _set(const fallback_setter_t& setter)
677 _fallback_setter = setter;
682 * Register a function which is called upon setting an unknown member of
683 * this ghost, and convert it to the given @a Param type.
685 template<class Param>
686 Ghost& _set(const boost::function<bool ( raw_type&,
690 // Setter signature: bool( raw_type&,
692 // const std::string&,
694 return _set(boost::bind(
698 boost::bind(from_nasal_ptr<Param>::get(), _2, _4)
703 * Register a method which is called upon setting an unknown member of
710 * bool setMember( const std::string& key,
711 * const std::string& value );
714 * Ghost<MyClassPtr>::init("Test")
715 * ._set(&MyClass::setMember);
718 template<class Param>
719 Ghost& _set(bool (raw_type::*setter)(const std::string&, Param))
722 boost::function<bool (raw_type&, const std::string&, Param)>(setter)
727 * Register a method which is called upon setting an unknown member of
734 * bool setMember( naContext c,
735 * const std::string& key,
739 * Ghost<MyClassPtr>::init("Test")
740 * ._set(&MyClass::setMember);
743 Ghost& _set(bool (raw_type::*setter)( naContext,
747 return _set( fallback_setter_t(setter) );
751 * Register anything that accepts an object instance and a
752 * nasal::CallContext and returns naRef as method.
758 * naRef myMethod(const nasal::CallContext& ctx);
761 * Ghost<MyClassPtr>::init("Test")
762 * .method("myMethod", &MyClass::myMethod);
765 Ghost& method(const std::string& name, const method_t& func)
767 _members[name].func = new MethodHolder(func);
772 * Register anything that accepts an object instance and a
773 * nasal::CallContext whith automatic conversion of the return type to
778 * void doIt(const MyClass& c, const nasal::CallContext& ctx);
780 * Ghost<MyClassPtr>::init("Test")
781 * .method("doIt", &doIt);
787 const std::string& name,
788 const boost::function<Ret (raw_type&, const CallContext&)>& func
791 return method(name, boost::bind(method_invoker<Ret>, func, _1, _2));
794 // Build dependency for CMake, gcc, etc.
795 #define SG_DONT_DO_ANYTHING
796 # include <simgear/nasal/cppbind/detail/functor_templates.hxx>
797 #undef SG_DONT_DO_ANYTHING
799 #define BOOST_PP_ITERATION_LIMITS (0, 9)
800 #define BOOST_PP_FILENAME_1 <simgear/nasal/cppbind/detail/functor_templates.hxx>
801 #include BOOST_PP_ITERATE()
804 * Create a shared pointer on the heap to handle the reference counting
805 * for the passed shared pointer while it is used in Nasal space.
807 template<class RefType>
809 typename boost::enable_if_c<
810 boost::is_same<RefType, strong_ref>::value
811 || boost::is_same<RefType, weak_ref>::value,
814 makeGhost(naContext c, RefType const& ref_ptr)
816 strong_ref ref(ref_ptr);
817 raw_type* ptr = get_pointer(ref);
821 // We are wrapping shared pointers to already existing objects which
822 // will then be hold be a new shared pointer. We therefore have to
823 // check for the dynamic type of the object as it might differ from
824 // the passed static type.
825 return toNasal<Ghost>(
828 shared_ptr_traits<RefType>::is_strong::value
833 * Convert Nasal object to C++ object. To get a valid object the passed
834 * Nasal objects has to be derived class of the target class (Either
835 * derived in C++ or in Nasal using a 'parents' vector)
837 static strong_ref fromNasal(naContext c, naRef me)
839 naGhostType* ghost_type = naGhost_type(me);
842 // Check if we got an instance of this class
843 bool is_weak = false;
844 if( isInstance(ghost_type, is_weak) )
846 return is_weak ? getPtr<strong_ref, true>(naGhost_ptr(me))
847 : getPtr<strong_ref, false>(naGhost_ptr(me));
850 // otherwise try the derived classes
851 for( typename DerivedList::reverse_iterator
852 derived = getSingletonPtr()->_derived_types.rbegin();
853 derived != getSingletonPtr()->_derived_types.rend();
856 strong_ref ref = (derived->from_nasal)(c, me);
858 if( get_pointer(ref) )
862 else if( naIsHash(me) )
864 naRef na_parents = naHash_cget(me, const_cast<char*>("parents"));
865 if( !naIsVector(na_parents) )
868 typedef std::vector<naRef> naRefs;
869 naRefs parents = from_nasal<naRefs>(c, na_parents);
870 for( naRefs::const_iterator parent = parents.begin();
871 parent != parents.end();
874 strong_ref ref = fromNasal(c, *parent);
875 if( get_pointer(ref) )
885 template<class, class>
888 static naGhostType _ghost_type_strong, //!< Stored as shared pointer
889 _ghost_type_weak; //!< Stored as weak shared pointer
891 typedef naRef (*to_nasal_t)(naContext, const strong_ref&, bool);
892 typedef strong_ref (*from_nasal_t)(naContext, naRef);
896 from_nasal_t from_nasal;
898 DerivedInfo( to_nasal_t to_nasal_func,
899 from_nasal_t from_nasal_func ):
900 to_nasal(to_nasal_func),
901 from_nasal(from_nasal_func)
905 typedef std::vector<DerivedInfo> DerivedList;
906 DerivedList _derived_types;
908 static bool isInstance(naGhostType* ghost_type, bool& is_weak)
910 if( !ghost_type || !getSingletonPtr() )
913 return getSingletonPtr()->GhostMetadata::isInstance( ghost_type,
917 template<class RefPtr, bool is_weak>
919 typename boost::enable_if_c<
925 typedef shared_ptr_storage<strong_ref> storage_type;
927 return storage_type::template get<RefPtr>(
928 static_cast<typename storage_type::storage_type*>(ptr)
934 template<class RefPtr, bool is_weak>
936 typename boost::enable_if_c<
937 is_weak && supports_weak_ref<T>::value,
942 typedef shared_ptr_storage<weak_ref> storage_type;
944 return storage_type::template get<RefPtr>(
945 static_cast<typename storage_type::storage_type*>(ptr)
951 template<class RefPtr, bool is_weak>
953 typename boost::enable_if_c<
954 is_weak && !supports_weak_ref<T>::value,
962 void addDerived( const internal::GhostMetadata* derived_meta,
963 to_nasal_t to_nasal_func,
964 from_nasal_t from_nasal_func )
966 GhostMetadata::addDerived(derived_meta);
967 _derived_types.push_back(DerivedInfo(to_nasal_func, from_nasal_func));
970 template<class BaseGhost>
972 typename boost::enable_if
973 < boost::is_polymorphic<typename BaseGhost::raw_type>,
976 toNasal( naContext c,
977 const typename BaseGhost::strong_ref& base_ref,
980 typename BaseGhost::raw_type* ptr = get_pointer(base_ref);
982 // Check first if passed pointer can by converted to instance of class
985 < typename BaseGhost::raw_type,
986 typename Ghost::raw_type
988 && dynamic_cast<const typename Ghost::raw_type*>(ptr) != ptr )
991 if( !getSingletonPtr() )
997 "Ghost::getTypeFor: can not get type for unregistered ghost"
1003 static_pointer_cast<typename Ghost::raw_type>(base_ref);
1005 // Now check if we can further downcast to one of our derived classes.
1006 for( typename DerivedList::reverse_iterator
1007 derived = getSingletonPtr()->_derived_types.rbegin();
1008 derived != getSingletonPtr()->_derived_types.rend();
1011 naRef ghost = (derived->to_nasal)(c, ref, strong);
1013 if( !naIsNil(ghost) )
1017 // If base is not an instance of any derived class, this class has to
1018 // be the dynamic type.
1020 ? create<false>(c, ref)
1021 : create<true>(c, ref);
1024 template<class BaseGhost>
1026 typename boost::disable_if
1027 < boost::is_polymorphic<typename BaseGhost::raw_type>,
1030 toNasal( naContext c,
1031 const typename BaseGhost::strong_ref& ref,
1034 // For non polymorphic classes there is no possibility to get the actual
1035 // dynamic type, therefore we can only use its static type.
1037 ? create<false>(c, ref)
1038 : create<true>(c, ref);
1041 template<class Type>
1042 static Type fromNasalWithCast(naContext c, naRef me)
1044 return Type(fromNasal(c, me));
1047 static Ghost* getSingletonPtr()
1049 return getSingletonHolder().get();
1053 getter_t to_getter(Ret (raw_type::*getter)() const)
1055 typedef typename boost::call_traits<Ret>::param_type param_type;
1056 naRef(*to_nasal_)(naContext, param_type) = &to_nasal;
1058 // Getter signature: naRef(raw_type&, naContext)
1063 boost::bind(getter, _1)
1067 template<class Param>
1068 setter_t to_setter(void (raw_type::*setter)(Param))
1070 // Setter signature: void(raw_type&, naContext, naRef)
1075 boost::bind(from_nasal_ptr<Param>::get(), _2, _3)
1080 * Invoke a method which writes the converted parameter to a reference
1082 template<class Param>
1084 bool convert_param_invoker
1086 const boost::function<bool ( raw_type&,
1091 const std::string& key,
1096 if( !func(obj, key, p) )
1099 out = to_nasal(c, p);
1104 * Invoke a method which returns a value and convert it to Nasal.
1108 typename boost::disable_if<boost::is_void<Ret>, naRef>::type
1111 const boost::function<Ret (raw_type&, const CallContext&)>& func,
1113 const CallContext& ctx
1116 return (*to_nasal_ptr<Ret>::get())(ctx.c, func(obj, ctx));
1120 * Invoke a method which returns void and "convert" it to nil.
1124 typename boost::enable_if<boost::is_void<Ret>, naRef>::type
1127 const boost::function<Ret (raw_type&, const CallContext&)>& func,
1129 const CallContext& ctx
1137 * Extract argument by index from nasal::CallContext and convert to given
1142 typename boost::disable_if<
1143 internal::reduced_is_same<Arg, CallContext>,
1144 typename from_nasal_ptr<Arg>::return_type
1146 arg_from_nasal(const CallContext& ctx, size_t index)
1148 return ctx.requireArg<Arg>(index);
1152 * Specialization to pass through nasal::CallContext.
1156 typename boost::enable_if<
1157 internal::reduced_is_same<Arg, CallContext>,
1158 typename from_nasal_ptr<Arg>::return_type
1160 arg_from_nasal(const CallContext& ctx, size_t)
1162 // Either const CallContext& or CallContext, non-const reference
1163 // does not make sense.
1164 BOOST_STATIC_ASSERT( (!boost::is_same<Arg, CallContext&>::value) );
1168 typedef std::auto_ptr<Ghost> GhostPtr;
1170 fallback_getter_t _fallback_getter;
1171 fallback_setter_t _fallback_setter;
1173 explicit Ghost(const std::string& name):
1174 GhostMetadata( name,
1175 &_ghost_type_strong,
1176 supports_weak_ref<T>::value ? &_ghost_type_weak : NULL )
1178 _ghost_type_strong.destroy =
1179 SG_GET_TEMPLATE_MEMBER(Ghost, queueDestroy<strong_ref>);
1180 _ghost_type_strong.name = _name_strong.c_str();
1181 _ghost_type_strong.get_member = &Ghost::getMemberStrong;
1182 _ghost_type_strong.set_member = &Ghost::setMemberStrong;
1184 _ghost_type_weak.destroy =
1185 SG_GET_TEMPLATE_MEMBER(Ghost, queueDestroy<weak_ref>);
1186 _ghost_type_weak.name = _name_weak.c_str();
1188 if( supports_weak_ref<T>::value )
1190 _ghost_type_weak.get_member = &Ghost::getMemberWeak;
1191 _ghost_type_weak.set_member = &Ghost::setMemberWeak;
1195 _ghost_type_weak.get_member = 0;
1196 _ghost_type_weak.set_member = 0;
1200 static GhostPtr& getSingletonHolder()
1202 static GhostPtr instance;
1206 template<bool is_weak>
1208 typename boost::enable_if_c<
1212 create(naContext c, const strong_ref& ref_ptr)
1214 typedef shared_ptr_storage<strong_ref> storage_type;
1215 return naNewGhost2( c,
1216 &Ghost::_ghost_type_strong,
1217 storage_type::ref(ref_ptr) );
1220 template<bool is_weak>
1222 typename boost::enable_if_c<
1223 is_weak && supports_weak_ref<T>::value,
1226 create(naContext c, const strong_ref& ref_ptr)
1228 typedef shared_ptr_storage<weak_ref> storage_type;
1229 return naNewGhost2( c,
1230 &Ghost::_ghost_type_weak,
1231 storage_type::ref(ref_ptr) );
1234 template<bool is_weak>
1236 typename boost::enable_if_c<
1237 is_weak && !supports_weak_ref<T>::value,
1240 create(naContext, const strong_ref&)
1245 template<class Type>
1246 static void destroy(void *ptr)
1248 typedef shared_ptr_storage<Type> storage_type;
1249 storage_type::unref(
1250 static_cast<typename storage_type::storage_type*>(ptr)
1254 template<class Type>
1255 static void queueDestroy(void *ptr)
1257 _destroy_list.push_back( DestroyList::value_type(&destroy<Type>, ptr) );
1260 static void raiseErrorExpired(naContext c, const char* str)
1262 Ghost* ghost_info = getSingletonPtr();
1265 "Ghost::%s: ghost has expired '%s'",
1267 ghost_info ? ghost_info->_name_strong.c_str() : "unknown"
1272 * Callback for retrieving a ghost member.
1274 static const char* getMemberImpl( naContext c,
1279 const std::string key_str = nasal::from_nasal<std::string>(c, key);
1280 // TODO merge instance parents with static class parents
1281 // if( key_str == "parents" )
1283 // if( getSingletonPtr()->_parents.empty() )
1286 // *out = getSingletonPtr()->getParents(c);
1290 typename MemberMap::iterator member =
1291 getSingletonPtr()->_members.find(key_str);
1293 if( member == getSingletonPtr()->_members.end() )
1295 fallback_getter_t fallback_get = getSingletonPtr()->_fallback_getter;
1297 || !fallback_get(obj, c, key_str, *out) )
1300 else if( member->second.func )
1301 *out = member->second.func->get_naRef(c);
1302 else if( !member->second.getter.empty() )
1303 *out = member->second.getter(obj, c);
1305 return "Read-protected member";
1311 getMemberWeak(naContext c, void* ghost, naRef key, naRef* out)
1313 // Keep a strong reference while retrieving member, to prevent deleting
1314 // object in between.
1315 strong_ref ref = getPtr<strong_ref, true>(ghost);
1317 raiseErrorExpired(c, "getMember");
1319 return getMemberImpl(c, *get_pointer(ref), key, out);
1323 getMemberStrong(naContext c, void* ghost, naRef key, naRef* out)
1325 // Just get a raw pointer as we are keeping a strong reference as ghost
1327 raw_type* ptr = getPtr<raw_type*, false>(ghost);
1328 return getMemberImpl(c, *ptr, key, out);
1332 * Callback for writing to a ghost member.
1334 static void setMemberImpl( naContext c,
1339 const std::string key = nasal::from_nasal<std::string>(c, field);
1340 const MemberMap& members = getSingletonPtr()->_members;
1342 typename MemberMap::const_iterator member = members.find(key);
1343 if( member == members.end() )
1345 fallback_setter_t fallback_set = getSingletonPtr()->_fallback_setter;
1347 naRuntimeError(c, "ghost: No such member: %s", key.c_str());
1348 else if( !fallback_set(obj, c, key, val) )
1349 naRuntimeError(c, "ghost: Failed to write (_set: %s)", key.c_str());
1351 else if( member->second.setter.empty() )
1352 naRuntimeError(c, "ghost: Write protected member: %s", key.c_str());
1353 else if( member->second.func )
1354 naRuntimeError(c, "ghost: Write to function: %s", key.c_str());
1356 member->second.setter(obj, c, val);
1360 setMemberWeak(naContext c, void* ghost, naRef field, naRef val)
1362 // Keep a strong reference while retrieving member, to prevent deleting
1363 // object in between.
1364 strong_ref ref = getPtr<strong_ref, true>(ghost);
1366 raiseErrorExpired(c, "setMember");
1368 setMemberImpl(c, *get_pointer(ref), field, val);
1372 setMemberStrong(naContext c, void* ghost, naRef field, naRef val)
1374 // Just get a raw pointer as we are keeping a strong reference as ghost
1376 raw_type* ptr = getPtr<raw_type*, false>(ghost);
1377 setMemberImpl(c, *ptr, field, val);
1383 Ghost<T, typename boost::enable_if<is_strong_ref<T> >::type>
1384 ::_ghost_type_strong;
1388 Ghost<T, typename boost::enable_if<is_strong_ref<T> >::type>
1391 } // namespace nasal
1393 // Needs to be outside any namespace to make ADL work
1395 * Convert every shared pointer to a ghost.
1398 typename boost::enable_if<
1399 nasal::internal::has_element_type<
1400 typename nasal::internal::reduced_type<T>::type
1404 to_nasal_helper(naContext c, T ptr)
1406 typedef typename nasal::shared_ptr_traits<T>::strong_ref strong_ref;
1407 return nasal::Ghost<strong_ref>::makeGhost(c, ptr);
1411 * Convert nasal ghosts/hashes to shared pointer (of a ghost).
1414 typename boost::enable_if<
1415 nasal::internal::has_element_type<
1416 typename nasal::internal::reduced_type<T>::type
1420 from_nasal_helper(naContext c, naRef ref, const T*)
1422 typedef typename nasal::shared_ptr_traits<T>::strong_ref strong_ref;
1423 return T(nasal::Ghost<strong_ref>::fromNasal(c, ref));
1427 * Convert any pointer to a SGReferenced based object to a ghost.
1430 typename boost::enable_if_c<
1431 boost::is_base_of<SGReferenced, T>::value
1432 || boost::is_base_of<SGWeakReferenced, T>::value,
1435 to_nasal_helper(naContext c, T* ptr)
1437 return nasal::Ghost<SGSharedPtr<T> >::makeGhost(c, SGSharedPtr<T>(ptr));
1441 * Convert nasal ghosts/hashes to pointer (of a SGReferenced based ghost).
1444 typename boost::enable_if_c<
1447 typename boost::remove_pointer<T>::type
1449 || boost::is_base_of<
1451 typename boost::remove_pointer<T>::type
1455 from_nasal_helper(naContext c, naRef ref, const T*)
1457 typedef SGSharedPtr<typename boost::remove_pointer<T>::type> TypeRef;
1458 return T(nasal::Ghost<TypeRef>::fromNasal(c, ref));
1462 * Convert any pointer to a osg::Referenced based object to a ghost.
1465 typename boost::enable_if<
1466 boost::is_base_of<osg::Referenced, T>,
1469 to_nasal_helper(naContext c, T* ptr)
1471 return nasal::Ghost<osg::ref_ptr<T> >::makeGhost(c, osg::ref_ptr<T>(ptr));
1475 * Convert nasal ghosts/hashes to pointer (of a osg::Referenced based ghost).
1478 typename boost::enable_if<
1479 boost::is_base_of<osg::Referenced, typename boost::remove_pointer<T>::type>,
1482 from_nasal_helper(naContext c, naRef ref, const T*)
1484 typedef osg::ref_ptr<typename boost::remove_pointer<T>::type> TypeRef;
1485 return T(nasal::Ghost<TypeRef>::fromNasal(c, ref));
1488 #endif /* SG_NASAL_GHOST_HXX_ */