From 2174c28aa25c2f1f8fccadd48f2d72c0e9494ca9 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 8 Sep 2018 08:21:13 +0200 Subject: [PATCH] Make INIT operator optionnal for ARRAY & LIST. --- m-array.h | 18 ++++++++++++++---- m-list.h | 14 ++++++++++---- tests/test-marray.c | 3 +++ tests/test-mlist.c | 5 +++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/m-array.h b/m-array.h index ec947387..62849510 100644 --- a/m-array.h +++ b/m-array.h @@ -260,6 +260,7 @@ M_GET_INIT_SET oplist(*data, x); \ } \ \ + M_IF_METHOD(INIT, oplist)( \ static inline type * \ M_C(name, _push_new)(array_t v) \ { \ @@ -269,6 +270,7 @@ M_GET_INIT oplist(*data); \ return data; \ } \ + , /* No INIT */ ) \ \ static inline void \ M_C(name, _push_move)(array_t v, type *x) \ @@ -308,6 +310,7 @@ ARRAYI_CONTRACT(v); \ } \ \ + M_IF_METHOD(INIT, oplist)( \ static inline void \ M_C(name, _resize)(array_t v, size_t size) \ { \ @@ -335,6 +338,7 @@ } \ ARRAYI_CONTRACT(v); \ } \ + , /* No INIT */ ) \ \ static inline void \ M_C(name, _reserve)(array_t v, size_t alloc) \ @@ -360,6 +364,7 @@ ARRAYI_CONTRACT(v); \ } \ \ + M_IF_METHOD(INIT, oplist)( \ static inline type * \ M_C(name, _get_at)(array_t v, size_t idx) \ { \ @@ -391,6 +396,7 @@ ARRAYI_CONTRACT(v); \ return &v->ptr[idx]; \ } \ + , /* No INIT */) \ \ static inline void \ M_C(name, _pop_back)(type *dest, array_t v) \ @@ -417,6 +423,7 @@ ARRAYI_CONTRACT(v); \ } \ \ + M_IF_METHOD(INIT, oplist)( \ static inline void \ M_C(name, _pop_until)(array_t v, array_it_t pos) \ { \ @@ -425,6 +432,7 @@ M_ASSUME (pos->index <= v->size); \ M_C(name, _resize)(v, pos->index); \ } \ + , /* No INIT */ ) \ \ static inline bool \ M_C(name, _empty_p)(const array_t v) \ @@ -461,6 +469,7 @@ ARRAYI_CONTRACT(v); \ } \ \ + M_IF_METHOD(INIT, oplist)( \ static inline void \ M_C(name, _insert_v)(array_t v, size_t i, size_t num) \ { \ @@ -488,6 +497,7 @@ v->size = size; \ ARRAYI_CONTRACT(v); \ } \ + , /* No INIT */) \ \ static inline void \ M_C(name, _remove_v)(array_t v, size_t i, size_t j) \ @@ -809,7 +819,7 @@ } \ , /* no OUT_STR */ ) \ \ - M_IF_METHOD(PARSE_STR, oplist)( \ + M_IF_METHOD2(PARSE_STR, INIT, oplist)( \ static inline bool \ M_C(name, _parse_str)(array_t array, const char str[], const char**endp) \ { \ @@ -838,9 +848,9 @@ if (endp) *endp = str; \ return success; \ } \ - , /* no PARSE_STR */ ) \ + , /* no PARSE_STR & INIT */ ) \ \ - M_IF_METHOD(IN_STR, oplist)( \ + M_IF_METHOD2(IN_STR, INIT, oplist)( \ static inline bool \ M_C(name, _in_str)(array_t array, FILE *file) \ { \ @@ -865,7 +875,7 @@ ARRAYI_CONTRACT(array); \ return c == ']'; \ } \ - , /* no IN_STR */ ) \ + , /* no IN_STR & INIT */ ) \ \ M_IF_METHOD(EQUAL, oplist)( \ static inline bool \ diff --git a/m-list.h b/m-list.h index db34bf09..bc529238 100644 --- a/m-list.h +++ b/m-list.h @@ -209,6 +209,7 @@ M_GET_INIT_SET oplist(*data, x); \ } \ \ + M_IF_METHOD(INIT, oplist)( \ static inline type * \ M_C(name, _push_new)(list_t v) \ { \ @@ -218,6 +219,7 @@ M_GET_INIT oplist(*data); \ return data; \ } \ + , /* No INIT */) \ \ static inline void \ M_C(name, _pop_back)(type *data, list_t v) \ @@ -618,7 +620,7 @@ } \ , /* no out_str */ ) \ \ - M_IF_METHOD(PARSE_STR, oplist)( \ + M_IF_METHOD2(PARSE_STR, INIT, oplist)( \ static inline bool \ M_C(name, _parse_str)(list_t list, const char str[], const char **endp) \ { \ @@ -646,9 +648,9 @@ if (endp) *endp = str; \ return success; \ } \ - , /* no parse_str */ ) \ + , /* no PARSE_STR & INIT */ ) \ \ - M_IF_METHOD(IN_STR, oplist)( \ + M_IF_METHOD2(IN_STR, INIT, oplist)( \ static inline bool \ M_C(name, _in_str)(list_t list, FILE *file) \ { \ @@ -672,7 +674,7 @@ M_C(name, _reverse)(list); \ return c == ']'; \ } \ - , /* no in_str */ ) \ + , /* no IN_STR & INIT */ ) \ \ M_IF_METHOD(EQUAL, oplist)( \ static inline bool \ @@ -825,6 +827,7 @@ M_GET_INIT_SET oplist(*data, x); \ } \ \ + M_IF_METHOD(INIT, oplist)( \ static inline type * \ M_C(name, _push_back_new)(list_t v) \ { \ @@ -834,6 +837,7 @@ M_GET_INIT oplist(*data); \ return data; \ } \ + , /* No INIT */ ) \ \ static inline void \ M_C(name, _push_back_move)(list_t v, type *x) \ @@ -938,6 +942,7 @@ M_DO_INIT_MOVE (oplist, *data, *x); \ } \ \ + M_IF_METHOD(INIT, oplist)( \ static inline type * \ M_C(name, _push_front_new)(list_t v) \ { \ @@ -947,6 +952,7 @@ M_GET_INIT oplist(*data); \ return data; \ } \ + , /* No INIT */) \ \ static inline bool \ M_C(name, _empty_p)(const list_t v) \ diff --git a/tests/test-marray.c b/tests/test-marray.c index 31e5897c..4592cd5d 100644 --- a/tests/test-marray.c +++ b/tests/test-marray.c @@ -31,6 +31,9 @@ ARRAY_DEF(array_mpz, testobj_t, TESTOBJ_OPLIST) END_COVERAGE #define ARRAY_UINT_OPLIST ARRAY_OPLIST(array_uint) +// Array with the minimum number of methods. +ARRAY_DEF(array_min_z, testobj_t, (INIT_SET(testobj_init_set), SET(testobj_set), CLEAR(testobj_clear))) + static void test_uint(void) { array_uint_t v; diff --git a/tests/test-mlist.c b/tests/test-mlist.c index 32557052..d046a147 100644 --- a/tests/test-mlist.c +++ b/tests/test-mlist.c @@ -35,6 +35,11 @@ LIST_DUAL_PUSH_DEF(list2_mpz, testobj_t, TESTOBJ_OPLIST) #define LIST_UINT_OPLIST LIST_OPLIST(list_uint) #define LIST2_DOUBLE_OPLIST LIST_OPLIST(list2_double) +// Array with the minimum number of methods. +LIST_DEF(list_min_z, testobj_t, (INIT_SET(testobj_init_set), SET(testobj_set), CLEAR(testobj_clear))) +LIST_DUAL_PUSH_DEF(list2_min_z, testobj_t, (INIT_SET(testobj_init_set), SET(testobj_set), CLEAR(testobj_clear))) + + static void test_uint(void) { list_uint_t v;