mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-28 02:11:43 +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:
37
wpiutil/src/main/native/include/wpi/scope
Normal file
37
wpiutil/src/main/native/include/wpi/scope
Normal file
@@ -0,0 +1,37 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace wpi {
|
||||
|
||||
template <typename F>
|
||||
class scope_exit {
|
||||
public:
|
||||
explicit scope_exit(F&& f) noexcept : m_f{std::forward<F>(f)} {}
|
||||
|
||||
~scope_exit() {
|
||||
if (m_active) {
|
||||
m_f();
|
||||
}
|
||||
}
|
||||
|
||||
scope_exit(scope_exit&& rhs) noexcept
|
||||
: m_f{std::move(rhs.m_f)}, m_active{rhs.m_active} {
|
||||
rhs.release();
|
||||
}
|
||||
|
||||
scope_exit(const scope_exit&) = delete;
|
||||
scope_exit& operator=(const scope_exit&) = delete;
|
||||
|
||||
void release() noexcept { m_active = false; }
|
||||
|
||||
private:
|
||||
F m_f;
|
||||
bool m_active = true;
|
||||
};
|
||||
|
||||
} // namespace wpi
|
||||
Reference in New Issue
Block a user