From: Mathieu Desnoyers Date: Thu, 21 Sep 2023 04:30:40 +0000 (+0100) Subject: macros.h: Implement SIDE_PARAM_SELECT_ARG1 X-Git-Url: https://git.efficios.com/?a=commitdiff_plain;h=d0ee69ff824fa3a480c520896d0a69385306a204;p=libside.git macros.h: Implement SIDE_PARAM_SELECT_ARG1 Implement the SIDE_PARAM_SELECT_ARG1 macro which allows side macros to support optional last argument. This is useful for attribute lists, which are typically empty. Suggested-by: Olivier Dion Signed-off-by: Mathieu Desnoyers --- diff --git a/include/side/macros.h b/include/side/macros.h index 796b708..aea7b9c 100644 --- a/include/side/macros.h +++ b/include/side/macros.h @@ -30,6 +30,30 @@ #define SIDE_PARAM(...) __VA_ARGS__ +/* + * SIDE_PARAM_SELECT_ARG1 + * + * Select second argument. Use inside macros to implement optional last + * macro argument, such as: + * + * #define macro(_a, _b, _c, _optional...) \ + * SIDE_PARAM_SELECT_ARG1(_, ##_optional, do_default_macro()) + * + * This macro is far from pretty, but attempts to create a cleaner layer + * on top fails for various reasons: + * + * - The libside API needs to use the default argument selection as an + * argument to itself (recursively), e.g. for fields and for types, so + * using the argument selection within an extra layer of macro fails + * because the extra layer cannot expand recursively. + * - Attempts to make the extra layer of macro support recursion through + * another layer of macros which expands all arguments failed because + * the optional argument may contain commas, and is therefore expanded + * into multiple arguments before argument selection, which fails to + * select the optional argument content after its first comma. + */ +#define SIDE_PARAM_SELECT_ARG1(_arg0, _arg1, ...) _arg1 + /* * side_container_of - Get the address of an object containing a field. *