X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fraii.h;fp=source%2Fcore%2Fraii.h;h=fde7e3e9dde2e8f72635d63654d8857039951f49;hp=0000000000000000000000000000000000000000;hb=0c1daa2eeda83e7359a66b2ff6d803e45d32a52f;hpb=4ea380ecdfefe2b7f14737a72fc996ce0546393a 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