Add class and macro to save and auto-restore a variable
This commit is contained in:
parent
433518de74
commit
fdb97a3e9c
1 changed files with 13 additions and 0 deletions
|
@ -121,3 +121,16 @@ inline void serial_delay(const millis_t ms) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void print_bin(const uint16_t val);
|
void print_bin(const uint16_t val);
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class restorer {
|
||||||
|
T& ref_;
|
||||||
|
T val_;
|
||||||
|
public:
|
||||||
|
restorer(T& perm) : ref_(perm), val_(perm) {}
|
||||||
|
~restorer() { restore(); }
|
||||||
|
inline void restore() { ref_ = val_; }
|
||||||
|
};
|
||||||
|
|
||||||
|
#define REMEMBER(X) restorer<typeof(X)> X##_restorer(X)
|
||||||
|
#define RESTORE(X) X##_restorer.restore()
|
||||||
|
|
Reference in a new issue