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