-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.hpp
334 lines (296 loc) · 9.12 KB
/
result.hpp
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#pragma once
// Copyright © 2021 Alex Qzminsky.
// License: MIT. All rights reserved.
/// \brief Result variant type
///
/// \author https://github.com/qzminsky
/// \version 1.0.0
/// \date 2021/01/16
#ifndef RESULT_H
#define RESULT_H
static_assert(__cplusplus >= 2017'00, "C++17 or higher is required");
#include <type_traits>
#include <variant>
#if __cplusplus >= 2020'00
# include <concepts>
#endif
/**
* \class result
*
* \brief Result monad implementation
*
* \details Stores an ok/error-state with corresponding value
*/
template <typename Ok_t = std::monostate,
typename Error_t = std::monostate
>
class result
{
public:
// ANCHOR Member types
using ok_type = Ok_t;
using error_type = Error_t;
private:
// Value container
std::variant<ok_type, error_type> _value;
public:
/// There is no default constructor for a result
result () = delete;
/// Default copy constructor
result (result const&) = default;
/// Default move constructor
result (result&&) = default;
/**
* \brief Converting constructor from specified value
*
* \param val Value to store in a result
*/
template <typename T>
result (T const& val) : _value{ val } {}
/**
* \brief Converting constructor from ok-typed variant
*
* \param other Variant to construct from
*/
template <typename Copy_Ok_t>
result (result<Copy_Ok_t, std::monostate> const& other) { *this = other; }
/**
* \brief Converting constructor from error-typed variant
*
* \param other Variant to construct from
*/
template <typename Copy_Error_t>
result (result<std::monostate, Copy_Error_t> const& other) { *this = other; }
/// Default copy assignment
auto operator = (result const&) -> result& = default;
/// Default move assignment
auto operator = (result&&) -> result& = default;
/**
* \brief Converting assignment from ok-typed variant
*
* \param other Variant to extract value from
*/
template <typename Copy_Ok_t>
auto operator = (result<Copy_Ok_t, std::monostate> const& other) -> result&
{
_value.template emplace<0>(other.unwrap());
return *this;
}
/**
* \brief Converting assignment from error-typed variant
*
* \param other Variant to extract value from
*/
template <typename Copy_Error_t>
auto operator = (result<std::monostate, Copy_Error_t> const& other) -> result&
{
_value.template emplace<1>(other.unwrap_error());
return *this;
}
/**
* \brief Constructs a success result object with specified value
*
* \param val Success value stored in result
*/
template <typename T>
static auto ok (T const& val) -> result<T, std::monostate>
{
return result<T, std::monostate>{ val };
}
/**
* \brief Constructs a failure result object with specified value
*
* \param val Failure value stored in result
*/
template <typename T>
static auto error (T const& val) -> result<std::monostate, T>
{
return result<std::monostate, T>{ val };
}
/**
* \brief Predicate. Returns `true` in case of success result
*/
[[nodiscard]]
auto is_ok () const noexcept -> bool
{
return _value.index() == 0;
}
/**
* \brief Predicate. Returns `true` in case of failure result
*/
[[nodiscard]]
auto is_error () const noexcept -> bool
{
return _value.index() == 1;
}
/**
* \brief Predicate. Returns `true` in case of success result with matching values
*/
template <typename T>
[[nodiscard]]
auto is_ok (T const& val) const noexcept -> bool
{
if constexpr (
std::is_same_v<ok_type, std::monostate> ||
std::is_same_v<T, std::monostate>
) {
return false;
}
else return is_ok() && (unwrap() == val);
}
/**
* \brief Predicate. Returns `true` in case of failure result with matching values
*/
template <typename T>
[[nodiscard]]
auto is_error (T const& val) const noexcept -> bool
{
if constexpr (
std::is_same_v<error_type, std::monostate> ||
std::is_same_v<T, std::monostate>
) {
return false;
}
else return is_error() && (unwrap_error() == val);
}
/**
* \brief Predicate operator. Returns `true` in case of success result
*/
[[nodiscard]]
explicit operator bool () const noexcept
{
return is_ok();
}
/**
* \brief Compares tho results by its states equality
*
* \param other Result to compare with
*
* \return `true` if comparing states is equal to each other; `false` otherwise
*/
template <typename T1, typename T2>
[[nodiscard]]
auto operator == (result<T1, T2> const& other) const noexcept -> bool
{
return (is_ok() && other.is_ok(unwrap())) || (is_error() && other.is_error(unwrap_error()));
}
#if __cplusplus < 2020'00
/**
* \brief Compares tho results by its states inequality
*
* \param other Result to compare with
*
* \return `true` if comparing states is not equal to each other; `false` otherwise
*/
template <typename T1, typename T2>
[[nodiscard]]
auto operator != (result<T1, T2> const& other) const noexcept -> bool
{
return !(*this == other);
}
#endif
/**
* \brief Extracts the stored value in case of success result and returns it
*
* \throw std::bad_variant_access
*/
[[nodiscard]]
auto unwrap () const -> ok_type
{
return std::get<0>(_value);
}
/**
* \brief Extracts the stored value in case of failure result and returns it
*
* \throw std::bad_variant_access
*/
[[nodiscard]]
auto unwrap_error () const -> error_type
{
return std::get<1>(_value);
}
/**
* \brief Extracts the stored vavlue in case of success result or a provided default otherwise
*
* \param def Default value for error case
*/
[[nodiscard]]
auto unwrap_or (ok_type const& def) const -> ok_type
{
return is_ok() ? unwrap() : def;
}
/**
* \brief Performs specified execution in case of success result
*
* \param func Functor to invoke
*
* \return Reference to original result object
*/
#if __cplusplus >= 2020'00
template <typename Functor>
requires
std::invocable<Functor, ok_type> || std::invocable<Functor>
#else
template <typename Functor,
typename = std::enable_if_t<std::disjunction_v<std::is_invocable<Functor, ok_type>, std::is_invocable<Functor>>>
>
#endif
auto if_ok (Functor&& func) -> result&
{
if (is_ok()) {
if constexpr (std::is_invocable_v<Functor>) {
func();
}
else func(unwrap());
}
return *this;
}
/**
* \brief Performs specified execution in case of failure result
*
* \param func Functor to invoke
*
* \return Reference to original result object
*/
#if __cplusplus >= 2020'00
template <typename Functor>
requires
std::invocable<Functor, error_type> || std::invocable<Functor>
#else
template <typename Functor,
typename = std::enable_if_t<std::disjunction_v<std::is_invocable<Functor, error_type>, std::is_invocable<Functor>>>
>
#endif
auto if_error (Functor&& func) -> result&
{
if (is_error()) {
if constexpr (std::is_invocable_v<Functor>) {
func();
}
else func(unwrap_error());
}
return *this;
}
}; // end class result
#endif // RESULT_H
// MIT License
//
// Copyright (c) 2021 Alex Qzminsky
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.