home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / include / xpassert.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.4 KB  |  185 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19.  
  20. #ifndef _XP_Assert_
  21. #define _XP_Assert_
  22.  
  23. #include "xp_trace.h"
  24. /*include <stdlib.h>*/
  25.  
  26. /*-----------------------------------------------------------------------------
  27.     abort
  28.     
  29.     For debug builds...
  30.     XP_ABORT(X), unlike abort(), takes a text string argument. It will print
  31.     it out and then call abort (to drop you into your debugger).
  32.     
  33.     For release builds...
  34.     XP_ABORT will call abort(). Whether you #define NDEBUG or not is up
  35.     to you.
  36. -----------------------------------------------------------------------------*/
  37. #define XP_ABORT(MESSAGE)    (XP_TRACE(MESSAGE),abort())
  38.  
  39. /*-----------------------------------------------------------------------------
  40.     XP_ASSERT is just like "assert" but calls XP_ABORT if it fails the test.
  41.     I need this (on a Mac) because "assert" and "abort" are braindead,
  42.     whereas my XP_Abort function will invoke the debugger. It could
  43.     possibly have been easier to just #define assert to be something decent.
  44. -----------------------------------------------------------------------------*/
  45.  
  46. #if defined (XP_UNIX)
  47. #if !defined(NO_UNIX_SKIP_ASSERTS)
  48. /* Turning UNIX_SKIP_ASSERTS on by default. */
  49. /* (Solaris 2.x) on Sol2.5, assert() does not work. Too bad...  */
  50. /* Therefore, we print the line where assert happened instead. */
  51. /* Print out a \007 to sound the bell. -mcafee */
  52. #define XP_AssertAtLine() fprintf(stderr, "assert: line %d, file %s%c\n", __LINE__, __FILE__, 7)
  53. #ifdef DEBUG
  54. #define XP_ASSERT(X) ( (((X))!=0)? (void)0: (void)XP_AssertAtLine() )
  55. #else
  56. #define XP_ASSERT(X) (void)0
  57. #endif
  58. #else
  59. #include <assert.h>
  60. #define XP_ASSERT(X)            assert(X) /* are we having fun yet? */
  61. #endif
  62.  
  63. #elif defined (XP_WIN)
  64. #ifdef DEBUG
  65.  
  66. /* LTNOTE: I got tired of seeing all asserts at FEGUI.CPP.  This should
  67.  * Fix the problem.  I intentionally left out Win16 because strings are stuffed
  68.  * into the datasegment we probably couldn't build.
  69. */
  70. #ifdef WIN32
  71. XP_BEGIN_PROTOS
  72. extern void XP_AssertAtLine( char *pFileName, int iLine );
  73. XP_END_PROTOS
  74. #define XP_ASSERT(X)   ( ((X)!=0)? (void)0: XP_AssertAtLine(__FILE__,__LINE__))
  75.  
  76. #else  /* win16 */
  77. #define XP_ASSERT(X)                ( ((X)!=0)? (void)0: XP_Assert((X) != 0)  )
  78. XP_BEGIN_PROTOS
  79. void XP_Assert(int);
  80. XP_END_PROTOS
  81. #endif
  82.  
  83. #else
  84. #define XP_ASSERT(X)  ((void) 0)
  85. #endif 
  86.  
  87. #elif defined (XP_OS2)
  88.   #ifdef DEBUG
  89.     #include <assert.h>
  90.     #define XP_ASSERT(X)            assert(X) /* IBM-DAK same as UNIX */
  91.   #else
  92.     #define XP_ASSERT(X)
  93.   #endif
  94.  
  95. #elif defined(XP_MAC)
  96.  
  97.     #ifdef DEBUG
  98.         #ifdef __cplusplus
  99.             #ifdef __MWERKS__
  100.                 /*
  101.                 **    See the comment above: XP_ASSERT is supposed to abort!
  102.                 **    The MacFE_Signal is actually not in the spirit of the creator of XP_Assert.
  103.                 **    Also, the previous version was annoying (always seen in uapp.cp)
  104.                 **    The Metrowerks Assert_ macro is much more helpful...
  105.                 */
  106.                 #include <UException.h>
  107.                 #define XP_ASSERT(X) Assert_(X)
  108.             #else
  109.                 extern "C" void debugstr(const char* s);
  110.             #endif
  111.         #else
  112.             extern void debugstr(const char* s);
  113.         #endif
  114.         #ifndef XP_ASSERT
  115.             /* The following is wrong... because it will cause
  116.                the use of an XP_ASSERT within an if ... else ... 
  117.                construct to match incorrectly with the "last prior if" :-/
  118.                XXX
  119.             */
  120.             #define XP_ASSERT(X) if (!(X)) debugstr(#X)
  121.         #endif
  122.     #else
  123.         #define XP_ASSERT(X)
  124.     #endif
  125.  
  126. #endif /* XP_MAC */
  127.  
  128. /*-----------------------------------------------------------------------------
  129.     assert variants
  130.     
  131.     XP_WARN_ASSERT if defined to nothing for release builds. This means
  132.     that instead of
  133.                 #ifdef DEBUG
  134.                 assert (X);
  135.                 #endif
  136.     you can just do
  137.                 XP_WARN_ASSERT(X);
  138.     
  139.     Of course when asserts fail that means something is going wrong and you
  140.     *should* have normal code to deal with that.
  141.     I frequently found myself writing code like this:
  142.                 #ifdef DEBUG
  143.                 assert (aPtr);
  144.                 #endif
  145.                 if (!aPtr)
  146.                     return error_something_has_gone_wrong;
  147.     so I just combined them into a macro that can be used like this:
  148.                 if (XP_FAIL_ASSERT(aPtr))
  149.                     return; // or whatever else you do when things go wrong
  150.     What this means is it will return if X *fails* the test. Essentially
  151.     the XP_FAIL_ASSERT bit replaces the "!" in the if test.
  152.     
  153.     XP_OK_ASSERT is the opposite. If if you want to do something only if
  154.     something is OK, then use it. For example:
  155.                 if (XP_OK_ASSERT(aPtr))
  156.                     aPtr->aField = 25;
  157.     Use this if you are 99% sure that aPtr will be valid. If it ever is not,
  158.     you'll drop into the debugger. For release builds, it turns into an
  159.     if statement, so it's completely safe to execute.
  160.  
  161.         You can also do XP_VERIFY, which essentially will throw an assert if a
  162.         condition fails in debug mode, but just do whatever at runtime. For
  163.         example:
  164.  
  165.                 XP_VERIFY(PR_LoadLibrary("foo") == 0);
  166.  
  167.         This will trigger an XP_ASSERT if the condition fails during debug, bug
  168.         just run the PR_LoadLibrary in release. Kind of the same as XP_WARN_ASSERT,
  169.         but the "verbiage" is a bit clearer (to me, anyway).
  170. -----------------------------------------------------------------------------*/
  171. #ifdef DEBUG
  172. #    define XP_WARN_ASSERT(X)    (  ((X)!=0)? (void)0: XP_ABORT((#X))  )
  173. #    define XP_OK_ASSERT(X)        (((X)!=0)? 1: (XP_ABORT((#X)),0))
  174. #    define XP_FAIL_ASSERT(X)    (((X)!=0)? 0: (XP_ABORT((#X)),1))
  175. #       define XP_VERIFY(X)             (  (X)? (void)0: XP_ASSERT(0)  )
  176. #else
  177. #    define XP_WARN_ASSERT(X)    (void)((X)!=0)
  178. #    define XP_OK_ASSERT(X)        (((X)!=0)? 1: 0)
  179. #    define XP_FAIL_ASSERT(X)    (((X)!=0)? 0: 1)
  180. #       define XP_VERIFY(X)             (  (void)(X)  )
  181. #endif
  182.  
  183. #endif /* _XP_Assert_ */
  184.  
  185.