]> git.tdb.fi Git - libs/core.git/commitdiff
Add RAII utilities
authorMikko Rasa <tdb@tdb.fi>
Fri, 31 May 2013 21:56:23 +0000 (00:56 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 31 May 2013 21:56:23 +0000 (00:56 +0300)
source/core/raii.h [new file with mode: 0644]

diff --git a/source/core/raii.h b/source/core/raii.h
new file mode 100644 (file)
index 0000000..fde7e3e
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef MSP_CORE_RAII_H_
+#define MSP_CORE_RAII_H_
+
+namespace Msp {
+
+/**
+Sets a value for the duration of a scope.  The old value is restored when the
+scope ends.
+*/
+template<typename T>
+class SetForScope
+{
+private:
+       T &ref;
+       T old;
+
+public:
+       SetForScope(T &r, T v): ref(r), old(r) { ref = v; }
+       ~SetForScope() { ref = old; }
+};
+
+class SetFlag: public SetForScope<bool>
+{
+public:
+       SetFlag(bool &r, bool v = true): SetForScope<bool>(r, v) { }
+};
+
+} // namespace Msp
+
+#endif