Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(839)

Side by Side Diff: webrtc/base/checks.h

Issue 2262173002: RTC_CHECK and RTC_DCHECK macros for C (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: rebase Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | webrtc/base/checks.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2006 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #ifndef WEBRTC_BASE_CHECKS_H_ 11 #ifndef WEBRTC_BASE_CHECKS_H_
12 #define WEBRTC_BASE_CHECKS_H_ 12 #define WEBRTC_BASE_CHECKS_H_
13 13
14 #include "webrtc/typedefs.h"
15
16 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
17 #define RTC_DCHECK_IS_ON 1
18 #else
19 #define RTC_DCHECK_IS_ON 0
20 #endif
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 NO_RETURN void rtc_FatalMessage(const char* file, int line, const char* msg);
26 #ifdef __cplusplus
27 } // extern "C"
28 #endif
29
30 #ifdef __cplusplus
31 // C++ version.
32
14 #include <sstream> 33 #include <sstream>
15 #include <string> 34 #include <string>
16 35
17 #include "webrtc/typedefs.h"
18
19 // The macros here print a message to stderr and abort under various 36 // The macros here print a message to stderr and abort under various
20 // conditions. All will accept additional stream messages. For example: 37 // conditions. All will accept additional stream messages. For example:
21 // RTC_DCHECK_EQ(foo, bar) << "I'm printed when foo != bar."; 38 // RTC_DCHECK_EQ(foo, bar) << "I'm printed when foo != bar.";
22 // 39 //
23 // - RTC_CHECK(x) is an assertion that x is always true, and that if it isn't, 40 // - RTC_CHECK(x) is an assertion that x is always true, and that if it isn't,
24 // it's better to terminate the process than to continue. During development, 41 // it's better to terminate the process than to continue. During development,
25 // the reason that it's better to terminate might simply be that the error 42 // the reason that it's better to terminate might simply be that the error
26 // handling code isn't in place yet; in production, the reason might be that 43 // handling code isn't in place yet; in production, the reason might be that
27 // the author of the code truly believes that x will always be true, but that 44 // the author of the code truly believes that x will always be true, but that
28 // she recognizes that if she is wrong, abrupt and unpleasant process 45 // she recognizes that if she is wrong, abrupt and unpleasant process
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 #define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(EQ, ==, val1, val2) 173 #define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(EQ, ==, val1, val2)
157 #define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(NE, !=, val1, val2) 174 #define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(NE, !=, val1, val2)
158 #define RTC_CHECK_LE(val1, val2) RTC_CHECK_OP(LE, <=, val1, val2) 175 #define RTC_CHECK_LE(val1, val2) RTC_CHECK_OP(LE, <=, val1, val2)
159 #define RTC_CHECK_LT(val1, val2) RTC_CHECK_OP(LT, < , val1, val2) 176 #define RTC_CHECK_LT(val1, val2) RTC_CHECK_OP(LT, < , val1, val2)
160 #define RTC_CHECK_GE(val1, val2) RTC_CHECK_OP(GE, >=, val1, val2) 177 #define RTC_CHECK_GE(val1, val2) RTC_CHECK_OP(GE, >=, val1, val2)
161 #define RTC_CHECK_GT(val1, val2) RTC_CHECK_OP(GT, > , val1, val2) 178 #define RTC_CHECK_GT(val1, val2) RTC_CHECK_OP(GT, > , val1, val2)
162 179
163 // The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates 180 // The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates
164 // code in debug builds. It does reference the condition parameter in all cases, 181 // code in debug builds. It does reference the condition parameter in all cases,
165 // though, so callers won't risk getting warnings about unused variables. 182 // though, so callers won't risk getting warnings about unused variables.
166 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) 183 #if RTC_DCHECK_IS_ON
167 #define RTC_DCHECK_IS_ON 1
168 #define RTC_DCHECK(condition) RTC_CHECK(condition) 184 #define RTC_DCHECK(condition) RTC_CHECK(condition)
169 #define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2) 185 #define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2)
170 #define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2) 186 #define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2)
171 #define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2) 187 #define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2)
172 #define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2) 188 #define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2)
173 #define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2) 189 #define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2)
174 #define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2) 190 #define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2)
175 #else 191 #else
176 #define RTC_DCHECK_IS_ON 0
177 #define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition) 192 #define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition)
178 #define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) == (v2)) 193 #define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) == (v2))
179 #define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) != (v2)) 194 #define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) != (v2))
180 #define RTC_DCHECK_LE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) <= (v2)) 195 #define RTC_DCHECK_LE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) <= (v2))
181 #define RTC_DCHECK_LT(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) < (v2)) 196 #define RTC_DCHECK_LT(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) < (v2))
182 #define RTC_DCHECK_GE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) >= (v2)) 197 #define RTC_DCHECK_GE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) >= (v2))
183 #define RTC_DCHECK_GT(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) > (v2)) 198 #define RTC_DCHECK_GT(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) > (v2))
184 #endif 199 #endif
185 200
186 // This is identical to LogMessageVoidify but in name. 201 // This is identical to LogMessageVoidify but in name.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 // remainder is zero. 235 // remainder is zero.
221 template <typename T> 236 template <typename T>
222 inline T CheckedDivExact(T a, T b) { 237 inline T CheckedDivExact(T a, T b) {
223 RTC_CHECK_EQ(a % b, static_cast<T>(0)) << a << " is not evenly divisible by " 238 RTC_CHECK_EQ(a % b, static_cast<T>(0)) << a << " is not evenly divisible by "
224 << b; 239 << b;
225 return a / b; 240 return a / b;
226 } 241 }
227 242
228 } // namespace rtc 243 } // namespace rtc
229 244
245 #else // __cplusplus not defined
246 // C version. Lacks many features compared to the C++ version, but usage
247 // guidelines are the same.
248
249 #define RTC_CHECK(condition) \
250 do { \
251 if (!(condition)) { \
252 rtc_FatalMessage(__FILE__, __LINE__, "CHECK failed: " #condition); \
253 } \
254 } while (0)
255
256 #define RTC_CHECK_EQ(a, b) RTC_CHECK((a) == (b))
257 #define RTC_CHECK_NE(a, b) RTC_CHECK((a) != (b))
258 #define RTC_CHECK_LE(a, b) RTC_CHECK((a) <= (b))
259 #define RTC_CHECK_LT(a, b) RTC_CHECK((a) < (b))
260 #define RTC_CHECK_GE(a, b) RTC_CHECK((a) >= (b))
261 #define RTC_CHECK_GT(a, b) RTC_CHECK((a) > (b))
262
263 #define RTC_DCHECK(condition) \
264 do { \
265 if (RTC_DCHECK_IS_ON && !(condition)) { \
266 rtc_FatalMessage(__FILE__, __LINE__, "DCHECK failed: " #condition); \
267 } \
268 } while (0)
269
270 #define RTC_DCHECK_EQ(a, b) RTC_DCHECK((a) == (b))
271 #define RTC_DCHECK_NE(a, b) RTC_DCHECK((a) != (b))
272 #define RTC_DCHECK_LE(a, b) RTC_DCHECK((a) <= (b))
273 #define RTC_DCHECK_LT(a, b) RTC_DCHECK((a) < (b))
274 #define RTC_DCHECK_GE(a, b) RTC_DCHECK((a) >= (b))
275 #define RTC_DCHECK_GT(a, b) RTC_DCHECK((a) > (b))
276
277 #endif // __cplusplus
278
230 #endif // WEBRTC_BASE_CHECKS_H_ 279 #endif // WEBRTC_BASE_CHECKS_H_
OLDNEW
« no previous file with comments | « no previous file | webrtc/base/checks.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698