Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
[email protected] | 5be7da24 | 2009-11-20 23:16:26 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_AUTO_RESET_H_ |
| 6 | #define BASE_AUTO_RESET_H_ |
| 7 | |
Sam McNally | 54bc028 | 2017-12-13 02:42:29 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
Peter Kasting | 0bca80f | 2023-03-07 04:37:55 | [diff] [blame] | 10 | #include "base/check_op.h" |
Keishi Hattori | 488b760 | 2022-05-02 13:09:31 | [diff] [blame] | 11 | #include "base/memory/raw_ptr_exclusion.h" |
| 12 | |
[email protected] | 997ec9f | 2012-11-21 04:44:14 | [diff] [blame] | 13 | // base::AutoReset<> is useful for setting a variable to a new value only within |
| 14 | // a particular scope. An base::AutoReset<> object resets a variable to its |
| 15 | // original value upon destruction, making it an alternative to writing |
| 16 | // "var = false;" or "var = old_val;" at all of a block's exit points. |
[email protected] | 5be7da24 | 2009-11-20 23:16:26 | [diff] [blame] | 17 | // |
[email protected] | 997ec9f | 2012-11-21 04:44:14 | [diff] [blame] | 18 | // This should be obvious, but note that an base::AutoReset<> instance should |
| 19 | // have a shorter lifetime than its scoped_variable, to prevent invalid memory |
| 20 | // writes when the base::AutoReset<> object is destroyed. |
| 21 | |
| 22 | namespace base { |
[email protected] | 5be7da24 | 2009-11-20 23:16:26 | [diff] [blame] | 23 | |
Jan Wilken Dörrie | bcc1259 | 2019-10-11 18:26:53 | [diff] [blame] | 24 | template <typename T> |
Peter Kasting | b0bac5d | 2023-03-08 21:26:56 | [diff] [blame] | 25 | class [[maybe_unused, nodiscard]] AutoReset { |
[email protected] | 5be7da24 | 2009-11-20 23:16:26 | [diff] [blame] | 26 | public: |
Jan Wilken Dörrie | bcc1259 | 2019-10-11 18:26:53 | [diff] [blame] | 27 | template <typename U> |
| 28 | AutoReset(T* scoped_variable, U&& new_value) |
[email protected] | 5be7da24 | 2009-11-20 23:16:26 | [diff] [blame] | 29 | : scoped_variable_(scoped_variable), |
Jan Wilken Dörrie | bcc1259 | 2019-10-11 18:26:53 | [diff] [blame] | 30 | original_value_( |
| 31 | std::exchange(*scoped_variable_, std::forward<U>(new_value))) {} |
[email protected] | 0fbd7033 | 2010-06-01 19:28:34 | [diff] [blame] | 32 | |
Peter Kasting | 0bca80f | 2023-03-07 04:37:55 | [diff] [blame] | 33 | // A constructor that's useful for asserting the old value of |
| 34 | // `scoped_variable`, especially when it's inconvenient to check this before |
| 35 | // constructing the AutoReset object (e.g. in a class member initializer |
| 36 | // list). |
| 37 | template <typename U> |
| 38 | AutoReset(T* scoped_variable, U&& new_value, const T& expected_old_value) |
| 39 | : AutoReset(scoped_variable, new_value) { |
| 40 | DCHECK_EQ(original_value_, expected_old_value); |
| 41 | } |
| 42 | |
Ahmed Fakhry | 3571b4f5 | 2019-05-15 20:55:49 | [diff] [blame] | 43 | AutoReset(AutoReset&& other) |
Jan Wilken Dörrie | bcc1259 | 2019-10-11 18:26:53 | [diff] [blame] | 44 | : scoped_variable_(std::exchange(other.scoped_variable_, nullptr)), |
| 45 | original_value_(std::move(other.original_value_)) {} |
| 46 | |
| 47 | AutoReset& operator=(AutoReset&& rhs) { |
| 48 | scoped_variable_ = std::exchange(rhs.scoped_variable_, nullptr); |
| 49 | original_value_ = std::move(rhs.original_value_); |
| 50 | return *this; |
Ahmed Fakhry | 3571b4f5 | 2019-05-15 20:55:49 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | ~AutoReset() { |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 54 | if (scoped_variable_) { |
Ahmed Fakhry | 3571b4f5 | 2019-05-15 20:55:49 | [diff] [blame] | 55 | *scoped_variable_ = std::move(original_value_); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 56 | } |
Ahmed Fakhry | 3571b4f5 | 2019-05-15 20:55:49 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | 5be7da24 | 2009-11-20 23:16:26 | [diff] [blame] | 59 | private: |
Lukasz Anforowicz | 877e622 | 2021-11-26 00:03:23 | [diff] [blame] | 60 | // `scoped_variable_` is not a raw_ptr<T> for performance reasons: Large |
| 61 | // number of non-PartitionAlloc pointees + AutoReset is typically short-lived |
| 62 | // (e.g. allocated on the stack). |
Keishi Hattori | 488b760 | 2022-05-02 13:09:31 | [diff] [blame] | 63 | RAW_PTR_EXCLUSION T* scoped_variable_; |
Lukasz Anforowicz | 877e622 | 2021-11-26 00:03:23 | [diff] [blame] | 64 | |
[email protected] | 0fbd7033 | 2010-06-01 19:28:34 | [diff] [blame] | 65 | T original_value_; |
[email protected] | 5be7da24 | 2009-11-20 23:16:26 | [diff] [blame] | 66 | }; |
| 67 | |
danakj | c3762b9 | 2015-03-07 01:51:42 | [diff] [blame] | 68 | } // namespace base |
[email protected] | 997ec9f | 2012-11-21 04:44:14 | [diff] [blame] | 69 | |
[email protected] | 5be7da24 | 2009-11-20 23:16:26 | [diff] [blame] | 70 | #endif // BASE_AUTO_RESET_H_ |