status.h
6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//===--- status.h - Status and Expected classes -----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef ACXXEL_STATUS_H
#define ACXXEL_STATUS_H
#include <cassert>
#include <string>
// The clang compiler supports annotating class declarations with the
// warn_unused_result attribute, and this has the meaning that whenever that
// type is returned from a function, the function is marked as
// warn_unused_result.
//
// The gcc compiler does not support warn_unused_result for classes, only for
// functions, so we only use this feature with clang.
#ifdef __clang__
#define ACXXEL_WARN_UNUSED_RESULT_TYPE __attribute__((warn_unused_result))
#else
#define ACXXEL_WARN_UNUSED_RESULT_TYPE
#endif
namespace acxxel {
/// Status type.
///
/// May represent failure with a string error message, or may indicate success.
class ACXXEL_WARN_UNUSED_RESULT_TYPE Status {
public:
/// Creates a Status representing success.
Status() : HasMessage(false) {}
/// Creates a Status representing failure with the given error message.
explicit Status(const std::string &Message)
: HasMessage(true), Message(Message) {}
Status(const Status &) = default;
Status &operator=(const Status &) = default;
Status(Status &&) noexcept = default;
// Cannot use default because the move assignment operator for std::string is
// not marked noexcept.
Status &operator=(Status &&That) noexcept {
HasMessage = That.HasMessage;
Message = std::move(That.Message);
return *this;
}
~Status() = default;
/// Returns true if this Status represents failure. Otherwise, returns false.
bool isError() const { return HasMessage; }
/// Returns true if this Status represents success. Otherwise, returns false.
operator bool() const { return !HasMessage; }
/// Gets a reference to the error message for this Status.
///
/// Should only be called if isError() returns true.
const std::string &getMessage() const { return Message; }
private:
bool HasMessage;
std::string Message;
};
class ExpectedBase {
protected:
enum class State {
SUCCESS,
FAILURE,
MOVED,
};
};
/// Either a value of type T or a Status representing failure.
template <typename T> class Expected : public ExpectedBase {
public:
/// Creates an Expected representing failure with the given Error status.
// Intentionally implicit.
Expected(Status AnError)
: TheState(State::FAILURE), TheError(std::move(AnError)) {
assert(AnError.isError() && "constructing an error Expected value from a "
"success status is not allowed");
}
/// Creates an Expected representing success with the given value.
// Intentionally implicit.
Expected(T Value) : TheState(State::SUCCESS), TheValue(std::move(Value)) {}
Expected(const Expected &That) : TheState(That.TheState) {
switch (TheState) {
case State::SUCCESS:
new (&TheValue) T(That.TheValue);
break;
case State::FAILURE:
new (&TheError) Status(That.TheError);
break;
case State::MOVED:
// Nothing to do in this case.
break;
}
}
Expected &operator=(Expected That) {
TheState = That.TheState;
switch (TheState) {
case State::SUCCESS:
new (&TheValue) T(std::move(That.TheValue));
break;
case State::FAILURE:
new (&TheError) Status(std::move(That.TheError));
break;
case State::MOVED:
// Nothing to do in this case.
break;
}
return *this;
}
Expected(Expected &&That) noexcept : TheState(That.TheState) {
switch (TheState) {
case State::SUCCESS:
new (&TheValue) T(std::move(That.TheValue));
break;
case State::FAILURE:
new (&TheError) Status(std::move(That.TheError));
break;
case State::MOVED:
// Nothing to do in this case.
break;
}
That.TheState = State::MOVED;
}
template <typename U>
Expected(const Expected<U> &That) : TheState(That.TheState) {
switch (TheState) {
case State::SUCCESS:
new (&TheValue) T(That.TheValue);
break;
case State::FAILURE:
new (&TheError) Status(That.TheError);
break;
case State::MOVED:
// Nothing to do in this case.
break;
}
}
template <typename U> Expected(Expected<U> &&That) : TheState(That.TheState) {
switch (TheState) {
case State::SUCCESS:
new (&TheValue) T(std::move(That.TheValue));
break;
case State::FAILURE:
new (&TheError) Status(std::move(That.TheError));
break;
case State::MOVED:
// Nothing to do in this case.
break;
}
}
~Expected() {
switch (TheState) {
case State::SUCCESS:
TheValue.~T();
break;
case State::FAILURE:
TheError.~Status();
break;
case State::MOVED:
// Nothing to do for this case.
break;
}
}
/// Returns true if this instance represents failure.
bool isError() const { return TheState != State::SUCCESS; }
/// Gets a reference to the Status object.
///
/// Should only be called if isError() returns true.
const Status &getError() const {
assert(isError());
return TheError;
}
/// Gets a const reference to the value object.
///
/// Should only be called if isError() returns false.
const T &getValue() const {
assert(!isError());
return TheValue;
}
/// Gets a reference to the value object.
///
/// Should only be called if isError() returns false.
T &getValue() {
assert(!isError());
return TheValue;
}
/// Takes the value from this object by moving it to the return value.
///
/// Should only be called if isError() returns false.
T takeValue() {
assert(!isError());
TheState = State::MOVED;
return std::move(TheValue);
}
private:
template <typename U> friend class Expected;
State TheState;
union {
T TheValue;
Status TheError;
};
};
} // namespace acxxel
#endif // ACXXEL_STATUS_H