2009-12-11 08:13:19 -08:00
|
|
|
/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
|
2012-05-21 04:12:37 -07: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 08:13:19 -08:00
|
|
|
|
|
|
|
/* functions for restoring saved values at the end of a C++ scope */
|
|
|
|
|
|
|
|
#ifndef mozilla_AutoRestore_h_
|
|
|
|
#define mozilla_AutoRestore_h_
|
|
|
|
|
2013-04-11 20:21:40 -07:00
|
|
|
#include "mozilla/Attributes.h" // MOZ_STACK_CLASS
|
2011-11-15 23:50:19 -08:00
|
|
|
#include "mozilla/GuardObjects.h"
|
2010-03-26 07:12:39 -07:00
|
|
|
|
2009-12-11 08:13:19 -08:00
|
|
|
namespace mozilla {
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
/**
|
|
|
|
* Save the current value of a variable and restore it when the object
|
|
|
|
* goes out of scope. For example:
|
|
|
|
* {
|
|
|
|
* AutoRestore<bool> savePainting(mIsPainting);
|
|
|
|
* mIsPainting = true;
|
|
|
|
*
|
|
|
|
* // ... your code here ...
|
|
|
|
*
|
|
|
|
* // mIsPainting is reset to its old value at the end of this block
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
template<class T>
|
|
|
|
class MOZ_STACK_CLASS AutoRestore
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
T& mLocation;
|
|
|
|
T mValue;
|
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
|
|
|
public:
|
|
|
|
AutoRestore(T& aValue MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
: mLocation(aValue)
|
|
|
|
, mValue(aValue)
|
2009-12-11 08:13:19 -08:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
|
|
|
~AutoRestore()
|
|
|
|
{
|
|
|
|
mLocation = mValue;
|
|
|
|
}
|
|
|
|
};
|
2009-12-11 08:13:19 -08:00
|
|
|
|
2011-11-15 23:50:19 -08:00
|
|
|
} // namespace mozilla
|
2009-12-11 08:13:19 -08:00
|
|
|
|
|
|
|
#endif /* !defined(mozilla_AutoRestore_h_) */
|