From 582940c789fd767d9e8c09cb4147d48b596261be Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sat, 24 Nov 2018 14:43:25 +0200 Subject: Add support for cancellation to make_guard() --- libbutl/utility.mxx | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/libbutl/utility.mxx b/libbutl/utility.mxx index 5d093ad..7fb4173 100644 --- a/libbutl/utility.mxx +++ b/libbutl/utility.mxx @@ -293,6 +293,13 @@ LIBBUTL_MODEXPORT namespace butl inline reverse_range reverse_iterate (T&& x) {return reverse_range (std::forward (x));} + // Cleanly cast between incompatible function types or dlsym() result + // (void*) to a function pointer. + // + template + F + function_cast (P*); + // Call a function on destruction. // template @@ -308,19 +315,15 @@ LIBBUTL_MODEXPORT namespace butl template struct guard_impl { - guard_impl (F f): f_ (std::move (f)) {} - ~guard_impl () {f_ ();} + guard_impl (F f): function (std::move (f)), active (true) {} + ~guard_impl () {if (active) function ();} - private: - F f_; - }; + void + cancel () {active = false;} - // Cleanly cast between incompatible function types or dlsym() result - // (void*) to a function pointer. - // - template - F - function_cast (P*); + F function; + bool active; + }; // Call a function if there is an exception. // -- cgit v1.1