diff options
-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 }; } |