home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / checks.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  6.6 KB  |  263 lines

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