diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2018-11-24 14:43:25 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2018-11-24 14:43:25 +0200 |
commit | 582940c789fd767d9e8c09cb4147d48b596261be (patch) | |
tree | b60a508f21ef50cee024f51fa7ed03d471461e27 | |
parent | 44a0006e0d5fea9b2420f7c3289b5e75ccac9cb0 (diff) |
Add support for cancellation to make_guard()
-rw-r--r-- | libbutl/utility.mxx | 25 |
1 files 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<T> reverse_iterate (T&& x) {return reverse_range<T> (std::forward<T> (x));} + // Cleanly cast between incompatible function types or dlsym() result + // (void*) to a function pointer. + // + template <typename F, typename P> + F + function_cast (P*); + // Call a function on destruction. // template <typename F> @@ -308,19 +315,15 @@ LIBBUTL_MODEXPORT namespace butl template <typename F> 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 <typename F, typename P> - F - function_cast (P*); + F function; + bool active; + }; // Call a function if there is an exception. // |