Fix number of compile errors and warnings with GCC 14
[lsnes.git] / include / library / lua-params.hpp
blob554fac362e831d511544f89ec38073f80959ea31
1 #ifndef _library__lua_params__hpp__included__
2 #define _library__lua_params__hpp__included__
4 #include "lua-base.hpp"
5 #include "lua-framebuffer.hpp"
6 #include "lua-class.hpp"
8 namespace lua
10 struct function_parameter_tag
12 function_parameter_tag(int& _idx) : idx(_idx) {}
13 int& idx;
16 struct table_parameter_tag
18 table_parameter_tag(int& _idx) : idx(_idx) {}
19 int& idx;
22 template<typename T, typename U> struct optional_parameter_tag
24 optional_parameter_tag(T& _target, U _dflt) : target(_target), dflt(_dflt) {}
25 T& target;
26 U dflt;
29 struct skipped_parameter_tag
33 template<typename T> static void arg_helper(state& L, T& x, int idx, const std::string& fname)
35 x = L.get_numeric_argument<T>(idx, fname);
38 template<> void arg_helper(state& L, bool& x, int idx, const std::string& fname)
40 x = L.get_bool(idx, fname);
43 template<> void arg_helper(state& L, std::string& x, int idx, const std::string& fname)
45 x = L.get_string(idx, fname);
48 template<typename T> void arg_helper(state& L, T*& x, int idx, const std::string& fname)
50 x = _class<T>::get(L, idx, fname);
53 template<typename T> void arg_helper(state& L, lua::objpin<T>& x, int idx, const std::string& fname)
55 x = _class<T>::pin(L, idx, fname);
58 template<> void arg_helper(state& L, framebuffer::color& x, int idx, const std::string& fname)
60 x = get_fb_color(L, idx, fname);
63 template<> void arg_helper(state& L, skipped_parameter_tag& x, int idx, const std::string& fname)
65 delete &x;
68 template<> void arg_helper(state& L, function_parameter_tag& x, int idx, const std::string& fname)
70 if(L.type(idx) != LUA_TFUNCTION)
71 (stringfmt() << "Expected function as argument #" << idx << " to " << fname).throwex();
72 x.idx = idx;
73 delete &x;
76 template<> void arg_helper(state& L, table_parameter_tag& x, int idx, const std::string& fname)
78 if(L.type(idx) != LUA_TTABLE)
79 (stringfmt() << "Expected table as argument #" << idx << " to " << fname).throwex();
80 x.idx = idx;
81 delete &x;
84 template<typename T, typename U> void arg_helper(state& L, optional_parameter_tag<T, U>& x, int idx,
85 const std::string& fname)
87 x.target = x.dflt;
88 L.get_numeric_argument<T>(idx, x.target, fname);
89 delete &x;
92 template<typename U> void arg_helper(state& L, optional_parameter_tag<bool, U>& x, int idx, const std::string& fname)
94 x.target = (L.type(idx) == LUA_TNIL || L.type(idx) == LUA_TNONE) ? x.dflt : L.get_bool(idx, fname);
95 delete &x;
98 template<typename U> void arg_helper(state& L, optional_parameter_tag<std::string, U>& x, int idx,
99 const std::string& fname)
101 x.target = (L.type(idx) == LUA_TNIL || L.type(idx) == LUA_TNONE) ? x.dflt : L.get_string(idx, fname);
102 delete &x;
105 template<typename U> void arg_helper(state& L, optional_parameter_tag<framebuffer::color, U>& x, int idx,
106 const std::string& fname)
108 x.target = get_fb_color(L, idx, fname, x.dflt);
109 delete &x;
112 template<typename T, typename U> void arg_helper(state& L, optional_parameter_tag<T*, U>& x, int idx,
113 const std::string& fname)
115 x.target = _class<T>::get(L, idx, fname, true);
116 delete &x;
120 * Parameters for Lua function.
122 class parameters
124 public:
126 * Make
128 parameters(state& _L, const std::string& _fname)
129 : L(_L), fname(_fname), next(1)
133 * Read mandatory argument.
135 * Parameter i: Index to read. If 0, read next and advance pointer.
136 * Returns: The read value.
138 * Notes: The following types can be read:
139 * - Numeric types.
140 * - Pointers to lua classes.
141 * - Pins of lua classes.
143 template<typename T> T arg(int i = 0)
145 T tmp;
146 arg_helper(L, tmp, i ? i : next, fname);
147 if(!i) next++;
148 return tmp;
151 * Read optional argument.
153 * Parameter d: The default value.
154 * Parameter i: Index to read. If 0, read next and advance pointer.
155 * Returns: The read value.
157 * Notes: The following types can be read:
158 * - Numeric types.
159 * - Pointers to lua classes (d is ignored, assumed NULL).
161 template<typename T> T arg_opt(T d, int i = 0)
163 T tmp;
164 arg_helper(L, optional<T>(tmp, d), i ? i : next, fname);
165 if(!i) next++;
166 return tmp;
169 * Is of specified class?
171 * Parameter i: Index to read. If 0, read next.
172 * Returns: True if it is, false if is is not.
174 template<typename T> bool is(int i = 0)
176 return _class<T>::is(L, i ? i : next);
179 * Skip argument and return index.
181 * Returns: The index.
183 int skip() { return next++; }
185 * Reset sequence.
187 void reset(int idx = 1) { next = idx; }
189 * Get name.
191 const std::string& get_fname() { return fname; }
193 * More arguments remain?
195 bool more() { return (L.type(next) != LUA_TNONE); }
197 * Is of lua type?
199 bool is_novalue(int i = 0) { int t = L.type(i ? i : next); return (t == LUA_TNONE || t == LUA_TNIL); }
200 bool is_none(int i = 0) { int t = L.type(i ? i : next); return (t == LUA_TNONE); }
201 bool is_nil(int i = 0) { int t = L.type(i ? i : next); return (t == LUA_TNIL); }
202 bool is_boolean(int i = 0) { int t = L.type(i ? i : next); return (t == LUA_TBOOLEAN); }
203 bool is_number(int i = 0) { int t = L.type(i ? i : next); return (t == LUA_TNUMBER); }
204 bool is_string(int i = 0) { int t = L.type(i ? i : next); return (t == LUA_TSTRING); }
205 bool is_thread(int i = 0) { int t = L.type(i ? i : next); return (t == LUA_TTHREAD); }
206 bool is_table(int i = 0) { int t = L.type(i ? i : next); return (t == LUA_TTABLE); }
207 bool is_function(int i = 0) { int t = L.type(i ? i : next); return (t == LUA_TFUNCTION); }
208 bool is_lightuserdata(int i = 0) { int t = L.type(i ? i : next); return (t == LUA_TLIGHTUSERDATA); }
209 bool is_userdata(int i = 0) { int t = L.type(i ? i : next); return (t == LUA_TUSERDATA); }
211 * Throw an error.
213 void expected(const std::string& what, int i = 0)
215 (stringfmt() << "Expected " << what << " as argument #" << (i ? i : next) << " of "
216 << fname).throwex();
219 * Read multiple at once.
221 template<typename T, typename... U> void operator()(T& x, U&... args)
223 arg_helper(L, x, next, fname);
224 next++;
225 (*this)(args...);
227 void operator()()
231 * Optional tag.
233 template<typename T, typename U> optional_parameter_tag<T, U>& optional(T& value, U dflt)
235 return *new optional_parameter_tag<T, U>(value, dflt);
238 * Optional tag, reference default value.
240 template<typename T, typename U> optional_parameter_tag<T, const U&>& optional2(T& value, const U& dflt)
242 return *new optional_parameter_tag<T, const U&>(value, dflt);
245 * Skipped tag.
247 skipped_parameter_tag& skipped() { return *new skipped_parameter_tag(); }
249 * Function tag.
251 function_parameter_tag& function(int& fnidx) { return *new function_parameter_tag(fnidx); }
253 * Table tag.
255 table_parameter_tag& table(int& fnidx) { return *new table_parameter_tag(fnidx); }
257 * Get Lua state.
259 state& get_state() { return L; }
260 private:
261 state& L;
262 std::string fname;
263 int next;
267 #endif