map: make for_each use a return value

Make it possible to stop pw_map_for_each by adding a return value
to the callback.
This commit is contained in:
Wim Taymans 2018-08-15 11:18:55 +02:00
parent 5fe230e5ff
commit f71be550c3
3 changed files with 21 additions and 10 deletions

View file

@ -279,10 +279,11 @@ void *pw_client_get_user_data(struct pw_client *client)
return client->user_data; return client->user_data;
} }
static void destroy_resource(void *object, void *data) static int destroy_resource(void *object, void *data)
{ {
if (object) if (object)
pw_resource_destroy(object); pw_resource_destroy(object);
return 0;
} }

View file

@ -161,18 +161,25 @@ static inline void *pw_map_lookup(struct pw_map *map, uint32_t id)
/** Iterate all map items /** Iterate all map items
* \param map the map to iterate * \param map the map to iterate
* \param func the function to call for each item * \param func the function to call for each item, the item data and \a data is
* passed to the function. When \a func returns a non-zero result,
* iteration ends and the result is returned.
* \param data data to pass to \a func * \param data data to pass to \a func
* \return the result of the last call to \a func or 0 when all callbacks returned 0.
* \memberof pw_map * \memberof pw_map
*/ */
static inline void pw_map_for_each(struct pw_map *map, void (*func) (void *, void *), void *data) static inline int pw_map_for_each(struct pw_map *map,
int (*func) (void *item_data, void *data), void *data)
{ {
union pw_map_item *item; union pw_map_item *item;
int res = 0;
pw_array_for_each(item, &map->items) { pw_array_for_each(item, &map->items) {
if (!pw_map_item_is_free(item)) if (!pw_map_item_is_free(item))
func(item->data, data); if ((res = func(item->data, data)) != 0)
break;
} }
return res;
} }
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -259,13 +259,13 @@ static void on_sync_reply(void *_data, uint32_t seq)
} }
} }
static void print_global(void *obj, void *data) static int print_global(void *obj, void *data)
{ {
struct global *global = obj; struct global *global = obj;
struct pw_type *t; struct pw_type *t;
if (global == NULL) if (global == NULL)
return; return 0;
t = global->rd->data->t; t = global->rd->data->t;
@ -275,6 +275,7 @@ static void print_global(void *obj, void *data)
if (global->properties) if (global->properties)
print_properties(&global->properties->dict, ' ', false); print_properties(&global->properties->dict, ' ', false);
return 0;
} }
static void registry_event_global(void *data, uint32_t id, uint32_t parent_id, static void registry_event_global(void *data, uint32_t id, uint32_t parent_id,
@ -303,17 +304,18 @@ static void registry_event_global(void *data, uint32_t id, uint32_t parent_id,
pw_map_insert_at(&rd->globals, id, global); pw_map_insert_at(&rd->globals, id, global);
} }
static void destroy_global(void *obj, void *data) static int destroy_global(void *obj, void *data)
{ {
struct global *global = obj; struct global *global = obj;
if (global == NULL) if (global == NULL)
return; return 0;
pw_map_remove(&global->rd->globals, global->id); pw_map_remove(&global->rd->globals, global->id);
if (global->properties) if (global->properties)
pw_properties_free(global->properties); pw_properties_free(global->properties);
free(global); free(global);
return 0;
} }
static void registry_event_global_remove(void *data, uint32_t id) static void registry_event_global_remove(void *data, uint32_t id)
@ -902,18 +904,19 @@ static bool do_global_info(struct global *global, char **error)
} }
return true; return true;
} }
static void do_global_info_all(void *obj, void *data) static int do_global_info_all(void *obj, void *data)
{ {
struct global *global = obj; struct global *global = obj;
char *error; char *error;
if (global == NULL) if (global == NULL)
return; return 0;
if (!do_global_info(global, &error)) { if (!do_global_info(global, &error)) {
fprintf(stderr, "info: %s\n", error); fprintf(stderr, "info: %s\n", error);
free(error); free(error);
} }
return 0;
} }
static bool do_info(struct data *data, const char *cmd, char *args, char **error) static bool do_info(struct data *data, const char *cmd, char *args, char **error)