michael@0: /* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* functions for restoring saved values at the end of a C++ scope */ michael@0: michael@0: #ifndef mozilla_AutoRestore_h_ michael@0: #define mozilla_AutoRestore_h_ michael@0: michael@0: #include "mozilla/Attributes.h" // MOZ_STACK_CLASS michael@0: #include "mozilla/GuardObjects.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: /** michael@0: * Save the current value of a variable and restore it when the object michael@0: * goes out of scope. For example: michael@0: * { michael@0: * AutoRestore savePainting(mIsPainting); michael@0: * mIsPainting = true; michael@0: * michael@0: * // ... your code here ... michael@0: * michael@0: * // mIsPainting is reset to its old value at the end of this block michael@0: * } michael@0: */ michael@0: template michael@0: class MOZ_STACK_CLASS AutoRestore michael@0: { michael@0: private: michael@0: T& mLocation; michael@0: T mValue; michael@0: MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER michael@0: public: michael@0: AutoRestore(T& aValue MOZ_GUARD_OBJECT_NOTIFIER_PARAM) michael@0: : mLocation(aValue), mValue(aValue) michael@0: { michael@0: MOZ_GUARD_OBJECT_NOTIFIER_INIT; michael@0: } michael@0: ~AutoRestore() { mLocation = mValue; } michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif /* !defined(mozilla_AutoRestore_h_) */