Sleipnir C++ API
Loading...
Searching...
No Matches
scope_exit.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <utility>
6
7namespace slp {
8
15template <typename F>
17 public:
23 explicit scope_exit(F&& f) noexcept : m_f{std::forward<F>(f)} {}
24
25 ~scope_exit() {
26 if (m_active) {
27 m_f();
28 }
29 }
30
37 : m_f{std::move(rhs.m_f)}, m_active{rhs.m_active} {
38 rhs.release();
39 }
40
41 scope_exit(const scope_exit&) = delete;
42 scope_exit& operator=(const scope_exit&) = delete;
43
47 void release() noexcept { m_active = false; }
48
49 private:
50 F m_f;
51 bool m_active = true;
52};
53
54} // namespace slp
Definition intrusive_shared_ptr.hpp:29
Definition scope_exit.hpp:16
void release() noexcept
Definition scope_exit.hpp:47
scope_exit(F &&f) noexcept
Definition scope_exit.hpp:23
scope_exit(scope_exit &&rhs) noexcept
Definition scope_exit.hpp:36