SolarCapture C Bindings
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups
Data Structures | Macros | Functions
dlist.h File Reference

sc_dlist: A doubly-linked list. More...

Go to the source code of this file.

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)) )
 Fetch a pointer to the outer container of a given sc_dlist pointer. 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_dlistsc_dlist_pop_head (struct sc_dlist *list)
 Pop off the head of a list. More...
 
static struct sc_dlistsc_dlist_pop_tail (struct sc_dlist *list)
 Pop the tail of a list. More...
 

Detailed Description

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);
    }