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_
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the current value of a variable and restore it when the object
|
|
|
|
* goes out of scope. For example:
|
|
|
|
* {
|
2011-09-28 23:19:26 -07:00
|
|
|
* AutoRestore<bool> savePainting(mIsPainting);
|
2011-10-17 07:59:28 -07:00
|
|
|
* mIsPainting = true;
|
2009-12-11 08:13:19 -08:00
|
|
|
*
|
|
|
|
* // ... your code here ...
|
|
|
|
*
|
|
|
|
* // mIsPainting is reset to its old value at the end of this block
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
template <class T>
|
2010-04-04 11:15:18 -07:00
|
|
|
class NS_STACK_CLASS AutoRestore
|
2009-12-11 08:13:19 -08:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
T& mLocation;
|
|
|
|
T mValue;
|
2012-01-09 21:29:30 -08:00
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
2009-12-11 08:13:19 -08:00
|
|
|
public:
|
2012-01-09 21:29:30 -08:00
|
|
|
AutoRestore(T& aValue MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2010-04-04 11:15:18 -07:00
|
|
|
: mLocation(aValue), mValue(aValue)
|
|
|
|
{
|
2012-01-09 21:29:30 -08:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
2010-04-04 11:15:18 -07:00
|
|
|
}
|
2009-12-11 08:13:19 -08:00
|
|
|
~AutoRestore() { mLocation = mValue; }
|
|
|
|
};
|
|
|
|
|
2011-11-15 23:50:19 -08:00
|
|
|
} // namespace mozilla
|
2009-12-11 08:13:19 -08:00
|
|
|
|
|
|
|
#endif /* !defined(mozilla_AutoRestore_h_) */
|