23 auto UTL_ANONYMOUS_VARIABLE(scopeExit) = utl::ScopeExitMacroHelper() + [&]() noexcept 39 auto UTL_ANONYMOUS_VARIABLE(scopeFail) = utl::ScopeFailMacroHelper() + [&]() noexcept 54 #define SCOPE_SUCCESS \ 55 auto UTL_ANONYMOUS_VARIABLE(scopeSuccess) = utl::ScopeSuccessMacroHelper() + [&]() noexcept 59 class ScopeGuardImplBase
79 mutable bool _dismissed;
83 ScopeGuardImplBase& operator=(
const ScopeGuardImplBase&);
88 template <
typename Callable>
89 class ScopeGuardExit :
public ScopeGuardImplBase
92 explicit ScopeGuardExit(
const Callable& callable)
97 explicit ScopeGuardExit(Callable&& callable)
98 : _callable(std::move(callable))
102 ~ScopeGuardExit() noexcept
104 if (_dismissed)
return;
115 class UncaughtExceptionCounter
118 UncaughtExceptionCounter()
119 : _exceptionCount(std::uncaught_exceptions())
124 isNewUncaughtException() noexcept
126 return std::uncaught_exceptions() > _exceptionCount;
130 int getUncaughtExceptionCount() noexcept;
138 template <
typename Callable,
bool executeOnException>
139 class ScopeGuardForNewException :
public ScopeGuardImplBase
142 explicit ScopeGuardForNewException(
const Callable& callable)
143 : _callable(callable)
147 explicit ScopeGuardForNewException(Callable&& callable)
148 : _callable(std::move(callable))
152 ~ScopeGuardForNewException() noexcept
154 if (executeOnException == _ec.isNewUncaughtException())
156 if (_dismissed)
return;
163 UncaughtExceptionCounter _ec;
168 enum class ScopeExitMacroHelper
172 enum class ScopeFailMacroHelper
176 enum class ScopeSuccessMacroHelper
182 template <
typename Callable>
183 ScopeGuardExit<Callable>
184 makeExitGuard(Callable&& callable)
186 return ScopeGuardExit<Callable>(std::forward(callable));
189 template <
typename Callable>
190 ScopeGuardForNewException<Callable, true>
191 makeFailureGuard(Callable&& callable)
193 return ScopeGuardForNewException<Callable, true>(std::forward(callable));
196 template <
typename Callable>
197 ScopeGuardForNewException<Callable, false>
198 makeSuccessGuard(Callable&& callable)
200 return ScopeGuardForNewException<Callable, false>(std::forward(callable));
209 template <
typename Callable>
210 utl::ScopeGuardExit<Callable> operator+(utl::ScopeExitMacroHelper, Callable&& callable)
212 return utl::ScopeGuardExit<Callable>(std::forward<Callable>(callable));
215 template <
typename Callable>
216 utl::ScopeGuardForNewException<Callable, true> operator+(utl::ScopeFailMacroHelper,
219 return utl::ScopeGuardForNewException<typename std::decay<Callable>::type,
true>(
220 std::forward<Callable>(callable));
223 template <
typename Callable>
224 utl::ScopeGuardForNewException<Callable, false> operator+(utl::ScopeSuccessMacroHelper,
227 return utl::ScopeGuardForNewException<typename std::decay<Callable>::type,
false>(
228 std::forward<Callable>(callable));