summaryrefslogtreecommitdiff
path: root/libadhocutil/scopeExit.cpp
blob: 0163aad00887c7e569a0dd6b58f139f56485ea94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "scopeExit.h"

namespace AdHoc {

ScopeExit::ScopeExit() = default;

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()
{
	for(const auto & e : onExitPre) {
		e();
	}
	if (std::uncaught_exceptions()) {
		for(const auto & e : onFailure) {
			e();
		}
	}
	else {
		for(const auto & e : onSuccess) {
			e();
		}
	}
	for(const auto & e : onExitPost) {
		e();
	}
}

}