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>
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>
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 {
157 }
158};
159
160template <class EF>
162
200template <class Mutex>
202{
204
205public:
206 explicit scope_unlock(std::unique_lock<Mutex>& lock) noexcept(true) : plock(&lock)
207 {
208 XRPL_ASSERT(plock->owns_lock(), "xrpl::scope_unlock::scope_unlock : mutex must be locked");
209 plock->unlock();
210 }
211
212 // Immovable type
213 scope_unlock(scope_unlock const&) = delete;
215 operator=(scope_unlock const&) = delete;
216
217 ~scope_unlock() noexcept(true)
218 {
219 plock->lock();
220 }
221};
222
223template <class Mutex>
225
226} // 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:49
bool execute_on_destruction_
Definition scope.h:28
void release() noexcept
Definition scope.h:60
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:73
EF exit_function_
Definition scope.h:72
void release() noexcept
Definition scope.h:107
scope_fail(scope_fail &&rhs) noexcept(std::is_nothrow_move_constructible_v< EF >||std::is_nothrow_copy_constructible_v< EF >)
Definition scope.h:83
int uncaught_on_creation_
Definition scope.h:74
scope_fail & operator=(scope_fail &&)=delete
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:96
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:143
scope_success & operator=(scope_success &&)=delete
int uncaught_on_creation_
Definition scope.h:121
bool execute_on_destruction_
Definition scope.h:120
scope_success(scope_success &&rhs) noexcept(std::is_nothrow_move_constructible_v< EF >||std::is_nothrow_copy_constructible_v< EF >)
Definition scope.h:130
~scope_success() noexcept(noexcept(exit_function_()))
Definition scope.h:124
void release() noexcept
Definition scope.h:154
Automatically unlocks and re-locks a unique_lock object.
Definition scope.h:202
scope_unlock & operator=(scope_unlock const &)=delete
scope_unlock(std::unique_lock< Mutex > &lock) noexcept(true)
Definition scope.h:206
std::unique_lock< Mutex > * plock
Definition scope.h:203
scope_unlock(scope_unlock const &)=delete
~scope_unlock() noexcept(true)
Definition scope.h:217
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)