home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / INC.PAK / CHECKS.H < prev    next >
C/C++ Source or Header  |  1995-08-29  |  9KB  |  274 lines

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