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 * Bindings between C++ and the Nasal scripting language
62 * Metadata for Ghost object types
68 * Add a nasal base class to the ghost. Will be available in the ghosts
71 void addNasalBase(const naRef& parent)
73 assert( naIsHash(parent) );
74 _parents.push_back(parent);
77 bool isBaseOf(naGhostType* ghost_type) const
79 if( ghost_type == _ghost_type_ptr )
82 for( DerivedList::const_iterator derived = _derived_classes.begin();
83 derived != _derived_classes.end();
86 if( (*derived)->isBaseOf(ghost_type) )
95 typedef std::vector<const GhostMetadata*> DerivedList;
97 const std::string _name;
98 const naGhostType *_ghost_type_ptr;
99 DerivedList _derived_classes;
100 std::vector<naRef> _parents;
102 GhostMetadata( const std::string& name,
103 const naGhostType* ghost_type ):
105 _ghost_type_ptr(ghost_type)
110 void addDerived(const GhostMetadata* derived)
113 _derived_classes.push_back(derived);
119 "Ghost::addDerived: " << _name << " -> " << derived->_name
123 naRef getParents(naContext c)
125 return nasal::to_nasal(c, _parents);
130 * Hold callable method and convert to Nasal function if required.
133 public SGWeakReferenced
136 virtual ~MethodHolder() {}
138 naRef get_naRef(naContext c)
141 _obj.reset(createNasalObject(c));
142 return _obj.get_naRef();
148 virtual naRef createNasalObject(naContext c) = 0;
151 BOOST_MPL_HAS_XXX_TRAIT_DEF(element_type)
156 typedef typename boost::remove_cv<
157 typename boost::remove_reference<T>::type
161 template<class T1, class T2>
162 struct reduced_is_same:
163 public boost::is_same<typename reduced_type<T1>::type, T2>
167 typedef SGSharedPtr<internal::MethodHolder> MethodHolderPtr;
168 typedef SGWeakPtr<internal::MethodHolder> MethodHolderWeakPtr;
171 * Class for exposing C++ objects to Nasal
174 * // Example class to be exposed to Nasal
182 * void doSomethingElse(const nasal::CallContext& ctx);
184 * typedef boost::shared_ptr<MyClass> MyClassPtr;
186 * std::string myOtherFreeMember(int num);
188 * void exposeClasses()
190 * // Register a nasal ghost type for MyClass. This needs to be done only
191 * // once before creating the first ghost instance. The exposed class needs
192 * // to be wrapped inside a shared pointer, eg. boost::shared_ptr.
193 * Ghost<MyClassPtr>::init("MyClass")
194 * // Members can be exposed by getters and setters
195 * .member("x", &MyClass::getX, &MyClass::setX)
196 * // For readonly variables only pass a getter
197 * .member("x_readonly", &MyClass::getX)
198 * // It is also possible to expose writeonly members
199 * .member("x_writeonly", &MyClass::setX)
200 * // Methods can be nearly anything callable and accepting a reference
201 * // to an instance of the class type. (member functions, free functions
202 * // and anything else bindable using boost::function and boost::bind)
203 * .method("myMember", &MyClass::myMember)
204 * .method("doSomething", &MyClass::doSomethingElse)
205 * .method("other", &myOtherFreeMember);
211 public internal::GhostMetadata
213 BOOST_STATIC_ASSERT( internal::has_element_type<T>::value );
216 typedef typename T::element_type raw_type;
218 typedef naRef (raw_type::*member_func_t)(const CallContext&);
219 typedef naRef (*free_func_t)(raw_type&, const CallContext&);
220 typedef boost::function<naRef(raw_type&, naContext)> getter_t;
221 typedef boost::function<void( raw_type&, naContext, naRef)> setter_t;
222 typedef boost::function<naRef(raw_type&, const CallContext&)> method_t;
223 typedef boost::function<bool( raw_type&,
226 naRef& )> fallback_getter_t;
227 typedef boost::function<bool( raw_type&,
230 naRef )> fallback_setter_t;
233 public internal::MethodHolder
236 explicit MethodHolder(const method_t& method):
242 typedef SGSharedPtr<MethodHolder> SharedPtr;
243 typedef SGWeakPtr<MethodHolder> WeakPtr;
247 virtual naRef createNasalObject(naContext c)
259 static void destroyHolder(void* user_data)
261 delete static_cast<WeakPtr*>(user_data);
264 static naRef call( naContext c,
270 WeakPtr* holder_weak = static_cast<WeakPtr*>(user_data);
272 naRuntimeError(c, "invalid method holder!");
276 SharedPtr holder = holder_weak->lock();
278 throw std::runtime_error("holder has expired");
280 return holder->_method
282 requireObject(c, me),
283 CallContext(c, argc, args)
286 catch(const std::exception& ex)
288 naRuntimeError(c, "Fatal error in method call: %s", ex.what());
292 naRuntimeError(c, "Unknown exception in method call.");
300 * A ghost member. Can consist either of getter and/or setter functions
301 * for exposing a data variable or a single callable function.
308 member_t( const getter_t& getter,
309 const setter_t& setter,
310 const MethodHolderPtr& func = MethodHolderPtr() ):
316 explicit member_t(const MethodHolderPtr& func):
322 MethodHolderPtr func;
325 typedef std::map<std::string, member_t> MemberMap;
328 * Register a new ghost type.
330 * @note Only intialize each ghost type once!
332 * @param name Descriptive name of the ghost type.
334 static Ghost& init(const std::string& name)
336 getSingletonHolder().reset( new Ghost(name) );
337 return *getSingletonPtr();
341 * Check whether ghost type has already been initialized.
345 return getSingletonPtr();
349 * Register a base class for this ghost. The base class needs to be
350 * registers on its own before it can be used as base class.
352 * @tparam BaseGhost Type of base class already wrapped into Ghost class
356 * Ghost<MyBasePtr>::init("MyBase");
357 * Ghost<MyClassPtr>::init("MyClass")
358 * .bases<Ghost<MyBasePtr> >();
361 template<class BaseGhost>
362 typename boost::enable_if
364 boost::is_base_of<GhostMetadata, BaseGhost>,
371 boost::is_base_of<typename BaseGhost::raw_type, raw_type>::value
374 BaseGhost* base = BaseGhost::getSingletonPtr();
378 // Both ways of retrieving the address of a static member function
379 // should be legal but not all compilers know this.
380 // g++-4.4.7+ has been tested to work with both versions
381 #if defined(SG_GCC_VERSION) && SG_GCC_VERSION < 40407
382 // The old version of g++ used on Jenkins (16.11.2012) only compiles
384 &getTypeFor<BaseGhost>
386 // VS (2008, 2010, ... ?) only allow this version.
387 &Ghost::getTypeFor<BaseGhost>
391 // Replace any getter that is not available in the current class.
392 // TODO check if this is the correct behavior of function overriding
393 for( typename BaseGhost::MemberMap::const_iterator member =
394 base->_members.begin();
395 member != base->_members.end();
398 if( _members.find(member->first) == _members.end() )
399 _members[member->first] = member_t
401 member->second.getter,
402 member->second.setter,
407 if( !_fallback_setter )
408 _fallback_setter = base->_fallback_setter;
409 if( !_fallback_getter )
410 _fallback_getter = base->_fallback_getter;
416 * Register a base class for this ghost. The base class needs to be
417 * registers on its own before it can be used as base class.
419 * @tparam Base Type of base class (Base as used in Ghost<BasePtr>)
422 * Ghost<MyBasePtr>::init("MyBase");
423 * Ghost<MyClassPtr>::init("MyClass")
424 * .bases<MyBasePtr>();
428 typename boost::disable_if
430 boost::is_base_of<GhostMetadata, Base>,
437 boost::is_base_of<typename Ghost<Base>::raw_type, raw_type>::value
440 return bases< Ghost<Base> >();
444 * Register an existing Nasal class/hash as base class for this ghost.
446 * @param parent Nasal hash/class
448 Ghost& bases(const naRef& parent)
450 addNasalBase(parent);
455 * Register a member variable by passing a getter and/or setter method.
457 * @param field Name of member
458 * @param getter Getter for variable
459 * @param setter Setter for variable (Pass 0 to prevent write access)
462 template<class Ret, class Param>
463 Ghost& member( const std::string& field,
464 Ret (raw_type::*getter)() const,
465 void (raw_type::*setter)(Param) )
467 return member(field, to_getter(getter), to_setter(setter));
471 * Register a read only member variable.
473 * @param field Name of member
474 * @param getter Getter for variable
477 Ghost& member( const std::string& field,
478 Ret (raw_type::*getter)() const )
480 return member(field, to_getter(getter), setter_t());
484 * Register a write only member variable.
486 * @param field Name of member
487 * @param setter Setter for variable
490 Ghost& member( const std::string& field,
491 void (raw_type::*setter)(Var) )
493 return member(field, getter_t(), to_setter(setter));
497 * Register a member variable by passing a getter and/or setter method.
499 * @param field Name of member
500 * @param getter Getter for variable
501 * @param setter Setter for variable (Pass empty to prevent write access)
504 Ghost& member( const std::string& field,
505 const getter_t& getter,
506 const setter_t& setter = setter_t() )
508 if( !getter.empty() || !setter.empty() )
509 _members[field] = member_t(getter, setter);
515 "Member '" << field << "' requires a getter or setter"
521 * Register a function which is called upon retrieving an unknown member
524 Ghost& _get(const fallback_getter_t& getter)
526 _fallback_getter = getter;
531 * Register a function which is called upon retrieving an unknown member
532 * of this ghost, and convert it to the given @a Param type.
534 template<class Param>
535 Ghost& _get( const boost::function<bool ( raw_type&,
539 return _get(boost::bind(
540 convert_param_invoker<Param>, getter, _1, _2, _3, _4
545 * Register a method which is called upon retrieving an unknown member of
552 * bool getMember( const std::string& key,
553 * std::string& value_out );
556 * Ghost<MyClassPtr>::init("Test")
557 * ._get(&MyClass::getMember);
560 template<class Param>
561 Ghost& _get(bool (raw_type::*getter)(const std::string&, Param&))
564 boost::function<bool (raw_type&, const std::string&, Param&)>(getter)
569 * Register a method which is called upon retrieving an unknown member of
576 * bool getMember( naContext c,
577 * const std::string& key,
578 * naRef& value_out );
581 * Ghost<MyClassPtr>::init("Test")
582 * ._get(&MyClass::getMember);
585 Ghost& _get(bool (raw_type::*getter)( naContext,
589 return _get( fallback_getter_t(getter) );
593 * Register a function which is called upon setting an unknown member of
596 Ghost& _set(const fallback_setter_t& setter)
598 _fallback_setter = setter;
603 * Register a function which is called upon setting an unknown member of
604 * this ghost, and convert it to the given @a Param type.
606 template<class Param>
607 Ghost& _set(const boost::function<bool ( raw_type&,
611 // Setter signature: bool( raw_type&,
613 // const std::string&,
615 return _set(boost::bind(
619 boost::bind(from_nasal_ptr<Param>::get(), _2, _4)
624 * Register a method which is called upon setting an unknown member of
631 * bool setMember( const std::string& key,
632 * const std::string& value );
635 * Ghost<MyClassPtr>::init("Test")
636 * ._set(&MyClass::setMember);
639 template<class Param>
640 Ghost& _set(bool (raw_type::*setter)(const std::string&, Param))
643 boost::function<bool (raw_type&, const std::string&, Param)>(setter)
648 * Register a method which is called upon setting an unknown member of
655 * bool setMember( naContext c,
656 * const std::string& key,
660 * Ghost<MyClassPtr>::init("Test")
661 * ._set(&MyClass::setMember);
664 Ghost& _set(bool (raw_type::*setter)( naContext,
668 return _set( fallback_setter_t(setter) );
672 * Register anything that accepts an object instance and a
673 * nasal::CallContext and returns naRef as method.
679 * naRef myMethod(const nasal::CallContext& ctx);
682 * Ghost<MyClassPtr>::init("Test")
683 * .method("myMethod", &MyClass::myMethod);
686 Ghost& method(const std::string& name, const method_t& func)
688 _members[name].func = new MethodHolder(func);
693 * Register anything that accepts an object instance and a
694 * nasal::CallContext whith automatic conversion of the return type to
699 * void doIt(const MyClass& c, const nasal::CallContext& ctx);
701 * Ghost<MyClassPtr>::init("Test")
702 * .method("doIt", &doIt);
708 const std::string& name,
709 const boost::function<Ret (raw_type&, const CallContext&)>& func
712 return method(name, boost::bind(method_invoker<Ret>, func, _1, _2));
715 // Build dependency for CMake, gcc, etc.
716 #define SG_DONT_DO_ANYTHING
717 # include <simgear/nasal/cppbind/detail/functor_templates.hxx>
718 #undef SG_DONT_DO_ANYTHING
720 #define BOOST_PP_ITERATION_LIMITS (0, 9)
721 #define BOOST_PP_FILENAME_1 <simgear/nasal/cppbind/detail/functor_templates.hxx>
722 #include BOOST_PP_ITERATE()
724 // TODO use variadic template when supporting C++11
725 // TODO check if default constructor exists
726 // static naRef create( naContext c )
728 // return makeGhost(c, createInstance());
732 * Create a Nasal instance of this ghost.
734 * @param c Active Nasal context
735 * @param a1 Parameter used for creating new instance
738 static naRef create( naContext c, const A1& a1 )
740 return makeGhost(c, createInstance(a1));
744 * Nasal callback for creating a new instance of this ghost.
746 static naRef f_create(naContext c, naRef me, int argc, naRef* args)
751 static bool isBaseOf(naGhostType* ghost_type)
756 return getSingletonPtr()->GhostMetadata::isBaseOf(ghost_type);
759 static bool isBaseOf(naRef obj)
761 return isBaseOf( naGhost_type(obj) );
765 * Convert Nasal object to C++ object. To get a valid object the passed
766 * Nasal objects has to be derived class of the target class (Either
767 * derived in C++ or in Nasal using a 'parents' vector)
769 static pointer fromNasal(naContext c, naRef me)
771 // Check if it's a ghost and if it can be converted
772 if( isBaseOf( naGhost_type(me) ) )
773 return getPtr( naGhost_ptr(me) );
775 // Now if it is derived from a ghost (hash with ghost in parent vector)
776 else if( naIsHash(me) )
778 naRef na_parents = naHash_cget(me, const_cast<char*>("parents"));
779 if( !naIsVector(na_parents) )
782 typedef std::vector<naRef> naRefs;
783 naRefs parents = from_nasal<naRefs>(c, na_parents);
784 for( naRefs::const_iterator parent = parents.begin();
785 parent != parents.end();
788 pointer ptr = fromNasal(c, *parent);
789 if( get_pointer(ptr) )
802 static naGhostType _ghost_type;
804 typedef naGhostType* (*type_checker_t)(const raw_type*);
805 typedef std::vector<type_checker_t> DerivedList;
806 DerivedList _derived_types;
809 * Create a shared pointer on the heap to handle the reference counting
810 * for the passed shared pointer while it is used in Nasal space.
812 static pointer* createInstance(const pointer& ptr)
814 return get_pointer(ptr) ? new pointer(ptr) : 0;
817 static pointer getPtr(void* ptr)
820 return *static_cast<pointer*>(ptr);
825 static raw_type* getRawPtr(void* ptr)
828 return get_pointer(*static_cast<pointer*>(ptr));
833 static raw_type* getRawPtr(const pointer& ptr)
835 return get_pointer(ptr);
838 void addDerived( const internal::GhostMetadata* derived_meta,
839 const type_checker_t& derived_info )
841 GhostMetadata::addDerived(derived_meta);
842 _derived_types.push_back(derived_info);
845 template<class BaseGhost>
847 typename boost::enable_if
848 < boost::is_polymorphic<typename BaseGhost::raw_type>,
851 getTypeFor(const typename BaseGhost::raw_type* base)
853 // Check first if passed pointer can by converted to instance of class
856 < typename BaseGhost::raw_type,
857 typename Ghost::raw_type
859 && dynamic_cast<const typename Ghost::raw_type*>(base) != base )
862 if( !getSingletonPtr() )
868 "Ghost::getTypeFor: can not get type for unregistered ghost"
873 // Now check if we can further downcast to one of our derived classes.
874 for( typename DerivedList::reverse_iterator
875 derived = getSingletonPtr()->_derived_types.rbegin();
876 derived != getSingletonPtr()->_derived_types.rend();
879 naGhostType* ghost_type =
880 (*derived)( static_cast<const typename Ghost::raw_type*>(base) );
885 // If base is not an instance of any derived class, this class has to
886 // be the dynamic type.
890 template<class BaseGhost>
892 typename boost::disable_if
893 < boost::is_polymorphic<typename BaseGhost::raw_type>,
896 getTypeFor(const typename BaseGhost::raw_type* base)
898 // For non polymorphic classes there is no possibility to get the actual
899 // dynamic type, therefore we can only use its static type.
900 return &BaseGhost::_ghost_type;
903 static Ghost* getSingletonPtr()
905 return getSingletonHolder().get();
908 static raw_type& requireObject(naContext c, naRef me)
910 raw_type* obj = getRawPtr( fromNasal(c, me) );
911 naGhostType* ghost_type = naGhost_type(me);
917 "method called on object of wrong type: is '%s' expected '%s'",
918 naIsNil(me) ? "nil" : (ghost_type ? ghost_type->name : "unknown"),
926 getter_t to_getter(Ret (raw_type::*getter)() const)
928 typedef typename boost::call_traits<Ret>::param_type param_type;
929 naRef(*to_nasal_)(naContext, param_type) = &to_nasal;
931 // Getter signature: naRef(raw_type&, naContext)
936 boost::bind(getter, _1)
940 template<class Param>
941 setter_t to_setter(void (raw_type::*setter)(Param))
943 // Setter signature: void(raw_type&, naContext, naRef)
948 boost::bind(from_nasal_ptr<Param>::get(), _2, _3)
953 * Invoke a method which writes the converted parameter to a reference
955 template<class Param>
957 bool convert_param_invoker
959 const boost::function<bool ( raw_type&,
964 const std::string& key,
969 if( !func(obj, key, p) )
972 out = to_nasal(c, p);
977 * Invoke a method which returns a value and convert it to Nasal.
981 typename boost::disable_if<boost::is_void<Ret>, naRef>::type
984 const boost::function<Ret (raw_type&, const CallContext&)>& func,
986 const CallContext& ctx
989 return (*to_nasal_ptr<Ret>::get())(ctx.c, func(obj, ctx));
993 * Invoke a method which returns void and "convert" it to nil.
997 typename boost::enable_if<boost::is_void<Ret>, naRef>::type
1000 const boost::function<Ret (raw_type&, const CallContext&)>& func,
1002 const CallContext& ctx
1010 * Extract argument by index from nasal::CallContext and convert to given
1015 typename boost::disable_if<
1016 internal::reduced_is_same<Arg, CallContext>,
1017 typename from_nasal_ptr<Arg>::return_type
1019 arg_from_nasal(const CallContext& ctx, size_t index)
1021 return ctx.requireArg<Arg>(index);
1025 * Specialization to pass through nasal::CallContext.
1029 typename boost::enable_if<
1030 internal::reduced_is_same<Arg, CallContext>,
1031 typename from_nasal_ptr<Arg>::return_type
1033 arg_from_nasal(const CallContext& ctx, size_t)
1035 // Either const CallContext& or CallContext, non-const reference
1036 // does not make sense.
1037 BOOST_STATIC_ASSERT( (!boost::is_same<Arg, CallContext&>::value) );
1041 typedef std::auto_ptr<Ghost> GhostPtr;
1043 fallback_getter_t _fallback_getter;
1044 fallback_setter_t _fallback_setter;
1046 explicit Ghost(const std::string& name):
1047 GhostMetadata(name, &_ghost_type)
1049 _ghost_type.destroy = &destroyGhost;
1050 _ghost_type.name = _name.c_str();
1051 _ghost_type.get_member = &getMember;
1052 _ghost_type.set_member = &setMember;
1055 static GhostPtr& getSingletonHolder()
1057 static GhostPtr instance;
1061 static naRef makeGhost(naContext c, void *ptr)
1063 if( getRawPtr(ptr) )
1065 // We are wrapping shared pointers to already existing objects which
1066 // will then be hold be a new shared pointer. We therefore have to
1067 // check for the dynamic type of the object as it might differ from
1068 // the passed static type.
1069 naGhostType* ghost_type = getTypeFor<Ghost>( getRawPtr(ptr) );
1072 return naNewGhost2(c, ghost_type, ptr);
1079 static void destroyGhost(void *ptr)
1081 delete static_cast<pointer*>(ptr);
1085 * Callback for retrieving a ghost member.
1087 static const char* getMember(naContext c, void* g, naRef key, naRef* out)
1089 const std::string key_str = nasal::from_nasal<std::string>(c, key);
1090 // TODO merge instance parents with static class parents
1091 // if( key_str == "parents" )
1093 // if( getSingletonPtr()->_parents.empty() )
1096 // *out = getSingletonPtr()->getParents(c);
1100 typename MemberMap::iterator member =
1101 getSingletonPtr()->_members.find(key_str);
1103 if( member == getSingletonPtr()->_members.end() )
1105 fallback_getter_t fallback_get = getSingletonPtr()->_fallback_getter;
1107 || !fallback_get(*getRawPtr(g), c, key_str, *out) )
1110 else if( member->second.func )
1111 *out = member->second.func->get_naRef(c);
1112 else if( !member->second.getter.empty() )
1113 *out = member->second.getter(*getRawPtr(g), c);
1115 return "Read-protected member";
1121 * Callback for writing to a ghost member.
1123 static void setMember(naContext c, void* g, naRef field, naRef val)
1125 const std::string key = nasal::from_nasal<std::string>(c, field);
1126 const MemberMap& members = getSingletonPtr()->_members;
1128 typename MemberMap::const_iterator member = members.find(key);
1129 if( member == members.end() )
1131 fallback_setter_t fallback_set = getSingletonPtr()->_fallback_setter;
1133 naRuntimeError(c, "ghost: No such member: %s", key.c_str());
1134 else if( !fallback_set(*getRawPtr(g), c, key, val) )
1135 naRuntimeError(c, "ghost: Failed to write (_set: %s)", key.c_str());
1137 else if( member->second.setter.empty() )
1138 naRuntimeError(c, "ghost: Write protected member: %s", key.c_str());
1139 else if( member->second.func )
1140 naRuntimeError(c, "ghost: Write to function: %s", key.c_str());
1142 member->second.setter(*getRawPtr(g), c, val);
1147 naGhostType Ghost<T>::_ghost_type;
1149 } // namespace nasal
1151 // Needs to be outside any namespace to make ADL work
1153 * Convert every shared pointer to a ghost.
1156 typename boost::enable_if<
1157 nasal::internal::has_element_type<
1158 typename nasal::internal::reduced_type<T>::type
1162 to_nasal_helper(naContext c, T ptr)
1164 return nasal::Ghost<T>::create(c, ptr);
1168 * Convert nasal ghosts/hashes to shared pointer (of a ghost).
1171 typename boost::enable_if<
1172 nasal::internal::has_element_type<
1173 typename nasal::internal::reduced_type<T>::type
1177 from_nasal_helper(naContext c, naRef ref, const T*)
1179 return nasal::Ghost<T>::fromNasal(c, ref);
1183 * Convert any pointer to a SGReference based object to a ghost.
1186 typename boost::enable_if_c<
1187 boost::is_base_of<SGReferenced, T>::value
1188 || boost::is_base_of<SGWeakReferenced, T>::value,
1191 to_nasal_helper(naContext c, T* ptr)
1193 return nasal::Ghost<SGSharedPtr<T> >::create(c, SGSharedPtr<T>(ptr));
1197 * Convert nasal ghosts/hashes to pointer (of a SGReference based ghost).
1200 typename boost::enable_if_c<
1203 typename boost::remove_pointer<T>::type
1205 || boost::is_base_of<
1207 typename boost::remove_pointer<T>::type
1211 from_nasal_helper(naContext c, naRef ref, const T*)
1213 typedef SGSharedPtr<typename boost::remove_pointer<T>::type> TypeRef;
1214 return nasal::Ghost<TypeRef>::fromNasal(c, ref).release();
1217 #endif /* SG_NASAL_GHOST_HXX_ */