src/chain.h
author Tero Marttila <terom@fixme.fi>
Sun, 15 Mar 2009 23:24:21 +0200
changeset 58 65bd90f94f4e
parent 35 3715247e8f5a
child 69 6f298b6e0d5f
permissions -rw-r--r--
merge modules -> default, this is a bit early as it breaks functionality, but who cares, need to replace the build system now :)
#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