core/io/callback_list.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file callback_list.c | ||
| 3 | * @brief Adding to and running a callback list. | ||
| 4 | * | ||
| 5 | * @ingroup lib_core | ||
| 6 | */ | ||
| 7 | #include "io/callback_list.h" | ||
| 8 | |||
| 9 | #include <stddef.h> | ||
| 10 | |||
| 11 | 29 | bool callback_list_add(CallbackList *list, CallbackListFn cb) | |
| 12 | { | ||
| 13 |
2/2✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 27 times.
|
29 | if (!cb) |
| 14 | { | ||
| 15 | 2 | return true; | |
| 16 | } | ||
| 17 | |||
| 18 |
2/2✓ Branch 8 → 5 taken 61 times.
✓ Branch 8 → 9 taken 24 times.
|
85 | for (int i = 0; i < list->count; i++) |
| 19 | { | ||
| 20 |
2/2✓ Branch 5 → 6 taken 3 times.
✓ Branch 5 → 7 taken 58 times.
|
61 | if (list->entries[i] == cb) |
| 21 | { | ||
| 22 | 3 | return true; | |
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 |
2/2✓ Branch 9 → 10 taken 4 times.
✓ Branch 9 → 11 taken 20 times.
|
24 | if (list->count >= list->max) |
| 27 | { | ||
| 28 | 4 | return false; | |
| 29 | } | ||
| 30 | |||
| 31 | 20 | list->entries[list->count++] = cb; | |
| 32 | 20 | return true; | |
| 33 | } | ||
| 34 | |||
| 35 | 3 | void callback_list_remove(CallbackList *list, CallbackListFn cb) | |
| 36 | { | ||
| 37 |
2/2✓ Branch 9 → 3 taken 4 times.
✓ Branch 9 → 10 taken 1 time.
|
5 | for (int i = 0; i < list->count; i++) |
| 38 | { | ||
| 39 |
2/2✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 8 taken 2 times.
|
4 | if (list->entries[i] == cb) |
| 40 | { | ||
| 41 |
2/2✓ Branch 6 → 5 taken 1 time.
✓ Branch 6 → 7 taken 2 times.
|
3 | for (int j = i + 1; j < list->count; j++) |
| 42 | { | ||
| 43 | 1 | list->entries[j - 1] = list->entries[j]; | |
| 44 | } | ||
| 45 | 2 | list->count--; | |
| 46 | 2 | return; | |
| 47 | } | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | 14 | void callback_list_fire(const CallbackList *list) | |
| 52 | { | ||
| 53 |
2/2✓ Branch 5 → 3 taken 25 times.
✓ Branch 5 → 6 taken 14 times.
|
39 | for (int i = 0; i < list->count; i++) |
| 54 | { | ||
| 55 | 25 | list->entries[i](); | |
| 56 | } | ||
| 57 | 14 | } | |
| 58 |