home *** CD-ROM | disk | FTP | other *** search
/ Total C++ 2 / TOTALCTWO.iso / borland / servpriv.pak / CHECKS.H next >
C/C++ Source or Header  |  1997-05-06  |  8KB  |  234 lines

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