Bitcoin ABC  0.26.3
P2P Digital Currency
check.h
Go to the documentation of this file.
1 // Copyright (c) 2019 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_UTIL_CHECK_H
6 #define BITCOIN_UTIL_CHECK_H
7 
8 #if defined(HAVE_CONFIG_H)
9 #include <config/bitcoin-config.h>
10 #endif
11 
12 #include <tinyformat.h>
13 
14 #include <stdexcept>
15 
16 class NonFatalCheckError : public std::runtime_error {
17  using std::runtime_error::runtime_error;
18 };
19 
20 #define format_internal_error(msg, file, line, func, report) \
21  strprintf("Internal bug detected: \"%s\"\n%s:%d (%s)\nPlease report this " \
22  "issue here: %s\n", \
23  msg, file, line, func, report)
24 
26 template <typename T>
27 T &&inline_check_non_fatal(T &&val, const char *file, int line,
28  const char *func, const char *assertion) {
29  if (!(val)) {
31  assertion, file, line, func, PACKAGE_BUGREPORT));
32  }
33 
34  return std::forward<T>(val);
35 }
36 
52 #define CHECK_NONFATAL(condition) \
53  inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
54 
55 #if defined(NDEBUG)
56 #error "Cannot compile without assertions!"
57 #endif
58 
60 template <typename T> T get_pure_r_value(T &&val) {
61  return std::forward<T>(val);
62 }
63 
65 #define Assert(val) \
66  ([&]() -> decltype(get_pure_r_value(val)) { \
67  auto &&check = (val); \
68  assert(#val &&check); \
69  return std::forward<decltype(get_pure_r_value(val))>(check); \
70  }())
71 
82 #ifdef ABORT_ON_FAILED_ASSUME
83 #define Assume(val) Assert(val)
84 #else
85 #define Assume(val) \
86  ([&]() -> decltype(get_pure_r_value(val)) { \
87  auto &&check = (val); \
88  return std::forward<decltype(get_pure_r_value(val))>(check); \
89  }())
90 #endif
91 
98 #define NONFATAL_UNREACHABLE() \
99  throw NonFatalCheckError(format_internal_error( \
100  "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__, \
101  PACKAGE_BUGREPORT))
102 
103 #endif // BITCOIN_UTIL_CHECK_H
T get_pure_r_value(T &&val)
Helper for Assert()
Definition: check.h:60
#define format_internal_error(msg, file, line, func, report)
Definition: check.h:20
T && inline_check_non_fatal(T &&val, const char *file, int line, const char *func, const char *assertion)
Helper for CHECK_NONFATAL()
Definition: check.h:27