home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / borhead.zip / CHECKS.H < prev    next >
C/C++ Source or Header  |  1994-11-09  |  9KB  |  269 lines

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