openMSX
ScopedAssign.hh
Go to the documentation of this file.
1#ifndef SCOPEDASSIGN_HH
2#define SCOPEDASSIGN_HH
3
7template<typename T> class ScopedAssign
8{
9public:
10 ScopedAssign(const ScopedAssign&) = delete;
14
15 [[nodiscard]] ScopedAssign(T& var_, T newValue)
16 : var(var_)
17 {
18 oldValue = var;
19 var = newValue;
20 }
22 {
23 var = oldValue;
24 }
25private:
26 T& var;
27 T oldValue;
28};
29
30#endif
Assign new value to some variable and restore the original value when this object goes out of scope.
ScopedAssign(T &var_, T newValue)
ScopedAssign & operator=(const ScopedAssign &)=delete
ScopedAssign & operator=(ScopedAssign &&)=delete
ScopedAssign(const ScopedAssign &)=delete
ScopedAssign(ScopedAssign &&)=delete