home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tlx501.zip / DEBUG / ASSERTS.H next >
C/C++ Source or Header  |  1996-07-08  |  8KB  |  178 lines

  1. /****************************************************************************
  2.     $Id: asserts.h 501.0 1995/03/07 12:26:50 RON Exp $
  3.  
  4.     Copyright (c) 1991-95 Tarma Software Research. All rights reserved.
  5.  
  6.     Project:    Tarma Library for C++ V5.0
  7.     Author:     Ron van der Wal
  8.  
  9.     The macros in this file supersede the traditional assert() macro and
  10.     should be used instead. As with the traditional macros, #defining NDEBUG
  11.     before #including this file turns them into null code (with a few
  12.     exceptions, noted below).
  13.  
  14.     Assertion macros
  15.     ----------------
  16.     In general, these macros are intended to test or verify assumptions,
  17.     (pre/post) conditions, invariants, etc. During debugging they perform
  18.     their test and terminate the program with some kind of error message if
  19.     the corresponding assumption is violated. In the retail version of
  20.     the program, they are converted to null expressions (with a few
  21.     exceptions, see below).
  22.  
  23.     To switch between the debug and the retail versions of these macros,
  24.     either leave NDEBUG #undefined (debugging code included), or #define
  25.     it to any value or null (debugging code excluded).
  26.  
  27.     These macros revert to void expressions if NDEBUG is #defined:
  28.  
  29.     TLX_ASSERT(c)       - verifies that 'c' evaluates to nonzero
  30.     TLX_ASSERT_PTR(p)   - verifies that 'p' evaluates to non-NULL pointer
  31.     TLX_ASSERT_NULL(p)  - verifies that 'p' evaluates to NULL pointer
  32.     TLX_ASSERT_UNREACHABLE    - verifies that it is never executed
  33.     TLX_ASSERT_TODO                - prints a "To do" message when executed
  34.     TLX_ASSERT_NOTIMPLEMENTED      - prints a "Not implemented" message
  35.     TLX_ASSERT_MSG(m,a1,a2)    - prints an assertion message when executed
  36.  
  37.     The next macros revert to their argument expression if NDEBUG is #defined,
  38.     so they should be used if the argument must be evaluated (and the result
  39.     of the evaluation used) whether or not debugging is enabled:
  40.  
  41.     TLX_CHECK(c)        - verifies that 'c' evaluates to nonzero
  42.     TLX_CHECK_PTR(p)    - verifies that 'p' evaluates to non-NULL pointer
  43.     TLX_CHECK_NULL(p)   - verifies that 'p' evaluates to NULL pointer
  44.  
  45.     Environment variables
  46.     ---------------------
  47.  
  48.     This feature is not functional on platforms that do not have an
  49.     environment, such as Macintosh.
  50.  
  51.     $Log: asserts.h $
  52.     Revision 501.0  1995/03/07 12:26:50  RON
  53.     Updated for TLX 5.01
  54.     Revision 1.2  1995/01/31 16:28:58  RON
  55.     Update for release 012
  56.     Added partial support for SunPro C++ compiler
  57.     Revision 1.1  1995/01/05  15:35:48  ron
  58.     Initial revision
  59.  
  60.     Revision 1.1  1994/11/16  15:25:46  ron
  61.     Initial revision
  62.  
  63. ****************************************************************************/
  64.  
  65. #ifndef _TLX_ASSERTS_H
  66. #define _TLX_ASSERTS_H
  67.  
  68. #ifndef _TLX_TLX_H
  69. #include <tlx\501\tlx.h>
  70. #endif
  71.  
  72. #ifdef NDEBUG
  73. /*---------------------------------------------------------------------------
  74.     Assertion macros when not debugging - resolve to null code
  75. ---------------------------------------------------------------------------*/
  76.  
  77. #define TLX_ASSERT(c)                   ((void)0)
  78. #define TLX_ASSERT_PTR(p)               ((void)0)
  79. #define TLX_ASSERT_NULL(p)              ((void)0)
  80. #define TLX_ASSERT_UNREACHABLE        ((void)0)
  81.  
  82. #define TLX_REQUIRE(c)                  ((void)0)
  83. #define TLX_REQUIRE_PTR(p)              ((void)0)
  84. #define TLX_REQUIRE_NULL(p)             ((void)0)
  85.  
  86. #define TLX_ENSURE(c)                   ((void)0)
  87. #define TLX_ENSURE_PTR(p)               ((void)0)
  88. #define TLX_ENSURE_NULL(p)              ((void)0)
  89.  
  90. #define TLX_CHECK(c)                    (c)
  91. #define TLX_CHECK_PTR(p)                (p)
  92. #define TLX_CHECK_NULL(p)               (!(p))
  93.  
  94. #define TLX_TODO(msg)            ((void)0)
  95. #define TLX_NOTIMPLEMENTED(msg)         ((void)0)
  96. #define TLX_DIAGNOSTIC(l,msg,p1,p2)    ((void)0)
  97.  
  98. #else
  99. /*---------------------------------------------------------------------------
  100.     Assertion macros when debugging - resolve to actual code
  101. ---------------------------------------------------------------------------*/
  102.  
  103. #define TLX_ASSERT(c)           ((c) ? (void)0 : \
  104.                                 TLAssert::AssertFailed(__FILE__, __LINE__))
  105. #define TLX_ASSERT_PTR(p)       (((p) != 0) ? (void)0 : \
  106.                                 TLAssert::PtrFailed(__FILE__, __LINE__))
  107. #define TLX_ASSERT_NULL(p)      (((p) == 0) ? (void)0 : \
  108.                                 TLAssert::NullPtrFailed(__FILE__, __LINE__))
  109. #define TLX_ASSERT_UNREACHABLE  TLAssert::Reached(__FILE__, __LINE__)
  110.  
  111. #define TLX_REQUIRE(c)          ((c) ? (void)0 : \
  112.                                 TLAssert::RequireFailed(__FILE__, __LINE__))
  113. #define TLX_REQUIRE_PTR(p)      (((p) != 0) ? (void)0 : \
  114.                                 TLAssert::RequirePtrFailed(__FILE__, __LINE__))
  115. #define TLX_REQUIRE_NULL(p)     (((p) == 0) ? (void)0 : \
  116.                                 TLAssert::RequireNullPtrFailed(__FILE__, __LINE__))
  117.  
  118. #define TLX_ENSURE(c)           ((c) ? (void)0 : \
  119.                                 TLAssert::EnsureFailed(__FILE__, __LINE__))
  120. #define TLX_ENSURE_PTR(p)       (((p) != 0) ? (void)0 : \
  121.                                 TLAssert::EnsurePtrFailed(__FILE__, __LINE__))
  122. #define TLX_ENSURE_NULL(p)      (((p) == 0) ? (void)0 : \
  123.                                 TLAssert::EnsureNullPtrFailed(__FILE__, __LINE__))
  124.  
  125. #define TLX_CHECK(c)            ((c) ? true : \
  126.                                 (TLAssert::CheckFailed(__FILE__, __LINE__), \
  127.                 false))
  128. #define TLX_CHECK_PTR(p)        (((p) != 0) ? true : \
  129.                                 (TLAssert::PtrCheck(__FILE__, __LINE__), \
  130.                 false))
  131. #define TLX_CHECK_NULL(p)       (((p) == 0) ? true : \
  132.                                 (TLAssert::NullPtrCheck(__FILE__, __LINE__),\
  133.                 false))
  134.  
  135. #define TLX_TODO(msg)        TLAssert::ToDo(#msg, __FILE__, __LINE__)
  136. #define TLX_NOTIMPLEMENTED(msg) TLAssert::NotImplemented(#msg, __FILE__, \
  137.                 __LINE__)
  138.  
  139. #define TLX_DIAGNOSTIC(lvl,msg,p1,p2) TLAssert::Diagnostic(lvl, \
  140.                 __FILE__, __LINE__, msg, p1, p2)
  141.  
  142. #endif    // NDEBUG
  143.  
  144. /*---------------------------------------------------------------------------
  145.     Auxiliary definitions
  146. ---------------------------------------------------------------------------*/
  147.  
  148. class _TLXCLASS TLAssert
  149. {
  150. public:
  151.     enum {
  152.         dfFatal,        // Diagnostic is fatal; program will be terminated
  153.         dfError,        // Diagnostic is an error; proceed at your risk
  154.         dfWarning,      // Diagnostic is a warning; may continue
  155.         dfInfo        // Diagnostic is only informational
  156.     };
  157.  
  158. public:
  159.     static void        AssertFailed(const char *, int);
  160.     static void        CheckFailed(const char *, int);
  161.     static void    __cdecl    Diagnostic(int, const char *,int,const char *, ...);
  162.     static void        EnsureFailed(const char *, int);
  163.     static void        EnsureNullPtrFailed(const char *, int);
  164.     static void        EnsurePtrFailed(const char *, int);
  165.     static void        NotImplemented(const char *, const char *, int);
  166.     static void        NullPtrCheck(const char *, int);
  167.     static void        NullPtrFailed(const char *, int);
  168.     static void        PtrCheck(const char *, int);
  169.     static void        PtrFailed(const char *, int);
  170.     static void        Reached(const char *, int);
  171.     static void        RequireFailed(const char *, int);
  172.     static void        RequireNullPtrFailed(const char *, int);
  173.     static void        RequirePtrFailed(const char *, int);
  174.     static void        ToDo(const char *, const char *, int);
  175. };
  176.  
  177. #endif    // _TLX_ASSERTS_H
  178.