瀏覽代碼

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

Scott Lahteine 6 年之前
父節點
當前提交
fdb97a3e9c
共有 1 個檔案被更改,包括 13 行新增0 行删除
  1. 13
    0
      Marlin/src/core/utility.h

+ 13
- 0
Marlin/src/core/utility.h 查看文件

@@ -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…
取消
儲存