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 bool isNumeric(size_t index) const
43 return (index < argc && naIsNum(args[index]));
46 bool isString(size_t index) const
48 return (index < argc && naIsString(args[index]));
51 bool isHash(size_t index) const
53 return (index < argc && naIsHash(args[index]));
56 bool isVector(size_t index) const
58 return (index < argc && naIsVector(args[index]));
61 bool isGhost(size_t index) const
63 return (index < argc && naIsGhost(args[index]));
66 void popFront(size_t num = 1)
75 void popBack(size_t num = 1)
84 * Get the argument with given index if it exists. Otherwise returns the
85 * passed default value.
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
92 typename from_nasal_ptr<T>::return_type
93 getArg(size_t index, const T& def = T()) const
98 return from_nasal<T>(args[index]);
102 * Get the argument with given index. Raises a Nasal runtime error if
103 * there are to few arguments available.
106 typename from_nasal_ptr<T>::return_type
107 requireArg(size_t index) const
110 naRuntimeError(c, "Missing required arg #%d", index);
112 return from_nasal<T>(args[index]);
116 naRef to_nasal(T arg) const
118 return nasal::to_nasal(c, arg);
122 typename from_nasal_ptr<T>::return_type
123 from_nasal(naRef ref) const
125 return (*from_nasal_ptr<T>::get())(c, ref);
136 #endif /* SG_NASAL_CALL_CONTEXT_HXX_ */