list_man.h 18.2 KB
/**
 * \file scot/list_man.h
 * \author  Georg Steffers <georg@steffers.org>
 * \brief   Templates of functions to manage typesafe lists.
 *
 * Here are macro definitions that create functions to manage typesafe
 * lists. That is create new list, free list, check nodes 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_man
 * \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_MAN_H
#define  LIST_MAN_H

#include <stdlib.h>
#include <scot/list_impl.h>
#include <scot/memory.h>			/* because we use functions from there */


/**
 * \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  MAN_NODE_NO_ANCHOR_ERROR(node, line)                        \
      if ((node->e) != NULL)                                         \
         LIST_ERROR ("list_man.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 that should contain the list anchor.
 * \pre              None
 * \return           NULL on error, but only if no exceptions are used.
 *                   If exceptions are used an exception is thrown on error.
 *                   On success a pointer to the newly created list_anchor
 *                   will be returned.
 * \retval  NULL     if an error occurs and no exceptions are used.
 * \retval  !=NULL   the pointer to the newly created list anchor.
 * \post             enough memory on the heap.
 *
 * \brief template for list constructor.
 *
 * The function creates a new list anchor on the heap and returns it.
 * This can be used with following list_[type]_*() functions.
 * The memory reserved for this anchor must be freed by the context
 * that uses this function if it did not need the list anymore.\n\n
 */
