]> git.tdb.fi Git - libs/core.git/blob - source/core/raii.h
Add RAII utilities
[libs/core.git] / source / core / raii.h
1 #ifndef MSP_CORE_RAII_H_
2 #define MSP_CORE_RAII_H_
3
4 namespace Msp {
5
6 /**
7 Sets a value for the duration of a scope.  The old value is restored when the
8 scope ends.
9 */
10 template<typename T>
11 class SetForScope
12 {
13 private:
14         T &ref;
15         T old;
16
17 public:
18         SetForScope(T &r, T v): ref(r), old(r) { ref = v; }
19         ~SetForScope() { ref = old; }
20 };
21
22 class SetFlag: public SetForScope<bool>
23 {
24 public:
25         SetFlag(bool &r, bool v = true): SetForScope<bool>(r, v) { }
26 };
27
28 } // namespace Msp
29
30 #endif