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