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