aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2020-06-08 08:47:10 -0400
committerMarcoFalke <falke.marco@gmail.com>2020-06-15 07:39:08 -0400
commitfa6ef701adba1cb48535cac25fd43c742a82e40d (patch)
tree260e6b7232c595094dea9e545e7e9e5dbed17d12 /src/util
parentfa457fbd3387661e1973a8f4e5cc2def79e0c625 (diff)
util: Add Assert identity function
The utility is primarily useful to dereference pointer types, which are known to be not null at that time. For example, the ArgsManager is known to exist when the wallets are started. Instead of silently relying on that assumption, Assert can be used to abort the program and avoid UB should the assumption ever be violated.
Diffstat (limited to 'src/util')
-rw-r--r--src/util/check.h12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/util/check.h b/src/util/check.h
index 5cc078b36b..3d534fd33e 100644
--- a/src/util/check.h
+++ b/src/util/check.h
@@ -25,7 +25,7 @@ class NonFatalCheckError : public std::runtime_error
* - where the condition is assumed to be true, not for error handling or validating user input
* - where a failure to fulfill the condition is recoverable and does not abort the program
*
- * For example in RPC code, where it is undersirable to crash the whole program, this can be generally used to replace
+ * For example in RPC code, where it is undesirable to crash the whole program, this can be generally used to replace
* asserts or recoverable logic errors. A NonFatalCheckError in RPC code is caught and passed as a string to the RPC
* caller, which can then report the issue to the developers.
*/
@@ -46,4 +46,14 @@ 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) */
+template <typename T>
+T get_pure_r_value(T&& val)
+{
+ return std::forward<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 check; }()
+
#endif // BITCOIN_UTIL_CHECK_H