2 /// Call context for Nasal extension functions
4 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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
20 #ifndef SG_NASAL_CALL_CONTEXT_HXX_
21 #define SG_NASAL_CALL_CONTEXT_HXX_
23 #include "from_nasal.hxx"
24 #include "to_nasal.hxx"
30 * Context passed to a function/method being called from Nasal
35 CallContext(naContext c, size_t argc, naRef* args):
41 #define SG_CTX_CHECK_ARG(name, check)\
42 bool is##name(size_t index) const\
44 return (index < argc && naIs##check(args[index]));\
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)
58 #undef SG_CTX_CHECK_ARG
60 void popFront(size_t num = 1)
69 void popBack(size_t num = 1)
78 * Get the argument with given index if it exists. Otherwise returns the
79 * passed default value.
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
86 typename from_nasal_ptr<T>::return_type
87 getArg(size_t index, const T& def = T()) const
92 return from_nasal<T>(args[index]);
96 * Get the argument with given index. Raises a Nasal runtime error if
97 * there are to few arguments available.
100 typename from_nasal_ptr<T>::return_type
101 requireArg(size_t index) const
104 naRuntimeError(c, "Missing required arg #%d", index);
106 return from_nasal<T>(args[index]);
110 naRef to_nasal(T arg) const
112 return nasal::to_nasal(c, arg);
116 typename from_nasal_ptr<T>::return_type
117 from_nasal(naRef ref) const
119 return (*from_nasal_ptr<T>::get())(c, ref);
130 #endif /* SG_NASAL_CALL_CONTEXT_HXX_ */