blob: f073fb9dd444be89073eb1f9406759fbc58d0ec1 [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// 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 sinclairfe4bfd42022-04-07 16:55:55 +000022namespace tint::utils {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000023
24/// Defer executes a function or function like object when it is destructed.
25template <typename F>
26class Defer {
dan sinclair41e4d9a2022-05-01 14:40:55 +000027 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 Harrisondbc13af2022-02-21 15:19:07 +000031
dan sinclair41e4d9a2022-05-01 14:40:55 +000032 /// Move constructor
33 Defer(Defer&&) = default;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000034
dan sinclair41e4d9a2022-05-01 14:40:55 +000035 /// Destructor
36 /// Calls the deferred function
37 ~Defer() { f_(); }
Ryan Harrisondbc13af2022-02-21 15:19:07 +000038
dan sinclair41e4d9a2022-05-01 14:40:55 +000039 private:
40 Defer(const Defer&) = delete;
41 Defer& operator=(const Defer&) = delete;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000042
dan sinclair41e4d9a2022-05-01 14:40:55 +000043 F f_;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000044};
45
46/// Constructor
47/// @param f the function to call when the Defer is destructed
48template <typename F>
49inline Defer<F> MakeDefer(F&& f) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000050 return Defer<F>(std::forward<F>(f));
Ryan Harrisondbc13af2022-02-21 15:19:07 +000051}
52
dan sinclairfe4bfd42022-04-07 16:55:55 +000053} // namespace tint::utils
Ryan Harrisondbc13af2022-02-21 15:19:07 +000054
55/// TINT_DEFER(S) executes the statement(s) `S` when exiting the current lexical
56/// scope.
dan sinclair41e4d9a2022-05-01 14:40:55 +000057#define TINT_DEFER(S) \
58 auto TINT_CONCAT(tint_defer_, __COUNTER__) = ::tint::utils::MakeDefer([&] { S; })
Ryan Harrisondbc13af2022-02-21 15:19:07 +000059
60#endif // SRC_TINT_UTILS_DEFER_H_