1 ///@file Expose C++ objects to Nasal as ghosts
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #ifndef SG_NASAL_GHOST_HXX_
20 #define SG_NASAL_GHOST_HXX_
22 #include "from_nasal.hxx"
23 #include "to_nasal.hxx"
25 #include <simgear/debug/logstream.hxx>
27 #include <boost/bind.hpp>
28 #include <boost/function.hpp>
29 #include <boost/lambda/lambda.hpp>
30 #include <boost/utility/enable_if.hpp>
36 * Traits for C++ classes exposed as Ghost. This is the basic template for
40 struct GhostTypeTraits
42 /** Whether the class is passed by shared pointer or raw pointer */
43 typedef boost::false_type::type is_shared;
45 /** The raw class type (Without any shared pointer) */
50 struct GhostTypeTraits<boost::shared_ptr<T> >
52 typedef boost::true_type::type is_shared;
58 struct GhostTypeTraits<osg::ref_ptr<T> >
60 typedef boost::true_type::type is_shared;
65 #ifdef SGSharedPtr_HXX
67 struct GhostTypeTraits<SGSharedPtr<T> >
69 typedef boost::true_type::type is_shared;
75 * Policy for creating ghost instances from shared pointer objects.
78 struct SharedPointerPolicy
80 typedef typename GhostTypeTraits<T>::raw_type raw_type;
83 * Create a shared pointer on the heap to handle the reference counting for
84 * the passed shared pointer while it is used in Nasal space.
86 static T* createInstance(const T& ptr)
91 static raw_type* getRawPtr(void* ptr)
93 return static_cast<T*>(ptr)->get();
98 * Policy for creating ghost instances as raw objects on the heap.
101 struct RawPointerPolicy
103 typedef typename GhostTypeTraits<T>::raw_type raw_type;
106 * Create a new object instance on the heap
108 static T* createInstance()
113 static raw_type* getRawPtr(void* ptr)
115 BOOST_STATIC_ASSERT((boost::is_same<raw_type, T>::value));
116 return static_cast<T*>(ptr);
123 * Metadata for Ghost object types
129 * Add a nasal base class to the ghost. Will be available in the ghosts
132 void addNasalBase(const naRef& parent)
134 assert( naIsHash(parent) );
135 _parents.push_back(parent);
139 const std::string _name;
140 naGhostType _ghost_type;
141 // std::vector<GhostMetadata*> _base_classes;
142 std::vector<naRef> _parents;
144 explicit GhostMetadata(const std::string& name):
150 // void addBaseClass(GhostMetadata* base)
153 // _base_classes.push_back(base);
156 naRef getParents(naContext c)
158 return nasal::to_nasal(c, _parents);
164 * Class for exposing C++ objects to Nasal
167 * // Example class to be exposed to Nasal
174 * naRef myMember(int argc, naRef* args);
177 * void exposeClasses()
179 * // Register a nasal ghost type for MyClass. This needs to be done only
180 * // once before creating the first ghost instance.
181 * Ghost<MyClass>::init("MyClass")
182 * // Members can be exposed by getters and setters
183 * .member("x", &MyClass::getX, &MyClass::setX)
184 * // For readonly variables only pass a getter
185 * .member("x_readonly", &MyClass::getX)
186 * // It is also possible to expose writeonly members
187 * .member("x_writeonly", &MyClass::setX)
188 * // Methods use a slightly different syntax - The pointer to the member
189 * // function has to be passed as template argument
190 * .method<&MyClass::myMember>("myMember");
196 public internal::GhostMetadata,
197 protected boost::mpl::if_< typename GhostTypeTraits<T>::is_shared,
198 SharedPointerPolicy<T>,
199 RawPointerPolicy<T> >::type
202 typedef typename GhostTypeTraits<T>::raw_type raw_type;
203 typedef naRef (T::*member_func_t)(int, naRef*);
204 typedef boost::function<naRef(naContext c, raw_type*)> getter_t;
205 typedef boost::function<void(naContext c, raw_type*, naRef)> setter_t;
208 * A ghost member. Can consist either of getter and/or setter functions
209 * for exposing a data variable or a single callable function.
217 member_t( const getter_t& getter,
218 const setter_t& setter,
219 naCFunction func = 0 ):
225 member_t(naCFunction func):
234 typedef std::map<std::string, member_t> MemberMap;
237 * Register a new ghost type.
239 * @param name Descriptive name of the ghost type.
241 static Ghost& init(const std::string& name)
243 getSingletonHolder().reset( new Ghost(name) );
244 return *getSingletonPtr();
248 * Register a base class for this ghost. The base class needs to be
249 * registers on its own before it can be used as base class.
251 * @tparam BaseGhost Type of base class already wrapped into Ghost class
255 * Ghost<MyBase>::init("MyBase");
256 * Ghost<MyClass>::init("MyClass")
257 * .bases<Ghost<MyBase> >();
260 template<class BaseGhost>
261 typename boost::enable_if_c
262 < boost::is_base_of<GhostMetadata, BaseGhost>::value
263 && boost::is_base_of<typename BaseGhost::raw_type, raw_type>::value,
268 BaseGhost* base = BaseGhost::getSingletonPtr();
269 //addBaseClass( base );
271 // Replace any getter that is not available in the current class.
272 // TODO check if this is the correct behavior of function overriding
273 for( typename BaseGhost::MemberMap::const_iterator member =
274 base->_members.begin();
275 member != base->_members.end();
278 if( _members.find(member->first) == _members.end() )
279 _members[member->first] = member_t
281 member->second.getter,
282 member->second.setter,
291 * Register a base class for this ghost. The base class needs to be
292 * registers on its own before it can be used as base class.
294 * @tparam Base Type of base class (Base as used in Ghost<Base>)
297 * Ghost<MyBase>::init("MyBase");
298 * Ghost<MyClass>::init("MyClass")
303 typename boost::enable_if_c
304 < !boost::is_base_of<GhostMetadata, Base>::value
305 && boost::is_base_of<typename Ghost<Base>::raw_type, raw_type>::value,
310 return bases< Ghost<Base> >();
314 * Register an existing Nasal class/hash as base class for this ghost.
316 * @param parent Nasal hash/class
318 Ghost& bases(const naRef& parent)
320 addNasalBase(parent);
325 * Register a member variable by passing a getter and/or setter method.
327 * @param field Name of member
328 * @param getter Getter for variable
329 * @param setter Setter for variable (Pass 0 to prevent write access)
333 Ghost& member( const std::string& field,
334 Var (raw_type::*getter)() const,
335 void (raw_type::*setter)(Var) = 0 )
340 naRef (*to_nasal_)(naContext, Var) = &nasal::to_nasal;
342 // Getter signature: naRef(naContext, raw_type*)
343 m.getter = boost::bind(to_nasal_, _1, boost::bind(getter, _2));
348 Var (*from_nasal_)(naContext, naRef) = &nasal::from_nasal;
350 // Setter signature: void(naContext, raw_type*, naRef)
351 m.setter = boost::bind(setter, _2, boost::bind(from_nasal_, _1, _3));
354 return member(field, m.getter, m.setter);
358 * Register a write only member variable.
360 * @param field Name of member
361 * @param setter Setter for variable
364 Ghost& member( const std::string& field,
365 void (raw_type::*setter)(Var) )
367 return member<Var>(field, 0, setter);
371 * Register a member variable by passing a getter and/or setter method.
373 * @param field Name of member
374 * @param getter Getter for variable
375 * @param setter Setter for variable (Pass empty to prevent write access)
378 Ghost& member( const std::string& field,
379 const getter_t& getter,
380 const setter_t& setter = setter_t() )
382 if( !getter.empty() || !setter.empty() )
383 _members[field] = member_t(getter, setter);
389 "Member '" << field << "' requires a getter or setter"
395 * Register a member function.
397 * @note Because only function pointers can be registered as Nasal
398 * functions it is needed to pass the function pointer as template
399 * argument. This allows us to create a separate instance of the
400 * MemberFunctionWrapper for each registered function and therefore
401 * provides us with the needed static functions to be passed on to
404 * @tparam func Pointer to member function being registered.
410 * naRef myMethod(int argc, naRef* args);
413 * Ghost<MyClass>::init("Test")
414 * .method<&MyClass::myMethod>("myMethod");
417 template<member_func_t func>
418 Ghost& method(const std::string& name)
420 _members[name].func = &MemberFunctionWrapper<func>::call;
424 // TODO use variadic template when supporting C++11
426 * Create a Nasal instance of this ghost.
428 * @param c Active Nasal context
430 static naRef create( naContext c )
432 return makeGhost(c, Ghost::createInstance());
436 * Create a Nasal instance of this ghost.
438 * @param c Active Nasal context
439 * @param a1 Parameter used for creating new instance
442 static naRef create( naContext c, const A1& a1 )
444 return makeGhost(c, Ghost::createInstance(a1));
448 * Nasal callback for creating a new instance of this ghost.
450 static naRef f_create(naContext c, naRef me, int argc, naRef* args)
460 static Ghost* getSingletonPtr()
462 return getSingletonHolder().get();
466 * Wrapper class to enable registering pointers to member functions as
467 * Nasal function callbacks. We need to use the function pointer as
468 * template parameter to ensure every registered function gets a static
469 * function which can be passed to Nasal.
471 template<member_func_t func>
472 struct MemberFunctionWrapper
475 * Called from Nasal upon invocation of the according registered
476 * function. Forwards the call to the passed object instance.
478 static naRef call(naContext c, naRef me, int argc, naRef* args)
480 if( naGhost_type(me) != &getSingletonPtr()->_ghost_type )
484 "method called on object of wrong type: '%s' expected",
485 getSingletonPtr()->_ghost_type.name
488 raw_type* obj = Ghost::getRawPtr( static_cast<T*>(naGhost_ptr(me)) );
491 return (obj->*func)(argc, args);
495 typedef std::auto_ptr<Ghost> GhostPtr;
498 explicit Ghost(const std::string& name):
499 GhostMetadata( name )
501 _ghost_type.destroy = &destroyGhost;
502 _ghost_type.name = _name.c_str();
503 _ghost_type.get_member = &getMember;
504 _ghost_type.set_member = &setMember;
507 static GhostPtr& getSingletonHolder()
509 static GhostPtr instance;
513 static naRef makeGhost(naContext c, void *ptr)
515 return naNewGhost2(c, &getSingletonPtr()->_ghost_type, ptr);
518 static void destroyGhost(void *ptr)
524 * Callback for retrieving a ghost member.
526 static const char* getMember(naContext c, void* g, naRef key, naRef* out)
528 const std::string key_str = nasal::from_nasal<std::string>(c, key);
529 if( key_str == "parents" )
531 if( getSingletonPtr()->_parents.empty() )
534 *out = getSingletonPtr()->getParents(c);
538 typename MemberMap::iterator member =
539 getSingletonPtr()->_members.find(key_str);
541 if( member == getSingletonPtr()->_members.end() )
544 if( member->second.func )
545 *out = nasal::to_nasal(c, member->second.func);
546 else if( !member->second.getter.empty() )
547 *out = member->second.getter(c, Ghost::getRawPtr(g));
549 return "Read-protected member";
555 * Callback for writing to a ghost member.
557 static void setMember(naContext c, void* g, naRef field, naRef val)
559 const std::string key = nasal::from_nasal<std::string>(c, field);
560 typename MemberMap::iterator member =
561 getSingletonPtr()->_members.find(key);
563 if( member == getSingletonPtr()->_members.end() )
564 naRuntimeError(c, "ghost: No such member: %s", key.c_str());
565 if( member->second.setter.empty() )
566 naRuntimeError(c, "ghost: Write protected member: %s", key.c_str());
568 member->second.setter(c, Ghost::getRawPtr(g), val);
574 #endif /* SG_NASAL_GHOST_HXX_ */