From 2178dcfcab878fc526f5790cf11920fbab950f4b Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sun, 15 Aug 2021 21:55:09 +0200 Subject: [PATCH] Add LIST_INIT_VALUE & LIST_DUAL_PUSH_INIT_VALUE. --- README.md | 28 +++++++++++++++++++++++++++- m-array.h | 2 +- m-list.h | 15 +++++++++++++++ tests/test-mlist.c | 12 ++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7c34250a..18a4887f 100644 --- a/README.md +++ b/README.md @@ -909,6 +909,19 @@ Return the oplist of the list defined by calling LIST\_DEF & LIST\_DUAL\_PUSH\_DEF with name & oplist. If there is no given oplist, the default oplist for standard C type is used. +#### LIST\_INIT\_VALUE() + +Define an initial value that is suitable to initialize global variable(s) +of type 'list' as created by LIST\_DEF or LIST\_DEF\_AS. +It enables to create a list as a global variable and to initialize it. + +The list should still be cleared manually to avoid leaking memory. + +Example: + + LIST_DEF(list_int_t, int) + list_int_t my_list = LIST_INIT_VALUE(); + #### Created methods In the following methods, name stands for the name given to the macro that is used to identify the type. @@ -1199,6 +1212,19 @@ The methods follow closely the methods defined by LIST\_DEF. Same as LIST\_DUAL\_PUSH\_DEF except the name of the types name\_t, name\_it\_t are provided. +#### LIST\_DUAL\_PUSH\_INIT\_VALUE() + +Define an initial value that is suitable to initialize global variable(s) +of type 'list' as created by LIST\_DUAL\_PUSH\_DEF or LIST\_DUAL\_PUSH\_DEF\_AS. +It enables to create a list as a global variable and to initialize it. + +The list should still be cleared manually to avoid leaking memory. + +Example: + + LIST_DUAL_PUSH_DEF(list_int_t, int) + list_int_t my_list = LIST_DUAL_PUSH_INIT_VALUE(); + #### Created methods In the following methods, name stands for the name given to the macro that is used to identify the type. @@ -1482,7 +1508,7 @@ If there is no given oplist, the default oplist for standard C type is used. #### ARRAY\_INIT\_VALUE() Define an initial value that is suitable to initialize global variable(s) -of the type associated to the (array) oplist 'oplist'. +of type 'array' as created by ARRAY\_DEF or ARRAY\_DEF\_AS. It enables to create an array as a global variable and to initialize it. The array should still be cleared manually to avoid leaking memory. diff --git a/m-array.h b/m-array.h index d895ed6e..7f0fd59b 100644 --- a/m-array.h +++ b/m-array.h @@ -55,7 +55,7 @@ /* Define an init value to init global variables of type array. USAGE: - array_t global_variable = ARRAY_INIT_VALUE(array_oplist); + array_t global_variable = ARRAY_INIT_VALUE(); */ #define ARRAY_INIT_VALUE() \ { { 0, 0, NULL } } diff --git a/m-list.h b/m-list.h index 3bce70c4..fce38543 100644 --- a/m-list.h +++ b/m-list.h @@ -68,6 +68,21 @@ ((__VA_ARGS__, M_DEFAULT_OPLIST ), \ (__VA_ARGS__ ))) +/* Define an init value to init global variables of type list. + USAGE: + list_t global_variable = LIST_INIT_VALUE(); + */ +#define LIST_INIT_VALUE() \ + { NULL } + + +/* Define an init value to init global variables of type dual push list. + USAGE: + list_t global_variable = LIST_DUAL_PUSH_INIT_VALUE(); + */ +#define LIST_DUAL_PUSH_INIT_VALUE() \ + { { NULL, NULL } } + /********************************** INTERNAL ************************************/ diff --git a/tests/test-mlist.c b/tests/test-mlist.c index 39f24c8c..5acb3c40 100644 --- a/tests/test-mlist.c +++ b/tests/test-mlist.c @@ -48,6 +48,9 @@ LIST_DEF_AS(list_double, ListDouble, ListDoubleIt, double) LIST_DUAL_PUSH_DEF_AS(list_doubleDP, ListDoubleDP, ListDoubleDPIt, double) #define M_OPL_ListDoubleDP() LIST_OPLIST(list_doubleDP, M_DEFAULT_OPLIST) +ListDouble g_array1 = LIST_INIT_VALUE(); +ListDoubleDP g_array2 = LIST_DUAL_PUSH_INIT_VALUE(); + static void test_uint(void) { list_uint_t v; @@ -674,6 +677,15 @@ static void test_double(void) ref += 1.0; } } + + assert( list_double_empty_p (g_array1) ); + assert( list_doubleDP_empty_p (g_array2) ); + + list_double_push_back(g_array1, 23.0); + list_doubleDP_push_front(g_array2, 23.0); + + list_double_clear(g_array1); + list_doubleDP_clear(g_array2); } static void test_let_string(void)