veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
assert.hpp
1
// Copyright (c) 2019-2022 Xenios SEZC
2
// https://www.veriblock.org
3
// Distributed under the MIT software license, see the accompanying
4
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
5
6
#ifndef VERIBLOCK_POP_CPP_ASSERT_HPP
7
#define VERIBLOCK_POP_CPP_ASSERT_HPP
8
9
#include "fmt.hpp"
10
#include "logger.hpp"
11
12
#ifdef VBK_HAS_BUILTIN_EXPECT
13
// tell branch predictor that condition is always true
14
#define VBK_LIKELY(condition) __builtin_expect(static_cast<bool>(condition), 1)
15
#define VBK_UNLIKELY(condition) \
16
__builtin_expect(static_cast<bool>(condition), 0)
17
#else
18
#define VBK_LIKELY(condition) (condition)
19
#define VBK_UNLIKELY(condition) (condition)
20
#endif
21
22
#if defined(__GNUC__) || defined(__clang__)
23
#define VBK_DEPRECATED __attribute__((deprecated))
24
#elif defined(_MSC_VER)
25
#define VBK_DEPRECATED __declspec(deprecated)
26
#else
27
#define VBK_DEPRECATED
28
#endif
29
30
#if defined(__GNUC__) || defined(__clang__)
31
#define VBK_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
32
#elif defined(_MSC_VER)
33
#define VBK_DEPRECATED_MSG(msg) __declspec(deprecated)
34
#else
35
#define VBK_DEPRECATED_MSG
36
#endif
37
38
#define VBK_ASSERT_MSG(x, ...) \
39
/* GCOVR_EXCL_START */
\
40
if (!VBK_LIKELY((x))) { \
41
auto msg = fmt::format("Assertion failed at {}:{} inside {}:\n{}\n{}\n"
, \
42
__FILE__, \
43
__LINE__, \
44
__FUNCTION__, \
45
#x, \
46
fmt::sprintf(__VA_ARGS__)); \
47
/* print to log */
VBK_LOG_CRITICAL(msg); \
48
/* print to stderr */
fmt::fprintf(stderr, msg); \
49
/* die */
std::terminate(); \
50
}
/* GCOVR_EXCL_STOP */
51
52
#define VBK_ASSERT(x) VBK_ASSERT_MSG(x, " "
);
53
54
// same as VBK_ASSERT but executes only when compiled in DEBUG mode. similar to
55
// how <cassert> works
56
#if defined(NDEBUG)
57
#define VBK_ASSERT_MSG_DEBUG(x, ...)
58
#define VBK_ASSERT_DEBUG(x)
59
#else
60
#define VBK_ASSERT_MSG_DEBUG(x, ...) VBK_ASSERT_MSG(x, __VA_ARGS__)
61
#define VBK_ASSERT_DEBUG(x) VBK_ASSERT(x)
62
#endif
63
64
// Similar to C++17 [[nodiscard]]
65
#if defined(__GNUC__) && (__GNUC__ >= 4)
66
#define VBK_CHECK_RETURN __attribute__((warn_unused_result))
67
#elif defined(_MSC_VER) && (_MSC_VER >= 1700)
68
#define VBK_CHECK_RETURN _Check_return_
69
#else
70
#define VBK_CHECK_RETURN
71
#endif
72
73
#endif
// VERIBLOCK_POP_CPP_ASSERT_HPP