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,
|
|
17 |
|
|
18 |
LUA_ARG_STRING,
|
|
19 |
LUA_ARG_BOOL,
|
|
20 |
};
|
|
21 |
|
|
22 |
/**
|
|
23 |
* Function argument def
|
|
24 |
*/
|
|
25 |
struct lua_func_arg {
|
|
26 |
/** Argument name */
|
|
27 |
const char *name;
|
|
28 |
|
|
29 |
/** Expected type */
|
|
30 |
enum lua_arg_type type;
|
|
31 |
|
|
32 |
/** Default value */
|
|
33 |
union {
|
|
34 |
const char *string;
|
|
35 |
int boolean;
|
|
36 |
} def;
|
|
37 |
};
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Function def
|
|
41 |
*/
|
|
42 |
struct lua_func {
|
145
|
43 |
/** Object type, or NULL */
|
|
44 |
const struct lua_type *type;
|
143
|
45 |
|
|
46 |
/** Function name */
|
|
47 |
const char *name;
|
|
48 |
|
|
49 |
/** Help string */
|
|
50 |
const char *help;
|
|
51 |
|
|
52 |
/** Arguments */
|
|
53 |
const struct lua_func_arg args[];
|
|
54 |
};
|
|
55 |
|
|
56 |
/**
|
|
57 |
* Used as the "invalid" default value
|
|
58 |
*/
|
|
59 |
#define LUA_ARG_REQUIRED (-1)
|
|
60 |
#define LUA_ARG_STRING_REQUIRED ((const char *) (-1))
|
|
61 |
|
|
62 |
/**
|
|
63 |
* Define a function argument
|
|
64 |
*/
|
|
65 |
#define LUA_FUNC_ARG_STRING(name, def) { (name), LUA_ARG_STRING, { .string = (def) } }
|
|
66 |
#define LUA_FUNC_ARG_BOOL(name, def) { (name), LUA_ARG_BOOL, { .boolean = (def) } }
|
145
|
67 |
#define LUA_FUNC_ARG_END { NULL, 0, { 0 } }
|
143
|
68 |
|
|
69 |
/**
|
|
70 |
* Define a function
|
|
71 |
*/
|
145
|
72 |
#define LUA_FUNC(type, name, help, ...) { (type), (name), (help), { __VA_ARGS__, LUA_FUNC_ARG_END } }
|
143
|
73 |
|
|
74 |
/**
|
|
75 |
* Parse and return a string argument
|
|
76 |
*/
|
|
77 |
const char *lua_arg_string (lua_State *L, int nargs, int index, const char *name, const char *def);
|
|
78 |
|
|
79 |
/**
|
|
80 |
* Parse and return a boolean argument
|
|
81 |
*/
|
|
82 |
bool lua_arg_bool (lua_State *L, int nargs, int index, const char *name, int def);
|
|
83 |
|
|
84 |
/**
|
|
85 |
* Parse function arguments as defined
|
|
86 |
*/
|
|
87 |
void lua_args_parse (lua_State *L, const struct lua_func *func, void **obj_ptr, ...);
|
|
88 |
|
|
89 |
#endif
|