home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / borland / cb / setup / cbuilder / data.z / CHECKS.H < prev    next >
C/C++ Source or Header  |  1997-02-28  |  9KB  |  275 lines

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