From a1935263ae19d2161b1e425135864c7edb9efe49 Mon Sep 17 00:00:00 2001 From: Dmitri Pal Date: Fri, 23 Jul 2010 14:33:56 -0400 Subject: [COLLECTION] Allow destroying collection with a callback Addresses ticket #572 Bumps the library and interface version. Updates the docs. [COLLECTION] Changed callback signature --- common/collection/collection.c | 57 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 10 deletions(-) (limited to 'common/collection/collection.c') diff --git a/common/collection/collection.c b/common/collection/collection.c index 87ac321..f24102b 100644 --- a/common/collection/collection.c +++ b/common/collection/collection.c @@ -172,8 +172,11 @@ static int col_validate_property(const char *property) return invalid; } -/* Function that cleans the item */ -void col_delete_item(struct collection_item *item) + +/* Function that cleans the item with callback */ +static void col_delete_item_with_cb(struct collection_item *item, + col_item_cleanup_fn cb, + void *custom_data) { struct collection_item *other_collection; @@ -189,9 +192,17 @@ void col_delete_item(struct collection_item *item) /* Our data is a pointer to a whole external collection so dereference * it or delete */ other_collection = *((struct collection_item **)(item->data)); - col_destroy_collection(other_collection); + col_destroy_collection_with_cb(other_collection, cb, custom_data); } + /* Call the callback */ + if (cb) cb(item->property, + item->property_len, + item->type, + item->data, + item->length, + custom_data); + TRACE_INFO_STRING("Deleting property:", item->property); TRACE_INFO_NUMBER("Type:", item->type); @@ -203,6 +214,18 @@ void col_delete_item(struct collection_item *item) TRACE_FLOW_STRING("col_delete_item","Exit."); } +/* Function that cleans the item */ +void col_delete_item(struct collection_item *item) +{ + TRACE_FLOW_STRING("col_delete_item","Entry point."); + + col_delete_item_with_cb(item, NULL, NULL); + + TRACE_FLOW_STRING("col_delete_item","Exit."); +} + + + /* A generic function to allocate a property item */ int col_allocate_item(struct collection_item **ci, const char *property, const void *item_data, int length, int type) @@ -1164,7 +1187,9 @@ static int col_get_subcollection(const char *property, /* Cleans the collection tree including current item. */ /* The passed in variable should not be used after the call * as memory is freed!!! */ -static void col_delete_collection(struct collection_item *ci) +static void col_delete_collection(struct collection_item *ci, + col_item_cleanup_fn cb, + void *custom_data) { TRACE_FLOW_STRING("col_delete_collection", "Entry."); @@ -1177,10 +1202,10 @@ static void col_delete_collection(struct collection_item *ci) TRACE_INFO_STRING("Property", ci->property); TRACE_INFO_NUMBER("Next item", ci->next); - col_delete_collection(ci->next); + col_delete_collection(ci->next, cb, custom_data); /* Delete this item */ - col_delete_item(ci); + col_delete_item_with_cb(ci, cb, custom_data); TRACE_FLOW_STRING("col_delete_collection", "Exit."); } @@ -2235,11 +2260,13 @@ int col_create_collection(struct collection_item **ci, const char *name, /* DESTROY */ /* Function that destroys a collection */ -void col_destroy_collection(struct collection_item *ci) +void col_destroy_collection_with_cb(struct collection_item *ci, + col_item_cleanup_fn cb, + void *custom_data) { struct collection_header *header; - TRACE_FLOW_STRING("col_destroy_collection", "Entry."); + TRACE_FLOW_STRING("col_destroy_collection_with_cb", "Entry."); /* Do not try to delete NULL */ if (ci == NULL) return; @@ -2263,13 +2290,23 @@ void col_destroy_collection(struct collection_item *ci) header->reference_count); } else { - col_delete_collection(ci); + col_delete_collection(ci, cb, custom_data); } - TRACE_FLOW_STRING("col_destroy_collection", "Exit."); + TRACE_FLOW_STRING("col_destroy_collection_with_cb", "Exit."); } +/* Function that destroys a collection */ +void col_destroy_collection(struct collection_item *ci) +{ + TRACE_FLOW_STRING("col_destroy_collection", "Entry."); + + col_destroy_collection_with_cb(ci, NULL, NULL); + + TRACE_FLOW_STRING("col_destroy_collection", "Exit."); +} + /* COPY */ /* Wrapper around a more advanced function */ -- cgit