00001 /* 00002 * The information in this file is 00003 * Copyright(c) 2007 Ball Aerospace & Technologies Corporation 00004 * and is subject to the terms and conditions of the 00005 * GNU Lesser General Public License Version 2.1 00006 * The license text is available from 00007 * http://www.gnu.org/licenses/lgpl.html 00008 */ 00009 00010 #ifndef APPVERIFY_H 00011 #define APPVERIFY_H 00012 00013 /** @file AppVerify.h 00014 * @brief Macros which perform verification with logging. 00015 * 00016 * These macros allow for concise verification of boolean expressions. When an expression 00017 * is false, they log an error to the message log then return, with an optional return value, 00018 * or throw an exception. 00019 */ 00020 00021 /** @def LogVerificationError(pExpression, pMsg) 00022 * @brief Add a log message stating that \a pExpression failed and add \a pMsg if not NULL. 00023 */ 00024 #define LogVerificationError(pExpression, pMsg) LogVerificationErrorProc(pExpression, __FILE__, __LINE__, pMsg) 00025 00026 void LogVerificationErrorProc(const char* pExpression, const char* pFilename, unsigned int line, const char* pMsg); 00027 00028 /** @def VERIFY_ACTION(expr, action, msg) 00029 * @brief If \a expr is false, log \a msg and perform the specified action. 00030 */ 00031 #define VERIFY_ACTION(expr, action, msg) \ 00032 if (!(expr)) \ 00033 {\ 00034 LogVerificationError(#expr, msg);\ 00035 action;\ 00036 } 00037 00038 /** @def VERIFY_VALUE(expr, value1, value2, msg) 00039 * @brief If \a expr is false, log \a msg and evaluate to value1, else evaluate to value2. 00040 */ 00041 #define VERIFY_VALUE(expr, value1, value2, msg) \ 00042 ( \ 00043 !(expr) ? \ 00044 LogVerificationError(#expr, msg), \ 00045 value1 \ 00046 : \ 00047 value2 \ 00048 ) 00049 00050 /** @def VERIFYRV_MSG(expr, rv, msg) 00051 * @brief If \a expr is false, log \a msg and return \a rv from the current function. 00052 */ 00053 #define VERIFYRV_MSG(expr, rv, msg) VERIFY_ACTION(expr, return rv, msg) 00054 00055 /** @def VERIFYNRV_MSG(expr, msg) 00056 * @brief If \a expr is false, log \a msg and return from the current function. 00057 */ 00058 #define VERIFYNRV_MSG(expr, msg) VERIFY_ACTION(expr, return, msg) 00059 00060 /** @def VERIFYNR_MSG(expr, msg) 00061 * @brief If \a expr is false, log \a msg and evaluate to false, else evaluate to true. 00062 */ 00063 #define VERIFYNR_MSG(expr, msg) VERIFY_VALUE(expr, false, true, msg) 00064 00065 /** @def VERIFYNR(expr) 00066 * @brief If \a expr is false, log a message and evaluate to false, else evaluate to true. 00067 */ 00068 #define VERIFYNR(expr) VERIFYNR_MSG(expr, NULL) 00069 00070 /** @def NN(ptr) 00071 * @brief If \a ptr is NULL, log a message and evaluate to false, else evaluate to true. 00072 */ 00073 #define NN(ptr) VERIFYNR(ptr != NULL) 00074 00075 /** @def VERIFY_MSG(expr, msg) 00076 * @brief If \a expr is false, log \a msg and return false from the current function. 00077 */ 00078 #define VERIFY_MSG(expr, msg) VERIFYRV_MSG(expr, false, msg) 00079 00080 /** @def VERIFYRV(expr, rv) 00081 * @brief If \a expr is false, log a message and return \a rv from the current function. 00082 */ 00083 #define VERIFYRV(expr, rv) VERIFYRV_MSG(expr, rv, NULL) 00084 00085 /** @def VERIFY(expr) 00086 * @brief If \a expr is false, log a message and return false from the current function. 00087 */ 00088 #define VERIFY(expr) VERIFYRV(expr, false) 00089 00090 /** @def VERIFYNRV(expr) 00091 * @brief If \a expr is false, log a message and return from the current function. 00092 */ 00093 #define VERIFYNRV(expr) VERIFYNRV_MSG(expr, NULL) 00094 00095 /** @def LOG_IF(expr, action) 00096 * @brief If \a expr is true, log \a msg and perform the specified action. 00097 */ 00098 #define LOG_IF(expr, action) VERIFY_ACTION(!(expr), action, NULL) 00099 00100 /** @def LOG_IF_ELSE(expr, value1 ,value2) 00101 * @brief If \a expr is true, log \a msg and evaluate to value1, else evaluate to value2. 00102 */ 00103 #define LOG_IF_ELSE(expr, value1, value2) VERIFY_VALUE(!(expr), value1, value2, NULL) 00104 00105 /** @def DO_IF(expr, action) 00106 * @brief If \a expr is true, perform the specified action. This is like LOG_IF without logging. 00107 */ 00108 #define DO_IF(expr, action) \ 00109 if (expr) \ 00110 { \ 00111 action; \ 00112 } 00113 00114 #ifdef NDEBUG 00115 #define VERIFY_DEBUG(s) s 00116 #define VERIFYRV_DEBUG(s, rv) s 00117 #define VERIFYNRV_DEBUG(s) s 00118 #else 00119 #define VERIFY_DEBUG(s) VERIFY(s) 00120 #define VERIFYRV_DEBUG(s, rv) VERIFYRV(s, rv) 00121 #define VERIFYNRV_DEBUG(s) VERIFYNRV(s) 00122 #endif 00123 00124 #endif