src/chain.h
author Tero Marttila <terom@fixme.fi>
Fri, 13 Mar 2009 17:38:23 +0200
branchmodules
changeset 54 9f74e924b01a
parent 35 3715247e8f5a
child 69 6f298b6e0d5f
permissions -rw-r--r--
initial modules code
#ifndef CHAIN_H
#define CHAIN_H

/**
 * Defines a generalized "chain of things" behaviour.
 *
 * The structure of this is a CHAIN_LIST, which contains a number of CHAIN_HEAD items, which then contain a number of
 * induvidual items.
 */
#include "error.h"
#include <sys/queue.h>

/**
 * The chain header.
 */
struct chain_head {
    const void *chain;

    void *arg;

    STAILQ_ENTRY(chain_head) node;
};

/**
 * @struct chain_list
 *
 * The chain list
 */
STAILQ_HEAD(chain_list, chain_head);

/**
 * Initialize a `struct chain_list`
 */
#define CHAIN_INIT(list_ptr) STAILQ_INIT(list_ptr)

/**
 * Add an item onto the end of a chain_list
 */
err_t chain_add (struct chain_list *list, const void *chain, void *arg);

/**
 * Iterate over the contents of a chain_list
 *
 * struct chain_list *list = ...;
 * struct chain_head *head;
 * struct foo *foo;
 *
 *  CHAIN_FOREACH(list, head) {
 *      for (foo = head->chain; foo->bar; foo++) {
 *          foo->bar(foo, head->arg);
 *      }
 *  }
 */
#define CHAIN_FOREACH(list_ptr, head_ptr) STAILQ_FOREACH(head_ptr, list_ptr, node)

/**
 * Free a chain_list
 */
void chain_free (struct chain_list *list);

#endif