params: add helper to provide default initialization.

@wythe points out that many cases want a default value, not NULL.
Nicer to do it in the param_parse() call.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2018-07-11 11:59:53 +09:30
parent 1220a4eb1d
commit cf86c74870
3 changed files with 36 additions and 16 deletions

View File

@@ -60,6 +60,7 @@ typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg);
*/
#define param_req(name, cb, arg) \
name"", \
true, \
(cb), \
(arg) + 0*sizeof((cb)((const char *)NULL, \
(const jsmntok_t *)NULL, \
@@ -72,12 +73,27 @@ typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg);
*/
#define param_opt(name, cb, arg) \
name"", \
false, \
(cb), \
(arg) + 0*sizeof((cb)((const char *)NULL, \
(const jsmntok_t *)NULL,\
*(arg)) == true), \
sizeof(**(arg))
/*
* Similar to param_req but for optional parameters.
* If not found during parsing, @arg will be set to @def.
* allocated, otherwise it will be set to NULL.
*/
#define param_opt_default(name, cb, arg, def) \
name"", \
false, \
(cb), \
(arg) + 0*sizeof((cb)((const char *)NULL, \
(const jsmntok_t *)NULL, \
(arg)) == true), \
((void)((*arg) = (def)), 0)
/*
* For when you want an optional raw token.
*
@@ -85,6 +101,7 @@ typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg);
*/
#define param_opt_tok(name, arg) \
name"", \
false, \
json_tok_tok, \
(arg) + 0*sizeof(*(arg) == (jsmntok_t *)NULL), \
sizeof(const jsmntok_t *)