rippled
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>
63
64template <class EF>
66
67template <class EF>
109
110template <class EF>
112
113template <class EF>
115{
119
120public:
126
135
138
139 template <class EFP>
141 EFP&& f,
143 0) noexcept(std::is_nothrow_constructible_v<EF, EFP> || std::is_nothrow_constructible_v<EF, EFP&>)
145 {
146 }
147
148 void
149 release() noexcept
150 {
152 }
153};
154
155template <class EF>
157
195template <class Mutex>
197{
199
200public:
201 explicit scope_unlock(std::unique_lock<Mutex>& lock) noexcept(true) : plock(&lock)
202 {
203 XRPL_ASSERT(plock->owns_lock(), "xrpl::scope_unlock::scope_unlock : mutex must be locked");
204 plock->unlock();
205 }
206
207 // Immovable type
208 scope_unlock(scope_unlock const&) = delete;
210 operator=(scope_unlock const&) = delete;
211
212 ~scope_unlock() noexcept(true)
213 {
214 plock->lock();
215 }
216};
217
218template <class Mutex>
220
221} // namespace xrpl
scope_exit(EFP &&f, std::enable_if_t<!std::is_same_v< std::remove_cv_t< EFP >, scope_exit > &&std::is_constructible_v< EF, EFP > > *=0) noexcept
Definition scope.h:48
bool execute_on_destruction_
Definition scope.h:28
void release() noexcept
Definition scope.h:58
scope_exit(scope_exit &&rhs) noexcept(std::is_nothrow_move_constructible_v< EF >||std::is_nothrow_copy_constructible_v< EF >)
Definition scope.h:37
EF exit_function_
Definition scope.h:27
scope_exit & operator=(scope_exit &&)=delete
bool execute_on_destruction_
Definition scope.h:71
EF exit_function_
Definition scope.h:70
scope_fail(EFP &&f, std::enable_if_t<!std::is_same_v< std::remove_cv_t< EFP >, scope_fail > &&std::is_constructible_v< EF, EFP > > *=0) noexcept
Definition scope.h:94
void release() noexcept
Definition scope.h:104
scope_fail(scope_fail &&rhs) noexcept(std::is_nothrow_move_constructible_v< EF >||std::is_nothrow_copy_constructible_v< EF >)
Definition scope.h:81
int uncaught_on_creation_
Definition scope.h:72
scope_fail & operator=(scope_fail &&)=delete
scope_success & operator=(scope_success &&)=delete
int uncaught_on_creation_
Definition scope.h:118
bool execute_on_destruction_
Definition scope.h:117
scope_success(EFP &&f, std::enable_if_t<!std::is_same_v< std::remove_cv_t< EFP >, scope_success > &&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:140
scope_success(scope_success &&rhs) noexcept(std::is_nothrow_move_constructible_v< EF >||std::is_nothrow_copy_constructible_v< EF >)
Definition scope.h:127
~scope_success() noexcept(noexcept(exit_function_()))
Definition scope.h:121
void release() noexcept
Definition scope.h:149
Automatically unlocks and re-locks a unique_lock object.
Definition scope.h:197
scope_unlock & operator=(scope_unlock const &)=delete
scope_unlock(std::unique_lock< Mutex > &lock) noexcept(true)
Definition scope.h:201
std::unique_lock< Mutex > * plock
Definition scope.h:198
scope_unlock(scope_unlock const &)=delete
~scope_unlock() noexcept(true)
Definition scope.h:212
T is_same_v
T lock(T... args)
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
T owns_lock(T... args)
T uncaught_exceptions(T... args)
T unlock(T... args)