1 /* AbiSource Program Utilities
2  * Copyright (C) 1998 AbiSource, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA.
18  */
19 
20 #ifndef UT_DEBUGMSG_H
21 #define UT_DEBUGMSG_H
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 /* pre-emptive dismissal; ut_types.h is needed by just about everything,
28  * so even if it's commented out in-file that's still a lot of work for
29  * the preprocessor to do...
30  */
31 #ifndef UT_TYPES_H
32 #include "ut_types.h"
33 #endif
34 
35 template<class T>
36 class UT_DebugOnly
37 {
38 public:
39 
40 #if DEBUG
41   T value;
UT_DebugOnly()42   UT_DebugOnly()
43   {
44   }
UT_DebugOnly(const T & other)45   UT_DebugOnly(const T& other)
46    : value(other)
47   {
48   }
49   UT_DebugOnly& operator=(const T& rhs)
50   {
51       value = rhs;
52       return *this;
53   }
54   void operator++(int)
55   {
56     value++;
57   }
58   void operator--(int)
59   {
60     value--;
61   }
62 
63   operator T&()
64   {
65     return value;
66   }
67   operator const T&() const
68   {
69     return value;
70   }
71 
72   T& operator->()
73   {
74     return value;
75   }
76 
77 #else
78   UT_DebugOnly()
79   {
80   }
81   UT_DebugOnly(const T&)
82   {
83   }
84   UT_DebugOnly& operator=(const T&)
85   {
86     return *this;
87   }
88   void operator++(int)
89   {
90   }
91   void operator--(int)
92   {
93   }
94 #endif
95 
~UT_DebugOnly()96   ~UT_DebugOnly()
97   {
98   }
99 };
100 
101 
102 ABI_EXPORT void _UT_OutputMessage(const char *s, ...) ABI_PRINTF_FORMAT(1,2);
103 
104 #ifdef DEBUG
105 #define UT_DEBUGMSG(M) _UT_OutputMessage M
106 #else
107 #define UT_DEBUGMSG(M)
108 #endif
109 
110 // define a quick way to no-op a debug message that
111 // you want to keep and without having to #if 0 it.
112 
113 #define xxx_UT_DEBUGMSG(M)
114 
115 
116 /*
117  * Similar to UT_DEBUGMSG, except exists even in production (non-debug) builds
118  */
119 #ifdef _WIN32
120 ABI_EXPORT void _UT_WarningMessage(const char *s, ...);
121 #else
122 #define _UT_WarningMessage _UT_OutputMessage
123 #endif
124 #define UT_WARNINGMSG(M) _UT_WarningMessage M
125 
126 
127 #endif /* UT_DEBUGMSG_H */
128