From 0c1daa2eeda83e7359a66b2ff6d803e45d32a52f Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 1 Jun 2013 00:56:23 +0300 Subject: [PATCH] Add RAII utilities --- source/core/raii.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 source/core/raii.h diff --git a/source/core/raii.h b/source/core/raii.h new file mode 100644 index 0000000..fde7e3e --- /dev/null +++ b/source/core/raii.h @@ -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 +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 +{ +public: + SetFlag(bool &r, bool v = true): SetForScope(r, v) { } +}; + +} // namespace Msp + +#endif -- 2.43.0