rippled
Loading...
Searching...
No Matches
scope.h
1#ifndef XRPL_BASICS_SCOPE_H_INCLUDED
2#define XRPL_BASICS_SCOPE_H_INCLUDED
3
4#include <xrpl/beast/utility/instrumentation.h>
5
6#include <exception>
7#include <mutex>
8#include <type_traits>
9#include <utility>
10
11namespace ripple {
12
13// RAII scope helpers. As specified in Library Fundamental, Version 3
14// Basic design of idea: https://www.youtube.com/watch?v=WjTrfoiB0MQ
15// Specification:
16// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/n4873.html#scopeguard
17
18// This implementation deviates from the spec slightly:
19// The scope_exit and scope_fail constructors taking a functor are not
20// permitted to throw an exception. This was done because some compilers
21// did not like the superfluous try/catch in the common instantiations
22// where the construction was noexcept. Instead a static_assert is used
23// to enforce this restriction.
24
25template <class EF>
27{
30
31public:
37
46
48 operator=(scope_exit&&) = delete;
49
50 template <class EFP>
62
63 void
64 release() noexcept
65 {
67 }
68};
69
70template <class EF>
72
73template <class EF>
120
121template <class EF>
123
124template <class EF>
126{
130
131public:
138
148
151
152 template <class EFP>
154 EFP&& f,
158 0) noexcept(std::is_nothrow_constructible_v<EF, EFP> || std::is_nothrow_constructible_v<EF, EFP&>)
160 {
161 }
162
163 void
164 release() noexcept
165 {
167 }
168};
169
170template <class EF>
172
210template <class Mutex>
212{
214
215public:
216 explicit scope_unlock(std::unique_lock<Mutex>& lock) noexcept(true)
217 : plock(&lock)
218 {
219 XRPL_ASSERT(
220 plock->owns_lock(),
221 "ripple::scope_unlock::scope_unlock : mutex must be locked");
222 plock->unlock();
223 }
224
225 // Immovable type
226 scope_unlock(scope_unlock const&) = delete;
228 operator=(scope_unlock const&) = delete;
229
230 ~scope_unlock() noexcept(true)
231 {
232 plock->lock();
233 }
234};
235
236template <class Mutex>
238
239} // namespace ripple
240
241#endif
scope_exit(scope_exit &&rhs) noexcept(std::is_nothrow_move_constructible_v< EF >||std::is_nothrow_copy_constructible_v< EF >)
Definition scope.h:38
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:51
scope_exit & operator=(scope_exit &&)=delete
bool execute_on_destruction_
Definition scope.h:29
void release() noexcept
Definition scope.h:64
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:102
void release() noexcept
Definition scope.h:115
int uncaught_on_creation_
Definition scope.h:78
scope_fail & operator=(scope_fail &&)=delete
bool execute_on_destruction_
Definition scope.h:77
scope_fail(scope_fail &&rhs) noexcept(std::is_nothrow_move_constructible_v< EF >||std::is_nothrow_copy_constructible_v< EF >)
Definition scope.h:88
bool execute_on_destruction_
Definition scope.h:128
scope_success & operator=(scope_success &&)=delete
void release() noexcept
Definition scope.h:164
scope_success(scope_success &&rhs) noexcept(std::is_nothrow_move_constructible_v< EF >||std::is_nothrow_copy_constructible_v< EF >)
Definition scope.h:139
~scope_success() noexcept(noexcept(exit_function_()))
Definition scope.h:132
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:153
Automatically unlocks and re-locks a unique_lock object.
Definition scope.h:212
scope_unlock(std::unique_lock< Mutex > &lock) noexcept(true)
Definition scope.h:216
scope_unlock & operator=(scope_unlock const &)=delete
std::unique_lock< Mutex > * plock
Definition scope.h:213
~scope_unlock() noexcept(true)
Definition scope.h:230
scope_unlock(scope_unlock const &)=delete
T is_same_v
T lock(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
STL namespace.
T owns_lock(T... args)
T uncaught_exceptions(T... args)
T unlock(T... args)