Bug 1825336 - Make toolkit/components/url-classifier/ buildable outside of a unified...
[gecko.git] / xpcom / base / AutoRestore.h
blobfc585da14b43c033fb28889f9d8f0f5385dd1054
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
14 namespace mozilla {
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;
23 * // ... your code here ...
25 * // mIsPainting is reset to its old value at the end of this block
26 * }
28 template <class T>
29 class MOZ_RAII AutoRestore {
30 private:
31 T& mLocation;
32 T mValue;
34 public:
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_) */