diff options
-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. // |