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"
10 struct function_parameter_tag
12 function_parameter_tag(int& _idx
) : idx(_idx
) {}
16 struct table_parameter_tag
18 table_parameter_tag(int& _idx
) : idx(_idx
) {}
22 template<typename T
, typename U
> struct optional_parameter_tag
24 optional_parameter_tag(T
& _target
, U _dflt
) : target(_target
), dflt(_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
)
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();
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();
84 template<typename T
, typename U
> void arg_helper(state
& L
, optional_parameter_tag
<T
, U
>& x
, int idx
,
85 const std::string
& fname
)
88 L
.get_numeric_argument
<T
>(idx
, x
.target
, fname
);
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
);
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
);
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
);
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);
120 * Parameters for Lua function.
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:
140 * - Pointers to lua classes.
141 * - Pins of lua classes.
143 template<typename T
> T
arg(int i
= 0)
146 arg_helper(L
, tmp
, i
? i
: next
, fname
);
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:
159 * - Pointers to lua classes (d is ignored, assumed NULL).
161 template<typename T
> T
arg_opt(T d
, int i
= 0)
164 arg_helper(L
, optional
<T
>(tmp
, d
), i
? i
: next
, fname
);
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
++; }
187 void reset(int idx
= 1) { next
= idx
; }
191 const std::string
& get_fname() { return fname
; }
193 * More arguments remain?
195 bool more() { return (L
.type(next
) != LUA_TNONE
); }
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
); }
213 void expected(const std::string
& what
, int i
= 0)
215 (stringfmt() << "Expected " << what
<< " as argument #" << (i
? i
: next
) << " of "
219 * Read multiple at once.
221 template<typename T
, typename
... U
> void operator()(T
& x
, U
&... args
)
223 arg_helper(L
, x
, next
, fname
);
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
);
247 skipped_parameter_tag
& skipped() { return *new skipped_parameter_tag(); }
251 function_parameter_tag
& function(int& fnidx
) { return *new function_parameter_tag(fnidx
); }
255 table_parameter_tag
& table(int& fnidx
) { return *new table_parameter_tag(fnidx
); }
259 state
& get_state() { return L
; }