exception.h 4.49 KB
/**
 * \file    scot/exception.h
 * \author  Georg Steffers <georg@steffers.org>
 * \brief   The user interface to exception handling.
 *
 * This describes the macros TRY, CATCH, THROW and EXC.
 *
 * 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  EXCEPTION_H
#define  EXCEPTION_H

#include <setjmp.h>

#include "excenv_t.h"
#include "exception_t.h"
#include <scot/thread.h>


#ifdef   USE_THREADS
#  define   EXC_INIT    threaded_exc_init
#else
#  define   EXC_INIT    exc_init
#endif   /* USE_THREADS */

/**
 * \pre     None
 * \return  Nothing
 * \post    a current exception environment exists.
 *
 * \brief   start exception handled code.
 *
 * This starts a block of exception handled code. This is done by
 * creating a current exception environment.
 */
#define  TRY                                                      \
   {                                                              \
      excenv_new (EXC_INIT ());                                   \
      if (setjmp (* excenv_jmp_buf (EXC_INIT ())) == 0)

/**
 * \param   ee will hold the exception environment actually handled.
 * \pre     a current exception environment must exists.
 * \return  Nothing
 * \post    ee holds the current exception environment and it is removed
 *          from the stack of exception environments.
 *
 * \brief   start exception handling.
 *
 * This starts a block of exception handling. This is done by
 * retrieving the actual exception environment into \a ee.
 */
#define  CATCH(ee)                                                \
      ee = excenv_catch (EXC_INIT ());                            \
   }                                                              \
   if (excenv_has_exception(ee) == 0)                             \
      free_catched (ee);                                          \
   else

/**
 * \param   e the exception to be thrown.
 * \pre     a current exception environment must exist.
 * \return  Nothing
 * \post    \a e is put into the current exception environment.
 *
 * \brief   Throws an exception into the actual exception environment.
 */
#define  THROW(e)                                                 \
   exc_throw (EXC_INIT (), (e))

/**
 * \brief this is just a wrapper around exc_new().
 *
 * This is just a wrapper around exc_new() that fills in automatically
 * file and line.
 */
#define  EXC(lvl, errnum, err_msg)                                \
   exc_new (lvl, __FILE__, __LINE__, errnum, err_msg)

/**
 * \brief   a wrapper for exc_in_this_try()
 */
#define  EXC_IN_THIS_TRY(e)                                       \
   exc_in_this_try (e)

void *      exc_init           ();
void *      threaded_exc_init  ();

void        excenv_new           (void *);
jmp_buf *   excenv_jmp_buf       (void *);
void        exc_throw            (void *, const exception_t *);
excenv_t *  excenv_catch         (void *);
int         excenv_has_exception (const excenv_t *);

exception_t *  exc_new           (
      const enum exclvl_t, 
      const char *,
      const int,
      const int, 
      const char *);

exception_t *  retrive_exception       (const excenv_t *);

void           free_catched            (excenv_t *);
void           free_exception          (exception_t *);
void           thread_exc_end          (THREAD_T);
void           exc_end                 (void);

void           print_exception         (exception_t *);
void           print_all_exceptions    (excenv_t *);
void           forward_all_exceptions  (excenv_t *);

int            exc_in_this_try         (exception_t *);
enum exclvl_t  exc_lvl_get             (exception_t *);
char *         exc_file_get            (exception_t *);
int            exc_line_get            (exception_t *);
int            exc_errnum_get          (exception_t *);
char *         exc_err_msg_get         (exception_t *);
#endif   /* EXCEPTION_H */