Browse Source

Add class and macro to save and auto-restore a variable

Scott Lahteine 6 years ago
parent
commit
fdb97a3e9c
1 changed files with 13 additions and 0 deletions
  1. 13
    0
      Marlin/src/core/utility.h

+ 13
- 0
Marlin/src/core/utility.h View File

@@ -121,3 +121,16 @@ inline void serial_delay(const millis_t ms) {
121 121
 #endif
122 122
 
123 123
 void print_bin(const uint16_t val);
124
+
125
+template<typename T>
126
+class restorer {
127
+  T& ref_;
128
+  T  val_;
129
+public:
130
+  restorer(T& perm) : ref_(perm), val_(perm) {}
131
+  ~restorer() { restore(); }
132
+  inline void restore() { ref_ = val_; }
133
+};
134
+
135
+#define REMEMBER(X) restorer<typeof(X)> X##_restorer(X)
136
+#define RESTORE(X) X##_restorer.restore()

Loading…
Cancel
Save