]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/Ghost.hxx
C++/Nasal bindings: allow registering free function as member
[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
88     /**
89      * Create a shared pointer on the heap to handle the reference counting for
90      * the passed shared pointer while it is used in Nasal space.
91      */
92     static T* createInstance(const T& ptr)
93     {
94       return new T(ptr);
95     }
96
97     static raw_type* getRawPtr(void* ptr)
98     {
99       return static_cast<T*>(ptr)->get();
100     }
101   };
102
103   /**
104    * Policy for creating ghost instances as raw objects on the heap.
105    */
106   template<class T>
107   struct RawPointerPolicy
108   {
109     typedef typename GhostTypeTraits<T>::raw_type raw_type;
110
111     /**
112      * Create a new object instance on the heap
113      */
114     static T* createInstance()
115     {
116       return new T();
117     }
118
119     static raw_type* getRawPtr(void* ptr)
120     {
121       BOOST_STATIC_ASSERT((boost::is_same<raw_type, T>::value));
122       return static_cast<T*>(ptr);
123     }
124   };
125
126   namespace internal
127   {
128     /**
129      * Metadata for Ghost object types
130      */
131     class GhostMetadata
132     {
133       public:
134         /**
135          * Add a nasal base class to the ghost. Will be available in the ghosts
136          * parents array.
137          */
138         void addNasalBase(const naRef& parent)
139         {
140           assert( naIsHash(parent) );
141           _parents.push_back(parent);
142         }
143
144       protected:
145         const std::string             _name;
146         naGhostType                   _ghost_type;
147   //      std::vector<GhostMetadata*>   _base_classes;
148         std::vector<naRef>            _parents;
149
150         explicit GhostMetadata(const std::string& name):
151           _name(name)
152         {
153
154         }
155
156   //      void addBaseClass(GhostMetadata* base)
157   //      {
158   //        assert(base);
159   //        _base_classes.push_back(base);
160   //      }
161
162         naRef getParents(naContext c)
163         {
164           return nasal::to_nasal(c, _parents);
165         }
166     };
167   }
168
169   /**
170    * Class for exposing C++ objects to Nasal
171    *
172    * @code{cpp}
173    * // Example class to be exposed to Nasal
174    * class MyClass
175    * {
176    *   public:
177    *     void setX(int x);
178    *     int getX() const;
179    *
180    *     naRef myMember(naContext c, int argc, naRef* args);
181    * }
182    *
183    * void exposeClasses()
184    * {
185    *   // Register a nasal ghost type for MyClass. This needs to be done only
186    *   // once before creating the first ghost instance.
187    *   Ghost<MyClass>::init("MyClass")
188    *     // Members can be exposed by getters and setters
189    *     .member("x", &MyClass::getX, &MyClass::setX)
190    *     // For readonly variables only pass a getter
191    *     .member("x_readonly", &MyClass::getX)
192    *     // It is also possible to expose writeonly members
193    *     .member("x_writeonly", &MyClass::setX)
194    *     // Methods use a slightly different syntax - The pointer to the member
195    *     // function has to be passed as template argument
196    *     .method<&MyClass::myMember>("myMember");
197    * }
198    * @endcode
199    */
200   template<class T>
201   class Ghost:
202     public internal::GhostMetadata,
203     protected boost::mpl::if_< typename GhostTypeTraits<T>::is_shared,
204                                SharedPointerPolicy<T>,
205                                RawPointerPolicy<T> >::type
206   {
207     public:
208       typedef typename GhostTypeTraits<T>::raw_type                 raw_type;
209       typedef naRef (raw_type::*member_func_t)(naContext, int, naRef*);
210       typedef naRef (*free_func_t)(raw_type&, naContext, int, naRef*);
211       typedef boost::function<naRef(naContext, raw_type*)>        getter_t;
212       typedef boost::function<void(naContext, raw_type*, naRef)>  setter_t;
213
214       /**
215        * A ghost member. Can consist either of getter and/or setter functions
216        * for exposing a data variable or a single callable function.
217        */
218       struct member_t
219       {
220         member_t():
221           func(0)
222         {}
223
224         member_t( const getter_t& getter,
225                   const setter_t& setter,
226                   naCFunction func = 0 ):
227           getter( getter ),
228           setter( setter ),
229           func( func )
230         {}
231
232         member_t(naCFunction func):
233           func( func )
234         {}
235
236         getter_t    getter;
237         setter_t    setter;
238         naCFunction func;
239       };
240
241       typedef std::map<std::string, member_t> MemberMap;
242
243       /**
244        * Register a new ghost type.
245        *
246        * @param name    Descriptive name of the ghost type.
247        */
248       static Ghost& init(const std::string& name)
249       {
250         getSingletonHolder().reset( new Ghost(name) );
251         return *getSingletonPtr();
252       }
253
254       /**
255        * Register a base class for this ghost. The base class needs to be
256        * registers on its own before it can be used as base class.
257        *
258        * @tparam BaseGhost  Type of base class already wrapped into Ghost class
259        *                    (Ghost<Base>)
260        *
261        * @code{cpp}
262        * Ghost<MyBase>::init("MyBase");
263        * Ghost<MyClass>::init("MyClass")
264        *   .bases<Ghost<MyBase> >();
265        * @endcode
266        */
267       template<class BaseGhost>
268       typename boost::enable_if_c
269         <    boost::is_base_of<GhostMetadata, BaseGhost>::value
270           && boost::is_base_of<typename BaseGhost::raw_type, raw_type>::value,
271           Ghost
272         >::type&
273       bases()
274       {
275         BaseGhost* base = BaseGhost::getSingletonPtr();
276         //addBaseClass( base );
277
278         // Replace any getter that is not available in the current class.
279         // TODO check if this is the correct behavior of function overriding
280         for( typename BaseGhost::MemberMap::const_iterator member =
281                base->_members.begin();
282                member != base->_members.end();
283              ++member )
284         {
285           if( _members.find(member->first) == _members.end() )
286             _members[member->first] = member_t
287             (
288               member->second.getter,
289               member->second.setter,
290               member->second.func
291             );
292         }
293
294         return *this;
295       }
296
297       /**
298        * Register a base class for this ghost. The base class needs to be
299        * registers on its own before it can be used as base class.
300        *
301        * @tparam Base   Type of base class (Base as used in Ghost<Base>)
302        *
303        * @code{cpp}
304        * Ghost<MyBase>::init("MyBase");
305        * Ghost<MyClass>::init("MyClass")
306        *   .bases<MyBase>();
307        * @endcode
308        */
309       template<class Base>
310       typename boost::enable_if_c
311         <   !boost::is_base_of<GhostMetadata, Base>::value
312           && boost::is_base_of<typename Ghost<Base>::raw_type, raw_type>::value,
313           Ghost
314         >::type&
315       bases()
316       {
317         return bases< Ghost<Base> >();
318       }
319
320       /**
321        * Register an existing Nasal class/hash as base class for this ghost.
322        *
323        * @param parent  Nasal hash/class
324        */
325       Ghost& bases(const naRef& parent)
326       {
327         addNasalBase(parent);
328         return *this;
329       }
330
331       /**
332        * Register a member variable by passing a getter and/or setter method.
333        *
334        * @param field   Name of member
335        * @param getter  Getter for variable
336        * @param setter  Setter for variable (Pass 0 to prevent write access)
337        *
338        */
339       template<class Var>
340       Ghost& member( const std::string& field,
341                      Var (raw_type::*getter)() const,
342                      void (raw_type::*setter)(Var) = 0 )
343       {
344         member_t m;
345         if( getter )
346         {
347           naRef (*to_nasal_)(naContext, Var) = &nasal::to_nasal;
348
349           // Getter signature: naRef(naContext, raw_type*)
350           m.getter = boost::bind(to_nasal_, _1, boost::bind(getter, _2));
351         }
352
353         if( setter )
354         {
355           Var (*from_nasal_)(naContext, naRef) = &nasal::from_nasal;
356
357           // Setter signature: void(naContext, raw_type*, naRef)
358           m.setter = boost::bind(setter, _2, boost::bind(from_nasal_, _1, _3));
359         }
360
361         return member(field, m.getter, m.setter);
362       }
363
364       /**
365        * Register a write only member variable.
366        *
367        * @param field   Name of member
368        * @param setter  Setter for variable
369        */
370       template<class Var>
371       Ghost& member( const std::string& field,
372                      void (raw_type::*setter)(Var) )
373       {
374         return member<Var>(field, 0, setter);
375       }
376
377       /**
378        * Register a member variable by passing a getter and/or setter method.
379        *
380        * @param field   Name of member
381        * @param getter  Getter for variable
382        * @param setter  Setter for variable (Pass empty to prevent write access)
383        *
384        */
385       Ghost& member( const std::string& field,
386                      const getter_t& getter,
387                      const setter_t& setter = setter_t() )
388       {
389         if( !getter.empty() || !setter.empty() )
390           _members[field] = member_t(getter, setter);
391         else
392           SG_LOG
393           (
394             SG_NASAL,
395             SG_WARN,
396             "Member '" << field << "' requires a getter or setter"
397           );
398         return *this;
399       }
400
401       /**
402        * Register a member function.
403        *
404        * @note Because only function pointers can be registered as Nasal
405        *       functions it is needed to pass the function pointer as template
406        *       argument. This allows us to create a separate instance of the
407        *       MemberFunctionWrapper for each registered function and therefore
408        *       provides us with the needed static functions to be passed on to
409        *       Nasal.
410        *
411        * @tparam func   Pointer to member function being registered.
412        *
413        * @code{cpp}
414        * class MyClass
415        * {
416        *   public:
417        *     naRef myMethod(naContext c, int argc, naRef* args);
418        * }
419        *
420        * Ghost<MyClass>::init("Test")
421        *   .method<&MyClass::myMethod>("myMethod");
422        * @endcode
423        */
424       template<member_func_t func>
425       Ghost& method(const std::string& name)
426       {
427         _members[name].func = &MemberFunctionWrapper<func>::call;
428         return *this;
429       }
430
431       /**
432        * Register a free function as member function. The object instance is
433        * passed as additional first argument.
434        *
435        * @tparam func   Pointer to free function being registered.
436        *
437        * @code{cpp}
438        * class MyClass;
439        * naRef myMethod(MyClass& obj, naContext c, int argc, naRef* args);
440        *
441        * Ghost<MyClass>::init("Test")
442        *   .method<&myMethod>("myMethod");
443        * @endcode
444        */
445       template<free_func_t func>
446       Ghost& method(const std::string& name)
447       {
448         _members[name].func = &FreeFunctionWrapper<func>::call;
449         return *this;
450       }
451
452       // TODO use variadic template when supporting C++11
453       /**
454        * Create a Nasal instance of this ghost.
455        *
456        * @param c   Active Nasal context
457        */
458       static naRef create( naContext c )
459       {
460         return makeGhost(c, Ghost::createInstance());
461       }
462
463       /**
464        * Create a Nasal instance of this ghost.
465        *
466        * @param c   Active Nasal context
467        * @param a1  Parameter used for creating new instance
468        */
469       template<class A1>
470       static naRef create( naContext c, const A1& a1 )
471       {
472         return makeGhost(c, Ghost::createInstance(a1));
473       }
474
475       /**
476        * Nasal callback for creating a new instance of this ghost.
477        */
478       static naRef f_create(naContext c, naRef me, int argc, naRef* args)
479       {
480         return create(c);
481       }
482
483     private:
484
485       template<class>
486       friend class Ghost;
487
488       static Ghost* getSingletonPtr()
489       {
490         return getSingletonHolder().get();
491       }
492
493       // TODO integrate with from_nasal template to be able to cast objects
494       //      passed as function argument.
495       static raw_type* from_nasal(naRef me)
496       {
497         if( naGhost_type(me) != &getSingletonPtr()->_ghost_type )
498           return 0;
499
500         return Ghost::getRawPtr( static_cast<T*>(naGhost_ptr(me)) );
501       }
502
503       static raw_type* requireObject(naContext c, naRef me)
504       {
505         raw_type* obj = Ghost::from_nasal(me);
506         if( !obj )
507           naRuntimeError
508           (
509             c,
510             "method called on object of wrong type: '%s' expected",
511             getSingletonPtr()->_ghost_type.name
512           );
513
514         return obj;
515       }
516
517       /**
518        * Wrapper class to enable registering pointers to member functions as
519        * Nasal function callbacks. We need to use the function pointer as
520        * template parameter to ensure every registered function gets a static
521        * function which can be passed to Nasal.
522        */
523       template<member_func_t func>
524       struct MemberFunctionWrapper
525       {
526         /**
527          * Called from Nasal upon invocation of the according registered
528          * function. Forwards the call to the passed object instance.
529          */
530         static naRef call(naContext c, naRef me, int argc, naRef* args)
531         {
532           return (requireObject(c, me)->*func)(c, argc, args);
533         }
534       };
535
536       /**
537        * Wrapper class to enable registering pointers to free functions (only
538        * external linkage). We need to use the function pointer as template
539        * parameter to ensure every registered function gets a static function
540        * which can be passed to Nasal. Even though we just wrap another simple
541        * function pointer this intermediate step is need to be able to retrieve
542        * the object the function call belongs to and pass it along as argument.
543        */
544       template<free_func_t func>
545       struct FreeFunctionWrapper
546       {
547         /**
548          * Called from Nasal upon invocation of the according registered
549          * function. Forwards the call to the passed function pointer and passes
550          * the required parameters.
551          */
552         static naRef call(naContext c, naRef me, int argc, naRef* args)
553         {
554           return func(*requireObject(c, me), c, argc, args);
555         }
556       };
557
558       typedef std::auto_ptr<Ghost> GhostPtr;
559       MemberMap _members;
560
561       explicit Ghost(const std::string& name):
562         GhostMetadata( name )
563       {
564         _ghost_type.destroy = &destroyGhost;
565         _ghost_type.name = _name.c_str();
566         _ghost_type.get_member = &getMember;
567         _ghost_type.set_member = &setMember;
568       }
569
570       static GhostPtr& getSingletonHolder()
571       {
572         static GhostPtr instance;
573         return instance;
574       }
575
576       static naRef makeGhost(naContext c, void *ptr)
577       {
578         return naNewGhost2(c, &getSingletonPtr()->_ghost_type, ptr);
579       }
580
581       static void destroyGhost(void *ptr)
582       {
583         delete (T*)ptr;
584       }
585
586       /**
587        * Callback for retrieving a ghost member.
588        */
589       static const char* getMember(naContext c, void* g, naRef key, naRef* out)
590       {
591         const std::string key_str = nasal::from_nasal<std::string>(c, key);
592         if( key_str == "parents" )
593         {
594           if( getSingletonPtr()->_parents.empty() )
595             return 0;
596
597           *out = getSingletonPtr()->getParents(c);
598           return "";
599         }
600
601         typename MemberMap::iterator member =
602           getSingletonPtr()->_members.find(key_str);
603
604         if( member == getSingletonPtr()->_members.end() )
605           return 0;
606
607         if( member->second.func )
608           *out = nasal::to_nasal(c, member->second.func);
609         else if( !member->second.getter.empty() )
610           *out = member->second.getter(c, Ghost::getRawPtr(g));
611         else
612           return "Read-protected member";
613
614         return "";
615       }
616
617       /**
618        * Callback for writing to a ghost member.
619        */
620       static void setMember(naContext c, void* g, naRef field, naRef val)
621       {
622         const std::string key = nasal::from_nasal<std::string>(c, field);
623         typename MemberMap::iterator member =
624           getSingletonPtr()->_members.find(key);
625
626         if( member == getSingletonPtr()->_members.end() )
627           naRuntimeError(c, "ghost: No such member: %s", key.c_str());
628         if( member->second.setter.empty() )
629           naRuntimeError(c, "ghost: Write protected member: %s", key.c_str());
630
631         member->second.setter(c, Ghost::getRawPtr(g), val);
632       }
633   };
634
635 } // namespace nasal
636
637 #endif /* SG_NASAL_GHOST_HXX_ */