Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef SRC_TINT_UTILS_DEFER_H_ |
| 16 | #define SRC_TINT_UTILS_DEFER_H_ |
| 17 | |
| 18 | #include <utility> |
| 19 | |
| 20 | #include "src/tint/utils/concat.h" |
| 21 | |
dan sinclair | fe4bfd4 | 2022-04-07 16:55:55 +0000 | [diff] [blame] | 22 | namespace tint::utils { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 23 | |
| 24 | /// Defer executes a function or function like object when it is destructed. |
| 25 | template <typename F> |
| 26 | class Defer { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 27 | public: |
| 28 | /// Constructor |
| 29 | /// @param f the function to call when the Defer is destructed |
| 30 | explicit Defer(F&& f) : f_(std::move(f)) {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 31 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 32 | /// Move constructor |
| 33 | Defer(Defer&&) = default; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 34 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 35 | /// Destructor |
| 36 | /// Calls the deferred function |
| 37 | ~Defer() { f_(); } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 38 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 39 | private: |
| 40 | Defer(const Defer&) = delete; |
| 41 | Defer& operator=(const Defer&) = delete; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 42 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 43 | F f_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | /// Constructor |
| 47 | /// @param f the function to call when the Defer is destructed |
| 48 | template <typename F> |
| 49 | inline Defer<F> MakeDefer(F&& f) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 50 | return Defer<F>(std::forward<F>(f)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 51 | } |
| 52 | |
dan sinclair | fe4bfd4 | 2022-04-07 16:55:55 +0000 | [diff] [blame] | 53 | } // namespace tint::utils |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 54 | |
| 55 | /// TINT_DEFER(S) executes the statement(s) `S` when exiting the current lexical |
| 56 | /// scope. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 57 | #define TINT_DEFER(S) \ |
| 58 | auto TINT_CONCAT(tint_defer_, __COUNTER__) = ::tint::utils::MakeDefer([&] { S; }) |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 59 | |
| 60 | #endif // SRC_TINT_UTILS_DEFER_H_ |