diff --git a/common/include/villas/list.h b/common/include/villas/list.h index 5680d285a..f3073427c 100644 --- a/common/include/villas/list.h +++ b/common/include/villas/list.h @@ -29,6 +29,9 @@ #pragma once #include +#include +#include +#include #include #include @@ -87,23 +90,8 @@ int vlist_remove(struct vlist *l, size_t idx); int vlist_insert(struct vlist *l, size_t idx, void *p); -/** Return the first list element which is identified by a string in its first member variable. - * - * List elements are pointers to structures of the following form: - * - * struct obj { - * char *name; - * // more members - * } - * - * @see Only possible because of ยง1424 of http://c0x.coding-guidelines.com/6.7.2.1.html - */ -void * vlist_lookup(struct vlist *l, const char *name); - -ssize_t vlist_lookup_index(struct vlist *l, const void *ptr); - /** Return the first element of the list for which cmp returns zero */ -void * vlist_search(struct vlist *l, cmp_cb_t cmp, void *ctx); +void * vlist_search(struct vlist *l, cmp_cb_t cmp, const void *ctx); /** Returns the number of occurences for which cmp returns zero when called on all list elements. */ int vlist_count(struct vlist *l, cmp_cb_t cmp, void *ctx); @@ -129,3 +117,38 @@ void vlist_extend(struct vlist *l, size_t len, void *val); /** Remove all elements for which the callback returns a non-zero return code. */ void vlist_filter(struct vlist *l, dtor_cb_t cb); + +#include + +/** Lookup an element from the list based on an UUID */ +template +T * vlist_lookup_uuid(struct vlist *l, uuid_t uuid) +{ + return (T *) vlist_search(l, [](const void *a, const void *b) -> int { + auto *n = reinterpret_cast(a); + uuid_t u; memcpy(u, b, sizeof(uuid_t)); + + return uuid_compare(n->uuid, u); + }, uuid); +} + +/** Lookup an element from the list based on a name */ +template +T * vlist_lookup_name(struct vlist *l, const std::string &name) +{ + return (T *) vlist_search(l, [](const void *a, const void *b) -> int { + auto *e = reinterpret_cast(a); + auto *s = reinterpret_cast(b); + + return *s == e->name ? 0 : 1; + }, &name); +} + +/** Lookup index of list element based on name */ +template +ssize_t vlist_lookup_index(struct vlist *l, const std::string &name) +{ + auto *f = vlist_lookup_name(l, name); + + return f ? vlist_index(l, f) : -1; +} \ No newline at end of file diff --git a/common/lib/list.cpp b/common/lib/list.cpp index b1501ad45..8050add7b 100644 --- a/common/lib/list.cpp +++ b/common/lib/list.cpp @@ -28,21 +28,6 @@ #include #include -/* Compare functions */ -static int cmp_lookup(const void *a, const void *b) { - struct name_tag { - char *name; - }; - - const name_tag *obj = (struct name_tag *) a; - - return strcmp(obj->name, (char *) b); -} - -static int cmp_contains(const void *a, const void *b) { - return a == b ? 0 : 1; -} - #ifdef __APPLE__ static int cmp_sort(void *thunk, const void *a, const void *b) { #else @@ -183,21 +168,11 @@ void vlist_remove_all(struct vlist *l, void *p) pthread_mutex_unlock(&l->lock); } -void * vlist_lookup(struct vlist *l, const char *name) -{ - return vlist_search(l, cmp_lookup, (void *) name); -} - -ssize_t vlist_lookup_index(struct vlist *l, const void *ptr) -{ - void *found = vlist_lookup(l, (char *) ptr); - - return found ? vlist_index(l, found) : -1; -} - int vlist_contains(struct vlist *l, void *p) { - return vlist_count(l, cmp_contains, p); + return vlist_count(l, [](const void *a, const void *b) { + return a == b ? 0 : 1; + }, p); } int vlist_count(struct vlist *l, cmp_cb_t cmp, void *ctx) @@ -220,7 +195,7 @@ int vlist_count(struct vlist *l, cmp_cb_t cmp, void *ctx) return c; } -void * vlist_search(struct vlist *l, cmp_cb_t cmp, void *ctx) +void * vlist_search(struct vlist *l, cmp_cb_t cmp, const void *ctx) { void *e; diff --git a/common/lib/log.cpp b/common/lib/log.cpp index 6a0560ae3..d2dc22ca4 100644 --- a/common/lib/log.cpp +++ b/common/lib/log.cpp @@ -88,8 +88,7 @@ void Log::parse(json_t *cfg) const char *path = nullptr; const char *pattern = nullptr; - int syslog; - int ret; + int ret, syslog = 0; json_error_t err; json_t *json_expressions = nullptr; diff --git a/common/tests/unit/list.cpp b/common/tests/unit/list.cpp index d51e0ede3..e072c753b 100644 --- a/common/tests/unit/list.cpp +++ b/common/tests/unit/list.cpp @@ -39,7 +39,7 @@ struct data { TestSuite(list, .description = "List datastructure"); -Test(list, vlist_lookup) +Test(list, vlist_lookup_name) { struct vlist l; @@ -56,7 +56,7 @@ Test(list, vlist_lookup) vlist_push(&l, d); } - struct data *found = (struct data *) vlist_lookup(&l, "woman"); + struct data *found = vlist_lookup_name(&l, "woman"); cr_assert_eq(found->data, 13);