home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 14.ddi / GENINC.PAK / CHECKS.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  8.3 KB  |  251 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  CHECKS.H                                                              */
  4. /*                                                                        */
  5. /*                                                                        */
  6. /*------------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 6.0
  10.  *
  11.  *      Copyright (c) 1992, 1993 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16. #ifndef __cplusplus
  17. #error Must use C++ for CHECKS.H
  18. #endif
  19.  
  20. #pragma warn -nak   /* Ignore non-ansi keywords */
  21.  
  22. #if !defined( __CHECKS_H )
  23. #define __CHECKS_H
  24.  
  25. #if !defined( __DEFS_H )
  26. #include <_defs.h>
  27. #endif  // __DEFS_H
  28.  
  29. #if !defined( __CSTRING_H )
  30. #include <cstring.h>
  31. #endif   // __CSTRING_H
  32.  
  33. #include <systypes.h>
  34. #include <strstrea.h>
  35.  
  36. #pragma option -Vo-
  37. #if defined( __BCOPT__ ) && !defined( _RTL_ALLOW_po) && !defined(__FLAT__ )
  38. #pragma option -po-
  39. #endif
  40.  
  41. //
  42. // TDiagBase - this class forms the base for TDiagGroup classes and
  43. // handles basic message output.
  44. //
  45. class _EXPCLASS TDiagBase
  46. {
  47. public:
  48.     static ostrstream Out;
  49. protected:
  50.     static void _RTLENTRY Trace( const char *group, const char *msg,
  51.                        const char *fname, uint32 line );
  52.     static void _RTLENTRY Warn( const char *group, const char *msg,
  53.                       const char *fname, uint32 line );
  54.     struct Flags
  55.     {
  56.         uint8 Enabled : 1;
  57.         uint8 Level   : 7;
  58.     };
  59.  
  60. private:
  61.     static void _RTLENTRY Message( const char *type,
  62.                          const char *group, const char *msg,
  63.                          const char *fname, uint32 line );
  64.     static void _RTLENTRY Output( const char *msg );
  65. };
  66.  
  67. #if !defined(__EXCEPT_H)
  68. #include <except.h>
  69. #endif
  70.  
  71.  
  72. class _EXPCLASS xerror : public xmsg
  73. {
  74. public:
  75.      _RTLENTRY xerror( const char *type,
  76.              const char *txt,
  77.              const char *file,
  78.              uint32 line ) :
  79.              xmsg( MakeString(type,txt,file,line) ) {}
  80. private:
  81.     static string _RTLENTRY MakeString( const char *type,
  82.                          const char *txt,
  83.                        const char *file,
  84.                        uint32 line );
  85. };
  86.  
  87. class precondition : public xerror
  88. {
  89. public:
  90.      _RTLENTRY precondition( const char *txt,
  91.                   const char *file,
  92.                   uint32 line ) : xerror( "Precondition", txt, file, line )
  93.                   {
  94.                         }
  95. };
  96.  
  97. class check : public xerror
  98. {
  99. public:
  100.     _RTLENTRY check( const char *txt,
  101.            const char *file,
  102.               uint32 line ) : xerror( "Check", txt, file, line )
  103.            {
  104.               }
  105. };
  106.  
  107. #if defined( __BCOPT__ ) && !defined( _ALLOW_po) && !defined(__FLAT__ )
  108. #pragma option -po.
  109. #endif
  110. #pragma option -Vo.
  111.  
  112. #endif  // __CHECKS_H
  113.  
  114. #if !defined( __DEBUG )
  115. #define __DEBUG 0
  116. #endif
  117.  
  118. #undef PRECONDITION
  119. #undef PRECONDITIONX
  120.  
  121. #define PRECONDITION(p) PRECONDITIONX(p,#p)
  122.  
  123. #if __DEBUG < 1
  124. #define PRECONDITIONX(p,s)   ((void)0)
  125. #else
  126. #define PRECONDITIONX(p,s)   \
  127.     if(!(p)) {throw precondition(s,__FILE__,__LINE__);}
  128. #endif
  129.  
  130. #undef CHECK
  131. #undef CHECKX
  132.  
  133. #define CHECK(p) CHECKX(p,#p)
  134.  
  135. #if __DEBUG < 2
  136. #define CHECKX(p,s)    ((void)0)
  137. #else
  138. #define CHECKX(p,s)   \
  139.     if(!(p)) {throw check(s,__FILE__,__LINE__);}
  140. #endif
  141.  
  142. #if defined(__TRACE) || defined(__WARN)
  143.  
  144. #if defined( __DLL__ ) || defined( _BUILDRTLDLL ) 
  145. #define DIAG_IMPORT __import
  146. #define DIAG_EXPORT __export
  147. #else
  148. #define DIAG_IMPORT
  149. #define DIAG_EXPORT
  150. #endif
  151.  
  152. #define DECLARE_DIAG_GROUP(g,qual)                                         \
  153. class qual TDiagGroup##g : private TDiagBase                               \
  154. {                                                                          \
  155. public:                                                                    \
  156.     static void _RTLENTRY Trace( uint8 level, const char *msg,             \
  157.                        const char *fname, uint32 line );                   \
  158.                                                                            \
  159.     static void _RTLENTRY Warn( uint8 level, const char *msg,              \
  160.                       const char *fname, uint32 line );                    \
  161.                                                                            \
  162.     static void _RTLENTRY Enable(uint8 enabled)                            \
  163.                     { Flags.Enabled = uint8(enabled ? 1 : 0); }            \
  164.     static int  _RTLENTRY IsEnabled() { return Flags.Enabled; }            \
  165.                                                                            \
  166.     static void  _RTLENTRY SetLevel( uint8 level ) { Flags.Level = level; }\
  167.     static uint8 _RTLENTRY GetLevel() { return Flags.Level; }              \
  168.                                                                            \
  169. private:                                                                   \
  170.      static Flags Flags;                                                   \
  171.      static char *Name;                                                    \
  172. }
  173.  
  174. #define DIAG_DECLARE_GROUP(g)                                              \
  175. DECLARE_DIAG_GROUP(g,DIAG_IMPORT);
  176.  
  177. #define DIAG_DEFINE_GROUP(g,e,l)                                           \
  178. DECLARE_DIAG_GROUP(g,DIAG_EXPORT);                                         \
  179. void _RTLENTRY TDiagGroup##g::Trace( uint8 level, const char *msg,         \
  180.                                     const char *fname, uint32 line )       \
  181. {                                                                          \
  182.      if( IsEnabled() && level <= GetLevel() )                              \
  183.           TDiagBase::Trace( Name, msg, fname, line );                      \
  184. }                                                                          \
  185.                                                                            \
  186. void _RTLENTRY TDiagGroup##g::Warn( uint8 level, const char *msg,          \
  187.                                   const char *fname, uint32 line )         \
  188. {                                                                          \
  189.      if( IsEnabled() && level <= GetLevel() )                              \
  190.           TDiagBase::Warn( Name, msg, fname, line );                       \
  191. }                                                                          \
  192.                                                                            \
  193. char *TDiagGroup##g::Name = #g;                                            \
  194. TDiagBase::Flags TDiagGroup##g::Flags = { (e), (l) }
  195.  
  196. #define DIAG_ENABLE(g,s)            TDiagGroup##g::Enable(s)
  197. #define DIAG_ISENABLED(g)           TDiagGroup##g::IsEnabled()
  198. #define DIAG_SETLEVEL(g,l)          TDiagGroup##g::SetLevel(l)
  199. #define DIAG_GETLEVEL(g)            TDiagGroup##g::GetLevel()
  200.  
  201. #if !defined(_BUILD_CHECKS) && !defined( _DEF_DECLARED )
  202. #define _DEF_DECLARED
  203. DIAG_DECLARE_GROUP(Def);
  204. #endif
  205.  
  206. #else   // !defined(__TRACE) && !defined(__WARN)
  207.  
  208. #define DIAG_DECLARE_GROUP(g)
  209. #define DIAG_DEFINE_GROUP(g,e,l)
  210.  
  211. #define DIAG_ENABLE(g,s)            ((void)0)
  212. #define DIAG_ISENABLED(g)           ((void)0)
  213. #define DIAG_SETLEVEL(g,l)          ((void)0)
  214. #define DIAG_GETLEVEL(g)            ((void)0)
  215.  
  216. #endif
  217.  
  218. #if defined(__TRACE)
  219.     #define TRACE(m)                    TRACEX(Def,0,m)
  220.     #define TRACEX(g,l,m)\
  221.             {\
  222.                 TDiagBase::Out.seekp(0,ostream::beg);\
  223.                 TDiagBase::Out << m << ends;\
  224.                 TDiagGroup##g::Trace(l,TDiagBase::Out.str(),__FILE__,__LINE__);\
  225.             }
  226. #else
  227.     #define TRACE(m)                    ((void)0)
  228.     #define TRACEX(g,l,m)               ((void)0)
  229. #endif
  230.  
  231. #if defined(__WARN)
  232.     #define WARN(c,m)                   WARNX(Def,c,0,m)
  233.     #define WARNX(g,c,l,m)\
  234.             if(c)\
  235.             {\
  236.                 TDiagBase::Out.seekp(0,ostream::beg);\
  237.                 TDiagBase::Out << m << ends;\
  238.                 TDiagGroup##g::Warn(l,TDiagBase::Out.str(),__FILE__,__LINE__);\
  239.             }
  240. #else
  241.     #define WARN(c,m)                   ((void)0)
  242.     #define WARNX(g,c,l,m)              ((void)0)
  243. #endif
  244.  
  245.  
  246. #pragma warn .nak   /* Ignore non-ansi keywords */
  247.  
  248.  
  249.  
  250. 
  251.