From 6dd60cf114701f1ff3526381c0e742c588af2f91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?= Date: Mon, 26 Aug 2019 16:06:40 +0900 Subject: st_foreach now free from ANYARGS After 5e86b005c0f2ef30df2f9906c7e2f3abefe286a2, I now think ANYARGS is dangerous and should be extinct. This commit deletes ANYARGS from st_foreach. I strongly believe that this commit should have had come with b0af0592fdd9e9d4e4b863fde006d67ccefeac21, which added extra parameter to st_foreach callbacks. --- st.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'st.c') diff --git a/st.c b/st.c index cecc2ac67f..c32a7ed208 100644 --- a/st.c +++ b/st.c @@ -1548,7 +1548,7 @@ st_update(st_table *tab, st_data_t key, different for ST_CHECK and when the current element is removed during traversing. */ static inline int -st_general_foreach(st_table *tab, int (*func)(ANYARGS), st_update_callback_func *replace, st_data_t arg, +st_general_foreach(st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg, int check_p) { st_index_t bin; @@ -1659,20 +1659,33 @@ st_general_foreach(st_table *tab, int (*func)(ANYARGS), st_update_callback_func } int -st_foreach_with_replace(st_table *tab, int (*func)(ANYARGS), st_update_callback_func *replace, st_data_t arg) +st_foreach_with_replace(st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg) { return st_general_foreach(tab, func, replace, arg, TRUE); } +struct functor { + st_foreach_callback_func *func; + st_data_t arg; +}; + +static int +apply_functor(st_data_t k, st_data_t v, st_data_t d, int _) +{ + const struct functor *f = (void *)d; + return f->func(k, v, f->arg); +} + int -st_foreach(st_table *tab, int (*func)(ANYARGS), st_data_t arg) +st_foreach(st_table *tab, st_foreach_callback_func *func, st_data_t arg) { - return st_general_foreach(tab, func, NULL, arg, FALSE); + const struct functor f = { func, arg }; + return st_general_foreach(tab, apply_functor, NULL, (st_data_t)&f, FALSE); } /* See comments for function st_delete_safe. */ int -st_foreach_check(st_table *tab, int (*func)(ANYARGS), st_data_t arg, +st_foreach_check(st_table *tab, st_foreach_check_callback_func *func, st_data_t arg, st_data_t never ATTRIBUTE_UNUSED) { return st_general_foreach(tab, func, NULL, arg, TRUE); -- cgit v1.2.3