Back to index

lightning-sunbird  0.9+nobinonly
nsDebug.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsDebug_h___
00039 #define nsDebug_h___
00040 
00041 #ifndef nscore_h___
00042 #include "nscore.h"
00043 #endif
00044 
00045 #ifndef nsError_h__
00046 #include "nsError.h"
00047 #endif 
00048 
00049 #ifdef DEBUG
00050 #define NS_DEBUG
00051 #endif
00052 
00059 PR_BEGIN_EXTERN_C
00060 
00064 NS_COM_GLUE void NS_FASTCALL
00065 NSGlue_Warning(const char *aMessage, const char *aFile, PRIntn aLine);
00066 
00070 NS_COM_GLUE void NS_FASTCALL
00071 NSGlue_Abort(const char *aFile, PRIntn aLine);
00072 
00076 NS_COM_GLUE void NS_FASTCALL
00077 NSGlue_Break(const char* aFile, PRIntn aLine);
00078 
00082 NS_COM_GLUE void NS_FASTCALL
00083 NSGlue_Assertion(const char* aStr, const char* aExpr,
00084                  const char* aFile, PRIntn aLine);
00085 
00086 PR_END_EXTERN_C
00087 
00088 #ifdef DEBUG
00089 
00106 #define NS_ABORT_IF_FALSE(_expr, _msg)                        \
00107   PR_BEGIN_MACRO                                              \
00108     if (!(_expr)) {                                           \
00109       NSGlue_Assertion(_msg, #_expr, __FILE__, __LINE__);     \
00110     }                                                         \
00111   PR_END_MACRO
00112 
00121 #define NS_WARN_IF_FALSE(_expr,_msg)                          \
00122   PR_BEGIN_MACRO                                              \
00123     if (!(_expr)) {                                           \
00124       NSGlue_Warning(_msg, __FILE__, __LINE__);               \
00125     }                                                         \
00126   PR_END_MACRO
00127 
00132 #define NS_PRECONDITION(expr, str)                            \
00133   PR_BEGIN_MACRO                                              \
00134     if (!(expr)) {                                            \
00135       NSGlue_Assertion(str, #expr, __FILE__, __LINE__);       \
00136     }                                                         \
00137   PR_END_MACRO
00138 
00143 #define NS_ASSERTION(expr, str)                               \
00144   PR_BEGIN_MACRO                                              \
00145     if (!(expr)) {                                            \
00146       NSGlue_Assertion(str, #expr, __FILE__, __LINE__);       \
00147     }                                                         \
00148   PR_END_MACRO
00149 
00154 #define NS_POSTCONDITION(expr, str)                           \
00155   PR_BEGIN_MACRO                                              \
00156     if (!(expr)) {                                            \
00157       NSGlue_Assertion(str, #expr, __FILE__, __LINE__);       \
00158     }                                                         \
00159   PR_END_MACRO
00160 
00165 #define NS_NOTYETIMPLEMENTED(str)                             \
00166   NSGlue_Assertion(str, "NotYetImplemented", __FILE__, __LINE__)
00167 
00172 #define NS_NOTREACHED(str)                                    \
00173   NSGlue_Assertion(str, "Not Reached", __FILE__, __LINE__)
00174 
00178 #define NS_ERROR(str)                                         \
00179   NSGlue_Assertion(str, "Error", __FILE__, __LINE__)
00180 
00184 #define NS_WARNING(str)                                       \
00185   NSGlue_Warning(str, __FILE__, __LINE__)
00186 
00190 #define NS_ABORT()                                            \
00191   NSGlue_Abort(__FILE__, __LINE__)
00192 
00196 #define NS_BREAK()                                            \
00197   NSGlue_Break(__FILE__, __LINE__)
00198 
00199 #else /* NS_DEBUG */
00200 
00205 #define NS_ABORT_IF_FALSE(_expr, _msg) PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
00206 #define NS_WARN_IF_FALSE(_expr, _msg)  PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
00207 #define NS_PRECONDITION(expr, str)     PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
00208 #define NS_ASSERTION(expr, str)        PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
00209 #define NS_POSTCONDITION(expr, str)    PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
00210 #define NS_NOTYETIMPLEMENTED(str)      PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
00211 #define NS_NOTREACHED(str)             PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
00212 #define NS_ERROR(str)                  PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
00213 #define NS_WARNING(str)                PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
00214 #define NS_ABORT()                     PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
00215 #define NS_BREAK()                     PR_BEGIN_MACRO /* nothing */ PR_END_MACRO
00216 
00217 #endif /* ! NS_DEBUG */
00218 
00219 /* Macros for checking the trueness of an expression passed in within an 
00220  * interface implementation.  These need to be compiled regardless of the */
00221 /* NS_DEBUG flag
00222 ******************************************************************************/
00223 
00224 #define NS_ENSURE_TRUE(x, ret)                                \
00225   PR_BEGIN_MACRO                                              \
00226     if (NS_UNLIKELY(!(x))) {                                  \
00227        NS_WARNING("NS_ENSURE_TRUE(" #x ") failed");           \
00228        return ret;                                            \
00229     }                                                         \
00230   PR_END_MACRO
00231 
00232 #define NS_ENSURE_FALSE(x, ret)                               \
00233   NS_ENSURE_TRUE(!(x), ret)
00234 
00235 /******************************************************************************
00236 ** Macros for checking results
00237 ******************************************************************************/
00238 
00239 #define NS_ENSURE_SUCCESS(res, ret) \
00240   NS_ENSURE_TRUE(NS_SUCCEEDED(res), ret)
00241 
00242 /******************************************************************************
00243 ** Macros for checking state and arguments upon entering interface boundaries
00244 ******************************************************************************/
00245 
00246 #define NS_ENSURE_ARG(arg)                                    \
00247   NS_ENSURE_TRUE(arg, NS_ERROR_INVALID_ARG)
00248 
00249 #define NS_ENSURE_ARG_POINTER(arg)                            \
00250   NS_ENSURE_TRUE(arg, NS_ERROR_INVALID_POINTER)
00251 
00252 #define NS_ENSURE_ARG_MIN(arg, min)                           \
00253   NS_ENSURE_TRUE((arg) >= min, NS_ERROR_INVALID_ARG)
00254 
00255 #define NS_ENSURE_ARG_MAX(arg, max)                           \
00256   NS_ENSURE_TRUE((arg) <= max, NS_ERROR_INVALID_ARG)
00257 
00258 #define NS_ENSURE_ARG_RANGE(arg, min, max)                    \
00259   NS_ENSURE_TRUE(((arg) >= min) && ((arg) <= max), NS_ERROR_INVALID_ARG)
00260 
00261 #define NS_ENSURE_STATE(state)                                \
00262   NS_ENSURE_TRUE(state, NS_ERROR_UNEXPECTED)
00263 
00264 #define NS_ENSURE_NO_AGGREGATION(outer)                       \
00265   NS_ENSURE_FALSE(outer, NS_ERROR_NO_AGGREGATION)
00266 
00267 #define NS_ENSURE_PROPER_AGGREGATION(outer, iid)              \
00268   NS_ENSURE_FALSE(outer && !iid.Equals(NS_GET_IID(nsISupports)), NS_ERROR_INVALID_ARG)
00269 
00270 /*****************************************************************************/
00271 
00272 #ifdef XPCOM_GLUE
00273 #define NS_CheckThreadSafe
00274 #else
00275 #define NS_CheckThreadSafe(owningThread, msg)                 \
00276   NS_ASSERTION(owningThread == PR_GetCurrentThread(), msg)
00277 #endif
00278 
00279 #endif /* nsDebug_h___ */