1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_AUTO_RESET_H_ 6*635a8641SAndroid Build Coastguard Worker #define BASE_AUTO_RESET_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include <utility> 9*635a8641SAndroid Build Coastguard Worker 10*635a8641SAndroid Build Coastguard Worker #include "base/macros.h" 11*635a8641SAndroid Build Coastguard Worker 12*635a8641SAndroid Build Coastguard Worker // base::AutoReset<> is useful for setting a variable to a new value only within 13*635a8641SAndroid Build Coastguard Worker // a particular scope. An base::AutoReset<> object resets a variable to its 14*635a8641SAndroid Build Coastguard Worker // original value upon destruction, making it an alternative to writing 15*635a8641SAndroid Build Coastguard Worker // "var = false;" or "var = old_val;" at all of a block's exit points. 16*635a8641SAndroid Build Coastguard Worker // 17*635a8641SAndroid Build Coastguard Worker // This should be obvious, but note that an base::AutoReset<> instance should 18*635a8641SAndroid Build Coastguard Worker // have a shorter lifetime than its scoped_variable, to prevent invalid memory 19*635a8641SAndroid Build Coastguard Worker // writes when the base::AutoReset<> object is destroyed. 20*635a8641SAndroid Build Coastguard Worker 21*635a8641SAndroid Build Coastguard Worker namespace base { 22*635a8641SAndroid Build Coastguard Worker 23*635a8641SAndroid Build Coastguard Worker template<typename T> 24*635a8641SAndroid Build Coastguard Worker class AutoReset { 25*635a8641SAndroid Build Coastguard Worker public: AutoReset(T * scoped_variable,T new_value)26*635a8641SAndroid Build Coastguard Worker AutoReset(T* scoped_variable, T new_value) 27*635a8641SAndroid Build Coastguard Worker : scoped_variable_(scoped_variable), 28*635a8641SAndroid Build Coastguard Worker original_value_(std::move(*scoped_variable)) { 29*635a8641SAndroid Build Coastguard Worker *scoped_variable_ = std::move(new_value); 30*635a8641SAndroid Build Coastguard Worker } 31*635a8641SAndroid Build Coastguard Worker ~AutoReset()32*635a8641SAndroid Build Coastguard Worker ~AutoReset() { *scoped_variable_ = std::move(original_value_); } 33*635a8641SAndroid Build Coastguard Worker 34*635a8641SAndroid Build Coastguard Worker private: 35*635a8641SAndroid Build Coastguard Worker T* scoped_variable_; 36*635a8641SAndroid Build Coastguard Worker T original_value_; 37*635a8641SAndroid Build Coastguard Worker 38*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(AutoReset); 39*635a8641SAndroid Build Coastguard Worker }; 40*635a8641SAndroid Build Coastguard Worker 41*635a8641SAndroid Build Coastguard Worker } // namespace base 42*635a8641SAndroid Build Coastguard Worker 43*635a8641SAndroid Build Coastguard Worker #endif // BASE_AUTO_RESET_H_ 44