2009-12-11 19:13:19 +03:00
|
|
|
/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
|
2012-05-21 15:12:37 +04: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 19:13:19 +03:00
|
|
|
|
|
|
|
/* functions for restoring saved values at the end of a C++ scope */
|
|
|
|
|
|
|
|
#ifndef mozilla_AutoRestore_h_
|
|
|
|
#define mozilla_AutoRestore_h_
|
|
|
|
|
2011-11-16 11:50:19 +04:00
|
|
|
#include "mozilla/GuardObjects.h"
|
2010-03-26 17:12:39 +03:00
|
|
|
|
2009-12-11 19:13:19 +03: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 10:19:26 +04:00
|
|
|
* AutoRestore<bool> savePainting(mIsPainting);
|
2011-10-17 18:59:28 +04:00
|
|
|
* mIsPainting = true;
|
2009-12-11 19:13:19 +03:00
|
|
|
*
|
|
|
|
* // ... your code here ...
|
|
|
|
*
|
|
|
|
* // mIsPainting is reset to its old value at the end of this block
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
template <class T>
|
2010-04-04 22:15:18 +04:00
|
|
|
class NS_STACK_CLASS AutoRestore
|
2009-12-11 19:13:19 +03:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
T& mLocation;
|
|
|
|
T mValue;
|
2012-01-10 09:29:30 +04:00
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
2009-12-11 19:13:19 +03:00
|
|
|
public:
|
2012-01-10 09:29:30 +04:00
|
|
|
AutoRestore(T& aValue MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2010-04-04 22:15:18 +04:00
|
|
|
: mLocation(aValue), mValue(aValue)
|
|
|
|
{
|
2012-01-10 09:29:30 +04:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
2010-04-04 22:15:18 +04:00
|
|
|
}
|
2009-12-11 19:13:19 +03:00
|
|
|
~AutoRestore() { mLocation = mValue; }
|
|
|
|
};
|
|
|
|
|
2011-11-16 11:50:19 +04:00
|
|
|
} // namespace mozilla
|
2009-12-11 19:13:19 +03:00
|
|
|
|
|
|
|
#endif /* !defined(mozilla_AutoRestore_h_) */
|