Code

utils llist: Added sdb_llist_clear().
[sysdb.git] / src / include / utils / llist.h
index af8285f6a3367fdc026f81bf0387243ff143c20f..28816a6a49abaaf58232f55ab5b7e9a9f6a2ffa8 100644 (file)
@@ -41,7 +41,7 @@ struct sdb_llist_iter;
 typedef struct sdb_llist_iter sdb_llist_iter_t;
 
 typedef int (*sdb_llist_cmp_cb)(const sdb_object_t *, const sdb_object_t *);
-typedef int (*sdb_llist_lookup_cb)(const sdb_object_t *, void *user_data);
+typedef int (*sdb_llist_lookup_cb)(const sdb_object_t *, const void *user_data);
 
 /*
  * sdb_llist_create, sdb_llist_destroy:
@@ -56,6 +56,14 @@ sdb_llist_create(void);
 void
 sdb_llist_destroy(sdb_llist_t *list);
 
+/*
+ * sdb_llist_clear:
+ * Remove all elements from the list, releasing the included objects
+ * (decrement the ref-count).
+ */
+void
+sdb_llist_clear(sdb_llist_t *list);
+
 /*
  * sdb_llist_clone:
  * Clone an existing list. The objects stored in the list will not be copied
@@ -119,6 +127,15 @@ int
 sdb_llist_insert_sorted(sdb_llist_t *list,
                sdb_object_t *obj, sdb_llist_cmp_cb);
 
+/*
+ * sdb_llist_get:
+ * Returns the i-th element of the list or NULL in case of an error. The
+ * reference count of the element is incremented before returning it to share
+ * ownership between the list and the caller.
+ */
+sdb_object_t *
+sdb_llist_get(sdb_llist_t *list, size_t i);
+
 /*
  * sdb_llist_search:
  * Search for a object in the given 'list'. The function will return the first
@@ -131,7 +148,7 @@ sdb_llist_insert_sorted(sdb_llist_t *list,
  */
 sdb_object_t *
 sdb_llist_search(sdb_llist_t *list,
-               sdb_llist_lookup_cb lookup, void *user_data);
+               sdb_llist_lookup_cb lookup, const void *user_data);
 
 /*
  * sdb_llist_search_by_name:
@@ -148,9 +165,9 @@ sdb_llist_search_by_name(sdb_llist_t *list, const char *key);
 
 /*
  * sdb_llist_remove:
- * Removes and returns the first matchin element of the list. The ref-count of
- * the item will not be changed, that is, if the element will not be used any
- * further, it should be de-referenced by the caller.
+ * Removes and returns the first matching element of the list. The ref-count
+ * of the item will not be changed, that is, if the element will not be used
+ * any further, it should be de-referenced by the caller.
  *
  * Returns:
  *  - a pointer to the first matching object
@@ -158,7 +175,20 @@ sdb_llist_search_by_name(sdb_llist_t *list, const char *key);
  */
 sdb_object_t *
 sdb_llist_remove(sdb_llist_t *list,
-               sdb_llist_lookup_cb lookup, void *user_data);
+               sdb_llist_lookup_cb lookup, const void *user_data);
+
+/*
+ * sdb_llist_remove_by_name:
+ * Removes and returns the first element whose name matches the specified key.
+ * The ref-count of the item will not be changed, that is, if the element will
+ * not be used any further, it should be de-referenced by the caller.
+ *
+ * Returns:
+ *  - a pointer to the first matching object
+ *  - NULL else
+ */
+sdb_object_t *
+sdb_llist_remove_by_name(sdb_llist_t *list, const char *key);
 
 /*
  * sdb_llist_shift:
@@ -173,7 +203,8 @@ sdb_llist_remove(sdb_llist_t *list,
 sdb_object_t *
 sdb_llist_shift(sdb_llist_t *list);
 
-/* sdb_llist_get_iter, sdb_llist_iter_has_next, sdb_llist_iter_get_next:
+/*
+ * sdb_llist_get_iter, sdb_llist_iter_has_next, sdb_llist_iter_get_next:
  * Iterate through the list, element by element.
  *
  * sdb_llist_iter_get_next returns NULL if there is no next element.
@@ -188,6 +219,27 @@ sdb_llist_iter_has_next(sdb_llist_iter_t *iter);
 sdb_object_t *
 sdb_llist_iter_get_next(sdb_llist_iter_t *iter);
 
+/*
+ * sdb_llist_iter_remove_current:
+ * Remove the current object from the list, that is, the object which was
+ * returned by the last call to sdb_llist_iter_get_next().
+ *
+ * This operation is not safe if another iterator is in use at the same time.
+ *
+ * Returns:
+ *  - 0 on success
+ *  - a negative value else
+ */
+int
+sdb_llist_iter_remove_current(sdb_llist_iter_t *iter);
+
+/*
+ * sdb_llist_len:
+ * Return the length (number of elements) of the list.
+ */
+size_t
+sdb_llist_len(sdb_llist_t *list);
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif