From fdb97a3e9cf598d7fe8fee01fd5fce798f837879 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 4 Feb 2019 02:15:20 -0600 Subject: [PATCH] Add class and macro to save and auto-restore a variable --- Marlin/src/core/utility.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Marlin/src/core/utility.h b/Marlin/src/core/utility.h index b59500f0ca..b8e2ffa38f 100644 --- a/Marlin/src/core/utility.h +++ b/Marlin/src/core/utility.h @@ -121,3 +121,16 @@ inline void serial_delay(const millis_t ms) { #endif void print_bin(const uint16_t val); + +template +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 X##_restorer(X) +#define RESTORE(X) X##_restorer.restore()