list_nav.h 13.6 KB
/**
 * \file scot/list_nav.h
 * \author  Georg Steffers <georg@steffers.org>
 * \brief   Templates of functions to navigate within typesafe lists.
 *
 * Here are macro definitions that create functions to navigate within
 * typesafe lists. That is get next node, get first node a.s.f
 *
 * Normally the macros defined here will be never called directly but only
 * via MACROS that group them in a sensefull way in scot/list_impl.h.\n
 * \anchor onlyfunc_nav
 * \attention
 * All documentation here does document the functions that are created by
 * the macros, as the macros themself are pretty easy and all used the same.
 * They are called with a type, that MUST be one word (use typedef if needed)
 * and generates the function defined with their value.
 *
 * Copyright (C)2006    Georg Steffers <georg@steffers.org>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
#ifndef  LIST_NAV_H
#define  LIST_NAV_H

#include <stdlib.h>
#include <scot/list_impl.h>



/**
 * \internal
 * \param   node     a list_[type]_node_t* that should be checked for
 *                   beeing an anchor.
 * \param   line     the line where this MACRO is called.
 * \pre              a variable of type list_[type]_node_t* must exist.
 * \return           the code fragment
 * \post             None
 *
 * \brief check for anchor.
 *
 * This checks if the given \a node is an anchor. If not it calls
 * LIST_ERROR, which either raises an exception if exceptions are user
 * or otherwise prints out an error message to stderr and aborts the 
 * program.
 */
#define  NAV_NODE_NO_ANCHOR_ERROR(node, line)                        \
      if ((node->e) != NULL)                                         \
         LIST_ERROR ("list_nav.h", (line), NODE_NO_ANCHOR_ERR);


/**
 * \attention
 * Only the generated function is explained here, for the reason look
 * \ref onlyfunc_man "here".
 *
 * \param   anchor   A pointer to the list anchor.
 * \pre              The \a anchor has to be not NULL and a valid
 *                   anchor.
 * \return           The first node in the list, that is normally
 *                   the anchor, thus this is pretty useless.
 * \post             None
 *
 * \brief Returns the first element in the list, that is the anchor...
 *        pathetic...
 */
#define  GEN_LIST_FRONT(type)                                        \
   STATIC                                                            \
   list_ ## type ## _node_t *                                        \
   list_ ## type ## _front (list_ ## type ## _node_t  *anchor)       \
   {                                                                 \
      LIST_EXC_START                                                 \
      {                                                              \
         LIST_CHECK_NULL (anchor, "list_nav.h", 20);                 \
         NAV_NODE_NO_ANCHOR_ERROR (anchor, 21);                      \
      }                                                              \
      LIST_EXC_END ("list_nav.h", 23, LIST_FRONT_ERR);               \
                                                                     \
      return anchor;                                                 \
   }

/**
 * \attention
 * Only the generated function is explained here, for the reason look
 * \ref onlyfunc_man "here".
 *
 * \param   anchor   A pointer to the list anchor.
 * \pre              The \a anchor has to be not NULL and a valid
 *                   anchor.
 * \return           The last node in the list.
 * \post             None
 *
 * \brief Get the last node in the list.
 */
#define  GEN_LIST_LAST(type)                                         \
   STATIC                                                            \
   list_ ## type ## _node_t *                                        \
   list_ ## type ## _last (list_ ## type ## _node_t  *anchor)        \
   {                                                                 \
      LIST_EXC_START                                                 \
      {                                                              \
         LIST_CHECK_NULL (anchor, "list_nav.h", 35);                 \
         NAV_NODE_NO_ANCHOR_ERROR (anchor, 36);                      \
      }                                                              \
      LIST_EXC_END ("list_nav.h", 38, LIST_LAST_ERR);                \
                                                                     \
      return anchor->prev;                                           \
   }

/**
 * \attention
 * Only the generated function is explained here, for the reason look
 * \ref onlyfunc_man "here".
 *
 * \param   node     A pointer to a node in the list.
 * \pre              The node has to be not NULL and should be in
 *                   a linked list.
 * \return           The next node after the given node.
 * \post             None
 *
 * \brief Get the next node in the list.
 */
#define  GEN_LIST_NEXT(type)                                         \
   STATIC                                                            \
   list_ ## type ## _node_t *                                        \
   list_ ## type ## _next (list_ ## type ## _node_t  *node)          \
   {                                                                 \
      LIST_EXC_START                                                 \
         LIST_CHECK_NULL (node, "list_nav.h", 49);                   \
      LIST_EXC_END ("list_nav.h", 50, LIST_NEXT_ERR);                \
                                                                     \
      return node->next;                                             \
   }

/**
 * \attention
 * Only the generated function is explained here, for the reason look
 * \ref onlyfunc_man "here".
 *
 * \param   node     A pointer to a node in the list.
 * \pre              The node has to be not NULL and should be in
 *                   a linked list.
 * \return           The previous node after the given node.
 * \post             None
 *
 * \brief Get the previous node in the list.
 */
