Dev Essential  1.6.3
Loading...
Searching...
No Matches
result_type_impl.h
Go to the documentation of this file.
1
14
15#ifndef A_UTIL_UTIL_RESULT_DETAIL_RESULT_TYPE_IMPL_HEADER_INCLUDED
16#define A_UTIL_UTIL_RESULT_DETAIL_RESULT_TYPE_IMPL_HEADER_INCLUDED
17
18#include <a_util/base/types.h> //a_util::maybe_unused
24
25#include <utility> //std::swap
26
27namespace a_util {
28namespace result {
29
30inline Result::Result() : _result_handle()
31{
32}
33
34inline Result::Result(bool val)
35 : _result_handle(DescriptionType::makeResultDescription(static_cast<std::int32_t>(!val) * -1))
36{
37}
38
39inline Result::Result(const ResultInfo<void>&) : _result_handle()
40{
41}
42
43template <typename ErrorType>
45 : _result_handle(DescriptionType::makeResultDescription(error))
46{
47 a_util::maybe_unused(error); // C4100
48}
49
50template <typename ErrorType>
52 const char* error_description,
53 std::int32_t line,
54 const char* file,
55 const char* function)
56 : _result_handle()
57{
58 a_util::maybe_unused(error); // C4100
59 *this = Result(error.getCode(),
60 error_description,
61 line,
62 file,
63 function,
65}
66
67inline Result::Result(std::int32_t error_code)
68 : _result_handle(DescriptionType::makeResultDescription(error_code))
69{
70}
71
72inline Result::Result(std::int32_t error_code,
73 const char* error_description,
74 std::int32_t line,
75 const char* file,
76 const char* function)
77 : _result_handle()
78{
79 *this = createFrom(error_code, error_description, line, file, function);
80}
81
82inline Result::Result(const Result& other_result,
83 const char* error_description,
84 std::int32_t line,
85 const char* file,
86 const char* function)
87 : _result_handle()
88{
89 *this = Result(other_result.getErrorCode(),
90 error_description,
91 line,
92 file,
93 function,
94 other_result.getErrorLabel());
95}
96
98{
99 static_assert(sizeof(*this) == 8, "sizeof(*this) != 8");
100 static_assert(std::is_standard_layout<Result>::value, "Result must be standard layout type");
101}
102
103inline Result::Result(const Result& other) : _result_handle(other._result_handle)
104{
105}
106
108{
109 swap(*this, other);
110 return *this;
111}
112
113inline Result& Result::operator=(bool val)
114{
115 return Result::operator=(Result(val));
116}
117
118inline Result& Result::operator=(std::int32_t val)
119{
120 return Result::operator=(Result(val));
121}
122
124{
125 return Result::operator=(Result());
126}
127
128inline Result::Result(Result&& other) : _result_handle()
129{
130 swap(*this, other);
131 if (SUCCESS != other) {
132 other = Result();
133 }
134}
135
136inline std::int32_t Result::getErrorCode() const
137{ // get error code from the user space if available
138 if (_result_handle.getDetailedDescription()) {
139 return _result_handle.getDetailedDescription()->getErrorCode();
140 }
141 return _result_handle.getErrorCode();
142}
143
144inline const char* Result::getErrorLabel() const
145{
146 if (_result_handle.getDetailedDescription()) {
147 // we can get the stringified error code from the detailed description
148 return _result_handle.getDetailedDescription()->getErrorCodeLabel();
149 }
150
151 return *this ? "SUCCESS" : "(unknown)";
152}
153
154inline const char* Result::getDescription() const
155{
156 if (0 != _result_handle.getErrorCode()) {
157 // only the error code was set, no description available
158 return "";
159 }
160
161 if (_result_handle.getDetailedDescription()) {
162 return _result_handle.getDetailedDescription()->getErrorDescription();
163 }
164
165 return "No error occurred";
166}
167
168inline std::int32_t Result::getLine() const
169{
170 if (_result_handle.getDetailedDescription()) {
171 return _result_handle.getDetailedDescription()->getLine();
172 }
173
174 return -1;
175}
176
177inline const char* Result::getFile() const
178{
179 if (_result_handle.getDetailedDescription()) {
180 return _result_handle.getDetailedDescription()->getFileName();
181 }
182
183 return "";
184}
185
186inline const char* Result::getFunction() const
187{
188 if (_result_handle.getDetailedDescription()) {
189 return _result_handle.getDetailedDescription()->getFunctionName();
190 }
191
192 return "";
193}
194
195inline Result::operator bool() const noexcept
196{
197 return getErrorCode() == 0;
198}
199
200inline Result::Result(std::int32_t error_code,
201 const char* error_description,
202 std::int32_t line,
203 const char* file,
204 const char* function,
205 const char* label)
206 : _result_handle(DescriptionType::makeResultDescription<detail::ErrorDescriptionType>(
207 error_code, error_description, line, file, function, label))
208{
209}
210
211inline Result Result::createFrom(std::int32_t error_code,
212 const char* error_description,
213 std::int32_t line,
214 const char* file,
215 const char* function)
216{
217 return Result(error_code,
218 error_description,
219 line,
220 file,
221 function,
222 0 == error_code ? "SUCCESS" : "(unknown)");
223}
224
225inline bool operator==(const Result& lhs, const Result& rhs)
226{
227 return lhs._result_handle == rhs._result_handle;
228}
229
230inline bool operator!=(const Result& lhs, const Result& rhs)
231{
232 return !(lhs == rhs);
233}
234
235template <typename T>
236inline bool operator==(const Result& result, const ResultInfo<T>&)
237{
238 return result.getErrorCode() == ResultInfo<T>::getCode();
239}
240
241template <typename T>
242inline bool operator!=(const Result& result, const ResultInfo<T>& error_code)
243{
244 return !(result == error_code);
245}
246
247template <typename T>
248inline bool operator==(const ResultInfo<T>& error_code, const Result& result)
249{
250 return result == error_code;
251}
252
253template <typename T>
254inline bool operator!=(const ResultInfo<T>& error_code, const Result& result)
255{
256 return result != error_code;
257}
258
259inline void swap(Result& lhs, Result& rhs)
260{
261 // enable ADL
262 using std::swap;
263 swap(lhs._result_handle, rhs._result_handle);
264}
265
266template <>
267inline bool isOk<Result>(const Result& result)
268{
269 return 0 == result.getErrorCode();
270}
271
272} // namespace result
273} // namespace a_util
274
275namespace std {
276template <>
277inline void swap<a_util::result::Result>(a_util::result::Result& lhs, a_util::result::Result& rhs)
278{
279 a_util::result::swap(lhs, rhs);
280}
281} // namespace std
282
283#endif // A_UTIL_UTIL_RESULT_DETAIL_RESULT_TYPE_IMPL_HEADER_INCLUDED
Definition result_type_decl.h:34
const char * getFile() const
Definition result_type_impl.h:177
const char * getErrorLabel() const
Definition result_type_impl.h:144
std::int32_t getLine() const
Definition result_type_impl.h:168
Result()
Definition result_type_impl.h:30
const char * getFunction() const
Definition result_type_impl.h:186
std::int32_t getErrorCode() const
Definition result_type_impl.h:136
const char * getDescription() const
Definition result_type_impl.h:154
friend void swap(Result &lhs, Result &rhs)
Definition result_type_impl.h:259
Result & operator=(const ResultInfo< void > &)
Definition result_type_impl.h:123
~Result()
Definition result_type_impl.h:97
void maybe_unused(T &&)
Definition types.h:51
Serves as namespace for result implementation details.
Definition result.h:22
Serves as component for functionality handling error and return types.
Definition result.h:20
void swap(Result &lhs, Result &rhs)
Definition result_type_impl.h:259
bool operator!=(const ResultInfo< ResultTypeLHS > &lhs, const ResultInfo< ResultTypeRHS > &rhs)
Definition result_info_impl.h:66
bool operator==(const ResultInfo< ResultTypeLHS > &lhs, const ResultInfo< ResultTypeRHS > &rhs)
Definition result_info_impl.h:58
bool isOk< Result >(const Result &result)
Definition result_type_impl.h:267
Serves as the root component, with common functionality documented in core functionality.
Definition base.h:24
Definition result_info_decl.h:86
static std::int32_t getCode()
Definition result_info_impl.h:42
static const char * getLabel()
Definition result_info_impl.h:36