[utils]: Add TINT_DEFER()
Runs the statement(s) at the end of the lexical scope Change-Id: I74de02b7204b56016c7bbe71fee4ece498d3570d Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/51923 Kokoro: Kokoro <noreply+kokoro@google.com> Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
parent
2f258d1bf8
commit
1e153613eb
|
@ -666,6 +666,7 @@ if(${TINT_BUILD_TESTS})
|
|||
sem/type_manager_test.cc
|
||||
sem/u32_type_test.cc
|
||||
sem/vector_type_test.cc
|
||||
utils/defer_test.cc
|
||||
utils/enum_set_test.cc
|
||||
utils/get_or_create_test.cc
|
||||
utils/hash_test.cc
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
|
||||
// Copyright 2021 The Tint Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef SRC_UTILS_CONCAT_H_
|
||||
#define SRC_UTILS_CONCAT_H_
|
||||
|
||||
#define TINT_CONCAT_2(a, b) a##b
|
||||
#define TINT_CONCAT(a, b) TINT_CONCAT_2(a, b)
|
||||
|
||||
#endif // SRC_UTILS_CONCAT_H_
|
|
@ -0,0 +1,63 @@
|
|||
// Copyright 2021 The Tint Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef SRC_UTILS_DEFER_H_
|
||||
#define SRC_UTILS_DEFER_H_
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "src/utils/concat.h"
|
||||
|
||||
namespace tint {
|
||||
namespace utils {
|
||||
|
||||
/// Defer executes a function or function like object when it is destructed.
|
||||
template <typename F>
|
||||
class Defer {
|
||||
public:
|
||||
/// Constructor
|
||||
/// @param f the function to call when the Defer is destructed
|
||||
explicit Defer(F&& f) : f_(std::move(f)) {}
|
||||
|
||||
/// Move constructor
|
||||
Defer(Defer&&) = default;
|
||||
|
||||
/// Destructor
|
||||
/// Calls the deferred function
|
||||
~Defer() { f_(); }
|
||||
|
||||
private:
|
||||
Defer(const Defer&) = delete;
|
||||
Defer& operator=(const Defer&) = delete;
|
||||
|
||||
F f_;
|
||||
};
|
||||
|
||||
/// Constructor
|
||||
/// @param f the function to call when the Defer is destructed
|
||||
template <typename F>
|
||||
inline Defer<F> MakeDefer(F&& f) {
|
||||
return Defer<F>(std::forward<F>(f));
|
||||
}
|
||||
|
||||
} // namespace utils
|
||||
} // namespace tint
|
||||
|
||||
/// TINT_DEFER(S) executes the statement(s) `S` when exiting the current lexical
|
||||
/// scope.
|
||||
#define TINT_DEFER(S) \
|
||||
auto TINT_CONCAT(tint_defer_, __COUNTER__) = \
|
||||
::tint::utils::MakeDefer([&] { S; })
|
||||
|
||||
#endif // SRC_UTILS_DEFER_H_
|
|
@ -0,0 +1,44 @@
|
|||
// Copyright 2021 The Tint Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "src/utils/defer.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace tint {
|
||||
namespace utils {
|
||||
namespace {
|
||||
|
||||
TEST(DeferTest, Basic) {
|
||||
bool deferCalled = false;
|
||||
{ TINT_DEFER(deferCalled = true); }
|
||||
ASSERT_TRUE(deferCalled);
|
||||
}
|
||||
|
||||
TEST(DeferTest, DeferOrder) {
|
||||
int counter = 0;
|
||||
int a = 0, b = 0, c = 0;
|
||||
{
|
||||
TINT_DEFER(a = ++counter);
|
||||
TINT_DEFER(b = ++counter);
|
||||
TINT_DEFER(c = ++counter);
|
||||
}
|
||||
ASSERT_EQ(a, 3);
|
||||
ASSERT_EQ(b, 2);
|
||||
ASSERT_EQ(c, 1);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace utils
|
||||
} // namespace tint
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
// Copyright 2021 The Tint Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
@ -17,6 +18,8 @@
|
|||
|
||||
#include <type_traits>
|
||||
|
||||
#include "src/utils/concat.h"
|
||||
|
||||
namespace tint {
|
||||
namespace utils {
|
||||
|
||||
|
@ -50,9 +53,6 @@ class ScopedAssignment {
|
|||
} // namespace utils
|
||||
} // namespace tint
|
||||
|
||||
#define TINT_CONCAT_2(a, b) a##b
|
||||
#define TINT_CONCAT(a, b) TINT_CONCAT_2(a, b)
|
||||
|
||||
/// TINT_SCOPED_ASSIGNMENT(var, val) assigns `val` to `var`, and automatically
|
||||
/// restores the original value of `var` when exiting the current lexical scope.
|
||||
#define TINT_SCOPED_ASSIGNMENT(var, val) \
|
||||
|
|
|
@ -299,6 +299,7 @@ tint_unittests_source_set("tint_unittests_core_src") {
|
|||
"../src/transform/vertex_pulling_test.cc",
|
||||
"../src/transform/wrap_arrays_in_structs_test.cc",
|
||||
"../src/transform/zero_init_workgroup_memory_test.cc",
|
||||
"../src/utils/defer_test.cc",
|
||||
"../src/utils/enum_set_test.cc",
|
||||
"../src/utils/get_or_create_test.cc",
|
||||
"../src/utils/hash_test.cc",
|
||||
|
|
Loading…
Reference in New Issue