OpenVPN 3 Core Library
Loading...
Searching...
No Matches
cleanup.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#ifndef OPENVPN_COMMON_CLEANUP_H
13#define OPENVPN_COMMON_CLEANUP_H
14
15#include <utility>
16#include <optional>
17#include <concepts>
18#include <functional>
19#include <type_traits>
20
21namespace openvpn {
22
33template <typename F>
34 requires std::invocable<F>
36{
37 public:
38 explicit CleanupType(F method) noexcept(std::is_nothrow_move_constructible_v<F>)
39 : clean(std::move(method))
40 {
41 }
42 CleanupType(const CleanupType &) = delete;
43 CleanupType &operator=(const CleanupType &) = delete;
44 CleanupType(CleanupType &&other) noexcept
45 : clean(std::exchange(other.clean, std::nullopt))
46 {
47 }
49 {
50 if (this != &other)
51 {
52 clean = std::exchange(other.clean, std::nullopt);
53 }
54 return *this;
55 }
56
64 ~CleanupType() noexcept
65 {
66 if (clean)
67 {
68 try
69 {
70 std::invoke(*clean);
71 }
72 catch (...)
73 {
74 // swallow exceptions to avoid throwing from destructor
75 }
76 }
77 }
78
79 public:
85 void dismiss() noexcept
86 {
87 clean.reset();
88 }
99 std::optional<std::function<void()>> release() noexcept
100 {
101 if (clean)
102 {
103 std::function<void()> func = std::move(*clean);
104 clean.reset();
105 return func;
106 }
107 return std::nullopt;
108 }
109
110 private:
111 std::optional<F> clean;
112};
113
122template <typename F>
123 requires std::invocable<F>
124inline CleanupType<F> Cleanup(F method) noexcept(std::is_nothrow_move_constructible_v<F>)
125{
126 return CleanupType<F>(std::move(method));
127}
128
129} // namespace openvpn
130
131#endif
A scope guard that runs a callable on destruction unless dismissed or released.
Definition cleanup.hpp:36
CleanupType & operator=(const CleanupType &)=delete
CleanupType(F method) noexcept(std::is_nothrow_move_constructible_v< F >)
Definition cleanup.hpp:38
std::optional< std::function< void()> > release() noexcept
Release the cleanup action, returning the callable and preventing it from being executed on destructi...
Definition cleanup.hpp:99
void dismiss() noexcept
Dismiss the cleanup action, preventing it from being executed on destruction.
Definition cleanup.hpp:85
std::optional< F > clean
Definition cleanup.hpp:111
CleanupType(const CleanupType &)=delete
~CleanupType() noexcept
Destructor that executes the cleanup action if not dismissed or released.
Definition cleanup.hpp:64
CleanupType & operator=(CleanupType &&other) noexcept
Definition cleanup.hpp:48
CleanupType(CleanupType &&other) noexcept
Definition cleanup.hpp:44
CleanupType< F > Cleanup(F method) noexcept(std::is_nothrow_move_constructible_v< F >)
Factory function to create a CleanupType object.
Definition cleanup.hpp:124