This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.hpp
176 lines (140 loc) · 5.83 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
#ifndef MUSH_RESULT_HPP
#define MUSH_RESULT_HPP
#include <memory>
#include <functional>
namespace mush
{
struct Basic_Error
{
};
// Result for non-void non-reference type
template <typename ValueType, typename ErrorType, bool IsReference>
class [[deprecated("monadic_error.hpp provides better alternative")]] Basic_Result
{
private:
ValueType value;
ErrorType error;
bool has_result;
public:
// constexpr Basic_Result(ErrorType err) { has_result = false; error = err; }
// constexpr Basic_Result(ValueType val) { has_result = true; value = val; }
// default constructor
Basic_Result() : has_result(false) {}
// Create from result
// Basic_Result(Basic_Result&& rhs) = default;
Basic_Result(const Basic_Result& rhs) : has_result(rhs.has_result)
{
if (has_result) value = rhs.value;
else error = rhs.error;
}
// create from values
Basic_Result(const ValueType& rhs) : value(rhs), has_result(true) {}
Basic_Result(ValueType&& rhs) : value(std::forward<ValueType>(rhs)), has_result(true) {}
// create from errors
Basic_Result(const ErrorType& err) : error(err), has_result(false) {}
Basic_Result(ErrorType&& err) : error(std::forward<ErrorType>(err)), has_result(false) {}
// assignments
Basic_Result& operator=(const ValueType& rhs)
{
has_result = true;
value = rhs;
return *this;
}
Basic_Result& operator=(ValueType&& rhs)
{
has_result = true;
value = std::forward(rhs);
return *this;
}
//Basic_Result& operator=(Result rhs)
// conversions
operator bool() { return has_result; }
// matching
ValueType&& match(std::function<ValueType(ErrorType&)> handler)
{
if (has_result)
{
has_result = false;
return std::move(value);
}
return handler(error);
}
};
// Result for reference type
template <typename ValueType, typename ErrorType>
class Basic_Result<ValueType, ErrorType, true>
{
private:
typename std::remove_reference<ValueType>::type* value;
ErrorType error;
bool has_result;
public:
constexpr Basic_Result(ErrorType err) { has_result = false; error = err; }
constexpr Basic_Result(ValueType val) { has_result = true; value = val; }
// default constructor
Basic_Result() : has_result(false) {}
// results
Basic_Result(const Basic_Result& rhs) : has_result(rhs.has_result)
{
if (has_result) value = rhs.value;
else error = rhs.error;
}
// values
Basic_Result(const ValueType& rhs) : value(rhs), has_result(true) {}
Basic_Result(ValueType&& rhs) : value(std::forward<ValueType>(rhs)), has_result(true) {}
// create from errors
Basic_Result(const ErrorType& err) : error(err), has_result(false) {}
Basic_Result(ErrorType&& err) : error(std::forward<ErrorType>(err)), has_result(false) {}
Basic_Result& operator=(const ValueType rhs)
{
has_result = true;
value = &rhs;
}
//Basic_Result& operator=(Basic_Result rhs) = default
operator bool() { return has_result; }
};
// Result for void type
template <typename ErrorType>
class Basic_Result<void, ErrorType, false>
{
private:
ErrorType error;
bool has_result;
public:
constexpr Basic_Result(bool) : error(), has_result(true) {}
constexpr Basic_Result(bool, ErrorType err) : error(err), has_result(false) {}
// constexpr static Basic_Result<void, ErrorType, false> OK = Basic_Result(true);
Basic_Result() : has_result(false) {}
Basic_Result(const ErrorType& err) : error(err), has_result(false) {}
Basic_Result(ErrorType&& err) : error(std::move(err)), has_result(false) {}
void match(std::function<void(ErrorType&)> handler)
{
if (has_result)
return;
return handler(error);
}
};
template <typename ValueType, typename ErrorType, bool IsReference>
Basic_Result<ValueType, ErrorType, IsReference> make_result(bool)
{
}
template <typename ValueType, bool IsReference = std::is_reference<ValueType>::value>
using Result = Basic_Result<ValueType, Basic_Error, IsReference>;
}
#endif
/*
Copyright (c) 2017 Jari Ronkainen
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose, including
commercial applications, and to alter it and redistribute it freely, subject to
the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim
that you wrote the original software. If you use this software in a product,
an acknowledgment in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/