author | Tero Marttila <terom@fixme.fi> |
Thu, 28 May 2009 01:17:36 +0300 | |
branch | new-lib-errors |
changeset 219 | cefec18b8268 |
parent 201 | src/lua_func.h@5c34c5d90a3f |
permissions | -rw-r--r-- |
143 | 1 |
#ifndef LUA_FUNC_H |
2 |
#define LUA_FUNC_H |
|
3 |
||
4 |
/** |
|
5 |
* @file |
|
6 |
* |
|
7 |
* Convenience functions for working with lua C functions |
|
8 |
*/ |
|
9 |
#include "lua_type.h" |
|
10 |
#include <stdbool.h> |
|
11 |
||
12 |
/** |
|
13 |
* Lua function argument types |
|
14 |
*/ |
|
15 |
enum lua_arg_type { |
|
16 |
LUA_ARG_INVALID, |
|
201 | 17 |
|
18 |
/** A `const char *` pointing to lua-GC'd memory */ |
|
19 |
LUA_ARG_STRING, |
|
143 | 20 |
|
201 | 21 |
/** A c99 `bool` */ |
143 | 22 |
LUA_ARG_BOOL, |
201 | 23 |
|
24 |
/** A `long signed int` */ |
|
25 |
LUA_ARG_INT, |
|
143 | 26 |
}; |
27 |
||
28 |
/** |
|
29 |
* Function argument def |
|
30 |
*/ |
|
31 |
struct lua_func_arg { |
|
32 |
/** Argument name */ |
|
33 |
const char *name; |
|
34 |
||
35 |
/** Expected type */ |
|
36 |
enum lua_arg_type type; |
|
37 |
||
38 |
/** Default value */ |
|
39 |
union { |
|
40 |
const char *string; |
|
41 |
int boolean; |
|
201 | 42 |
long integer; |
143 | 43 |
} def; |
44 |
}; |
|
45 |
||
46 |
/** |
|
47 |
* Function def |
|
48 |
*/ |
|
49 |
struct lua_func { |
|
145 | 50 |
/** Object type, or NULL */ |
51 |
const struct lua_type *type; |
|
143 | 52 |
|
53 |
/** Function name */ |
|
54 |
const char *name; |
|
55 |
||
56 |
/** Help string */ |
|
57 |
const char *help; |
|
58 |
||
59 |
/** Arguments */ |
|
60 |
const struct lua_func_arg args[]; |
|
61 |
}; |
|
62 |
||
63 |
/** |
|
64 |
* Used as the "invalid" default value |
|
65 |
*/ |
|
66 |
#define LUA_ARG_REQUIRED (-1) |
|
67 |
#define LUA_ARG_STRING_REQUIRED ((const char *) (-1)) |
|
68 |
||
69 |
/** |
|
70 |
* Define a function argument |
|
71 |
*/ |
|
72 |
#define LUA_FUNC_ARG_STRING(name, def) { (name), LUA_ARG_STRING, { .string = (def) } } |
|
73 |
#define LUA_FUNC_ARG_BOOL(name, def) { (name), LUA_ARG_BOOL, { .boolean = (def) } } |
|
201 | 74 |
#define LUA_FUNC_ARG_INT(name, def) { (name), LUA_ARG_INT, { .integer = (def) } } |
145 | 75 |
#define LUA_FUNC_ARG_END { NULL, 0, { 0 } } |
143 | 76 |
|
77 |
/** |
|
78 |
* Define a function |
|
79 |
*/ |
|
145 | 80 |
#define LUA_FUNC(type, name, help, ...) { (type), (name), (help), { __VA_ARGS__, LUA_FUNC_ARG_END } } |
143 | 81 |
|
82 |
/** |
|
83 |
* Parse and return a string argument |
|
84 |
*/ |
|
85 |
const char *lua_arg_string (lua_State *L, int nargs, int index, const char *name, const char *def); |
|
86 |
||
87 |
/** |
|
88 |
* Parse and return a boolean argument |
|
89 |
*/ |
|
90 |
bool lua_arg_bool (lua_State *L, int nargs, int index, const char *name, int def); |
|
91 |
||
92 |
/** |
|
201 | 93 |
* Parse and return an integer argument |
94 |
*/ |
|
95 |
long lua_arg_int (lua_State *L, int nargs, int index, const char *name, long def); |
|
96 |
||
97 |
/** |
|
199
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
145
diff
changeset
|
98 |
* Return a userdata argument at the given fixed index |
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
145
diff
changeset
|
99 |
*/ |
201 | 100 |
void* lua_arg_obj (lua_State *L, int nargs, int index, const struct lua_type *type, bool optional); |
199
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
145
diff
changeset
|
101 |
|
8eb839fbabba
replace lua_client_networks/lua_network_channels with iterators
Tero Marttila <terom@fixme.fi>
parents:
145
diff
changeset
|
102 |
/** |
143 | 103 |
* Parse function arguments as defined |
104 |
*/ |
|
105 |
void lua_args_parse (lua_State *L, const struct lua_func *func, void **obj_ptr, ...); |
|
106 |
||
107 |
#endif |