mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[wpiutil] Add wpi::scope_exit (#4432)
This is based on std::scope_exit in the C++ library fundamentals TS v3.
This commit is contained in:
36
wpiutil/src/test/native/cpp/ScopeExitTest.cpp
Normal file
36
wpiutil/src/test/native/cpp/ScopeExitTest.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "wpi/scope"
|
||||
|
||||
TEST(ScopeExitTest, ScopeExit) {
|
||||
int exitCount = 0;
|
||||
|
||||
{
|
||||
wpi::scope_exit exit{[&] { ++exitCount; }};
|
||||
|
||||
EXPECT_EQ(0, exitCount);
|
||||
}
|
||||
|
||||
EXPECT_EQ(1, exitCount);
|
||||
}
|
||||
|
||||
TEST(ScopeExitTest, Release) {
|
||||
int exitCount = 0;
|
||||
|
||||
{
|
||||
wpi::scope_exit exit1{[&] { ++exitCount; }};
|
||||
wpi::scope_exit exit2 = std::move(exit1);
|
||||
wpi::scope_exit exit3 = std::move(exit1);
|
||||
EXPECT_EQ(0, exitCount);
|
||||
}
|
||||
EXPECT_EQ(1, exitCount);
|
||||
|
||||
{
|
||||
wpi::scope_exit exit{[&] { ++exitCount; }};
|
||||
exit.release();
|
||||
}
|
||||
EXPECT_EQ(1, exitCount);
|
||||
}
|
||||
Reference in New Issue
Block a user