]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/Ghost.hxx
cppbind: Prepare for improved bindings.
[simgear.git] / simgear / nasal / cppbind / Ghost.hxx
1 ///@file
2 /// Expose C++ objects to Nasal as ghosts
3 ///
4 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
5 //
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.
10 //
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.
15 //
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
19
20 #ifndef SG_NASAL_GHOST_HXX_
21 #define SG_NASAL_GHOST_HXX_
22
23 #include "from_nasal.hxx"
24 #include "to_nasal.hxx"
25
26 #include <simgear/debug/logstream.hxx>
27
28 #include <boost/bind.hpp>
29 #include <boost/call_traits.hpp>
30 #include <boost/function.hpp>
31 #include <boost/lambda/lambda.hpp>
32 #include <boost/mpl/has_xxx.hpp>
33 #include <boost/utility/enable_if.hpp>
34
35 #include <map>
36
37 /**
38  * Bindings between C++ and the Nasal scripting language
39  */
40 namespace nasal
41 {
42
43   namespace internal
44   {
45     /**
46      * Metadata for Ghost object types
47      */
48     class GhostMetadata
49     {
50       public:
51         /**
52          * Add a nasal base class to the ghost. Will be available in the ghosts
53          * parents array.
54          */
55         void addNasalBase(const naRef& parent)
56         {
57           assert( naIsHash(parent) );
58           _parents.push_back(parent);
59         }
60
61         bool isBaseOf(naGhostType* ghost_type) const
62         {
63           if( ghost_type == &_ghost_type )
64             return true;
65
66           for( DerivedList::const_iterator derived = _derived_classes.begin();
67                                            derived != _derived_classes.end();
68                                          ++derived )
69           {
70             if( (*derived)->isBaseOf(ghost_type) )
71               return true;
72           }
73
74           return false;
75         }
76
77       protected:
78
79         typedef std::vector<const GhostMetadata*> DerivedList;
80
81         const std::string   _name;
82         naGhostType         _ghost_type;
83         DerivedList         _derived_classes;
84         std::vector<naRef>  _parents;
85
86         explicit GhostMetadata(const std::string& name):
87           _name(name)
88         {
89
90         }
91
92         void addDerived(const GhostMetadata* derived)
93         {
94           assert(derived);
95           _derived_classes.push_back(derived);
96
97           SG_LOG
98           (
99             SG_NASAL,
100             SG_INFO,
101             "Ghost::addDerived: " <<_ghost_type.name << " -> " << derived->_name
102           );
103         }
104
105         naRef getParents(naContext c)
106         {
107           return nasal::to_nasal(c, _parents);
108         }
109     };
110
111     BOOST_MPL_HAS_XXX_TRAIT_DEF(element_type)
112   }
113
114   /**
115    * Context passed to a function/method being called from Nasal
116    */
117   struct CallContext
118   {
119     CallContext(naContext c, size_t argc, naRef* args):
120       c(c),
121       argc(argc),
122       args(args)
123     {}
124
125     /**
126      * Get the argument with given index if it exists. Otherwise returns the
127      * passed default value.
128      *
129      * @tparam T    Type of argument (converted using ::from_nasal)
130      * @param index Index of requested argument
131      * @param def   Default value returned if too few arguments available
132      */
133     template<class T>
134     T getArg(size_t index, const T& def = T()) const
135     {
136       if( index >= argc )
137         return def;
138
139       return from_nasal<T>(c, args[index]);
140     }
141
142     /**
143      * Get the argument with given index. Raises a Nasal runtime error if there
144      * are to few arguments available.
145      */
146     template<class T>
147     T requireArg(size_t index) const
148     {
149       if( index >= argc )
150         naRuntimeError(c, "Missing required arg #%d", index);
151
152       return from_nasal<T>(c, args[index]);
153     }
154
155     naContext   c;
156     size_t      argc;
157     naRef      *args;
158   };
159
160   /**
161    * Class for exposing C++ objects to Nasal
162    *
163    * @code{cpp}
164    * // Example class to be exposed to Nasal
165    * class MyClass
166    * {
167    *   public:
168    *     void setX(int x);
169    *     int getX() const;
170    *
171    *     naRef myMember(naContext c, int argc, naRef* args);
172    * }
173    * typedef boost::shared_ptr<MyClass> MyClassPtr;
174    *
175    * void exposeClasses()
176    * {
177    *   // Register a nasal ghost type for MyClass. This needs to be done only
178    *   // once before creating the first ghost instance. The exposed class needs
179    *   // to be wrapped inside a shared pointer, eg. boost::shared_ptr.
180    *   Ghost<MyClassPtr>::init("MyClass")
181    *     // Members can be exposed by getters and setters
182    *     .member("x", &MyClass::getX, &MyClass::setX)
183    *     // For readonly variables only pass a getter
184    *     .member("x_readonly", &MyClass::getX)
185    *     // It is also possible to expose writeonly members
186    *     .member("x_writeonly", &MyClass::setX)
187    *     // Methods use a slightly different syntax - The pointer to the member
188    *     // function has to be passed as template argument
189    *     .method<&MyClass::myMember>("myMember");
190    * }
191    * @endcode
192    */
193   template<class T>
194   class Ghost:
195     public internal::GhostMetadata
196   {
197       BOOST_STATIC_ASSERT( internal::has_element_type<T>::value );
198
199     public:
200       typedef typename T::element_type                              raw_type;
201       typedef T                                                     pointer;
202       typedef naRef (raw_type::*member_func_t)(const CallContext&);
203       typedef naRef (*free_func_t)(raw_type&, const CallContext&);
204       typedef boost::function<naRef(naContext, raw_type&)>          getter_t;
205       typedef boost::function<void(naContext, raw_type&, naRef)>    setter_t;
206       typedef boost::function<naRef(raw_type&, const CallContext&)> method_t;
207       typedef boost::shared_ptr<method_t>                           method_ptr;
208
209       /**
210        * A ghost member. Can consist either of getter and/or setter functions
211        * for exposing a data variable or a single callable function.
212        */
213       struct member_t
214       {
215         member_t():
216           func(0)
217         {}
218
219         member_t( const getter_t& getter,
220                   const setter_t& setter,
221                   naCFunction func = 0 ):
222           getter( getter ),
223           setter( setter ),
224           func( func )
225         {}
226
227         member_t(naCFunction func):
228           func( func )
229         {}
230
231         getter_t    getter;
232         setter_t    setter;
233         naCFunction func;
234       };
235
236       typedef std::map<std::string, member_t> MemberMap;
237
238       /**
239        * Register a new ghost type.
240        *
241        * @note Only intialize each ghost type once!
242        *
243        * @param name    Descriptive name of the ghost type.
244        */
245       static Ghost& init(const std::string& name)
246       {
247         assert( !getSingletonPtr() );
248
249         getSingletonHolder().reset( new Ghost(name) );
250         return *getSingletonPtr();
251       }
252
253       /**
254        * Register a base class for this ghost. The base class needs to be
255        * registers on its own before it can be used as base class.
256        *
257        * @tparam BaseGhost  Type of base class already wrapped into Ghost class
258        *                    (Ghost<BasePtr>)
259        *
260        * @code{cpp}
261        * Ghost<MyBasePtr>::init("MyBase");
262        * Ghost<MyClassPtr>::init("MyClass")
263        *   .bases<Ghost<MyBasePtr> >();
264        * @endcode
265        */
266       template<class BaseGhost>
267       typename boost::enable_if
268         <
269           boost::is_base_of<GhostMetadata, BaseGhost>,
270           Ghost
271         >::type&
272       bases()
273       {
274         BOOST_STATIC_ASSERT
275         ((
276           boost::is_base_of<typename BaseGhost::raw_type, raw_type>::value
277         ));
278
279         BaseGhost* base = BaseGhost::getSingletonPtr();
280         base->addDerived
281         (
282           this,
283           // Both ways of retrieving the address of a static member function
284           // should be legal but not all compilers know this.
285           // g++-4.4.7+ has been tested to work with both versions
286 #if defined(GCC_VERSION) && GCC_VERSION < 40407
287           // The old version of g++ used on Jenkins (16.11.2012) only compiles
288           // this version.
289           &getTypeFor<BaseGhost>
290 #else
291           // VS (2008, 2010, ... ?) only allow this version.
292           &Ghost::getTypeFor<BaseGhost>
293 #endif
294         );
295
296         // Replace any getter that is not available in the current class.
297         // TODO check if this is the correct behavior of function overriding
298         for( typename BaseGhost::MemberMap::const_iterator member =
299                base->_members.begin();
300                member != base->_members.end();
301              ++member )
302         {
303           if( _members.find(member->first) == _members.end() )
304             _members[member->first] = member_t
305             (
306               member->second.getter,
307               member->second.setter,
308               member->second.func
309             );
310         }
311
312         return *this;
313       }
314
315       /**
316        * Register a base class for this ghost. The base class needs to be
317        * registers on its own before it can be used as base class.
318        *
319        * @tparam Base   Type of base class (Base as used in Ghost<BasePtr>)
320        *
321        * @code{cpp}
322        * Ghost<MyBasePtr>::init("MyBase");
323        * Ghost<MyClassPtr>::init("MyClass")
324        *   .bases<MyBasePtr>();
325        * @endcode
326        */
327       template<class Base>
328       typename boost::disable_if
329         <
330           boost::is_base_of<GhostMetadata, Base>,
331           Ghost
332         >::type&
333       bases()
334       {
335         BOOST_STATIC_ASSERT
336         ((
337           boost::is_base_of<typename Ghost<Base>::raw_type, raw_type>::value
338         ));
339
340         return bases< Ghost<Base> >();
341       }
342
343       /**
344        * Register an existing Nasal class/hash as base class for this ghost.
345        *
346        * @param parent  Nasal hash/class
347        */
348       Ghost& bases(const naRef& parent)
349       {
350         addNasalBase(parent);
351         return *this;
352       }
353
354       /**
355        * Register a member variable by passing a getter and/or setter method.
356        *
357        * @param field   Name of member
358        * @param getter  Getter for variable
359        * @param setter  Setter for variable (Pass 0 to prevent write access)
360        *
361        */
362       template<class Ret, class Param>
363       Ghost& member( const std::string& field,
364                      Ret (raw_type::*getter)() const,
365                      void (raw_type::*setter)(Param) )
366       {
367         member_t m;
368         if( getter )
369         {
370           typedef typename boost::call_traits<Ret>::param_type param_type;
371           naRef (*to_nasal_)(naContext, param_type) = &nasal::to_nasal;
372
373           // Getter signature: naRef(naContext, raw_type&)
374           m.getter = boost::bind(to_nasal_, _1, boost::bind(getter, _2));
375         }
376
377         if( setter )
378         {
379           typename boost::remove_const
380             < typename boost::remove_reference<Param>::type
381             >::type
382           (*from_nasal_)(naContext, naRef) = &nasal::from_nasal;
383
384           // Setter signature: void(naContext, raw_type&, naRef)
385           m.setter = boost::bind(setter, _2, boost::bind(from_nasal_, _1, _3));
386         }
387
388         return member(field, m.getter, m.setter);
389       }
390
391       /**
392        * Register a read only member variable.
393        *
394        * @param field   Name of member
395        * @param getter  Getter for variable
396        */
397       template<class Ret>
398       Ghost& member( const std::string& field,
399                      Ret (raw_type::*getter)() const )
400       {
401         return member<Ret, Ret>(field, getter, 0);
402       }
403
404       /**
405        * Register a write only member variable.
406        *
407        * @param field   Name of member
408        * @param setter  Setter for variable
409        */
410       template<class Var>
411       Ghost& member( const std::string& field,
412                      void (raw_type::*setter)(Var) )
413       {
414         return member<Var, Var>(field, 0, setter);
415       }
416
417       /**
418        * Register a member variable by passing a getter and/or setter method.
419        *
420        * @param field   Name of member
421        * @param getter  Getter for variable
422        * @param setter  Setter for variable (Pass empty to prevent write access)
423        *
424        */
425       Ghost& member( const std::string& field,
426                      const getter_t& getter,
427                      const setter_t& setter = setter_t() )
428       {
429         if( !getter.empty() || !setter.empty() )
430           _members[field] = member_t(getter, setter);
431         else
432           SG_LOG
433           (
434             SG_NASAL,
435             SG_WARN,
436             "Member '" << field << "' requires a getter or setter"
437           );
438         return *this;
439       }
440
441       /**
442        * Register a member function.
443        *
444        * @note Because only function pointers can be registered as Nasal
445        *       functions it is needed to pass the function pointer as template
446        *       argument. This allows us to create a separate instance of the
447        *       MemberFunctionWrapper for each registered function and therefore
448        *       provides us with the needed static functions to be passed on to
449        *       Nasal.
450        *
451        * @tparam func   Pointer to member function being registered.
452        *
453        * @code{cpp}
454        * class MyClass
455        * {
456        *   public:
457        *     naRef myMethod(naContext c, int argc, naRef* args);
458        * }
459        *
460        * Ghost<MyClassPtr>::init("Test")
461        *   .method<&MyClass::myMethod>("myMethod");
462        * @endcode
463        */
464       template<member_func_t func>
465       Ghost& method(const std::string& name)
466       {
467         _members[name].func = &MemberFunctionWrapper<func>::call;
468         return *this;
469       }
470
471       /**
472        * Invoke a method which returns a value and convert it to Nasal.
473        */
474       template<class Ret>
475       static
476       typename boost::disable_if<boost::is_void<Ret>, naRef>::type
477       method_invoker
478       (
479         const boost::function<Ret (raw_type&, const CallContext&)>& func,
480         raw_type& obj,
481         const CallContext& ctx
482       )
483       {
484         typedef typename boost::call_traits<Ret>::param_type param_type;
485         naRef (*to_nasal_)(naContext, param_type) = &nasal::to_nasal;
486
487         return to_nasal_(ctx.c, func(obj, ctx));
488       };
489
490       /**
491        * Invoke a method which returns void and "convert" it to nil.
492        */
493       template<class Ret>
494       static
495       typename boost::enable_if<boost::is_void<Ret>, naRef>::type
496       method_invoker
497       (
498         const boost::function<void (raw_type&, const CallContext&)>& func,
499         raw_type& obj,
500         const CallContext& ctx
501       )
502       {
503         func(obj, ctx);
504         return naNil();
505       };
506
507       /**
508        * Bind any callable entity as method callable from Nasal
509        *
510        * Does not really register method yet!!!
511        */
512       template<class Ret>
513       Ghost& method
514       (
515         const std::string& name,
516         const boost::function<Ret (raw_type&, const CallContext&)>& func
517       )
518       {
519 //        _members[name].func.reset
520 //        (
521           new method_t( boost::bind(method_invoker<Ret>, func, _1, _2) );
522 //        );
523         return *this;
524       }
525
526       template<class Ret>
527       struct method_raw
528       {
529         typedef boost::function<Ret (raw_type&, const CallContext&)> type;
530       };
531
532       /**
533        * Bind member function as method callable from Nasal
534        *
535        * Does not really register method yet!!!
536        */
537       template<class Ret>
538       Ghost& method( const std::string& name,
539                      Ret (raw_type::*fn)() const )
540       {
541         return method<Ret>
542         (
543           name,
544           typename method_raw<Ret>::type(boost::bind(fn, _1))
545         );
546       }
547
548       /**
549        * Register a free function as member function. The object instance is
550        * passed as additional first argument.
551        *
552        * @tparam func   Pointer to free function being registered.
553        *
554        * @note Due to a severe bug in Visual Studio it is not possible to create
555        *       a specialization of #method for free function pointers and
556        *       member function pointers at the same time. Do overcome this
557        *       limitation we had to use a different name for this function.
558        *
559        * @code{cpp}
560        * class MyClass;
561        * naRef myMethod(MyClass& obj, naContext c, int argc, naRef* args);
562        *
563        * Ghost<MyClassPtr>::init("Test")
564        *   .method_func<&myMethod>("myMethod");
565        * @endcode
566        */
567       template<free_func_t func>
568       Ghost& method_func(const std::string& name)
569       {
570         _members[name].func = &FreeFunctionWrapper<func>::call;
571         return *this;
572       }
573
574       // TODO use variadic template when supporting C++11
575       /**
576        * Create a Nasal instance of this ghost.
577        *
578        * @param c   Active Nasal context
579        */
580       // TODO check if default constructor exists
581 //      static naRef create( naContext c )
582 //      {
583 //        return makeGhost(c, createInstance());
584 //      }
585
586       /**
587        * Create a Nasal instance of this ghost.
588        *
589        * @param c   Active Nasal context
590        * @param a1  Parameter used for creating new instance
591        */
592       template<class A1>
593       static naRef create( naContext c, const A1& a1 )
594       {
595         return makeGhost(c, createInstance(a1));
596       }
597
598       /**
599        * Nasal callback for creating a new instance of this ghost.
600        */
601       static naRef f_create(naContext c, naRef me, int argc, naRef* args)
602       {
603         return create(c);
604       }
605
606       static bool isBaseOf(naGhostType* ghost_type)
607       {
608         if( !ghost_type )
609           return false;
610
611         return getSingletonPtr()->GhostMetadata::isBaseOf(ghost_type);
612       }
613
614       static bool isBaseOf(naRef obj)
615       {
616         return isBaseOf( naGhost_type(obj) );
617       }
618
619       /**
620        * Convert Nasal object to C++ object. To get a valid object the passed
621        * Nasal objects has to be derived class of the target class (Either
622        * derived in C++ or in Nasal using a 'parents' vector)
623        */
624       static pointer fromNasal(naContext c, naRef me)
625       {
626         // Check if it's a ghost and if it can be converted
627         if( isBaseOf( naGhost_type(me) ) )
628           return getPtr( naGhost_ptr(me) );
629
630         // Now if it is derived from a ghost (hash with ghost in parent vector)
631         else if( naIsHash(me) )
632         {
633           naRef na_parents = naHash_cget(me, const_cast<char*>("parents"));
634           if( !naIsVector(na_parents) )
635           {
636             SG_LOG(SG_NASAL, SG_DEBUG, "Missing 'parents' vector for ghost");
637             return pointer();
638           }
639
640           typedef std::vector<naRef> naRefs;
641           naRefs parents = from_nasal<naRefs>(c, na_parents);
642           for( naRefs::const_iterator parent = parents.begin();
643                                       parent != parents.end();
644                                     ++parent )
645           {
646             pointer ptr = fromNasal(c, *parent);
647             if( ptr )
648               return ptr;
649           }
650         }
651
652         return pointer();
653       }
654
655     private:
656
657       template<class>
658       friend class Ghost;
659
660       typedef naGhostType* (*type_checker_t)(const raw_type*);
661       typedef std::vector<type_checker_t> DerivedList;
662       DerivedList _derived_types;
663
664       /**
665        * Create a shared pointer on the heap to handle the reference counting
666        * for the passed shared pointer while it is used in Nasal space.
667        */
668       static pointer* createInstance(const pointer& ptr)
669       {
670         return ptr ? new pointer(ptr) : 0;
671       }
672
673       static pointer getPtr(void* ptr)
674       {
675         if( ptr )
676           return *static_cast<pointer*>(ptr);
677         else
678           return pointer();
679       }
680
681       static raw_type* getRawPtr(void* ptr)
682       {
683         if( ptr )
684           return static_cast<pointer*>(ptr)->get();
685         else
686           return 0;
687       }
688
689       static raw_type* getRawPtr(const pointer& ptr)
690       {
691         return ptr.get();
692       }
693
694       void addDerived( const internal::GhostMetadata* derived_meta,
695                        const type_checker_t& derived_info )
696       {
697         GhostMetadata::addDerived(derived_meta);
698         _derived_types.push_back(derived_info);
699       }
700
701       template<class BaseGhost>
702       static
703       typename boost::enable_if
704         < boost::is_polymorphic<typename BaseGhost::raw_type>,
705           naGhostType*
706         >::type
707       getTypeFor(const typename BaseGhost::raw_type* base)
708       {
709         // Check first if passed pointer can by converted to instance of class
710         // this ghost wraps.
711         if(   !boost::is_same
712                  < typename BaseGhost::raw_type,
713                    typename Ghost::raw_type
714                  >::value
715             && dynamic_cast<const typename Ghost::raw_type*>(base) != base )
716           return 0;
717
718         // Now check if we can further downcast to one of our derived classes.
719         for( typename DerivedList::reverse_iterator
720                derived = getSingletonPtr()->_derived_types.rbegin();
721                derived != getSingletonPtr()->_derived_types.rend();
722              ++derived )
723         {
724           naGhostType* ghost_type =
725             (*derived)( static_cast<const typename Ghost::raw_type*>(base) );
726           if( ghost_type )
727             return ghost_type;
728         }
729
730         // If base is not an instance of any derived class, this class has to
731         // be the dynamic type.
732         return &getSingletonPtr()->_ghost_type;
733       }
734
735       template<class BaseGhost>
736       static
737       typename boost::disable_if
738         < boost::is_polymorphic<typename BaseGhost::raw_type>,
739           naGhostType*
740         >::type
741       getTypeFor(const typename BaseGhost::raw_type* base)
742       {
743         // For non polymorphic classes there is no possibility to get the actual
744         // dynamic type, therefore we can only use its static type.
745         return &BaseGhost::getSingletonPtr()->_ghost_type;
746       }
747
748       static Ghost* getSingletonPtr()
749       {
750         return getSingletonHolder().get();
751       }
752
753       static raw_type& requireObject(naContext c, naRef me)
754       {
755         raw_type* obj = getRawPtr( fromNasal(c, me) );
756         naGhostType* ghost_type = naGhost_type(me);
757
758         if( !obj )
759           naRuntimeError
760           (
761             c,
762             "method called on object of wrong type: is '%s' expected '%s'",
763             ghost_type ? ghost_type->name : "unknown",
764             getSingletonPtr()->_ghost_type.name
765           );
766
767         return *obj;
768       }
769
770       /**
771        * Wrapper class to enable registering pointers to member functions as
772        * Nasal function callbacks. We need to use the function pointer as
773        * template parameter to ensure every registered function gets a static
774        * function which can be passed to Nasal.
775        */
776       template<member_func_t func>
777       struct MemberFunctionWrapper
778       {
779         /**
780          * Called from Nasal upon invocation of the according registered
781          * function. Forwards the call to the passed object instance.
782          */
783         static naRef call(naContext c, naRef me, int argc, naRef* args)
784         {
785           return (requireObject(c, me).*func)(CallContext(c, argc, args));
786         }
787       };
788
789       /**
790        * Wrapper class to enable registering pointers to free functions (only
791        * external linkage). We need to use the function pointer as template
792        * parameter to ensure every registered function gets a static function
793        * which can be passed to Nasal. Even though we just wrap another simple
794        * function pointer this intermediate step is need to be able to retrieve
795        * the object the function call belongs to and pass it along as argument.
796        */
797       template<free_func_t func>
798       struct FreeFunctionWrapper
799       {
800         /**
801          * Called from Nasal upon invocation of the according registered
802          * function. Forwards the call to the passed function pointer and passes
803          * the required parameters.
804          */
805         static naRef call(naContext c, naRef me, int argc, naRef* args)
806         {
807           return func(requireObject(c, me), CallContext(c, argc, args));
808         }
809       };
810
811       typedef std::auto_ptr<Ghost> GhostPtr;
812       MemberMap _members;
813
814       explicit Ghost(const std::string& name):
815         GhostMetadata( name )
816       {
817         _ghost_type.destroy = &destroyGhost;
818         _ghost_type.name = _name.c_str();
819         _ghost_type.get_member = &getMember;
820         _ghost_type.set_member = &setMember;
821       }
822
823       static GhostPtr& getSingletonHolder()
824       {
825         static GhostPtr instance;
826         return instance;
827       }
828
829       static naRef makeGhost(naContext c, void *ptr)
830       {
831         if( getRawPtr(ptr) )
832         {
833           // We are wrapping shared pointers to already existing objects which
834           // will then be hold be a new shared pointer. We therefore have to
835           // check for the dynamic type of the object as it might differ from
836           // the passed static type.
837           naGhostType* ghost_type = getTypeFor<Ghost>( getRawPtr(ptr) );
838
839           if( ghost_type )
840             return naNewGhost2(c, ghost_type, ptr);
841         }
842
843         destroyGhost(ptr);
844         return naNil();
845       }
846
847       static void destroyGhost(void *ptr)
848       {
849         delete static_cast<pointer*>(ptr);
850       }
851
852       /**
853        * Callback for retrieving a ghost member.
854        */
855       static const char* getMember(naContext c, void* g, naRef key, naRef* out)
856       {
857         const std::string key_str = nasal::from_nasal<std::string>(c, key);
858         if( key_str == "parents" )
859         {
860           if( getSingletonPtr()->_parents.empty() )
861             return 0;
862
863           *out = getSingletonPtr()->getParents(c);
864           return "";
865         }
866
867         typename MemberMap::iterator member =
868           getSingletonPtr()->_members.find(key_str);
869
870         if( member == getSingletonPtr()->_members.end() )
871           return 0;
872
873         if( member->second.func )
874           *out = nasal::to_nasal(c, member->second.func);
875         else if( !member->second.getter.empty() )
876           *out = member->second.getter(c, *getRawPtr(g));
877         else
878           return "Read-protected member";
879
880         return "";
881       }
882
883       /**
884        * Callback for writing to a ghost member.
885        */
886       static void setMember(naContext c, void* g, naRef field, naRef val)
887       {
888         const std::string key = nasal::from_nasal<std::string>(c, field);
889         typename MemberMap::iterator member =
890           getSingletonPtr()->_members.find(key);
891
892         if( member == getSingletonPtr()->_members.end() )
893           naRuntimeError(c, "ghost: No such member: %s", key.c_str());
894         if( member->second.setter.empty() )
895           naRuntimeError(c, "ghost: Write protected member: %s", key.c_str());
896
897         member->second.setter(c, *getRawPtr(g), val);
898       }
899   };
900
901 } // namespace nasal
902
903 #endif /* SG_NASAL_GHOST_HXX_ */