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

Conversation

mame98
Copy link

@mame98 mame98 commented Mar 20, 2016

This implementation is similar to what you suggested in #10

It can only trigger the callback if you add a single element to the list. Any ideas how we should do this with add_all or add_all_at?

Signed-off-by: Marius Messerschmidt marius.messerschmidt@googlemail.com

Signed-off-by: Marius Messerschmidt <marius.messerschmidt@googlemail.com>
@mame98
Copy link
Author

mame98 commented Mar 20, 2016

Some example code

#include <collectc/list.h>
#include <stdio.h>

void add_element(List *list, void *data)
{
    printf("Added %s\n", (char *) data);
}

int main(void)
{
    List *list;
    list_new(&list);
    list_set_add_callback(list, add_element);

    return 0;
}

@srdja
Copy link
Owner

srdja commented Mar 20, 2016

This looks great.

As for the add_all and the like, I'm not sure what we should do. I guess that we can trigger the callback for each element these functions successfuly add / remove. This would be the obvious behavior because you register a callback for an insert and you get a callback triggered for each separate insert regardless of whether or not the elements were added individually or in bulk. But then the question is whether you really want to treat bulk and and individual inserts / deletes the same way. For example if you add a 100,000 elements through add_all do you actually want that many callbacks? In the end I'm not sure.

Have you had any specific ideas on how to solve this?

@mame98
Copy link
Author

mame98 commented Mar 20, 2016

I thought of maybe another callback set for bulk add/remove which will be triggered on each element...

@srdja
Copy link
Owner

srdja commented Mar 20, 2016

I agree, you should be able to specify if you want the callback triggered on add_all or not.

How about this:

#include <collectc/list.h>
#include <stdio.h>

void add_element(List *list, void *data)
{
    printf("Added %s\n", (char *) data);
}

int main(void)
{
    List *list;
    list_new(&list);
    list_set_add_callback(list, add_element, false);  // false if you don't want add_all to trigger the callbacks
    return 0;
}

then there's no need to set separate callbacks.

We can then spare ourselves two function pointers and instead use a flags field in the List structure

#define CB_ON_BULK_ADD    0x01
#define CB_ON_BULK_REMOVE 0x02

struct list_s {
    uint8_t flags;
    ...
};

We could set them like so:

void list_set_add_callback(List *list, void (*on_add) (List *list, void *data), bool on_bulk)
{
    if (on_bulk)
        list->flags = list->flags | CB_ON_BULK_ADD; // set flag
    else
        list->flats = list->flags & ~CB_ON_BULK_ADD; // clear flag

    ...
}

and in add_all:

...list_add_all_at(...) {

    ...
    if (list->on_add && (list->flags & CB_ON_BULK_ADD)) {
        // do callbacks
    }
    ...
}

and so on...

@mame98
Copy link
Author

mame98 commented Mar 20, 2016

This sounds solid, I think that this solution should work fine!

@srdja
Copy link
Owner

srdja commented Nov 11, 2016

I'm closing this PR since it has gone stale and there doesn't seem to be any progress on it.

@srdja srdja closed this Nov 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants