1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* functions for restoring saved values at the end of a C++ scope */
9 #ifndef mozilla_AutoRestore_h_
10 #define mozilla_AutoRestore_h_
12 #include "mozilla/Attributes.h" // MOZ_STACK_CLASS
17 * Save the current value of a variable and restore it when the object
18 * goes out of scope. For example:
20 * AutoRestore<bool> savePainting(mIsPainting);
23 * // ... your code here ...
25 * // mIsPainting is reset to its old value at the end of this block
29 class MOZ_RAII AutoRestore
{
35 explicit AutoRestore(T
& aValue
) : mLocation(aValue
), mValue(aValue
) {}
36 ~AutoRestore() { mLocation
= mValue
; }
37 T
SavedValue() const { return mValue
; }
40 } // namespace mozilla
42 #endif /* !defined(mozilla_AutoRestore_h_) */