From dd1a7c13f2ce730a9da83c4467ebd34778a88c9d Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 18 Mar 2017 20:38:59 +0100 Subject: [PATCH] Add MACRO M_VA --- m-core.h | 17 ++++++++++++++--- tests/test-mcore.c | 8 ++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/m-core.h b/m-core.h index 5690130a..0e673d89 100644 --- a/m-core.h +++ b/m-core.h @@ -794,6 +794,15 @@ m_core_hash(const void *ptr, size_t s) #define M_IF_METHOD_ALL(method, ...) \ M_IF(M_REDUCE2(M_TEST_METHOD_P, M_AND, method, __VA_ARGS__)) +/* By putting this after a method, we transform the argument list + so that the first argument becomes a pointer to the destination. */ +#define M_IPTR(...) ( & __VA_ARGS__ ) + + +/************************************************************/ +/******************** Syntax Enhancing **********************/ +/************************************************************/ + /* Define M_EACH macro allowing to iterate over a container First argument will be a created pointer to the underlying type. Example: for M_EACH(item, list, LIST_OPLIST) { action; } */ @@ -824,6 +833,7 @@ m_core_hash(const void *ptr, size_t s) !M_GET_IT_END_P oplist (iterator) && (item = M_GET_IT_CREF oplist (iterator), true) ; \ M_GET_IT_NEXT oplist (iterator)) + /* Define M_LET macro allowing to define, auto init & auto clear an object within the next bracket. Example: @@ -847,9 +857,10 @@ m_core_hash(const void *ptr, size_t s) cont && (M_GET_INIT oplist (name), true); \ (M_GET_CLEAR oplist (name), cont = false)) -/* By putting this after a method, we transform the argument list - so that the first argument becomes a pointer to the destination. */ -#define M_IPTR(...) ( & __VA_ARGS__ ) +/* Transform the va list by adding their number as the first argument of + the list. + Example: M_VA(a,b,c,d,e) ==> 5,a,b,c,d,e */ +#define M_VA(...) M_NARGS(__VA_ARGS__), __VA_ARGS__ /************************************************************/ diff --git a/tests/test-mcore.c b/tests/test-mcore.c index 5d9b0ea4..d718bed8 100644 --- a/tests/test-mcore.c +++ b/tests/test-mcore.c @@ -145,6 +145,13 @@ static void test_let(void) } } +static void test_va(void) +{ + assert (M_RET_ARG1 (M_VA(a),zz) == 1); + assert (M_RET_ARG2 (M_VA(17),zz) == 17); + assert (M_RET_ARG1 (M_VA(a,b,c),zz) == 3); + assert (M_RET_ARG2 (M_VA(17,42,59),zz) == 17); +} int main(void) { @@ -155,5 +162,6 @@ int main(void) test_invert(); test_min(); test_let(); + test_va(); exit(0); }