diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-09-12 18:02:33 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-09-12 18:02:33 +0100 |
commit | 7f5931fd470862e9d111fd4d3820b8635e28e531 (patch) | |
tree | 1385888c94a0b545a55cb8a0cf7aed01bfaff2ab | |
parent | Add a code coverage profile (diff) | |
download | libadhocutil-7f5931fd470862e9d111fd4d3820b8635e28e531.tar.bz2 libadhocutil-7f5931fd470862e9d111fd4d3820b8635e28e531.tar.xz libadhocutil-7f5931fd470862e9d111fd4d3820b8635e28e531.zip |
Allow multiple scope exit events and allow them to be changed prior to triggering
-rw-r--r-- | libadhocutil/scopeExit.cpp | 22 | ||||
-rw-r--r-- | 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 <boost/function.hpp> +#include <vector> #include "visibility.h" namespace AdHoc { @@ -12,6 +13,10 @@ class DLL_PUBLIC ScopeExit { /** Callback for code to be run. */ typedef boost::function<void()> 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<Event> onExitPre; + std::vector<Event> onSuccess; + std::vector<Event> onFailure; + std::vector<Event> onExitPost; + /// @endcond }; } |