xrpld
Loading...
Searching...
No Matches
scope.h
1#pragma once
2
3#include <xrpl/beast/utility/instrumentation.h>
4
5#include <exception>
6#include <mutex>
7#include <type_traits>
8#include <utility>
9
10namespace xrpl {
11
12// RAII scope helpers. As specified in Library Fundamental, Version 3
13// Basic design of idea: https://www.youtube.com/watch?v=WjTrfoiB0MQ
14// Specification:
15// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/n4873.html#scopeguard
16
17// This implementation deviates from the spec slightly:
18// The scope_exit and scope_fail constructors taking a functor are not
19// permitted to throw an exception. This was done because some compilers
20// did not like the superfluous try/catch in the common instantiations
21// where the construction was noexcept. Instead a static_assert is used
22// to enforce this restriction.
23
24template <class EF>
26{
29
30public:
36
44
46 operator=(ScopeExit&&) = delete;
47
48 template <class EFP>
49 explicit ScopeExit(EFP&& f) noexcept
50 requires(
52 : exitFunction_{std::forward<EFP>(f)}
53 {
55 }
56
57 void
58 release() noexcept
59 {
61 }
62};
63
64template <class EF>
66
67template <class EF>
69{
73
74public:
80
89
91 operator=(ScopeFail&&) = delete;
92
93 template <class EFP>
94 explicit ScopeFail(EFP&& f) noexcept
95 requires(
97 : exitFunction_{std::forward<EFP>(f)}
98 {
100 }
101
102 void
103 release() noexcept
104 {
105 executeOnDestruction_ = false;
106 }
107};
108
109template <class EF>
111
112template <class EF>
154
155template <class EF>
157
194
195template <class Mutex>
197{
199
200public:
201 explicit ScopeUnlock(std::unique_lock<Mutex>& lock) noexcept(true) : plock_(&lock)
202 {
203 XRPL_ASSERT(plock_->owns_lock(), "xrpl::ScopeUnlock::ScopeUnlock : mutex must be locked");
204 plock_->unlock();
205 }
206
207 // Immovable type
208 ScopeUnlock(ScopeUnlock const&) = delete;
210 operator=(ScopeUnlock const&) = delete;
211
212 ~ScopeUnlock() noexcept(true)
213 {
214 plock_->lock();
215 }
216};
217
218template <class Mutex>
220
221} // namespace xrpl
bool executeOnDestruction_
Definition scope.h:28
EF exitFunction_
Definition scope.h:27
void release() noexcept
Definition scope.h:58
ScopeExit(ScopeExit &&rhs) noexcept(std::is_nothrow_move_constructible_v< EF >||std::is_nothrow_copy_constructible_v< EF >)
Definition scope.h:37
ScopeExit(EFP &&f) noexcept
Definition scope.h:49
ScopeExit & operator=(ScopeExit &&)=delete
ScopeFail & operator=(ScopeFail &&)=delete
int uncaughtOnCreation_
Definition scope.h:72
ScopeFail(EFP &&f) noexcept
Definition scope.h:94
ScopeFail(ScopeFail &&rhs) noexcept(std::is_nothrow_move_constructible_v< EF >||std::is_nothrow_copy_constructible_v< EF >)
Definition scope.h:81
bool executeOnDestruction_
Definition scope.h:71
EF exitFunction_
Definition scope.h:70
void release() noexcept
Definition scope.h:103
ScopeSuccess & operator=(ScopeSuccess &&)=delete
ScopeSuccess(EFP &&f) noexcept(std::is_nothrow_constructible_v< EF, EFP >||std::is_nothrow_constructible_v< EF, EFP & >)
Definition scope.h:139
void release() noexcept
Definition scope.h:149
bool executeOnDestruction_
Definition scope.h:116
~ScopeSuccess() noexcept(noexcept(exitFunction_()))
Definition scope.h:120
int uncaughtOnCreation_
Definition scope.h:117
ScopeSuccess(ScopeSuccess &&rhs) noexcept(std::is_nothrow_move_constructible_v< EF >||std::is_nothrow_copy_constructible_v< EF >)
Definition scope.h:126
Automatically unlocks and re-locks a unique_lock object.
Definition scope.h:197
~ScopeUnlock() noexcept(true)
Definition scope.h:212
ScopeUnlock & operator=(ScopeUnlock const &)=delete
std::unique_lock< Mutex > * plock_
Definition scope.h:198
ScopeUnlock(ScopeUnlock const &)=delete
ScopeUnlock(std::unique_lock< Mutex > &lock) noexcept(true)
Definition scope.h:201
T forward(T... args)
T is_constructible_v
T is_nothrow_copy_constructible_v
T is_nothrow_move_constructible_v
T is_same_v
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
ScopeExit(EF) -> ScopeExit< EF >
ScopeUnlock(std::unique_lock< Mutex > &) -> ScopeUnlock< Mutex >
ScopeSuccess(EF) -> ScopeSuccess< EF >
ScopeFail(EF) -> ScopeFail< EF >
T uncaught_exceptions(T... args)