mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-24 01:24:26 +01:00
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:
@@ -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 *)
|
||||
|
||||
Reference in New Issue
Block a user