#define  GEN_LIST_NEW(type)                                          \
   STATIC                                                            \
   list_ ## type ## _node_t *                                        \
   list_ ## type ## _new (list_ ## type ## _node_t *anchor)          \
   {                                                                 \
      LIST_EXC_START                                                 \
      {                                                              \
         anchor = (list_ ## type ## _node_t *)                       \
            LIST_MALLOC (sizeof (list_ ## type ## _node_t),          \
                  "list_man.h", 93);                                 \
                                                                     \
         anchor->e      = NULL;                                      \
         anchor->prev   = anchor;                                    \
         anchor->next   = anchor;                                    \
      }                                                              \
      LIST_EXC_END ("list_man.h", 99, LIST_NEW_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              anchor must point to a valid list anchor previously
 *                   assigned by list_[type]_new().
 * \return           Nothing
 * \post             The list is completely removed from the heap.
 *
 * \brief template for list destructor.
 *
 * The function frees a list given by its anchor. It uses list_[type]_delete()
 * to delete every node one by one and finally it calls free on the anchor.
 * list_[type]_delete does by default only delete the node and not the
 * stored element. Read here to learn more.
 */
#define  GEN_LIST_FREE(type)                                         \
   STATIC                                                            \
   void                                                              \
   list_ ## type ## _free (list_ ## type ## _node_t *anchor)         \
   {                                                                 \
      list_ ## type ## _node_t   *next;                              \
                                                                     \
      LIST_EXC_START                                                 \
      {                                                              \
         LIST_CHECK_NULL (anchor, "list_man.h", 48);                 \
         MAN_NODE_NO_ANCHOR_ERROR (anchor, 49);                      \
                                                                     \
         next = list_ ## type ## _next (anchor);                     \
         while (anchor != next)                                      \
            next = list_ ## type ## _delete (next);                  \
                                                                     \
         SCOT_MEM_FREE (anchor);                                          \
      }                                                              \
      LIST_EXC_END ("list_man.h", 58, LIST_FREE_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.
 * \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_COUNT(type)                                        \
   STATIC                                                            \
   int                                                               \
   list_ ## type ## _count (list_ ## type ## _node_t * anchor)       \
   {                                                                 \
      int ret = 0;                                                   \
                                                                     \
      LIST_EXC_START                                                 \
      {                                                              \
         list_ ## type ## _node_t *node;                             \
                                                                     \
         LIST_CHECK_NULL (anchor, "list_nav.h", 170);                \
         NAV_NODE_NO_ANCHOR_ERROR (anchor, 171);                     \
                                                                     \
         node = anchor;                                              \
         while (! list_ ## type ## _eol (anchor, node))              \
         {                                                           \
            node = node->next;                                       \
                                                                     \
            if (node->e != NULL)                                     \
               ret ++;                                               \
         }                                                           \
      }                                                              \
      LIST_EXC_END ("list_man.h", 182, LIST_COUNT_ERR);              \
                                                                     \
      return ret;                                                    \
   }

/**
 * \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   node     A pointer to a node in the list.
 * \pre              The \a anchor has to be not NULL and a valid
 *                   anchor, the node has to be not NULL too.
 * \return           A boolean indicating if the first non-anchor
 *                   node in the list is node. Tested by the address
 *                   of the node, so it really must be the same
 *                   address.
 * \retval  TRUE     The node is the first node in the list.
 * \retval  FALSE    The node is not the first node in the list.
 * \post             None
 *
 * \brief Checks if node is at the beginning of the list.
 */
#define  GEN_LIST_BOL(type)                                          \
   STATIC                                                            \
   int                                                               \
   list_ ## type ## _bol (                                           \
         list_ ## type ## _node_t   *anchor,                         \
         list_ ## type ## _node_t   *node)                           \
   {                                                                 \
      LIST_EXC_START                                                 \
      {                                                              \
         LIST_CHECK_NULL (anchor, "list_man.h", 70);                 \
         LIST_CHECK_NULL (node, "list_man.h", 71);                   \
         MAN_NODE_NO_ANCHOR_ERROR (anchor, 72);                      \
      }                                                              \
      LIST_EXC_END ("list_man.h", 74, LIST_BOL_ERR);                 \
                                                                     \
      return anchor->next==node;                                     \
   }

/**
 * \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   node     A pointer to a node in the list.
 * \pre              The \a anchor has to be not NULL and a valid
 *                   anchor, the node has to be not NULL too.
 * \return           A boolean indicating if the last non-anchor
 *                   node in the list is node. Tested by the address
 *                   of the node, so it really must be the same
 *                   address.
 * \retval  TRUE     The node is the last node in the list.
 * \retval  FALSE    The node is not the last node in the list.
 * \post             None
 *
 * \brief Checks if node is at the end of the list.
 */
#define  GEN_LIST_EOL(type)                                          \
   STATIC                                                            \
   int                                                               \
   list_ ## type ## _eol (                                           \
         list_ ## type ## _node_t   *anchor,                         \
         list_ ## type ## _node_t   *node)                           \
   {                                                                 \
      LIST_EXC_START                                                 \
      {                                                              \
         LIST_CHECK_NULL (anchor, "list_man.h", 88);                 \
         LIST_CHECK_NULL (node, "list_man.h", 89);                   \
         MAN_NODE_NO_ANCHOR_ERROR (anchor, 90);                      \
      }                                                              \
      LIST_EXC_END ("list_man.h", 92, LIST_EOL_ERR);                 \
                                                                     \
		if (list_ ## type ## _isempty (anchor)) return -1;             \
      return node->next==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           A boolean indicating if the last non-anchor
 *                   node in the list is node. Tested by the address
 *                   of the node, so it really must be the same
 *                   address.
 * \retval  TRUE     The node is the last node in the list.
 * \retval  FALSE    The node is not the last node in the list.
 * \post             None
 *
 * \brief Checks if node is at the end of the list.
 */
#define  GEN_LIST_ISEMPTY(type)                                      \
   STATIC                                                            \
   int                                                               \
   list_ ## type ## _isempty (list_ ## type ## _node_t *anchor)      \
   {                                                                 \
      LIST_EXC_START                                                 \
         LIST_CHECK_NULL (anchor, "list_man.h", 103);                \
      LIST_EXC_END ("list_man.h", 104, LIST_ISEMPTY_ERR);            \
                                                                     \
      return (anchor->prev==anchor && anchor->next==anchor);         \
   }

/**
 * \attention
 * Only the generated function is explained here, for the reason look
 * \ref onlyfunc_man "here".
 *
 * \param   cmp      A pointer to a function that compares two
 *                   variables of \a type.
 * \pre              None
 * \return           Nothing
 * \post             None
 *
 * \brief Set the compare function to \a cmp.
 *
 * This sets the compare function used by some functions generated
 * for a list of the \a type datatype. This compare function has
 * to work similar to strcmp. It should return either <0, ==0 or >0
 * depending on what comparator is lesser, greater or equal.
 */
#define  GEN_LIST_SET_CMP(type)                                      \
   STATIC                                                            \
   void                                                              \
   list_ ## type ## _set_cmp (list_ ## type ## _cmp_fptr cmp)        \
   {                                                                 \
      list_ ## type ## _compare = cmp;                               \
   }

/**
 * \attention
 * Only the generated function is explained here, for the reason look
 * \ref onlyfunc_man "here".
 *
 * \param   cmp      A pointer to a function that compares two
 *                   variables of \a type.
 * \pre              None
 * \return           Nothing
 * \post             None
 *
 * \brief Set the compare function to \a cmp.
 *
 * This sets the compare function used by some functions generated
 * for a list of the \a type datatype. This compare function has
 * to work similar to strcmp. It should return either <0, ==0 or >0
 * depending on what comparator is lesser, greater or equal.
 */
#define  GEN_LIST_SET_ELEM_FREE(type)                                \
   STATIC                                                            \
   void                                                              \
   list_ ## type ## _set_elem_free (                                 \
         list_ ## type ## _elem_free_fptr efree)                     \
   {                                                                 \
      list_ ## type ## _elem_free = efree;                           \
   }



#define  GEN_LIST_ELEM_FREE_IS_SET(type)                             \
   STATIC                                                            \
   long                                                               \
   list_ ## type ## _elem_free_is_set (void)                         \
   {                                                                 \
      return (long) list_ ## type ## _elem_free;                      \
   }


/**
 * \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 manage lists of the given \a type.
 *
 * Normally this is not called directly, but by GEN_LIST_IMPL() because this
 * defined just a subset of all functions neccesarry to handle typesafe lists.
 */
#define  GEN_LIST_MANAGEMENT(type)                                   \
   GEN_LIST_SET_CMP        (type);                                   \
   GEN_LIST_SET_ELEM_FREE  (type);                                   \
   GEN_LIST_ELEM_FREE_IS_SET (type)                                  \
   GEN_LIST_NEW            (type);                                   \
   GEN_LIST_FREE           (type);                                   \
   GEN_LIST_COUNT          (type);                                   \
   GEN_LIST_BOL            (type);                                   \
   GEN_LIST_ISEMPTY        (type);                                   \
   GEN_LIST_EOL            (type);


#endif   /* LIST_MAN_H */