From 0324ad68eed762ebf00ed4fb5a7ea2f2e3bd77b6 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 22 Nov 2016 12:08:21 +0200 Subject: Add diagnostics facility --- butl/utility | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) (limited to 'butl/utility') diff --git a/butl/utility b/butl/utility index a513325..6155207 100644 --- a/butl/utility +++ b/butl/utility @@ -6,9 +6,10 @@ #define BUTL_UTILITY #include -#include // size_t -#include // forward() -#include // strcmp(), strlen() +#include // size_t +#include // move(), forward() +#include // strcmp(), strlen() +#include // uncaught_exception() //#include // hash namespace butl @@ -146,6 +147,46 @@ namespace butl template inline reverse_range reverse_iterate (T&& x) {return reverse_range (std::forward (x));} + + // Call a function if there is an exception. + // + + // True means we are in the body of a destructor that is being called as + // part of the exception stack unwindining. Used to compensate for the + // deficiencies of uncaught_exception() until C++17 uncaught_exceptions() + // becomes available. + // + // @@ MT: will have to be TLS. + // + extern bool exception_unwinding_dtor; + + template + struct exception_guard; + + template + inline exception_guard + make_exception_guard (F f) + { + return exception_guard (std::move (f)); + } + + template + struct exception_guard + { + exception_guard (F f): f_ (std::move (f)) {} + ~exception_guard () + { + if (std::uncaught_exception ()) + { + exception_unwinding_dtor = true; + f_ (); + exception_unwinding_dtor = false; + } + } + + private: + F f_; + }; } #include -- cgit v1.1