#define  GEN_LIST_PREV(type)                                         \
   STATIC                                                            \
   list_ ## type ## _node_t *                                        \
   list_ ## type ## _prev (list_ ## type ## _node_t  *node)          \
   {                                                                 \
      LIST_EXC_START                                                 \
         LIST_CHECK_NULL (node, "list_nav.h", 61);                   \
      LIST_EXC_END ("list_nav.h", 62, LIST_PREV_ERR);                \
                                                                     \
      return node->prev;                                             \
   }

/**
 * \attention
 * Only the generated function is explained here, for the reason look
 * \ref onlyfunc_man "here".
 *
 * \param   anchor   A pointer to the list anchor.
 * \param   e        A pointer to a variable of the \a type, the
 *                   list was created for.
 * \pre              The \a anchor has to be not NULL and a valid
 *                   anchor.\a e must not be NULL.
 * \return           The node that contains \a e.
 * \post             None
 *
 * \brief Find the node in the list that contains \a e.
 */
#define  GEN_LIST_FIND(type)                                         \
   STATIC                                                            \
   list_ ## type ## _node_t *                                        \
   list_ ## type ## _find (                                          \
         list_ ## type ## _node_t   *anchor,                         \
         const type                 *e)                              \
   {                                                                 \
      list_ ## type ## _node_t *ret = NULL;                          \
                                                                     \
      LIST_EXC_START                                                 \
      {                                                              \
         list_ ## type ## _node_t *node;                             \
                                                                     \
         LIST_CHECK_NULL (anchor, "list_nav.h", 80);                 \
         NAV_NODE_NO_ANCHOR_ERROR (anchor, 81);                      \
                                                                     \
         node = anchor;                                              \
         while (! list_ ## type ## _eol (anchor, node))              \
         {                                                           \
            node = node->next;                                       \
                                                                     \
            if (node->e != NULL)                                     \
               if (list_ ## type ## _compare (e, node->e) == 0)      \
                  ret = node;                                        \
         }                                                           \
      }                                                              \
      LIST_EXC_END ("list_nav.h", 93, LIST_FIND_ERR);                \
                                                                     \
      return ret;                                                    \
   }

/**
 * \attention
 * Only the generated function is explained here, for the reason look
 * \ref onlyfunc_man "here".
 *
 * \param   node     A pointer to a node in the list.
 * \pre              The node has to be not NULL and should be in
 *                   a linked list.
 * \return           The anchor of the list, the \a node is in.
 * \post             None
 *
 * \brief Get the anchor of the list.
 */
#define  GEN_LIST_FIND_ANCHOR(type)                                  \
   STATIC                                                            \
   list_ ## type ## _node_t *                                        \
   list_ ## type ## _find_anchor (                                   \
         list_ ## type ## _node_t   *entry)                          \
   {                                                                 \
      list_ ## type ## _node_t *ret = NULL;                          \
                                                                     \
      LIST_EXC_START                                                 \
      {                                                              \
         list_ ## type ## _node_t *node;                             \
                                                                     \
         LIST_CHECK_NULL (entry, "list_nav.h", 110);                 \
                                                                     \
         node = entry;                                               \
         if (node->e == NULL)                                        \
            ret = node;                                              \
                                                                     \
         while (! list_ ## type ## _eol (entry, node))               \
         {                                                           \
            node = node->next;                                       \
                                                                     \
            if (node->e == NULL)                                     \
               ret = node;                                           \
         }                                                           \
                                                                     \
         if (ret == NULL)                                            \
            LIST_ERROR ("list_nav.h", 125, MALFORMED_LIST_ERR);      \
      }                                                              \
      LIST_EXC_END ("list_nav.h", 127, LIST_FIND_ANCHOR_ERR);        \
                                                                     \
      return NULL;                                                   \
   }


/**
 * \param   type     the datatype that this list code should handle.
 * \pre              Type must be a single word typename. If one wants
 *                   to use e.g. lists of structs one has to use typedef
 *                   to create a single word type name like this:
 *                   typedef struct mystruct_t mystruct_t;
 * \return           Nothing
 * \post             The functions for the given datatype that are described
 *                   here are generated within the calling build file.
 *
 * \brief create functions neccesary to modify lists of the given \a type.
 *
 * Normally this is not called directly, but by GEN_LIST_IMPL() because this
 * defines just a subset of all functions neccesarry to handle typesafe lists.
 */
#define  GEN_LIST_NAVIGATION(type)                                   \
   GEN_LIST_FRONT       (type);                                      \
   GEN_LIST_LAST        (type);                                      \
   GEN_LIST_NEXT        (type);                                      \
   GEN_LIST_PREV        (type);                                      \
   GEN_LIST_FIND        (type);                                      \
   GEN_LIST_FIND_ANCHOR (type);



#endif   /* LIST_NAV_H */