]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/NasalCallContext.hxx
cppbind.Ghost: clean up a bit
[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       bool isNumeric(size_t index) const
42       {
43         return (index < argc && naIsNum(args[index]));
44       }
45
46       bool isString(size_t index) const
47       {
48         return (index < argc && naIsString(args[index]));
49       }
50
51       bool isHash(size_t index) const
52       {
53         return (index < argc && naIsHash(args[index]));
54       }
55
56       bool isVector(size_t index) const
57       {
58         return (index < argc && naIsVector(args[index]));
59       }
60
61       bool isGhost(size_t index) const
62       {
63         return (index < argc && naIsGhost(args[index]));
64       }
65
66       void popFront(size_t num = 1)
67       {
68         if( argc < num )
69           return;
70
71         args += num;
72         argc -= num;
73       }
74
75       void popBack(size_t num = 1)
76       {
77         if( argc < num )
78           return;
79
80         argc -= num;
81       }
82
83       /**
84        * Get the argument with given index if it exists. Otherwise returns the
85        * passed default value.
86        *
87        * @tparam T    Type of argument (converted using ::from_nasal)
88        * @param index Index of requested argument
89        * @param def   Default value returned if too few arguments available
90        */
91       template<class T>
92       typename from_nasal_ptr<T>::return_type
93       getArg(size_t index, const T& def = T()) const
94       {
95         if( index >= argc )
96           return def;
97
98         return from_nasal<T>(args[index]);
99       }
100
101       /**
102        * Get the argument with given index. Raises a Nasal runtime error if
103        * there are to few arguments available.
104        */
105       template<class T>
106       typename from_nasal_ptr<T>::return_type
107       requireArg(size_t index) const
108       {
109         if( index >= argc )
110           naRuntimeError(c, "Missing required arg #%d", index);
111
112         return from_nasal<T>(args[index]);
113       }
114
115       template<class T>
116       naRef to_nasal(T arg) const
117       {
118         return nasal::to_nasal(c, arg);
119       }
120
121       template<class T>
122       typename from_nasal_ptr<T>::return_type
123       from_nasal(naRef ref) const
124       {
125         return (*from_nasal_ptr<T>::get())(c, ref);
126       }
127
128       naContext   c;
129       size_t      argc;
130       naRef      *args;
131   };
132
133 } // namespace nasal
134
135
136 #endif /* SG_NASAL_CALL_CONTEXT_HXX_ */