Skip to content

Commit

Permalink
Add 'cowl_iterator_retain' and 'cowl_iterator_release'
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanoBilenchi committed Aug 27, 2024
1 parent ab12e05 commit 045b92b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/cowl_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/cowl_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 };
}

0 comments on commit 045b92b

Please sign in to comment.