home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / odtlktv4.zip / ODTLKT / TOOLKIT / BETA / SAMPLES / OPENDOC / PUBUTILS / ODDEBUG.H < prev    next >
C/C++ Source or Header  |  1995-11-29  |  6KB  |  161 lines

  1. /********************************************************************/
  2. /*  Licensed Materials - Property of IBM                            */
  3. /*                                                                  */
  4. /*                                                                  */
  5. /* Copyright (C) International Business Machines Corp., 1994.       */
  6. /* Copyright (C) Apple Computer, Inc., 1994                         */
  7. /*                                                                  */
  8. /*  US Government Users Restricted Rights -                         */
  9. /*  Use, duplication, or disclosure restricted                      */
  10. /*  by GSA ADP Schedule Contract with IBM Corp.                     */
  11. /*                                                                  */
  12. /*  File:    ODDebug.h                                              */
  13. /*                                                                  */
  14. /*  Contains:  Useful debugging macros and functions.               */
  15. /*                                                                  */
  16. /********************************************************************/
  17.  
  18.  
  19. #ifndef _ODDEBUG_
  20. #define _ODDEBUG_
  21.  
  22. #ifndef INCL_ODDTS // include non-DTS C++ headers
  23.  
  24. #include <somobj.xh>
  25. #include <somcls.xh>
  26.  
  27. #ifndef SOM_Module_OpenDoc_Global_TypesB_defined
  28. #include "ODTypesB.xh"    
  29. #endif
  30.  
  31. #ifndef SOM_Module_ErrorDef_OpenDoc_Errors_defined
  32. #include "ErrorDef.xh"
  33. #endif
  34.  
  35. #else // include DTS C++ headers
  36.  
  37. #include <somobj.hh>
  38. #include <somcls.hh>
  39. #ifndef _DTS_HH_INCLUDED_ODTypesB
  40. #include <ODTypesB.hh>
  41. #endif
  42. #ifndef _DTS_HH_INCLUDED_ErrorDef
  43. #include <ErrorDef.hh>
  44. #endif
  45.  
  46. #endif // ! INCL_ODDTS
  47.  
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51.  
  52. //=====================================================================================
  53. // Warnings
  54. //=====================================================================================
  55.  
  56. // WARN has the same syntax as printf but produces a SysBreakStr.
  57. // Warnings are disabled (and generate no code) when ODDebug is off.
  58.  
  59. #define WARN  if(!ODDebug) ; else _Warn
  60.  
  61.  
  62. //=====================================================================================
  63. // Assertions
  64. //=====================================================================================
  65.  
  66. // These all cause a debugger break if the condition evaluates to false or 0.
  67. // Leading "W" is a Warning: it doesn't raise an exception.
  68. // Trailing "M" means special Message displayed instead of condition.
  69.  
  70. #if ODDebug
  71. #define ASSERT( COND, ERR ) \
  72.   if(COND) ; else _AssertionFailed( #COND, ERR, 0, __LINE__, __FUNCTION__, __FILE__)
  73. #define ASSERTM( COND, ERR, MSG )  \
  74.   if(COND) ; else _AssertionFailed( #COND, ERR, MSG, __LINE__, __FUNCTION__, __FILE__)
  75. #define WASSERT( COND )  \
  76.   if(COND) ; else _AssertionFailed( #COND, 0, 0, __LINE__, __FUNCTION__, __FILE__)
  77. #define WASSERTM( COND, MSG )  \
  78.   if(COND) ; else _AssertionFailed( #COND, 0, MSG, __LINE__, __FUNCTION__, __FILE__)
  79. #else
  80. #define ASSERT( COND, ERR )
  81. #define ASSERTM( COND, ERR, MSG )
  82. #define WASSERT( COND )
  83. #define WASSERTM( COND, MSG )
  84. #endif
  85.  
  86. // ASSERT_NOT_NULL causes a debugger break and an exception if the parameter
  87. // is NULL. Use this in functions that take a pointer as a parameter but do
  88. // not allow the parameter to be NULL.
  89. // Do **NOT** use this macro to make sure memory allocation succeeded! It
  90. // has no effect in non-debug builds. Use THROW_IF_NULL instead.
  91.  
  92. #define ASSERT_NOT_NULL(PARAM) \
  93.   ASSERT((PARAM)!=kODNULL,kODErrInvalidParameter)
  94.  
  95.  
  96. //=====================================================================================
  97. // Logging
  98. //=====================================================================================
  99.  
  100. // PRINT writes to the standard output via somPrintf if ODDebug is on. Use
  101. // SetOutputMode (or the ODDebug menu) to direct output to a file or to the
  102. // DebugWindow app.
  103.  
  104. #define PRINT  if(!ODDebug) ; else somPrintf
  105.  
  106. // LOG is like PRINT but can easily be turned on or off on a per-file basis.
  107. // To enable logging in a source file, you must redefine the symbol LOGGING
  108. // as "1" in that file, otherwise LOG statements will not be compiled. Make
  109. // sure to #undef the symbol before you re-#define it, as some compilers
  110. // (e.g. Symantec) won't redefine an already-defined symbol.
  111.  
  112. // PRINT and LOG statements do not generate any code if logging is off.
  113.  
  114. #define LOGGING 0    // Redefine as 1 in source file to enable logging
  115.  
  116. #define LOG    if(!ODDebug || !LOGGING) ; else somPrintf
  117.  
  118.  
  119. //==============================================================================
  120. // Safe Type-Casting
  121. //==============================================================================
  122.  
  123. /*  Use CAST as a safe way to cast a SOM object from one class to another.
  124.   For instance, if "o" is declared as an ODObject*, but your code knows
  125.   it's an ODPart*, you can say:
  126.       ODPart *part = CAST(o,ODPart);
  127.   If ODDebug is turned on, this will do a runtime check and cause an assertion
  128.   failure if o does not point to an ODPart (or subclass). Without ODDebug,
  129.   it degenerates into a simple C-style cast that generates no code.
  130.  
  131.   ASSERT_IS_A is similar to CAST but is used when you just want to assert
  132.   that the pointer points to the right kind of object.
  133. */
  134.  
  135. #if ODDebug
  136.   #define CAST(OBJ, CLASS)  ( (CLASS*)_Cast((OBJ),CLASS##ClassData.classObject,__LINE__, __FILE__, __FUNCTION__) )
  137.   #define ASSERT_IS_A(OBJ,CLASS)  ( (void) CAST(OBJ,CLASS) )
  138. #else
  139.   #define CAST(OBJ, CLASS)  ( (CLASS*) (OBJ) )
  140.   #define ASSERT_IS_A(OBJ,CLASS)  /* */
  141. #endif
  142.  
  143.  
  144. //=====================================================================================
  145. // Internal goop...
  146. //=====================================================================================
  147.  
  148. void _Warn        ( char *fmt, ... );
  149. void _AssertionFailed  ( const char *cond,
  150.               ODError, const char *msg, int, const char*, const char*);
  151. #if ODDebug
  152. SOMObject* _Cast( SOMObject *obj, SOMClass *cls, int line, const char* file, const char* function );
  153. #endif
  154.  
  155.  
  156. #ifdef __cplusplus
  157. }
  158. #endif
  159.  
  160. #endif /*_ODDEBUG_*/
  161.