2014-06-30 15:39:45 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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 {
|
|
|
|
|
2014-06-27 01:35:39 +00: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>
|
2015-09-03 16:15:23 +00:00
|
|
|
class MOZ_RAII AutoRestore
|
2014-06-27 01:35:39 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
T& mLocation;
|
|
|
|
T mValue;
|
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
|
|
|
public:
|
2014-07-28 17:19:06 +00:00
|
|
|
explicit AutoRestore(T& aValue MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2014-06-27 01:35:39 +00:00
|
|
|
: mLocation(aValue)
|
|
|
|
, mValue(aValue)
|
2009-12-11 16:13:19 +00:00
|
|
|
{
|
2014-06-27 01:35:39 +00:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
|
|
|
~AutoRestore()
|
|
|
|
{
|
|
|
|
mLocation = mValue;
|
|
|
|
}
|
2016-05-14 01:47:14 +00:00
|
|
|
T SavedValue() const
|
|
|
|
{
|
|
|
|
return mValue;
|
|
|
|
}
|
2014-06-27 01:35:39 +00:00
|
|
|
};
|
2009-12-11 16:13:19 +00:00
|
|
|
|
2011-11-16 07:50:19 +00:00
|
|
|
} // namespace mozilla
|
2009-12-11 16:13:19 +00:00
|
|
|
|
|
|
|
#endif /* !defined(mozilla_AutoRestore_h_) */
|