blob: da3f388e58349974413d62cc2c2d962210ae1b14 (
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#ifndef _SCOPE_EXECUTE_H
#define _SCOPE_EXECUTE_H
template <typename T>
class scoped_variable : public boost::noncopyable
{
T& var;
T prev;
bool enabled;
public:
explicit scoped_variable(T& _var)
: var(_var), prev(var), enabled(true) {}
explicit scoped_variable(T& _var, const T& value)
: var(_var), prev(var), enabled(true) {
var = value;
}
~scoped_variable() {
if (enabled)
var = prev;
}
void clear() {
enabled = false;
}
};
template <typename T>
class scoped_execute : public boost::noncopyable
{
typedef boost::function<void (T)> function_t;
function_t code;
T arg;
bool enabled;
public:
explicit scoped_execute(const function_t& _code, T _arg)
: code(_code), arg(_arg), enabled(true) {}
~scoped_execute() {
if (enabled)
code(arg);
}
void clear() {
enabled = false;
}
};
template <>
class scoped_execute<void> : public boost::noncopyable
{
typedef boost::function<void ()> function_t;
function_t code;
bool enabled;
public:
explicit scoped_execute(const function_t& _code)
: code(_code), enabled(true) {}
~scoped_execute() {
if (enabled)
code();
}
void clear() {
enabled = false;
}
};
#endif // _SCOPE_EXECUTE_H
|