![]() |
SolarCapture C Bindings User Guide
SF-115721-CD
Draft 2A
|
sc_dlist: A doubly-linked list. More...
Data Structures | |
struct | sc_dlist |
Doubly linked list pointers. More... | |
Macros | |
#define | SC_CONTAINER(c_type, mbr_name, p_mbr) ( (c_type*) ((char*)(p_mbr) - SC_MEMBER_OFFSET(c_type, mbr_name)) ) |
Get pointer to container from pointer to member. More... | |
#define | SC_DLIST_FOR_EACH_OBJ(list, iter, mbr) |
Create a for statement that loops over each container item in the list. It is not safe to modify the list using this macro, if list modifications are required see SC_DLIST_FOR_EACH_OBJ_SAFE. More... | |
#define | SC_DLIST_FOR_EACH_OBJ_SAFE(list, iter, next_entry, mbr) |
Create a for statement that loops over each container item in the list which can be safely be modified during traversal. More... | |
Functions | |
static void | sc_dlist_init (struct sc_dlist *list) |
Initialise a pre-allocated sc_dlist to be an empty doubly linked list. More... | |
static int | sc_dlist_is_empty (const struct sc_dlist *list) |
Check if a doubly linked list is empty, returns 1 if true 0 otherwise. | |
static void | sc_dlist_push_head (struct sc_dlist *list, struct sc_dlist *l) |
Prepend an item to the head of a doubly-linked list. More... | |
static void | sc_dlist_push_tail (struct sc_dlist *list, struct sc_dlist *l) |
Append an item to the tail of a doubly-linked list. More... | |
static void | sc_dlist_remove (struct sc_dlist *l) |
Remove an item from the list. More... | |
static struct sc_dlist * | sc_dlist_pop_head (struct sc_dlist *list) |
Pop off the head of a list. More... | |
static struct sc_dlist * | sc_dlist_pop_tail (struct sc_dlist *list) |
Pop the tail of a list. More... | |
static void | sc_dlist_rehome (struct sc_dlist *to_list, struct sc_dlist *from_list) |
Replace an item in a list with another item. More... | |
sc_dlist: A doubly-linked list.
A doubly-linked list always has one item with no data at the head. This can be used by embedding dlist in a parent struct.
For example:
#include <stdio.h> #include <stdlib.h> #include <solar_capture.h> int main() { struct my_struct { int my_int; double my_double; struct sc_dlist list_ptr; }; struct sc_dlist my_list; sc_dlist_init(&my_list); int i; struct my_struct* element; // Add some elements to the list for( i=0; i < 10; ++i ) { element = malloc(sizeof(struct my_struct)); element->my_int = i; element->my_double = i; sc_dlist_push_tail(&my_list, &element->list_ptr); } // cycle over the list SC_DLIST_FOR_EACH_OBJ(&my_list, element, list_ptr) printf("element->my_int=%d, element->my_double=%f\n", element->my_int, element->my_double); // remove each item from the list struct sc_dlist* list_ptr; while( !sc_dlist_is_empty(&my_list) ) { list_ptr = sc_dlist_pop_tail(&my_list); element = SC_CONTAINER(struct my_struct, list_ptr, list_ptr); printf("Just popped element with element->my_int=%d", element->my_int); free(element); }
#define SC_CONTAINER | ( | c_type, | |
mbr_name, | |||
p_mbr | |||
) | ( (c_type*) ((char*)(p_mbr) - SC_MEMBER_OFFSET(c_type, mbr_name)) ) |
Get pointer to container from pointer to member.
c_type | The container type. |
mbr_name | The name of the member in c_type. |
p_mbr | Pointer to the member. |
#define SC_DLIST_FOR_EACH_OBJ | ( | list, | |
iter, | |||
mbr | |||
) |
Create a for statement that loops over each container item in the list. It is not safe to modify the list using this macro, if list modifications are required see SC_DLIST_FOR_EACH_OBJ_SAFE.
#define SC_DLIST_FOR_EACH_OBJ_SAFE | ( | list, | |
iter, | |||
next_entry, | |||
mbr | |||
) |
Create a for statement that loops over each container item in the list which can be safely be modified during traversal.
|
inlinestatic |
Pop off the head of a list.
list | The point to pop the head from. |
list
. Pop the tail of a list.
list | The point to pop the tail from. |
list
. Prepend an item to the head of a doubly-linked list.
list | The list to prepend to. |
l | The item to prepend to list . |
Append an item to the tail of a doubly-linked list.
list | The list to append to. |
l | The item to append to list . |
Replace an item in a list with another item.
to_list | The item to add to the list, replacing from_list . |
from_list | The item to remove from the list. |
|
inlinestatic |
Remove an item from the list.
l | The item to remove. |