LeechCraft 0.6.70-14794-g33744ae6ce
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include <memory>
12#include <functional>
13#include <stdexcept>
14
15namespace LC
16{
17namespace Util
18{
19 namespace detail
20 {
21 using DefaultScopeGuardDeleter = std::function<void ()>;
22
23 class [[nodiscard]] SharedScopeGuard
24 {
25 std::shared_ptr<void> Guard_;
26 public:
27 template<typename F>
28 SharedScopeGuard (const F& f)
29 : Guard_ { nullptr, [f] (void*) { f (); } }
30 {
31 }
32
33 SharedScopeGuard () = delete;
34
37
38 SharedScopeGuard& operator= (const SharedScopeGuard&) = default;
39 SharedScopeGuard& operator= (SharedScopeGuard&&) = default;
40 };
41
42 template<typename F>
43 class [[nodiscard]] ScopeGuard
44 {
45 F F_;
46 bool Perform_ = true;
47 public:
48 ScopeGuard () noexcept
49 : F_ {}
50 , Perform_ { false }
51 {
52 }
53
54 ScopeGuard (const F& f) noexcept
55 : F_ { f }
56 {
57 }
58
59 ScopeGuard (const ScopeGuard&) = delete;
60 ScopeGuard& operator= (const ScopeGuard&) = delete;
61
62 ScopeGuard& operator= (ScopeGuard&& other)
63 {
64 if (Perform_)
65 F_ ();
66
67 F_ = other.F_;
68 Perform_ = other.Perform_;
69 other.Perform_ = false;
70 return *this;
71 }
72
73 ScopeGuard (ScopeGuard&& other) noexcept
74 : F_ { other.F_ }
75 , Perform_ { other.Perform_ }
76 {
77 other.Perform_ = false;
78 }
79
81 {
82 if (Perform_)
83 F_ ();
84 }
85
86 void Dismiss () noexcept
87 {
88 Perform_ = false;
89 }
90
92 {
93 Dismiss ();
95 }
96
98 {
99 return EraseType ();
100 }
101
103 {
104 if (!Perform_)
105 throw std::logic_error { "this scope guard has already been converted to a shared one" };
106
107 Perform_ = false;
108 return { F_ };
109 }
110 };
111 }
112
114
135 template<typename F>
136 [[nodiscard]] detail::ScopeGuard<F> MakeScopeGuard (const F& f)
137 {
138 return { f };
139 }
140}
141}
ScopeGuard() noexcept
Definition: util.h:48
SharedScopeGuard Shared()
Definition: util.h:102
ScopeGuard(const ScopeGuard &)=delete
ScopeGuard(ScopeGuard &&other) noexcept
Definition: util.h:73
ScopeGuard(const F &f) noexcept
Definition: util.h:54
void Dismiss() noexcept
Definition: util.h:86
ScopeGuard< DefaultScopeGuardDeleter > EraseType()
Definition: util.h:91
SharedScopeGuard(SharedScopeGuard &&)=default
SharedScopeGuard(const SharedScopeGuard &)=default
std::function< void()> DefaultScopeGuardDeleter
Definition: util.h:21
detail::ScopeGuard< F > MakeScopeGuard(const F &f)
Returns an object performing passed function on scope exit.
Definition: util.h:136
Definition: constants.h:15