Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | |
| 2 | // Copyright 2021 The Tint Authors. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 16 | #ifndef SRC_TINT_UTILS_MACROS_SCOPED_ASSIGNMENT_H_ |
| 17 | #define SRC_TINT_UTILS_MACROS_SCOPED_ASSIGNMENT_H_ |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 18 | |
| 19 | #include <type_traits> |
| 20 | |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 21 | #include "src/tint/utils/macros/concat.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 22 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 23 | namespace tint { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 24 | |
| 25 | /// Helper class that temporarily assigns a value to a variable for the lifetime |
| 26 | /// of the ScopedAssignment object. Once the ScopedAssignment is destructed, the |
| 27 | /// original value is restored. |
| 28 | template <typename T> |
| 29 | class ScopedAssignment { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 30 | public: |
| 31 | /// Constructor |
| 32 | /// @param var the variable to temporarily assign a new value to |
| 33 | /// @param val the value to assign to `ref` for the lifetime of this |
| 34 | /// ScopedAssignment. |
| 35 | ScopedAssignment(T& var, T val) : ref_(var) { |
| 36 | old_value_ = var; |
| 37 | var = val; |
| 38 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 39 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 40 | /// Destructor |
| 41 | /// Restores the original value of the variable. |
| 42 | ~ScopedAssignment() { ref_ = old_value_; } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 43 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 44 | private: |
| 45 | ScopedAssignment(const ScopedAssignment&) = delete; |
| 46 | ScopedAssignment& operator=(const ScopedAssignment&) = delete; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 47 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 48 | T& ref_; |
| 49 | T old_value_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 52 | } // namespace tint |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 53 | |
| 54 | /// TINT_SCOPED_ASSIGNMENT(var, val) assigns `val` to `var`, and automatically |
| 55 | /// restores the original value of `var` when exiting the current lexical scope. |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 56 | #define TINT_SCOPED_ASSIGNMENT(var, val) \ |
| 57 | ::tint::ScopedAssignment<std::remove_reference_t<decltype(var)>> TINT_CONCAT( \ |
| 58 | tint_scoped_assignment_, __COUNTER__) { \ |
| 59 | var, val \ |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 60 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 61 | |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 62 | #endif // SRC_TINT_UTILS_MACROS_SCOPED_ASSIGNMENT_H_ |