| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2020 Collabora Ltd. |
| 4 |
* @author Julian Bouzas <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "iterator.h" |
| 10 |
#include "log.h" |
| 11 |
#include <spa/utils/defs.h> |
| 12 |
|
| 13 |
WP_DEFINE_LOCAL_LOG_TOPIC ("wp-iterator") |
| 14 |
|
| 15 |
/*! \defgroup wpiterator WpIterator */ |
| 16 |
/*! |
| 17 |
* \struct WpIterator |
| 18 |
* A generic iterator API |
| 19 |
*/ |
| 20 |
struct _WpIterator |
| 21 |
{ |
| 22 |
const WpIteratorMethods *methods; |
| 23 |
gpointer user_data; |
| 24 |
}; |
| 25 |
|
| 26 |
G_DEFINE_BOXED_TYPE (WpIterator, wp_iterator, wp_iterator_ref, wp_iterator_unref) |
| 27 |
|
| 28 |
static gboolean |
| 29 |
wp_iterator_default_fold (WpIterator *self, WpIteratorFoldFunc func, |
| 30 |
GValue *item, gpointer data) |
| 31 |
{ |
| 32 |
GValue next = G_VALUE_INIT; |
| 33 |
|
| 34 |
wp_iterator_reset (self); |
| 35 |
|
| 36 |
while (wp_iterator_next (self, &next)) { |
| 37 |
const gboolean res = func (&next, item, data); |
| 38 |
g_value_unset (&next); |
| 39 |
if (!res) |
| 40 |
return FALSE; |
| 41 |
} |
| 42 |
|
| 43 |
return TRUE; |
| 44 |
} |
| 45 |
|
| 46 |
struct foreach_fold_data { |
| 47 |
WpIteratorForeachFunc func; |
| 48 |
gpointer data; |
| 49 |
}; |
| 50 |
|
| 51 |
static gboolean |
| 52 |
foreach_fold_func (const GValue *item, GValue *ret, gpointer data) |
| 53 |
{ |
| 54 |
struct foreach_fold_data *d = data; |
| 55 |
d->func (item, d->data); |
| 56 |
return TRUE; |
| 57 |
} |
| 58 |
|
| 59 |
static gboolean |
| 60 |
wp_iterator_default_foreach (WpIterator *self, WpIteratorForeachFunc func, |
| 61 |
gpointer data) |
| 62 |
{ |
| 63 |
struct foreach_fold_data d = {func, data}; |
| 64 |
return wp_iterator_fold (self, foreach_fold_func, NULL, &d); |
| 65 |
} |
| 66 |
|
| 67 |
/*! |
| 68 |
* \brief Constructs an iterator that uses the provided \a methods to implement |
| 69 |
* its API. |
| 70 |
* |
| 71 |
* The WpIterator structure is internally allocated with \a user_size additional |
| 72 |
* space at the end. A pointer to this space can be retrieved with |
| 73 |
* wp_iterator_get_user_data() and is available for implementation-specific |
| 74 |
* storage. |
| 75 |
* |
| 76 |
* \ingroup wpiterator |
| 77 |
* \param methods method implementations for the new iterator |
| 78 |
* \param user_size size of the user_data structure to be allocated |
| 79 |
* \returns (transfer full): a new custom iterator |
| 80 |
*/ |
| 81 |
WpIterator * |
| 82 |
wp_iterator_new (const WpIteratorMethods *methods, size_t user_size) |
| 83 |
{ |
| 84 |
WpIterator *self = NULL; |
| 85 |
|
| 86 |
g_return_val_if_fail (methods, NULL); |
| 87 |
|
| 88 |
self = g_rc_box_alloc0 (sizeof (WpIterator) + user_size); |
| 89 |
self->methods = methods; |
| 90 |
if (user_size > 0) |
| 91 |
self->user_data = SPA_MEMBER (self, sizeof (WpIterator), void); |
| 92 |
|
| 93 |
return self; |
| 94 |
} |
| 95 |
|
| 96 |
/*! |
| 97 |
* \brief Gets the implementation-specific storage of an iterator |
| 98 |
* \note this only for use by implementations of WpIterator |
| 99 |
* |
| 100 |
* \ingroup wpiterator |
| 101 |
* \param self an iterator object |
| 102 |
* \returns a pointer to the implementation-specific storage area |
| 103 |
*/ |
| 104 |
gpointer |
| 105 |
wp_iterator_get_user_data (WpIterator *self) |
| 106 |
{ |
| 107 |
return self->user_data; |
| 108 |
} |
| 109 |
|
| 110 |
/*! |
| 111 |
* \brief Increases the reference count of an iterator |
| 112 |
* \ingroup wpiterator |
| 113 |
* \param self an iterator object |
| 114 |
* \returns (transfer full): \a self with an additional reference count on it |
| 115 |
*/ |
| 116 |
WpIterator * |
| 117 |
wp_iterator_ref (WpIterator *self) |
| 118 |
{ |
| 119 |
return (WpIterator *) g_rc_box_acquire ((gpointer) self); |
| 120 |
} |
| 121 |
|
| 122 |
static void |
| 123 |
wp_iterator_free (WpIterator *self) |
| 124 |
{ |
| 125 |
if (self->methods->finalize) |
| 126 |
self->methods->finalize (self); |
| 127 |
} |
| 128 |
|
| 129 |
/*! |
| 130 |
* \brief Decreases the reference count on \a self and frees it when the ref |
| 131 |
* count reaches zero. |
| 132 |
* |
| 133 |
* \ingroup wpiterator |
| 134 |
* \param self (transfer full): an iterator object |
| 135 |
*/ |
| 136 |
void |
| 137 |
wp_iterator_unref (WpIterator *self) |
| 138 |
{ |
| 139 |
g_rc_box_release_full (self, (GDestroyNotify) wp_iterator_free); |
| 140 |
} |
| 141 |
|
| 142 |
/*! |
| 143 |
* \brief Resets the iterator so we can iterate again from the beginning. |
| 144 |
* |
| 145 |
* \ingroup wpiterator |
| 146 |
* \param self the iterator |
| 147 |
*/ |
| 148 |
void |
| 149 |
wp_iterator_reset (WpIterator *self) |
| 150 |
{ |
| 151 |
g_return_if_fail (self); |
| 152 |
g_return_if_fail (self->methods->reset); |
| 153 |
|
| 154 |
self->methods->reset (self); |
| 155 |
} |
| 156 |
|
| 157 |
/*! |
| 158 |
* \brief Gets the next item of the iterator. |
| 159 |
* |
| 160 |
* \ingroup wpiterator |
| 161 |
* \param self the iterator |
| 162 |
* \param item (out): the next item of the iterator |
| 163 |
* \returns TRUE if next iterator was obtained, FALSE when the iterator has no |
| 164 |
* more items to iterate through. |
| 165 |
*/ |
| 166 |
|
| 167 |
gboolean |
| 168 |
wp_iterator_next (WpIterator *self, GValue *item) |
| 169 |
{ |
| 170 |
g_return_val_if_fail (self, FALSE); |
| 171 |
g_return_val_if_fail (self->methods->next, FALSE); |
| 172 |
|
| 173 |
return self->methods->next (self, item); |
| 174 |
} |
| 175 |
|
| 176 |
/*! |
| 177 |
* \brief Fold a function over the items of the iterator. |
| 178 |
* |
| 179 |
* \ingroup wpiterator |
| 180 |
* \param self the iterator |
| 181 |
* \param func (scope call)(closure data): the fold function |
| 182 |
* \param ret (inout): the accumulator data |
| 183 |
* \param data the user data |
| 184 |
* \returns TRUE if all the items were processed, FALSE otherwise. |
| 185 |
*/ |
| 186 |
gboolean |
| 187 |
wp_iterator_fold (WpIterator *self, WpIteratorFoldFunc func, GValue *ret, |
| 188 |
gpointer data) |
| 189 |
{ |
| 190 |
g_return_val_if_fail (self, FALSE); |
| 191 |
|
| 192 |
if (self->methods->fold) |
| 193 |
return self->methods->fold (self, func, ret, data); |
| 194 |
|
| 195 |
return wp_iterator_default_fold (self, func, ret, data); |
| 196 |
} |
| 197 |
|
| 198 |
/*! |
| 199 |
* \brief Iterates over all items of the iterator calling a function. |
| 200 |
* |
| 201 |
* \ingroup wpiterator |
| 202 |
* \param self the iterator |
| 203 |
* \param func (scope call)(closure data): the foreach function |
| 204 |
* \param data the user data |
| 205 |
* \returns TRUE if all the items were processed, FALSE otherwise. |
| 206 |
*/ |
| 207 |
gboolean |
| 208 |
wp_iterator_foreach (WpIterator *self, WpIteratorForeachFunc func, |
| 209 |
gpointer data) |
| 210 |
{ |
| 211 |
g_return_val_if_fail (self, FALSE); |
| 212 |
|
| 213 |
if (self->methods->foreach) |
| 214 |
return self->methods->foreach (self, func, data); |
| 215 |
|
| 216 |
return wp_iterator_default_foreach (self, func, data); |
| 217 |
} |
| 218 |
|
| 219 |
struct ptr_array_iterator_data |
| 220 |
{ |
| 221 |
GPtrArray *array; |
| 222 |
GType item_type; |
| 223 |
guint index; |
| 224 |
void (*set_value) (GValue *, gpointer); |
| 225 |
}; |
| 226 |
|
| 227 |
static void |
| 228 |
ptr_array_iterator_reset (WpIterator *it) |
| 229 |
{ |
| 230 |
struct ptr_array_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 231 |
it_data->index = 0; |
| 232 |
} |
| 233 |
|
| 234 |
static gboolean |
| 235 |
ptr_array_iterator_next (WpIterator *it, GValue *item) |
| 236 |
{ |
| 237 |
struct ptr_array_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 238 |
|
| 239 |
while (it_data->index < it_data->array->len) { |
| 240 |
gpointer ptr = g_ptr_array_index (it_data->array, it_data->index++); |
| 241 |
if (!ptr) |
| 242 |
continue; |
| 243 |
g_value_init (item, it_data->item_type); |
| 244 |
it_data->set_value (item, ptr); |
| 245 |
return TRUE; |
| 246 |
} |
| 247 |
return FALSE; |
| 248 |
} |
| 249 |
|
| 250 |
static gboolean |
| 251 |
ptr_array_iterator_fold (WpIterator *it, WpIteratorFoldFunc func, GValue *ret, |
| 252 |
gpointer data) |
| 253 |
{ |
| 254 |
struct ptr_array_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 255 |
gpointer *ptr, *base; |
| 256 |
guint len; |
| 257 |
|
| 258 |
ptr = base = it_data->array->pdata; |
| 259 |
len = it_data->array->len; |
| 260 |
|
| 261 |
while ((ptr - base) < len) { |
| 262 |
if (*ptr) { |
| 263 |
g_auto (GValue) item = G_VALUE_INIT; |
| 264 |
g_value_init (&item, it_data->item_type); |
| 265 |
it_data->set_value (&item, *ptr); |
| 266 |
if (!func (&item, ret, data)) |
| 267 |
return FALSE; |
| 268 |
} |
| 269 |
ptr++; |
| 270 |
} |
| 271 |
return TRUE; |
| 272 |
} |
| 273 |
|
| 274 |
static void |
| 275 |
ptr_array_iterator_finalize (WpIterator *it) |
| 276 |
{ |
| 277 |
struct ptr_array_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 278 |
g_ptr_array_unref (it_data->array); |
| 279 |
} |
| 280 |
|
| 281 |
static const WpIteratorMethods ptr_array_iterator_methods = { |
| 282 |
.version = WP_ITERATOR_METHODS_VERSION, |
| 283 |
.reset = ptr_array_iterator_reset, |
| 284 |
.next = ptr_array_iterator_next, |
| 285 |
.fold = ptr_array_iterator_fold, |
| 286 |
.finalize = ptr_array_iterator_finalize, |
| 287 |
}; |
| 288 |
|
| 289 |
/*! |
| 290 |
* \brief Creates an iterator from a pointer array |
| 291 |
* |
| 292 |
* \ingroup wpiterator |
| 293 |
* \param items (element-type gpointer) (transfer full): the items to iterate over |
| 294 |
* \param item_type the type of each item |
| 295 |
* \returns (transfer full): a new iterator that iterates over \a items |
| 296 |
*/ |
| 297 |
WpIterator * |
| 298 |
wp_iterator_new_ptr_array (GPtrArray * items, GType item_type) |
| 299 |
{ |
| 300 |
g_autoptr (WpIterator) it = NULL; |
| 301 |
struct ptr_array_iterator_data *it_data; |
| 302 |
|
| 303 |
g_return_val_if_fail (items != NULL, NULL); |
| 304 |
|
| 305 |
it = wp_iterator_new (&ptr_array_iterator_methods, |
| 306 |
sizeof (struct ptr_array_iterator_data)); |
| 307 |
it_data = wp_iterator_get_user_data (it); |
| 308 |
it_data->array = items; |
| 309 |
it_data->item_type = item_type; |
| 310 |
it_data->index = 0; |
| 311 |
|
| 312 |
if (g_type_is_a (item_type, G_TYPE_POINTER)) |
| 313 |
it_data->set_value = g_value_set_pointer; |
| 314 |
else if (g_type_is_a (item_type, G_TYPE_BOXED)) |
| 315 |
it_data->set_value = (void (*) (GValue *, gpointer)) g_value_set_boxed; |
| 316 |
else if (g_type_is_a (item_type, G_TYPE_OBJECT)) |
| 317 |
it_data->set_value = g_value_set_object; |
| 318 |
else if (g_type_is_a (item_type, G_TYPE_INTERFACE)) |
| 319 |
it_data->set_value = g_value_set_object; |
| 320 |
else if (g_type_is_a (item_type, G_TYPE_VARIANT)) |
| 321 |
it_data->set_value = (void (*) (GValue *, gpointer)) g_value_set_variant; |
| 322 |
else if (g_type_is_a (item_type, G_TYPE_STRING)) |
| 323 |
it_data->set_value = (void (*) (GValue *, gpointer)) g_value_set_string; |
| 324 |
else |
| 325 |
g_return_val_if_reached (NULL); |
| 326 |
|
| 327 |
return g_steal_pointer (&it); |
| 328 |
} |