1c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Copyright (c) 2010 The Chromium Authors. All rights reserved. 2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be 3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file. 4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifndef BASE_AUTO_RESET_H_ 6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#define BASE_AUTO_RESET_H_ 73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once 8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/basictypes.h" 10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// AutoResetValue is useful for setting a variable to some value only 12c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// during a particular scope. If you have code that has to add "var = 13c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// false;" or "var = old_var;" at all the exit points of a block, for 14c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// example, you would benefit from using this instead. 15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 16c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// This should be obvious, but note that the AutoResetValue instance 17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// should have a shorter lifetime than the scoped_variable, to prevent 18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// writing to invalid memory when the AutoResetValue goes out of 19c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// scope. 20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochtemplate<typename T> 22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass AutoReset { 23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public: 24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch AutoReset(T* scoped_variable, T new_value) 25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott : scoped_variable_(scoped_variable), 26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott original_value_(*scoped_variable) { 27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott *scoped_variable_ = new_value; 28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ~AutoReset() { *scoped_variable_ = original_value_; } 31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private: 33c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch T* scoped_variable_; 34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch T original_value_; 35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott DISALLOW_COPY_AND_ASSIGN(AutoReset); 37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif // BASE_AUTO_RESET_H_ 40