From 7f5931fd470862e9d111fd4d3820b8635e28e531 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 12 Sep 2015 18:02:33 +0100 Subject: Allow multiple scope exit events and allow them to be changed prior to triggering --- libadhocutil/scopeExit.cpp | 22 +++++++++++++--------- libadhocutil/scopeExit.h | 16 +++++++++++----- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/libadhocutil/scopeExit.cpp b/libadhocutil/scopeExit.cpp index b9ff8f5..a93f8b6 100644 --- a/libadhocutil/scopeExit.cpp +++ b/libadhocutil/scopeExit.cpp @@ -2,24 +2,28 @@ namespace AdHoc { -ScopeExit::ScopeExit(const Event & onexitpre, const Event & onsuccess, const Event & onfailure, const Event & onexitpost) : - onExitPre(onexitpre), - onSuccess(onsuccess), - onFailure(onfailure), - onExitPost(onexitpost) +ScopeExit::ScopeExit() { } +ScopeExit::ScopeExit(const Event & onexitpre, const Event & onsuccess, const Event & onfailure, const Event & onexitpost) +{ + if (onexitpre) onExitPre.push_back(onexitpre); + if (onsuccess) onSuccess.push_back(onsuccess); + if (onfailure) onFailure.push_back(onfailure); + if (onexitpost) onExitPost.push_back(onexitpost); +} + ScopeExit::~ScopeExit() { - if (onExitPre) onExitPre(); + for(const auto & e : onExitPre) e(); if (std::uncaught_exception()) { - if (onFailure) onFailure(); + for(const auto & e : onFailure) e(); } else { - if (onSuccess) onSuccess(); + for(const auto & e : onSuccess) e(); } - if (onExitPost) onExitPost(); + for(const auto & e : onExitPost) e(); } } diff --git a/libadhocutil/scopeExit.h b/libadhocutil/scopeExit.h index a8eb9a2..17618a4 100644 --- a/libadhocutil/scopeExit.h +++ b/libadhocutil/scopeExit.h @@ -2,6 +2,7 @@ #define ADHOCUTIL_SCOPEEXIT_H #include +#include #include "visibility.h" namespace AdHoc { @@ -12,6 +13,10 @@ class DLL_PUBLIC ScopeExit { /** Callback for code to be run. */ typedef boost::function Event; + /** + * Construct an empty trigger for running code yet to be determined at scope exit + */ + ScopeExit(); /** Construct a trigger for running code at scope exit. * @param pre Run this code (unconditionally) first. * @param success Only run this if the scope is exitted cleanly. @@ -24,11 +29,12 @@ class DLL_PUBLIC ScopeExit { ScopeExit(const ScopeExit &) = delete; void operator=(const ScopeExit &) = delete; - private: - const Event onExitPre; - const Event onSuccess; - const Event onFailure; - const Event onExitPost; + /// @cond + std::vector onExitPre; + std::vector onSuccess; + std::vector onFailure; + std::vector onExitPost; + /// @endcond }; } -- cgit v1.2.3