Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic callbacks to the list #13

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct list_s {
void *(*mem_alloc) (size_t size);
void *(*mem_calloc) (size_t blocks, size_t size);
void (*mem_free) (void *block);
void (*on_add) (List *list, void *data);
void (*on_remove) (List *list);
};

static void *unlink (List *list, Node *node);
Expand Down Expand Up @@ -135,6 +137,35 @@ bool list_destroy_free(List *list)
return success;
}

/**
* Sets the on_add callback for list. The function passed to this function
* will be called when there are single elements added to the list.
*
* @param[in] list the list which should recive the callback
* @param[in] on_add the callback
*/

void list_set_add_callback (List *list, void (*on_add) (List *list, void *data))
{
if(list)
list->on_add = on_add;
}

/**
* Sets the on_remove callback for list. The function passed to this function
* will be called when there are single elements removed from the list.
*
* @param[in] list the list which should recive the callback
* @param[in] on_remove the callback
*/

void list_set_remove_callback(List *list, void (*on_remove) (List *list))
{
if(list)
list->on_remove = on_remove;
}


/**
* Adds a new element to the list. The element is appended to the list making it
* the last element in the list. This function returns false if the memory
Expand Down Expand Up @@ -178,6 +209,10 @@ enum cc_stat list_add_first(List *list, void *element)
list->head = node;
}
list->size++;

if(list->on_add)
list->on_add(list, element);

return CC_OK;
}

Expand Down Expand Up @@ -209,6 +244,10 @@ enum cc_stat list_add_last(List *list, void *element)
list->tail = node;
}
list->size++;

if(list->on_add)
list->on_add(list, element);

return CC_OK;
}

Expand Down Expand Up @@ -247,6 +286,9 @@ enum cc_stat list_add_at(List *list, void *element, size_t index)

list->size++;

if(list->on_add)
list->on_add(list, element);

return CC_OK;
}

Expand Down Expand Up @@ -295,6 +337,7 @@ static enum cc_stat add_all_to_empty(List *list1, List *list2)
list1->head = head;
list1->tail = tail;
list1->size = list2->size;

return CC_OK;
}

Expand Down Expand Up @@ -522,6 +565,10 @@ enum cc_stat list_remove(List *list, void *element, void **out)
*out = node->data;

unlink(list, node);

if(list->on_remove)
list->on_remove(list);

return CC_OK;
}

Expand Down Expand Up @@ -550,6 +597,10 @@ enum cc_stat list_remove_at(List *list, size_t index, void **out)
*out = node->data;

unlink(list, node);

if(list->on_remove)
list->on_remove(list);

return CC_OK;
}

Expand All @@ -571,6 +622,9 @@ enum cc_stat list_remove_first(List *list, void **out)
if (out)
*out = e;

if(list->on_remove)
list->on_remove(list);

return CC_OK;
}

Expand All @@ -592,6 +646,9 @@ enum cc_stat list_remove_last(List *list, void **out)
if (out)
*out = node->data;

if(list->on_remove)
list->on_remove(list);

return CC_OK;
}

Expand All @@ -611,6 +668,10 @@ enum cc_stat list_remove_all(List *list)
if (unlinked) {
list->head = NULL;
list->tail = NULL;

if(list->on_remove)
list->on_remove(list);

return CC_OK;
}
return CC_ERR_VALUE_NOT_FOUND;
Expand Down Expand Up @@ -639,6 +700,10 @@ enum cc_stat list_remove_all_free(List *list)
if (unlinked) {
list->head = NULL;
list->tail = NULL;

if(list->on_remove)
list->on_remove(list);

return CC_OK;
}
return CC_ERR_VALUE_NOT_FOUND;
Expand Down Expand Up @@ -1220,6 +1285,10 @@ enum cc_stat list_iter_remove(ListIter *iter, void **out)

if (out)
*out = e;

if(iter->list->on_remove)
iter->list->on_remove(iter->list);

return CC_OK;
}

Expand Down Expand Up @@ -1250,6 +1319,9 @@ enum cc_stat list_iter_add(ListIter *iter, void *element)
iter->list->size++;
iter->index++;

if(iter->list->on_add)
iter->list->on_add(iter->list, element);

return CC_OK;
}

Expand Down
4 changes: 4 additions & 0 deletions src/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ enum cc_stat list_new_conf (const ListConf const* conf, List **list);
bool list_destroy (List *list);
bool list_destroy_free (List *list);

void list_set_add_callback (List *list, void (*on_add) (List *list, void *data));
void list_set_remove_callback(List *list, void (*on_remove) (List *list));


enum cc_stat list_splice (List *list1, List *list2);
enum cc_stat list_splice_at (List *list, List *list2, size_t index);

Expand Down