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
13template <typename F>
15 public:
19 explicit scope_exit(F&& f) noexcept : m_f{std::forward<F>(f)} {}
20
21 ~scope_exit() {
22 if (m_active) {
23 m_f();
24 }
25 }
26
31 : m_f{std::move(rhs.m_f)}, m_active{rhs.m_active} {
32 rhs.release();
33 }
34
35 scope_exit(const scope_exit&) = delete;
36 scope_exit& operator=(const scope_exit&) = delete;
37
39 void release() noexcept { m_active = false; }
40
41 private:
42 F m_f;
43 bool m_active = true;
44};
45
46} // namespace slp
Definition intrusive_shared_ptr.hpp:27
Definition scope_exit.hpp:14
void release() noexcept
Makes the scope_exit inactive.
Definition scope_exit.hpp:39
scope_exit(F &&f) noexcept
Definition scope_exit.hpp:19
scope_exit(scope_exit &&rhs) noexcept
Definition scope_exit.hpp:30