src/chain.h
changeset 35 3715247e8f5a
child 69 6f298b6e0d5f
equal deleted inserted replaced
34:763f65f9df0c 35:3715247e8f5a
       
     1 #ifndef CHAIN_H
       
     2 #define CHAIN_H
       
     3 
       
     4 /**
       
     5  * Defines a generalized "chain of things" behaviour.
       
     6  *
       
     7  * The structure of this is a CHAIN_LIST, which contains a number of CHAIN_HEAD items, which then contain a number of
       
     8  * induvidual items.
       
     9  */
       
    10 #include "error.h"
       
    11 #include <sys/queue.h>
       
    12 
       
    13 /**
       
    14  * The chain header.
       
    15  */
       
    16 struct chain_head {
       
    17     const void *chain;
       
    18 
       
    19     void *arg;
       
    20 
       
    21     STAILQ_ENTRY(chain_head) node;
       
    22 };
       
    23 
       
    24 /**
       
    25  * @struct chain_list
       
    26  *
       
    27  * The chain list
       
    28  */
       
    29 STAILQ_HEAD(chain_list, chain_head);
       
    30 
       
    31 /**
       
    32  * Initialize a `struct chain_list`
       
    33  */
       
    34 #define CHAIN_INIT(list_ptr) STAILQ_INIT(list_ptr)
       
    35 
       
    36 /**
       
    37  * Add an item onto the end of a chain_list
       
    38  */
       
    39 err_t chain_add (struct chain_list *list, const void *chain, void *arg);
       
    40 
       
    41 /**
       
    42  * Iterate over the contents of a chain_list
       
    43  *
       
    44  * struct chain_list *list = ...;
       
    45  * struct chain_head *head;
       
    46  * struct foo *foo;
       
    47  *
       
    48  *  CHAIN_FOREACH(list, head) {
       
    49  *      for (foo = head->chain; foo->bar; foo++) {
       
    50  *          foo->bar(foo, head->arg);
       
    51  *      }
       
    52  *  }
       
    53  */
       
    54 #define CHAIN_FOREACH(list_ptr, head_ptr) STAILQ_FOREACH(head_ptr, list_ptr, node)
       
    55 
       
    56 /**
       
    57  * Free a chain_list
       
    58  */
       
    59 void chain_free (struct chain_list *list);
       
    60 
       
    61 #endif