2017-08-17 09:06:26 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-08-17 09:06:26 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-08-17 09:06:26 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
namespace base {
|
|
|
|
namespace assertion {
|
|
|
|
|
|
|
|
// Client must define that method.
|
|
|
|
void log(const char *message, const char *file, int line);
|
|
|
|
|
|
|
|
// Release build assertions.
|
2017-09-05 17:46:16 +00:00
|
|
|
inline constexpr void noop() {
|
2017-08-17 09:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[noreturn]] inline void fail(const char *message, const char *file, int line) {
|
|
|
|
log(message, file, line);
|
|
|
|
|
|
|
|
// Crash with access violation and generate crash report.
|
|
|
|
volatile auto nullptr_value = (int*)nullptr;
|
|
|
|
*nullptr_value = 0;
|
|
|
|
|
|
|
|
// Silent the possible failure to comply noreturn warning.
|
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
|
2017-09-03 12:44:21 +00:00
|
|
|
#ifndef GSL_UNLIKELY
|
|
|
|
#define DEFINED_GSL_UNLIKELY_
|
|
|
|
#define GSL_UNLIKELY(expression) (expression)
|
|
|
|
#endif // GSL_UNLIKELY
|
|
|
|
|
2017-09-05 17:46:16 +00:00
|
|
|
inline constexpr void validate(bool condition, const char *message, const char *file, int line) {
|
2017-08-17 09:06:26 +00:00
|
|
|
(GSL_UNLIKELY(!(condition))) ? fail(message, file, line) : noop();
|
|
|
|
}
|
|
|
|
|
2017-09-03 12:44:21 +00:00
|
|
|
#ifdef DEFINED_GSL_UNLIKELY_
|
|
|
|
#undef GSL_UNLIKELY
|
|
|
|
#undef DEFINED_GSL_UNLIKELY_
|
|
|
|
#endif // DEFINED_GSL_UNLIKELY_
|
|
|
|
|
2017-08-17 09:06:26 +00:00
|
|
|
} // namespace assertion
|
|
|
|
} // namespace base
|
|
|
|
|
|
|
|
#define AssertCustom(condition, message) (::base::assertion::validate(condition, message, __FILE__, __LINE__))
|
|
|
|
#define Assert(condition) AssertCustom(condition, "\"" #condition "\"")
|
|
|
|
|
|
|
|
// Define our own versions of Expects() and Ensures().
|
|
|
|
// Let them crash with reports and logging.
|
|
|
|
#ifdef Expects
|
|
|
|
#undef Expects
|
|
|
|
#endif // Expects
|
|
|
|
#define Expects(condition) (::base::assertion::validate(condition, "\"" #condition "\"", __FILE__, __LINE__))
|
|
|
|
|
|
|
|
#ifdef Ensures
|
|
|
|
#undef Ensures
|
|
|
|
#endif // Ensures
|
|
|
|
#define Ensures(condition) (::base::assertion::validate(condition, "\"" #condition "\"", __FILE__, __LINE__))
|
|
|
|
|
|
|
|
#ifdef Unexpected
|
|
|
|
#undef Unexpected
|
|
|
|
#endif // Unexpected
|
|
|
|
#define Unexpected(message) (::base::assertion::fail("Unexpected: " message, __FILE__, __LINE__))
|
2018-09-27 20:31:48 +00:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
#define AssertIsDebug(...)
|
|
|
|
#endif // _DEBUG
|