summaryrefslogtreecommitdiffstats
path: root/base/auto_reset.h
diff options
context:
space:
mode:
authorpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-20 23:16:26 +0000
committerpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-20 23:16:26 +0000
commit5be7da24155466bf0e9bf2e5fda1a581e549df8d (patch)
treee14153b1254d3eacaaa65a9997d7edf9775d4036 /base/auto_reset.h
parent8a92555e667090d8c6592ebe670628ca33b86ef0 (diff)
downloadchromium_src-5be7da24155466bf0e9bf2e5fda1a581e549df8d.zip
chromium_src-5be7da24155466bf0e9bf2e5fda1a581e549df8d.tar.gz
chromium_src-5be7da24155466bf0e9bf2e5fda1a581e549df8d.tar.bz2
Use AutoReset (formerly ScopedBool) where possible.
This frequently saves a tiny bit of code, but even when it doesn't I think it's more future-proof (less error-prone). BUG=none TEST=none Review URL: http://codereview.chromium.org/399096 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@32708 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/auto_reset.h')
-rw-r--r--base/auto_reset.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/base/auto_reset.h b/base/auto_reset.h
new file mode 100644
index 0000000..dd968ef
--- /dev/null
+++ b/base/auto_reset.h
@@ -0,0 +1,35 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_AUTO_RESET_H_
+#define BASE_AUTO_RESET_H_
+
+#include "base/basictypes.h"
+
+// AutoReset is useful for setting a variable to some value only during a
+// particular scope. If you have code that has to add "var = false;" or
+// "var = old_var;" at all the exit points of a block, for example, you would
+// benefit from using this instead.
+//
+// NOTE: Right now this is hardcoded to work on bools, since that covers all the
+// cases where we've used it. It would be reasonable to turn it into a template
+// class in the future.
+
+class AutoReset {
+ public:
+ explicit AutoReset(bool* scoped_variable, bool new_value)
+ : scoped_variable_(scoped_variable),
+ original_value_(*scoped_variable) {
+ *scoped_variable_ = new_value;
+ }
+ ~AutoReset() { *scoped_variable_ = original_value_; }
+
+ private:
+ bool* scoped_variable_;
+ bool original_value_;
+
+ DISALLOW_COPY_AND_ASSIGN(AutoReset);
+};
+
+#endif // BASE_AUTO_RESET_H_