xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
beast
unit_test
thread.h
1
// Distributed under the Boost Software License, Version 1.0. (See accompanying
2
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
3
//
4
5
#pragma once
6
7
#include <xrpl/beast/unit_test/suite.h>
8
9
#include <
exception
>
10
#include <
functional
>
11
#include <
string
>
12
#include <
thread
>
13
#include <
utility
>
14
15
namespace
beast::unit_test
{
16
20
class
Thread
21
{
22
private
:
23
Suite
*
s_
=
nullptr
;
24
std::thread
t_
;
25
26
public
:
27
using
Id
=
std::thread::id
;
28
using
native_handle_type
= std::thread::native_handle_type;
29
30
Thread
() =
default
;
31
Thread
(
Thread
const
&) =
delete
;
32
Thread
&
33
operator=
(
Thread
const
&) =
delete
;
34
35
Thread
(
Thread
&& other) :
s_
(other.
s_
),
t_
(
std
::move(other.
t_
))
36
{
37
}
38
39
Thread
&
40
operator=
(
Thread
&& other)
41
{
42
s_
= other.s_;
43
t_
= std::move(other.t_);
44
return
*
this
;
45
}
46
47
template
<
class
F,
class
... Args>
48
explicit
Thread
(
Suite
& s, F&& f, Args&&... args) :
s_
(&s)
49
{
50
std::function
<void(
void
)> b = [f =
std::forward<F>
(f),
51
... args =
std::forward<Args>
(args)]()
mutable
{
52
std::invoke
(f, args...);
53
};
54
t_
=
std::thread
(&
Thread::run
,
this
, std::move(b));
55
}
56
57
[[nodiscard]]
bool
58
joinable
()
const
59
{
60
return
t_
.joinable();
61
}
62
63
[[nodiscard]]
std::thread::id
64
getId
()
const
65
{
66
return
t_
.get_id();
67
}
68
69
static
unsigned
70
hardwareConcurrency
() noexcept
71
{
72
return
std::thread::hardware_concurrency
();
73
}
74
75
void
76
join
()
77
{
78
t_
.join();
79
s_
->propagateAbort();
80
}
81
82
void
83
detach
()
84
{
85
t_
.detach();
86
}
87
88
void
89
swap
(
Thread
& other)
90
{
91
std::swap
(
s_
, other.
s_
);
92
std::swap
(
t_
, other.
t_
);
93
}
94
95
private
:
96
void
97
run
(
std::function
<
void
(
void
)> f)
98
{
99
try
100
{
101
f();
102
}
103
catch
(
Suite::AbortException
const
&)
// NOLINT(bugprone-empty-catch)
104
{
105
}
106
catch
(
std::exception
const
& e)
107
{
108
s_
->fail(
"unhandled exception: "
+
std::string
(e.
what
()));
109
}
110
catch
(...)
111
{
112
s_
->fail(
"unhandled exception"
);
113
}
114
}
115
};
116
117
}
// namespace beast::unit_test
std::string
beast::unit_test::Suite
A testsuite class.
Definition
suite.h:52
beast::unit_test::Thread
Replacement for std::thread that handles exceptions in unit tests.
Definition
thread.h:21
beast::unit_test::Thread::Thread
Thread(Thread const &)=delete
beast::unit_test::Thread::s_
Suite * s_
Definition
thread.h:23
beast::unit_test::Thread::operator=
Thread & operator=(Thread const &)=delete
beast::unit_test::Thread::Thread
Thread(Suite &s, F &&f, Args &&... args)
Definition
thread.h:48
beast::unit_test::Thread::Thread
Thread(Thread &&other)
Definition
thread.h:35
beast::unit_test::Thread::native_handle_type
std::thread::native_handle_type native_handle_type
Definition
thread.h:28
beast::unit_test::Thread::swap
void swap(Thread &other)
Definition
thread.h:89
beast::unit_test::Thread::hardwareConcurrency
static unsigned hardwareConcurrency() noexcept
Definition
thread.h:70
beast::unit_test::Thread::getId
std::thread::id getId() const
Definition
thread.h:64
beast::unit_test::Thread::join
void join()
Definition
thread.h:76
beast::unit_test::Thread::operator=
Thread & operator=(Thread &&other)
Definition
thread.h:40
beast::unit_test::Thread::Id
std::thread::id Id
Definition
thread.h:27
beast::unit_test::Thread::detach
void detach()
Definition
thread.h:83
beast::unit_test::Thread::run
void run(std::function< void(void)> f)
Definition
thread.h:97
beast::unit_test::Thread::t_
std::thread t_
Definition
thread.h:24
beast::unit_test::Thread::joinable
bool joinable() const
Definition
thread.h:58
beast::unit_test::Thread::Thread
Thread()=default
exception
std::forward
T forward(T... args)
std::function
functional
std::thread::hardware_concurrency
T hardware_concurrency(T... args)
std::thread::id
std::invoke
T invoke(T... args)
beast::unit_test
Definition
include/xrpl/beast/unit_test/amount.h:11
std
STL namespace.
string
beast::unit_test::Suite::AbortException
Definition
suite.h:61
std::swap
T swap(T... args)
thread
utility
std::exception::what
T what(T... args)
Generated by
1.17.0