From 045b92ba56553e111bef5b0b396e50fe17018d1a Mon Sep 17 00:00:00 2001 From: Ivano Bilenchi Date: Tue, 27 Aug 2024 11:25:53 +0200 Subject: [PATCH] Add 'cowl_iterator_retain' and 'cowl_iterator_release' --- include/cowl_iterator.h | 16 ++++++++++++++++ src/cowl_iterator.c | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/cowl_iterator.h b/include/cowl_iterator.h index 17d4aa6..db215ec 100644 --- a/include/cowl_iterator.h +++ b/include/cowl_iterator.h @@ -128,6 +128,22 @@ CowlIterator cowl_iterator_count(ulib_uint *count); COWL_API CowlIterator cowl_iterator_contains(CowlAny *object); +/** + * Initializes an iterator that retains all objects it iterates on. + * + * @return Initialized iterator. + */ +COWL_API +CowlIterator cowl_iterator_retain(void); + +/** + * Initializes an iterator that releases all objects it iterates on. + * + * @return Initialized iterator. + */ +COWL_API +CowlIterator cowl_iterator_release(void); + /// @} COWL_END_DECLS diff --git a/src/cowl_iterator.c b/src/cowl_iterator.c index 237a15c..ca0d3a9 100644 --- a/src/cowl_iterator.c +++ b/src/cowl_iterator.c @@ -45,6 +45,16 @@ static bool for_each_contains_primitive(void *ctx, CowlAny *obj) { return obj != ctx; } +static bool for_each_retain(cowl_unused void *ctx, CowlAny *obj) { + cowl_retain(obj); + return true; +} + +static bool for_each_release(cowl_unused void *ctx, CowlAny *obj) { + cowl_release(obj); + return true; +} + CowlIterator cowl_iterator_vec(UVec(CowlObjectPtr) *vec, bool retain) { return (CowlIterator){ .ctx = vec, @@ -69,3 +79,11 @@ CowlIterator cowl_iterator_contains(CowlAny *object) { .for_each = cowl_is_primitive(object) ? for_each_contains_primitive : for_each_contains, }; } + +CowlIterator cowl_iterator_retain(void) { + return (CowlIterator){ .for_each = for_each_retain }; +} + +CowlIterator cowl_iterator_release(void) { + return (CowlIterator){ .for_each = for_each_release }; +}