Code

parser: Let the TIMESERIES command accept optional data-source names.
[sysdb.git] / src / include / utils / llist.h
1 /*
2  * SysDB - src/include/utils/llist.h
3  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #ifndef SDB_UTILS_LLIST_H
29 #define SDB_UTILS_LLIST_H 1
31 #include "core/object.h"
33 #include <stdbool.h>
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
39 struct sdb_llist;
40 typedef struct sdb_llist sdb_llist_t;
42 struct sdb_llist_iter;
43 typedef struct sdb_llist_iter sdb_llist_iter_t;
45 /*
46  * sdb_llist_create, sdb_llist_destroy:
47  * Create and destroy a doubly linked list object.
48  *
49  * sdb_llist_create returns NULL on error.
50  * sdb_llist_destroy will also destroy all remaining elements, thus releasing
51  * the included objects (decrement the ref-count).
52  */
53 sdb_llist_t *
54 sdb_llist_create(void);
55 void
56 sdb_llist_destroy(sdb_llist_t *list);
58 /*
59  * sdb_llist_clear:
60  * Remove all elements from the list, releasing the included objects
61  * (decrement the ref-count).
62  */
63 void
64 sdb_llist_clear(sdb_llist_t *list);
66 /*
67  * sdb_llist_clone:
68  * Clone an existing list. The objects stored in the list will not be copied
69  * but rather their reference count incremented.
70  *
71  * Returns:
72  *  - the copied list on success
73  *  - NULL else
74  */
75 sdb_llist_t *
76 sdb_llist_clone(sdb_llist_t *list);
78 /*
79  * sdb_llist_append:
80  * Append the given 'obj' to the end of 'list'. The list will take ownership
81  * of the object, that is, increment the reference count by one. In case the
82  * caller does not longer use the object for other purposes, it should thus
83  * deref it.
84  *
85  * Returns:
86  *  - 0 on success
87  *  - a negative value on failure
88  */
89 int
90 sdb_llist_append(sdb_llist_t *list, sdb_object_t *obj);
92 /*
93  * sdb_llist_insert:
94  * Insert the new element at the specified position (zero being the head of
95  * the list; the length of the list being the tail)). If the index is greater
96  * than the length of the list (i.e. past the tail of the list), an error is
97  * returned. The list will take ownership of the object, that is, increment
98  * the reference count by one. In case the caller does not longer use the
99  * object for other purposes, it should thus deref it.
100  *
101  * Returns:
102  *  - 0 on success
103  *  - a negative value on failure
104  */
105 int
106 sdb_llist_insert(sdb_llist_t *list, sdb_object_t *obj, size_t idx);
108 /*
109  * sdb_llist_insert_sorted:
110  * Insert the given 'obj' in the 'list' using a sort order as determined by
111  * the 'compare' function. The function will insert the new entry before the
112  * first entry which sorts later than the new entry. It will not ensure that
113  * the rest of the list is sorted. The list will take ownership of the object,
114  * that is, increment the reference count by one. In case the caller does not
115  * longer use the object for other purposes, it should thus deref it.
116  *
117  * The 'compare' function should return less than zero, zero, greater than
118  * zero if the first argument sorts less than, equal or greater than the
119  * second argument respectively.
120  *
121  * Returns:
122  *  - 0 on success
123  *  - a negative value on failure
124  */
125 int
126 sdb_llist_insert_sorted(sdb_llist_t *list,
127                 sdb_object_t *obj, sdb_object_cmp_cb);
129 /*
130  * sdb_llist_get:
131  * Returns the i-th element of the list or NULL in case of an error. The
132  * reference count of the element is incremented before returning it to share
133  * ownership between the list and the caller.
134  */
135 sdb_object_t *
136 sdb_llist_get(sdb_llist_t *list, size_t i);
138 /*
139  * sdb_llist_search:
140  * Search for a object in the given 'list'. The function will return the first
141  * entry for which the 'lookup' callback returns 0. The 'user_data' is passed
142  * on to the lookup function on each invocation.
143  *
144  * Returns:
145  *  - a pointer to the first matching object
146  *  - NULL else
147  */
148 sdb_object_t *
149 sdb_llist_search(sdb_llist_t *list,
150                 sdb_object_lookup_cb lookup, const void *user_data);
152 /*
153  * sdb_llist_search_by_name:
154  * Search for an object named 'key' in the given 'list'. The function will
155  * return the first entry whose name matches the specified 'key' ignoring the
156  * case of the characters.
157  *
158  * Returns:
159  *  - a pointer to the first matching object
160  *  - NULL else
161  */
162 sdb_object_t *
163 sdb_llist_search_by_name(sdb_llist_t *list, const char *key);
165 /*
166  * sdb_llist_remove:
167  * Removes and returns the first matching element of the list. The ref-count
168  * of the item will not be changed, that is, if the element will not be used
169  * any further, it should be de-referenced by the caller.
170  *
171  * Returns:
172  *  - a pointer to the first matching object
173  *  - NULL else
174  */
175 sdb_object_t *
176 sdb_llist_remove(sdb_llist_t *list,
177                 sdb_object_lookup_cb lookup, const void *user_data);
179 /*
180  * sdb_llist_remove_by_name:
181  * Removes and returns the first element whose name matches the specified key.
182  * The ref-count of the item will not be changed, that is, if the element will
183  * not be used any further, it should be de-referenced by the caller.
184  *
185  * Returns:
186  *  - a pointer to the first matching object
187  *  - NULL else
188  */
189 sdb_object_t *
190 sdb_llist_remove_by_name(sdb_llist_t *list, const char *key);
192 /*
193  * sdb_llist_shift:
194  * Removes and returns the first element of the list. The ref-count of the
195  * item will not be changed, that is, if the element will not be used any
196  * further, it should be de-referenced by the caller.
197  *
198  * Returns:
199  *  - the former first element of the list
200  *  - NULL if the list is empty
201  */
202 sdb_object_t *
203 sdb_llist_shift(sdb_llist_t *list);
205 /*
206  * sdb_llist_get_iter, sdb_llist_iter_has_next, sdb_llist_iter_get_next:
207  * Iterate through the list, element by element.
208  *
209  * sdb_llist_iter_get_next returns NULL if there is no next element.
210  */
211 sdb_llist_iter_t *
212 sdb_llist_get_iter(sdb_llist_t *list);
213 void
214 sdb_llist_iter_destroy(sdb_llist_iter_t *iter);
216 bool
217 sdb_llist_iter_has_next(sdb_llist_iter_t *iter);
218 sdb_object_t *
219 sdb_llist_iter_get_next(sdb_llist_iter_t *iter);
221 /*
222  * sdb_llist_iter_remove_current:
223  * Remove the current object from the list, that is, the object which was
224  * returned by the last call to sdb_llist_iter_get_next().
225  *
226  * This operation is not safe if another iterator is in use at the same time.
227  *
228  * Returns:
229  *  - 0 on success
230  *  - a negative value else
231  */
232 int
233 sdb_llist_iter_remove_current(sdb_llist_iter_t *iter);
235 /*
236  * sdb_llist_len:
237  * Return the length (number of elements) of the list.
238  */
239 size_t
240 sdb_llist_len(sdb_llist_t *list);
242 #ifdef __cplusplus
243 } /* extern "C" */
244 #endif
246 #endif /* ! SDB_UTILS_LLIST_H */
248 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */