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>
58
59 void
60 release() noexcept
61 {
63 }
64};
65
66template <class EF>
68
69template <class EF>
112
113template <class EF>
115
116template <class EF>
118{
122
123public:
129
138
141
142 template <class EFP>
143 explicit ScopeSuccess(
144 EFP&& f,
148 0) noexcept(std::is_nothrow_constructible_v<EF, EFP> || std::is_nothrow_constructible_v<EF, EFP&>)
150 {
151 }
152
153 void
154 release() noexcept
155 {
156 executeOnDestruction_ = false;
157 }
158};
159
160template <class EF>
162
199
200template <class Mutex>
202{
204
205public:
206 explicit ScopeUnlock(std::unique_lock<Mutex>& lock) noexcept(true) : plock_(&lock)
207 {
208 XRPL_ASSERT(plock_->owns_lock(), "xrpl::ScopeUnlock::ScopeUnlock : mutex must be locked");
209 plock_->unlock();
210 }
211
212 // Immovable type
213 ScopeUnlock(ScopeUnlock const&) = delete;
215 operator=(ScopeUnlock const&) = delete;
216
217 ~ScopeUnlock() noexcept(true)
218 {
219 plock_->lock();
220 }
221};
222
223template <class Mutex>
225
226} // namespace xrpl
ScopeExit(EFP &&f, std::enable_if_t< !std::is_same_v< std::remove_cv_t< EFP >, ScopeExit > &&std::is_constructible_v< EF, EFP > > *=0) noexcept
Definition scope.h:49
bool executeOnDestruction_
Definition scope.h:28
EF exitFunction_
Definition scope.h:27
void release() noexcept
Definition scope.h:60
ScopeExit(ScopeExit &&rhs) noexcept(std::is_nothrow_move_constructible_v< EF >||std::is_nothrow_copy_constructible_v< EF >)
Definition scope.h:37
ScopeExit & operator=(ScopeExit &&)=delete
ScopeFail & operator=(ScopeFail &&)=delete
int uncaughtOnCreation_
Definition scope.h:74
ScopeFail(EFP &&f, std::enable_if_t< !std::is_same_v< std::remove_cv_t< EFP >, ScopeFail > &&std::is_constructible_v< EF, EFP > > *=0) noexcept
Definition scope.h:96
ScopeFail(ScopeFail &&rhs) noexcept(std::is_nothrow_move_constructible_v< EF >||std::is_nothrow_copy_constructible_v< EF >)
Definition scope.h:83
bool executeOnDestruction_
Definition scope.h:73
EF exitFunction_
Definition scope.h:72
void release() noexcept
Definition scope.h:107
ScopeSuccess & operator=(ScopeSuccess &&)=delete
void release() noexcept
Definition scope.h:154
bool executeOnDestruction_
Definition scope.h:120
ScopeSuccess(EFP &&f, std::enable_if_t< !std::is_same_v< std::remove_cv_t< EFP >, ScopeSuccess > &&std::is_constructible_v< EF, EFP > > *=0) noexcept(std::is_nothrow_constructible_v< EF, EFP >||std::is_nothrow_constructible_v< EF, EFP & >)
Definition scope.h:143
~ScopeSuccess() noexcept(noexcept(exitFunction_()))
Definition scope.h:124
int uncaughtOnCreation_
Definition scope.h:121
ScopeSuccess(ScopeSuccess &&rhs) noexcept(std::is_nothrow_move_constructible_v< EF >||std::is_nothrow_copy_constructible_v< EF >)
Definition scope.h:130
Automatically unlocks and re-locks a unique_lock object.
Definition scope.h:202
~ScopeUnlock() noexcept(true)
Definition scope.h:217
ScopeUnlock & operator=(ScopeUnlock const &)=delete
std::unique_lock< Mutex > * plock_
Definition scope.h:203
ScopeUnlock(ScopeUnlock const &)=delete
ScopeUnlock(std::unique_lock< Mutex > &lock) noexcept(true)
Definition scope.h:206
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)