OSSP CVS Repository

ossp - ossp-pkg/ex/ex.h 1.1
Not logged in
[Honeypot]  [Browse]  [Directory]  [Home]  [Login
[Reports]  [Search]  [Ticket]  [Timeline
  [Raw

ossp-pkg/ex/ex.h 1.1
/*
**  Copyright (c) 2001-2002 The OSSP Project <http://www.ossp.org/>
**  Copyright (c) 2001-2002 Cable & Wireless Deutschland <http://www.cw.com/de/>
**
**  This file is part of OSSP ex, an exception library
**  which can be found at http://www.ossp.org/pkg/ex/.
**
**  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.0 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 file; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
**  USA, or contact the OSSP project <ossp@ossp.org>.
**
**  ex.h: exception handling (pre-processor part)
*/

#ifndef __TEXAS_H__
#define __TEXAS_H__

/* the required ISO-C standard facilities */
#include <stdio.h>   /* for NULL */
#include <setjmp.h>  /* for jmp_buf, setjmp(3), longjmp(3) */

/* determine how the current function name can be fetched */
#if (   defined(__STDC__) \
     && defined(__STDC_VERSION__) \
     && __STDC_VERSION__ >= 199901L)
#define __TEXAS_FUNC__ __func__      /* ISO C99 compliant */
#elif (   defined(__GNUC__) \
       && defined(__GNUC_MINOR__) \
       && (   __GNUC__ > 2 \
           || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)))
#define __TEXAS_FUNC__ __FUNCTION__  /* gcc >= 2.8 */
#else
#define __TEXAS_FUNC__ "#NA#"        /* not available */
#endif

/* declare the exception type (public) */
typedef struct {
    /* throw value */
    void *ex_class;
    void *ex_object;
    void *ex_value;
    /* throw point */
    char *ex_file; 
    int   ex_line; 
    char *ex_func;
} ex_t;

/* declare the exception context type (private) */
typedef struct {
    jmp_buf      *ctx_jbprev; /* previous jump buffer */
    int           ctx_caught; /* flag whether exception was caught */
    volatile ex_t ctx_ex;     /* the exception temporary storage */
} ex_ctx_t;

/* ex context (default requires linking against libex) */
#ifndef __ex_ctx
#define __ex_ctx (&__ex_ctx_global)
extern ex_ctx_t __ex_ctx_global;
#endif

/* block for trying execution */
#define ex_try \
    { \
        jmp_buf *__ex_jbprev; \
        jmp_buf __ex_jbme; \
        __ex_jbprev = __ex_ctx->ctx_jbprev; \
        __ex_ctx->ctx_jbprev = &__ex_jbme; \
        if (setjmp(__ex_jbme) == 0) { \
            if (1)

/* block for catching an exception */
#define ex_catch(e) \
            else { \
            } \
            __ex_ctx->ctx_caught = 0; \
        } \
        else { \
            __ex_ctx->ctx_caught = 1; \
        } \
        __ex_ctx->ctx_jbprev = __ex_jbprev; \
    } \
    if (!__ex_ctx->ctx_caught || ((e) = __ex_ctx->ctx_ex, 0)) { \
    } \
    else

/* throw a new exception */
#define ex_throw(c,o,v) \
    (__ex_ctx->ctx_jbprev == NULL ? \
     (abort(), 0) : \
     ( __ex_ctx->ctx_ex.ex_class  = (void *)(c), \
       __ex_ctx->ctx_ex.ex_object = (void *)(o), \
       __ex_ctx->ctx_ex.ex_value  = (void *)(v), \
       __ex_ctx->ctx_ex.ex_file   = __FILE__, \
       __ex_ctx->ctx_ex.ex_line   = __LINE__, \
       __ex_ctx->ctx_ex.ex_func   = __TEXAS_FUNC__, \
       longjmp(*(__ex_ctx->ctx_jbprev), 1), \
       0 \
     ) \
    )

/* re-throw a caught exception */
#define ex_rethrow \
    (__ex_ctx->ctx_jbprev == NULL ? \
     (abort(), 0) : \
     (longjmp(*(__ex_ctx->ctx_jbprev), 1), 0) \
    )

/* optional namespace mapping */
#if !defined(__cplusplus) && !defined(__TEXAS_NO_CXX_NS__)
#define try      ex_try
#define catch    ex_catch
#define throw    ex_throw
#define rethrow  ex_rethrow
#endif
#if !defined(__TEXAS_NO_SIMPLE_NS__)
#define Try      ex_try
#define Catch    ex_catch
#define Throw    ex_throw
#define Rethrow  ex_rethrow
#endif

#endif /* __TEXAS_H__ */


CVSTrac 2.0.1