diff options
author | MarcoFalke <falke.marco@gmail.com> | 2020-12-04 11:06:57 +0100 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2020-12-04 11:07:28 +0100 |
commit | dca80ffb45fcc8e6eedb6dc481d500dedab4248b (patch) | |
tree | c7137925cfe6cb6740dd9bded7d6d3d5740d7ea4 /src | |
parent | ca759bc743c9b3b240289c467349cefc5f4444af (diff) | |
parent | faa05854f832405231c9198787a4eafe2cd4c5f0 (diff) |
Merge #20255: util: Add Assume() identity function
faa05854f832405231c9198787a4eafe2cd4c5f0 util: Remove probably misleading TODO (MarcoFalke)
fac5efe730ab5b8694920547203deefc5252d294 util: Add Assume() identity function (MarcoFalke)
fa861569dcfff9e72686dc5f30b1faa645b4d54e util: Allow Assert(...) to be used in all contexts (practicalswift)
Pull request description:
This is needed for #20138. Please refer to the added documentation for motivation.
ACKs for top commit:
practicalswift:
cr ACK faa05854f832405231c9198787a4eafe2cd4c5f0
jnewbery:
utACK faa05854f832405231c9198787a4eafe2cd4c5f0
hebasto:
ACK faa05854f832405231c9198787a4eafe2cd4c5f0, I have reviewed the code and it looks OK, I agree it can be merged.
Tree-SHA512: 72165fbd898b92ab9a79b070993fa1faa86c2e3545b6645e72c652bda295d5107bc298d0482bf3aaf0926fc0c3e6418a445c0e073b08568c44231f547f76a688
Diffstat (limited to 'src')
-rw-r--r-- | src/util/check.h | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/util/check.h b/src/util/check.h index 9edf394492..e7620d97a0 100644 --- a/src/util/check.h +++ b/src/util/check.h @@ -46,7 +46,7 @@ class NonFatalCheckError : public std::runtime_error #error "Cannot compile without assertions!" #endif -/** Helper for Assert(). TODO remove in C++14 and replace `decltype(get_pure_r_value(val))` with `T` (templated lambda) */ +/** Helper for Assert() */ template <typename T> T get_pure_r_value(T&& val) { @@ -54,6 +54,22 @@ T get_pure_r_value(T&& val) } /** Identity function. Abort if the value compares equal to zero */ -#define Assert(val) [&]() -> decltype(get_pure_r_value(val)) { auto&& check = (val); assert(#val && check); return std::forward<decltype(get_pure_r_value(val))>(check); }() +#define Assert(val) ([&]() -> decltype(get_pure_r_value(val)) { auto&& check = (val); assert(#val && check); return std::forward<decltype(get_pure_r_value(val))>(check); }()) + +/** + * Assume is the identity function. + * + * - Should be used to run non-fatal checks. In debug builds it behaves like + * Assert()/assert() to notify developers and testers about non-fatal errors. + * In production it doesn't warn or log anything. + * - For fatal errors, use Assert(). + * - For non-fatal errors in interactive sessions (e.g. RPC or command line + * interfaces), CHECK_NONFATAL() might be more appropriate. + */ +#ifdef ABORT_ON_FAILED_ASSUME +#define Assume(val) Assert(val) +#else +#define Assume(val) ((void)(val)) +#endif #endif // BITCOIN_UTIL_CHECK_H |