]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/NasalCallContext.hxx
Canvas/Layout: tweak the way elements are exposed to Nasal.
[simgear.git] / simgear / nasal / cppbind / NasalCallContext.hxx
1 ///@file
2 /// Call context for Nasal extension functions
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_CALL_CONTEXT_HXX_
21 #define SG_NASAL_CALL_CONTEXT_HXX_
22
23 #include "from_nasal.hxx"
24 #include "to_nasal.hxx"
25
26 namespace nasal
27 {
28
29   /**
30    * Context passed to a function/method being called from Nasal
31    */
32   class CallContext
33   {
34     public:
35       CallContext(naContext c, size_t argc, naRef* args):
36         c(c),
37         argc(argc),
38         args(args)
39       {}
40
41 #define SG_CTX_CHECK_ARG(name, check)\
42       bool is##name(size_t index) const\
43       {\
44         return (index < argc && naIs##check(args[index]));\
45       }
46
47       SG_CTX_CHECK_ARG(Code,    Code)
48       SG_CTX_CHECK_ARG(CCode,   CCode)
49       SG_CTX_CHECK_ARG(Func,    Func)
50       SG_CTX_CHECK_ARG(Ghost,   Ghost)
51       SG_CTX_CHECK_ARG(Hash,    Hash)
52       SG_CTX_CHECK_ARG(Nil,     Nil)
53       SG_CTX_CHECK_ARG(Numeric, Num)
54       SG_CTX_CHECK_ARG(Scalar,  Scalar)
55       SG_CTX_CHECK_ARG(String,  String)
56       SG_CTX_CHECK_ARG(Vector,  Vector)
57
58 #undef SG_CTX_CHECK_ARG
59
60       void popFront(size_t num = 1)
61       {
62         if( argc < num )
63           return;
64
65         args += num;
66         argc -= num;
67       }
68
69       void popBack(size_t num = 1)
70       {
71         if( argc < num )
72           return;
73
74         argc -= num;
75       }
76
77       /**
78        * Get the argument with given index if it exists. Otherwise returns the
79        * passed default value.
80        *
81        * @tparam T    Type of argument (converted using ::from_nasal)
82        * @param index Index of requested argument
83        * @param def   Default value returned if too few arguments available
84        */
85       template<class T>
86       typename from_nasal_ptr<T>::return_type
87       getArg(size_t index, const T& def = T()) const
88       {
89         if( index >= argc )
90           return def;
91
92         return from_nasal<T>(args[index]);
93       }
94
95       /**
96        * Get the argument with given index. Raises a Nasal runtime error if
97        * there are to few arguments available.
98        */
99       template<class T>
100       typename from_nasal_ptr<T>::return_type
101       requireArg(size_t index) const
102       {
103         if( index >= argc )
104           naRuntimeError(c, "Missing required arg #%d", index);
105
106         return from_nasal<T>(args[index]);
107       }
108
109       template<class T>
110       naRef to_nasal(T arg) const
111       {
112         return nasal::to_nasal(c, arg);
113       }
114
115       template<class T>
116       typename from_nasal_ptr<T>::return_type
117       from_nasal(naRef ref) const
118       {
119         return (*from_nasal_ptr<T>::get())(c, ref);
120       }
121
122       naContext   c;
123       size_t      argc;
124       naRef      *args;
125   };
126
127 } // namespace nasal
128
129
130 #endif /* SG_NASAL_CALL_CONTEXT_HXX_ */