2009-12-11 16:13:19 +00:00
|
|
|
/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-12-11 16:13:19 +00:00
|
|
|
|
|
|
|
/* functions for restoring saved values at the end of a C++ scope */
|
|
|
|
|
|
|
|
#ifndef mozilla_AutoRestore_h_
|
|
|
|
#define mozilla_AutoRestore_h_
|
|
|
|
|
2013-04-12 03:21:40 +00:00
|
|
|
#include "mozilla/Attributes.h" // MOZ_STACK_CLASS
|
2011-11-16 07:50:19 +00:00
|
|
|
#include "mozilla/GuardObjects.h"
|
2010-03-26 14:12:39 +00:00
|
|
|
|
2009-12-11 16:13:19 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the current value of a variable and restore it when the object
|
|
|
|
* goes out of scope. For example:
|
|
|
|
* {
|
2011-09-29 06:19:26 +00:00
|
|
|
* AutoRestore<bool> savePainting(mIsPainting);
|
2011-10-17 14:59:28 +00:00
|
|
|
* mIsPainting = true;
|
2009-12-11 16:13:19 +00:00
|
|
|
*
|
|
|
|
* // ... your code here ...
|
|
|
|
*
|
|
|
|
* // mIsPainting is reset to its old value at the end of this block
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
template <class T>
|
2013-04-12 03:21:40 +00:00
|
|
|
class MOZ_STACK_CLASS AutoRestore
|
2009-12-11 16:13:19 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
T& mLocation;
|
|
|
|
T mValue;
|
2012-01-10 05:29:30 +00:00
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
2009-12-11 16:13:19 +00:00
|
|
|
public:
|
2012-01-10 05:29:30 +00:00
|
|
|
AutoRestore(T& aValue MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2010-04-04 18:15:18 +00:00
|
|
|
: mLocation(aValue), mValue(aValue)
|
|
|
|
{
|
2012-01-10 05:29:30 +00:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
2010-04-04 18:15:18 +00:00
|
|
|
}
|
2009-12-11 16:13:19 +00:00
|
|
|
~AutoRestore() { mLocation = mValue; }
|
|
|
|
};
|
|
|
|
|
2011-11-16 07:50:19 +00:00
|
|
|
} // namespace mozilla
|
2009-12-11 16:13:19 +00:00
|
|
|
|
|
|
|
#endif /* !defined(mozilla_AutoRestore_h_) */
|