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