userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
result_store.hpp
Go to the documentation of this file.
1
#pragma once
2
5
6
#include <exception>
7
#include <optional>
8
#include <stdexcept>
9
#include <utility>
10
11
#include <userver/compiler/impl/lifetime.hpp>
12
13
USERVER_NAMESPACE_BEGIN
14
15
namespace
utils
{
16
20
template
<
typename
T>
21
class
ResultStore
final {
22
public
:
26
T
Retrieve
();
27
30
const
T&
Get
()
const
& USERVER_IMPL_LIFETIME_BOUND;
31
33
void
SetValue
(
const
T&);
34
36
void
SetValue
(T&&);
37
39
void
SetException
(std::exception_ptr&&)
noexcept
;
40
41
// Returns the stored exception if present
42
std::exception_ptr GetException()
const
noexcept
;
43
44
private
:
45
// variant here would require a specialization for exception_ptr
46
std::optional<T> value_;
47
std::exception_ptr exception_;
48
};
49
53
template
<>
54
class
ResultStore
<void> final {
55
public
:
58
void
Retrieve
();
59
60
void
Get
()
const
&;
61
63
void
SetValue
()
noexcept
;
64
66
void
SetException
(std::exception_ptr&&)
noexcept
;
67
68
// Returns the stored exception if present
69
std::exception_ptr GetException()
const
noexcept
;
70
71
private
:
72
bool
has_value_{
false
};
73
std::exception_ptr exception_;
74
};
75
76
template
<
typename
T>
77
T
ResultStore<T>::Retrieve
() {
78
if
(value_) {
79
return
std::move(*value_);
80
}
81
if
(exception_) {
82
std::rethrow_exception(exception_);
83
}
84
throw
std::logic_error(
"result store is not ready"
);
85
}
86
87
template
<
typename
T>
88
const
T&
ResultStore<T>::Get
() const& USERVER_IMPL_LIFETIME_BOUND {
89
if
(value_) {
90
return
*value_;
91
}
92
if
(exception_) {
93
std::rethrow_exception(exception_);
94
}
95
throw
std::logic_error(
"result store is not ready"
);
96
}
97
98
template
<
typename
T>
99
void
ResultStore<T>::SetValue
(
const
T& value) {
100
value_ = value;
101
}
102
103
template
<
typename
T>
104
void
ResultStore<T>::SetValue
(T&& value) {
105
value_.emplace(std::move(value));
106
}
107
108
template
<
typename
T>
109
void
ResultStore<T>::SetException
(std::exception_ptr&& exception)
noexcept
{
110
exception_ = std::move(exception);
111
}
112
113
template
<
typename
T>
114
std::exception_ptr ResultStore<T>::GetException() const noexcept {
115
return
exception_;
116
}
117
118
// NOLINTNEXTLINE(readability-make-member-function-const)
119
inline
void
ResultStore<void>::Retrieve
() { Get(); }
120
121
inline
void
ResultStore<void>::Get
() const& {
122
if
(has_value_) {
123
return
;
124
}
125
if
(exception_) {
126
std::rethrow_exception(exception_);
127
}
128
throw
std::logic_error(
"result store is not ready"
);
129
}
130
131
inline
void
ResultStore<void>::SetValue
() noexcept { has_value_ =
true
; }
132
133
inline
void
ResultStore<void>::SetException
(std::exception_ptr&& exception)
noexcept
{
134
exception_ = std::move(exception);
135
}
136
137
inline
std::exception_ptr ResultStore<void>::GetException() const noexcept {
return
exception_; }
138
139
}
// namespace utils
140
141
USERVER_NAMESPACE_END
userver
utils
result_store.hpp
Generated on
for userver by
Doxygen
1.17.0