Procházet zdrojové kódy

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

Scott Lahteine před 6 roky
rodič
revize
fdb97a3e9c
1 změnil soubory, kde provedl 13 přidání a 0 odebrání
  1. 13
    0
      Marlin/src/core/utility.h

+ 13
- 0
Marlin/src/core/utility.h Zobrazit soubor

121
 #endif
121
 #endif
122
 
122
 
123
 void print_bin(const uint16_t val);
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…
Zrušit
Uložit