1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2011 2 // Google Inc. All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the name Chromium Embedded 15 // Framework nor the names of its contributors may be used to endorse 16 // or promote products derived from this software without specific prior 17 // written permission. 18 // 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 // base::AutoReset<> is useful for setting a variable to a new value only within 32 // a particular scope. An base::AutoReset<> object resets a variable to its 33 // original value upon destruction, making it an alternative to writing 34 // "var = false;" or "var = old_val;" at all of a block's exit points. 35 // 36 // This should be obvious, but note that an base::AutoReset<> instance should 37 // have a shorter lifetime than its scoped_variable, to prevent invalid memory 38 // writes when the base::AutoReset<> object is destroyed. 39 40 #ifndef CEF_INCLUDE_BASE_CEF_AUTO_RESET_H_ 41 #define CEF_INCLUDE_BASE_CEF_AUTO_RESET_H_ 42 #pragma once 43 44 #if defined(USING_CHROMIUM_INCLUDES) 45 // When building CEF include the Chromium header directly. 46 #include "base/auto_reset.h" 47 #else // !USING_CHROMIUM_INCLUDES 48 // The following is substantially similar to the Chromium implementation. 49 // If the Chromium implementation diverges the below implementation should be 50 // updated to match. 51 52 #include <utility> 53 54 namespace base { 55 56 template <typename T> 57 class AutoReset { 58 public: 59 template <typename U> AutoReset(T * scoped_variable,U && new_value)60 AutoReset(T* scoped_variable, U&& new_value) 61 : scoped_variable_(scoped_variable), 62 original_value_( 63 std::exchange(*scoped_variable_, std::forward<U>(new_value))) {} 64 AutoReset(AutoReset && other)65 AutoReset(AutoReset&& other) 66 : scoped_variable_(std::exchange(other.scoped_variable_, nullptr)), 67 original_value_(std::move(other.original_value_)) {} 68 69 AutoReset& operator=(AutoReset&& rhs) { 70 scoped_variable_ = std::exchange(rhs.scoped_variable_, nullptr); 71 original_value_ = std::move(rhs.original_value_); 72 return *this; 73 } 74 ~AutoReset()75 ~AutoReset() { 76 if (scoped_variable_) 77 *scoped_variable_ = std::move(original_value_); 78 } 79 80 private: 81 T* scoped_variable_; 82 T original_value_; 83 }; 84 85 } // namespace base 86 87 #endif // !USING_CHROMIUM_INCLUDES 88 89 #endif // CEF_INCLUDE_BASE_CEF_AUTO_RESET_H_ 90