h5tools_error.h

00001 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00002  * Copyright by The HDF Group.                                               *
00003  * Copyright by the Board of Trustees of the University of Illinois.         *
00004  * All rights reserved.                                                      *
00005  *                                                                           *
00006  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
00007  * terms governing use, modification, and redistribution, is contained in    *
00008  * the files COPYING and Copyright.html.  COPYING can be found at the root   *
00009  * of the source code distribution tree; Copyright.html can be found at the  *
00010  * root level of an installed copy of the electronic HDF5 document set and   *
00011  * is linked from the top-level documents page.  It can also be found at     *
00012  * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
00013  * access to either file, you may request a copy from help@hdfgroup.org.     *
00014  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00015 
00016 /*
00017  *  Header file for error values, etc.
00018  */
00019 #ifndef H5TOOLS_ERROR_H_
00020 #define H5TOOLS_ERROR_H_
00021 
00022 #include "H5Epublic.h"
00023 
00024 /* tools-HDF5 Error variables */
00025 extern hid_t H5tools_ERR_CLS_g;
00026 extern hid_t H5E_tools_g;
00027 extern hid_t H5E_tools_min_id_g;
00028 
00029 /* Use FUNC to safely handle variations of C99 __func__ keyword handling */
00030 #ifdef H5_HAVE_C99_FUNC
00031 #define FUNC __func__
00032 #elif defined(H5_HAVE_FUNCTION)
00033 #define FUNC __FUNCTION__
00034 #else
00035 #error "We need __func__ or __FUNCTION__ to test function names!"
00036 #endif
00037 
00038 /*
00039  * H5TOOLS_INIT_ERROR macro, used to initialize error reporting.
00040  */
00041 #define H5TOOLS_INIT_ERROR() {                                                                      \
00042         H5tools_ERR_CLS_g = H5Eregister_class("H5tools", "HDF5:tools", lib_str);                    \
00043         H5E_tools_g= H5Ecreate_msg(H5tools_ERR_CLS_g, H5E_MAJOR, "Failure in tools library");       \
00044         H5E_tools_min_id_g = H5Ecreate_msg(H5tools_ERR_CLS_g, H5E_MINOR, "error in function");      \
00045 }
00046 
00047 /*
00048  * H5TOOLS_CLOSE_ERROR macro, used to initialize error reporting.
00049  */
00050 #define H5TOOLS_CLOSE_ERROR() {                                 \
00051         H5Eclose_msg(H5E_tools_min_id_g);                       \
00052         H5Eclose_msg(H5E_tools_g);                              \
00053         H5Eunregister_class(H5tools_ERR_CLS_g);                 \
00054 }
00055 
00056 /*
00057  * HERR_INIT macro, used to facilitate error reporting. Declaration and assignments of error variables.
00058  * Use at the beginning of a function using error handling macros.
00059  */
00060 #define HERR_INIT(ret_typ, ret_init)                    \
00061     hbool_t past_catch = FALSE;                         \
00062     ret_typ ret_value = ret_init;
00063 
00064 
00065 /*
00066  * HERROR macro, used to facilitate error reporting .  The arguments are the major
00067  * error number, the minor error number, and a description of the error.
00068  */
00069 #define HERROR(maj_id, min_id, str) H5Epush2(H5E_DEFAULT, __FILE__, FUNC, __LINE__, H5tools_ERR_CLS_g, maj_id, min_id, str)
00070 
00071 /* Macro for "catching" flow of control when an error occurs.  Note that the
00072  *      H5_LEAVE macro won't jump back here once it's past this point.
00073  */
00074 #define CATCH catch_except:; past_catch = TRUE;
00075 
00076 /*
00077  * H5_LEAVE macro, used to facilitate control flow between a
00078  * BEGIN_FUNC() and an END_FUNC() within a function body.  The argument is
00079  * the return value.
00080  * The return value is assigned to a variable `ret_value' and control branches
00081  * to the `catch_except' label, if we're not already past it.
00082  */
00083 #define H5_LEAVE(v) {                           \
00084     ret_value = v;                              \
00085     if(!past_catch)                             \
00086         goto catch_except;                      \
00087 }
00088 
00089 /*
00090  * H5E_THROW macro, used to facilitate error reporting within a function body.
00091  * The arguments are the minor error number, and an error string.
00092  * The return value is assigned to a variable `ret_value' and control branches
00093  * to the `catch_except' label, if we're not already past it.
00094  */
00095 #define H5E_THROW(fail_value, min_id, str) {        \
00096     HERROR(H5E_tools_g, min_id, str);                 \
00097     H5_LEAVE(fail_value)                            \
00098 }
00099 
00100 /*
00101  * HGOTO_ERROR macro, used to facilitate error reporting within a function body.  The arguments are
00102  * the major error number, the minor error number, the return value, and an
00103  * error string.  The return value is assigned to a variable `ret_value' and
00104  * control branches to the `done' label.
00105  */
00106 #define HGOTO_ERROR(fail_value, min_id, str) {        \
00107    HERROR(H5E_tools_g, min_id, str);                 \
00108    HGOTO_DONE(fail_value)                          \
00109 }
00110 
00111 /*
00112  * HGOTO_DONE macro, used to facilitate normal return within a function body.
00113  * The argument is the return value which is assigned to the `ret_value'
00114  * variable.  Control branches to the `done' label.
00115  */
00116 #define HGOTO_DONE(ret_val) {ret_value = ret_val; goto done;}
00117 
00118 #endif /* H5TOOLS_ERROR_H_ */
00119