1f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org/*
2f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org *  Copyright 2006 The WebRTC Project Authors. All rights reserved.
3f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org *
4f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org *  Use of this source code is governed by a BSD-style license
5f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org *  that can be found in the LICENSE file in the root of the source
6f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org *  tree. An additional intellectual property rights grant can be found
7f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org *  in the file PATENTS.  All contributing project authors may
8f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org */
10f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org
11f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#ifndef WEBRTC_BASE_CHECKS_H_
12f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#define WEBRTC_BASE_CHECKS_H_
13f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org
14a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org#include <sstream>
1534a6764981a656c62daed0d962f01374a08f9bacandrew@webrtc.org#include <string>
16a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org
17a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org#include "webrtc/typedefs.h"
18a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org
19a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// The macros here print a message to stderr and abort under various
20a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// conditions. All will accept additional stream messages. For example:
2191d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg// RTC_DCHECK_EQ(foo, bar) << "I'm printed when foo != bar.";
22a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org//
2391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg// - RTC_CHECK(x) is an assertion that x is always true, and that if it isn't,
2491d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg//   it's better to terminate the process than to continue. During development,
2591d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg//   the reason that it's better to terminate might simply be that the error
26a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org//   handling code isn't in place yet; in production, the reason might be that
27a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org//   the author of the code truly believes that x will always be true, but that
28a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org//   she recognizes that if she is wrong, abrupt and unpleasant process
29a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org//   termination is still better than carrying on with the assumption violated.
30a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org//
3191d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg//   RTC_CHECK always evaluates its argument, so it's OK for x to have side
329c6dc46c6d11474b8d24f719193f8b1c7b844b92kwiberg@webrtc.org//   effects.
339c6dc46c6d11474b8d24f719193f8b1c7b844b92kwiberg@webrtc.org//
3491d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg// - RTC_DCHECK(x) is the same as RTC_CHECK(x)---an assertion that x is always
35a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org//   true---except that x will only be evaluated in debug builds; in production
36a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org//   builds, x is simply assumed to be true. This is useful if evaluating x is
37a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org//   expensive and the expected cost of failing to detect the violated
38a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org//   assumption is acceptable. You should not handle cases where a production
39a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org//   build fails to spot a violated condition, even those that would result in
40a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org//   crashes. If the code needs to cope with the error, make it cope, but don't
4191d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg//   call RTC_DCHECK; if the condition really can't occur, but you'd sleep
4291d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg//   better at night knowing that the process will suicide instead of carrying
4391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg//   on in case you were wrong, use RTC_CHECK instead of RTC_DCHECK.
44a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org//
4591d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg//   RTC_DCHECK only evaluates its argument in debug builds, so if x has visible
469c6dc46c6d11474b8d24f719193f8b1c7b844b92kwiberg@webrtc.org//   side effects, you need to write e.g.
4791d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg//     bool w = x; RTC_DCHECK(w);
489c6dc46c6d11474b8d24f719193f8b1c7b844b92kwiberg@webrtc.org//
4991d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg// - RTC_CHECK_EQ, _NE, _GT, ..., and RTC_DCHECK_EQ, _NE, _GT, ... are
5091d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg//   specialized variants of RTC_CHECK and RTC_DCHECK that print prettier
5191d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg//   messages if the condition doesn't hold. Prefer them to raw RTC_CHECK and
5291d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg//   RTC_DCHECK.
53a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org//
54a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// - FATAL() aborts unconditionally.
5597395b64cab8d3f48b710655d24758a75a32ec12henrikg//
5697395b64cab8d3f48b710655d24758a75a32ec12henrikg// TODO(ajm): Ideally, checks.h would be combined with logging.h, but
5797395b64cab8d3f48b710655d24758a75a32ec12henrikg// consolidation with system_wrappers/logging.h should happen first.
58a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org
59c50bf7cbd0483806c50a848d24349a79939e1161henrike@webrtc.orgnamespace rtc {
60c50bf7cbd0483806c50a848d24349a79939e1161henrike@webrtc.org
61a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// Helper macro which avoids evaluating the arguments to a stream if
62a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// the condition doesn't hold.
6391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_LAZY_STREAM(stream, condition)                                    \
6434a6764981a656c62daed0d962f01374a08f9bacandrew@webrtc.org  !(condition) ? static_cast<void>(0) : rtc::FatalMessageVoidify() & (stream)
65f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org
6655d42c32a4d2ec71d56e863c0cee54f8db270074kwiberg@webrtc.org// The actual stream used isn't important. We reference condition in the code
6755d42c32a4d2ec71d56e863c0cee54f8db270074kwiberg@webrtc.org// but don't evaluate it; this is to avoid "unused variable" warnings (we do so
6855d42c32a4d2ec71d56e863c0cee54f8db270074kwiberg@webrtc.org// in a particularly convoluted way with an extra ?: because that appears to be
6955d42c32a4d2ec71d56e863c0cee54f8db270074kwiberg@webrtc.org// the simplest construct that keeps Visual Studio from complaining about
7055d42c32a4d2ec71d56e863c0cee54f8db270074kwiberg@webrtc.org// condition being unused).
7191d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_EAT_STREAM_PARAMETERS(condition) \
7291d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  (true ? true : !(condition))               \
7391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg      ? static_cast<void>(0)                 \
7455d42c32a4d2ec71d56e863c0cee54f8db270074kwiberg@webrtc.org      : rtc::FatalMessageVoidify() & rtc::FatalMessage("", 0).stream()
75a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org
7691d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg// RTC_CHECK dies with a fatal error if condition is not true.  It is *not*
77a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// controlled by NDEBUG, so the check will be executed regardless of
78a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// compilation mode.
79a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org//
8091d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg// We make sure RTC_CHECK et al. always evaluates their arguments, as
8191d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg// doing RTC_CHECK(FunctionWithSideEffect()) is a common idiom.
8291d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_CHECK(condition)                                      \
8391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_LAZY_STREAM(rtc::FatalMessage(__FILE__, __LINE__).stream(), \
8491d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg                  !(condition))                                   \
8591d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg      << "Check failed: " #condition << std::endl << "# "
86a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org
87a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// Helper macro for binary operators.
8891d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg// Don't use this macro directly in your code, use RTC_CHECK_EQ et al below.
89a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org//
90a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// TODO(akalin): Rewrite this so that constructs like if (...)
9191d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg// RTC_CHECK_EQ(...) else { ... } work properly.
9291d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_CHECK_OP(name, op, val1, val2)                                 \
9391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  if (std::string* _result =                                               \
9491d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg          rtc::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \
95a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org    rtc::FatalMessage(__FILE__, __LINE__, _result).stream()
96a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org
97a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// Build the error message string.  This is separate from the "Impl"
98a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// function template because it is not performance critical and so can
99a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// be out of line, while the "Impl" code should be inline.  Caller
100a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// takes ownership of the returned string.
101a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.orgtemplate<class t1, class t2>
102a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.orgstd::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
103a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org  std::ostringstream ss;
104a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org  ss << names << " (" << v1 << " vs. " << v2 << ")";
105a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org  std::string* msg = new std::string(ss.str());
106a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org  return msg;
107a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org}
108a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org
109a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// MSVC doesn't like complex extern templates and DLLs.
110a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org#if !defined(COMPILER_MSVC)
111a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
112a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// in logging.cc.
113a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.orgextern template std::string* MakeCheckOpString<int, int>(
114a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org    const int&, const int&, const char* names);
115a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.orgextern template
116a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.orgstd::string* MakeCheckOpString<unsigned long, unsigned long>(
117a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org    const unsigned long&, const unsigned long&, const char* names);
118a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.orgextern template
119a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.orgstd::string* MakeCheckOpString<unsigned long, unsigned int>(
120a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org    const unsigned long&, const unsigned int&, const char* names);
121a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.orgextern template
122a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.orgstd::string* MakeCheckOpString<unsigned int, unsigned long>(
123a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org    const unsigned int&, const unsigned long&, const char* names);
124a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.orgextern template
125a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.orgstd::string* MakeCheckOpString<std::string, std::string>(
126a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org    const std::string&, const std::string&, const char* name);
127a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org#endif
128f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org
12991d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg// Helper functions for RTC_CHECK_OP macro.
130a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// The (int, int) specialization works around the issue that the compiler
131a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// will not instantiate the template version of the function on values of
132a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// unnamed enum type - see comment below.
13391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define DEFINE_RTC_CHECK_OP_IMPL(name, op)                                   \
13491d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  template <class t1, class t2>                                              \
13591d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  inline std::string* Check##name##Impl(const t1& v1, const t2& v2,          \
13691d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg                                        const char* names) {                 \
13791d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg    if (v1 op v2)                                                            \
13891d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg      return NULL;                                                           \
13991d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg    else                                                                     \
14091d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg      return rtc::MakeCheckOpString(v1, v2, names);                          \
14191d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  }                                                                          \
142a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org  inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
14391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg    if (v1 op v2)                                                            \
14491d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg      return NULL;                                                           \
14591d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg    else                                                                     \
14691d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg      return rtc::MakeCheckOpString(v1, v2, names);                          \
147a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org  }
14891d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikgDEFINE_RTC_CHECK_OP_IMPL(EQ, ==)
14991d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikgDEFINE_RTC_CHECK_OP_IMPL(NE, !=)
15091d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikgDEFINE_RTC_CHECK_OP_IMPL(LE, <=)
15191d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikgDEFINE_RTC_CHECK_OP_IMPL(LT, < )
15291d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikgDEFINE_RTC_CHECK_OP_IMPL(GE, >=)
15391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikgDEFINE_RTC_CHECK_OP_IMPL(GT, > )
15491d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#undef DEFINE_RTC_CHECK_OP_IMPL
15591d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg
15691d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(EQ, ==, val1, val2)
15791d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(NE, !=, val1, val2)
15891d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_CHECK_LE(val1, val2) RTC_CHECK_OP(LE, <=, val1, val2)
15991d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_CHECK_LT(val1, val2) RTC_CHECK_OP(LT, < , val1, val2)
16091d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_CHECK_GE(val1, val2) RTC_CHECK_OP(GE, >=, val1, val2)
16191d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_CHECK_GT(val1, val2) RTC_CHECK_OP(GT, > , val1, val2)
16291d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg
16391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg// The RTC_DCHECK macro is equivalent to RTC_CHECK except that it only generates
16491d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg// code in debug builds. It does reference the condition parameter in all cases,
16555d42c32a4d2ec71d56e863c0cee54f8db270074kwiberg@webrtc.org// though, so callers won't risk getting warnings about unused variables.
16618617cfde8537293ab3fa537f7231b5007844cb3henrik.lundin@webrtc.org#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
167e2a83eee7337e5a8244c7abacc552bf5ef344442Karl Wiberg#define RTC_DCHECK_IS_ON 1
16891d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_DCHECK(condition) RTC_CHECK(condition)
16991d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2)
17091d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2)
17191d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2)
17291d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2)
17391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2)
17491d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2)
175a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org#else
176e2a83eee7337e5a8244c7abacc552bf5ef344442Karl Wiberg#define RTC_DCHECK_IS_ON 0
17791d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_DCHECK(condition) RTC_EAT_STREAM_PARAMETERS(condition)
17891d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_DCHECK_EQ(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) == (v2))
17991d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_DCHECK_NE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) != (v2))
18091d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_DCHECK_LE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) <= (v2))
18191d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_DCHECK_LT(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) < (v2))
18291d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_DCHECK_GE(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) >= (v2))
18391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_DCHECK_GT(v1, v2) RTC_EAT_STREAM_PARAMETERS((v1) > (v2))
184a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org#endif
185a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org
18634a6764981a656c62daed0d962f01374a08f9bacandrew@webrtc.org// This is identical to LogMessageVoidify but in name.
18734a6764981a656c62daed0d962f01374a08f9bacandrew@webrtc.orgclass FatalMessageVoidify {
18834a6764981a656c62daed0d962f01374a08f9bacandrew@webrtc.org public:
18934a6764981a656c62daed0d962f01374a08f9bacandrew@webrtc.org  FatalMessageVoidify() { }
19034a6764981a656c62daed0d962f01374a08f9bacandrew@webrtc.org  // This has to be an operator with a precedence lower than << but
19134a6764981a656c62daed0d962f01374a08f9bacandrew@webrtc.org  // higher than ?:
19234a6764981a656c62daed0d962f01374a08f9bacandrew@webrtc.org  void operator&(std::ostream&) { }
19334a6764981a656c62daed0d962f01374a08f9bacandrew@webrtc.org};
19434a6764981a656c62daed0d962f01374a08f9bacandrew@webrtc.org
195487afc704d5c6a2519f2decdb47130c7b3424992magjed@webrtc.org#define RTC_UNREACHABLE_CODE_HIT false
19691d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg#define RTC_NOTREACHED() RTC_DCHECK(RTC_UNREACHABLE_CODE_HIT)
197487afc704d5c6a2519f2decdb47130c7b3424992magjed@webrtc.org
198a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org#define FATAL() rtc::FatalMessage(__FILE__, __LINE__).stream()
19991d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg// TODO(ajm): Consider adding RTC_NOTIMPLEMENTED macro when
200a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// base/logging.h and system_wrappers/logging.h are consolidated such that we
201a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// can match the Chromium behavior.
202a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org
203a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org// Like a stripped-down LogMessage from logging.h, except that it aborts.
204a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.orgclass FatalMessage {
205a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org public:
206a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org  FatalMessage(const char* file, int line);
20791d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  // Used for RTC_CHECK_EQ(), etc. Takes ownership of the given string.
208a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org  FatalMessage(const char* file, int line, std::string* result);
209a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org  NO_RETURN ~FatalMessage();
210a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org
211a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org  std::ostream& stream() { return stream_; }
212a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org
213a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org private:
214a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org  void Init(const char* file, int line);
215a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org
216a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org  std::ostringstream stream_;
217a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org};
218a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org
219b6fab2b1cdcc8fd93ab8ac3dad19ee213a31a89ehenrik.lundin@webrtc.org// Performs the integer division a/b and returns the result. CHECKs that the
220b6fab2b1cdcc8fd93ab8ac3dad19ee213a31a89ehenrik.lundin@webrtc.org// remainder is zero.
221b6fab2b1cdcc8fd93ab8ac3dad19ee213a31a89ehenrik.lundin@webrtc.orgtemplate <typename T>
222b6fab2b1cdcc8fd93ab8ac3dad19ee213a31a89ehenrik.lundin@webrtc.orginline T CheckedDivExact(T a, T b) {
22391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg  RTC_CHECK_EQ(a % b, static_cast<T>(0));
224b6fab2b1cdcc8fd93ab8ac3dad19ee213a31a89ehenrik.lundin@webrtc.org  return a / b;
225b6fab2b1cdcc8fd93ab8ac3dad19ee213a31a89ehenrik.lundin@webrtc.org}
226b6fab2b1cdcc8fd93ab8ac3dad19ee213a31a89ehenrik.lundin@webrtc.org
227a5b7869f3d9887d67a17f9607dcc0922c7d2b278andrew@webrtc.org}  // namespace rtc
228f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org
229f048872e915a3ee229044ec4bc541f6cbf9e4de1henrike@webrtc.org#endif  // WEBRTC_BASE_CHECKS_H_
230