|
1 /* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* functions for restoring saved values at the end of a C++ scope */ |
|
7 |
|
8 #ifndef mozilla_AutoRestore_h_ |
|
9 #define mozilla_AutoRestore_h_ |
|
10 |
|
11 #include "mozilla/Attributes.h" // MOZ_STACK_CLASS |
|
12 #include "mozilla/GuardObjects.h" |
|
13 |
|
14 namespace mozilla { |
|
15 |
|
16 /** |
|
17 * Save the current value of a variable and restore it when the object |
|
18 * goes out of scope. For example: |
|
19 * { |
|
20 * AutoRestore<bool> savePainting(mIsPainting); |
|
21 * mIsPainting = true; |
|
22 * |
|
23 * // ... your code here ... |
|
24 * |
|
25 * // mIsPainting is reset to its old value at the end of this block |
|
26 * } |
|
27 */ |
|
28 template <class T> |
|
29 class MOZ_STACK_CLASS AutoRestore |
|
30 { |
|
31 private: |
|
32 T& mLocation; |
|
33 T mValue; |
|
34 MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER |
|
35 public: |
|
36 AutoRestore(T& aValue MOZ_GUARD_OBJECT_NOTIFIER_PARAM) |
|
37 : mLocation(aValue), mValue(aValue) |
|
38 { |
|
39 MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
|
40 } |
|
41 ~AutoRestore() { mLocation = mValue; } |
|
42 }; |
|
43 |
|
44 } // namespace mozilla |
|
45 |
|
46 #endif /* !defined(mozilla_AutoRestore_h_) */ |