From: Mikko Rasa Date: Fri, 31 May 2013 21:56:23 +0000 (+0300) Subject: Add RAII utilities X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=0c1daa2eeda83e7359a66b2ff6d803e45d32a52f;p=libs%2Fcore.git Add RAII utilities --- 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