Line data Source code
1 : /*!
2 : * \file esys/repo/result.cpp
3 : * \brief
4 : *
5 : * \cond
6 : * __legal_b__
7 : *
8 : * Copyright (c) 2022-2023 Michel Gillet
9 : * Distributed under the MIT License.
10 : * (See accompanying file LICENSE.txt or
11 : * copy at https://opensource.org/licenses/MIT)
12 : *
13 : * __legal_e__
14 : * \endcond
15 : *
16 : */
17 :
18 : #include "esys/repo/esysrepo_prec.h"
19 : #include "esys/repo/result.h"
20 :
21 : namespace esys::repo
22 : {
23 :
24 : Result Result::OK;
25 :
26 3976 : Result::Result() = default;
27 :
28 4084 : Result::Result(const Result &result)
29 : {
30 4084 : set_result_code(result.get_result_code());
31 4084 : set_error_info(result.get_error_info());
32 4084 : set_line_number(result.get_line_number());
33 4084 : }
34 :
35 7309 : Result::Result(ResultCode result_code, const std::string &file, int line_number, const std::string &function)
36 7309 : : m_result_code(result_code)
37 : {
38 7309 : handle_result_code(result_code, file, line_number, function);
39 7309 : }
40 :
41 40 : Result::Result(ResultCode result_code, const std::string &file, int line_number, const std::string &function,
42 40 : const std::string &text)
43 40 : : m_result_code(result_code)
44 : {
45 40 : handle_result_code(result_code, file, line_number, function);
46 :
47 80 : if (get_error_info() != nullptr)
48 : {
49 40 : auto error_info = get_error_info();
50 40 : error_info->set_text(text);
51 40 : }
52 40 : }
53 :
54 106 : Result::Result(ResultCode result_code, const std::string &file, int line_number, const std::string &function,
55 106 : int raw_error, const std::string &text)
56 106 : : m_result_code(result_code)
57 : {
58 106 : handle_result_code(result_code, file, line_number, function);
59 :
60 212 : if (get_error_info() != nullptr)
61 : {
62 106 : auto error_info = get_error_info();
63 106 : error_info->set_raw_error(raw_error);
64 106 : error_info->set_text(text);
65 106 : }
66 106 : }
67 :
68 1878 : Result::Result(const Result &result, const std::string &file, int line_number, const std::string &function)
69 : {
70 1878 : handle_result(result, file, line_number, function);
71 1878 : }
72 :
73 273 : Result::Result(const Result &result, const std::string &file, int line_number, const std::string &function,
74 273 : const std::string &text)
75 : {
76 273 : handle_result(result, file, line_number, function);
77 :
78 274 : if (get_error_info() != nullptr)
79 : {
80 2 : get_error_info()->set_text(text);
81 : }
82 273 : }
83 :
84 0 : Result::Result(const Result &result, ResultCode result_code, const std::string &file, int line_number,
85 0 : const std::string &function, const std::string &text)
86 : {
87 0 : handle_result(result, file, line_number, function);
88 :
89 0 : set_result_code(result_code);
90 :
91 0 : if (get_error_info() != nullptr)
92 : {
93 0 : get_error_info()->set_result_code(result_code);
94 0 : get_error_info()->set_text(text);
95 : }
96 0 : }
97 :
98 18217 : Result::~Result() = default;
99 :
100 14525 : void Result::set_result_code(ResultCode result_code)
101 : {
102 14525 : m_result_code = result_code;
103 14525 : }
104 :
105 9606 : void Result::handle_result_code(ResultCode result_code, const std::string &file, int line_number,
106 : const std::string &function)
107 : {
108 9606 : set_line_number(line_number);
109 :
110 9606 : if (result_code != ResultCode::OK)
111 : {
112 329 : auto error_info = std::make_shared<ErrorInfo>();
113 329 : error_info->set_file(file);
114 329 : error_info->set_line_number(line_number);
115 329 : error_info->set_function(function);
116 329 : error_info->set_result_code(result_code);
117 987 : set_error_info(error_info);
118 329 : }
119 9606 : }
120 :
121 2151 : void Result::handle_result(const Result &result, const std::string &file, int line_number, const std::string &function)
122 : {
123 2151 : set_result_code(result.get_result_code());
124 :
125 2151 : handle_result_code(result.get_result_code(), file, line_number, function);
126 :
127 2304 : if (get_error_info() != nullptr)
128 : {
129 153 : auto error_info = get_error_info();
130 153 : error_info->set_prev(result.get_error_info());
131 306 : error_info->set_index(result.get_error_info()->get_index() + 1);
132 153 : }
133 2151 : }
134 :
135 26202 : ResultCode Result::get_result_code() const
136 : {
137 26202 : return m_result_code;
138 : }
139 :
140 215 : int Result::get_result_code_int() const
141 : {
142 215 : return static_cast<int>(get_result_code());
143 : }
144 :
145 21915 : void Result::set_line_number(int line_number)
146 : {
147 21915 : m_line_number = line_number;
148 21915 : }
149 :
150 12356 : int Result::get_line_number() const
151 : {
152 12356 : return m_line_number;
153 : }
154 :
155 12703 : void Result::set_error_info(std::shared_ptr<ErrorInfo> error_info)
156 : {
157 12703 : m_error_info = error_info;
158 12703 : }
159 :
160 2894 : std::shared_ptr<ErrorInfo> Result::get_error_info()
161 : {
162 2894 : return m_error_info;
163 : }
164 :
165 1 : void Result::add(const Result &result)
166 : {
167 1 : if (result.ok()) return;
168 :
169 1 : if (get_error_info() == nullptr)
170 : {
171 1 : auto error_info = std::make_shared<ErrorInfo>();
172 1 : error_info->set_result_code(ResultCode::ERROR_VECTOR);
173 3 : set_error_info(error_info);
174 1 : }
175 :
176 1 : if (ok()) set_result_code(ResultCode::ERROR_VECTOR);
177 :
178 3 : get_error_info()->add_prev(result.get_error_info());
179 : }
180 :
181 12771 : const std::shared_ptr<ErrorInfo> Result::get_error_info() const
182 : {
183 12771 : return m_error_info;
184 : }
185 :
186 1894 : bool Result::success() const
187 : {
188 1894 : return get_result_code() == ResultCode::OK;
189 : }
190 :
191 1877 : bool Result::ok() const
192 : {
193 1877 : return success();
194 : }
195 :
196 7308 : bool Result::error() const
197 : {
198 7308 : return get_result_code() != ResultCode::OK;
199 : }
200 :
201 0 : Result::operator bool() const
202 : {
203 0 : return success();
204 : }
205 :
206 0 : Result::operator ResultCode() const
207 : {
208 0 : return get_result_code();
209 : }
210 :
211 8118 : Result &Result::operator=(const Result &other)
212 : {
213 8118 : if (&other == this) return *this;
214 :
215 8118 : set_result_code(other.get_result_code());
216 8118 : set_line_number(other.get_line_number());
217 8118 : set_error_info(other.get_error_info());
218 8118 : return *this;
219 : }
220 :
221 58 : bool Result::operator==(const ResultCode &result_code) const
222 : {
223 58 : return get_result_code() == result_code;
224 : }
225 :
226 0 : bool Result::operator!=(const ResultCode &result_code) const
227 : {
228 0 : return !operator==(result_code);
229 : }
230 :
231 0 : bool Result::operator==(const Result &other) const
232 : {
233 0 : if (get_result_code() != other.get_result_code()) return false;
234 :
235 : //! \TODO should the ErrorInfo be compared as well?
236 : return true;
237 : }
238 :
239 0 : bool Result::operator!=(const Result &other) const
240 : {
241 0 : return !operator==(other);
242 : }
243 :
244 2 : void Result::print(std::ostream &os) const
245 : {
246 2 : }
247 :
248 0 : ESYSREPO_API bool operator==(const ResultCode &result_code, const Result &result)
249 : {
250 0 : return result.get_result_code() == result_code;
251 : }
252 :
253 0 : ESYSREPO_API bool operator!=(const ResultCode &result_code, const Result &result)
254 : {
255 0 : return result.get_result_code() != result_code;
256 : }
257 :
258 : } // namespace esys::repo
259 :
260 : namespace std
261 : {
262 :
263 47 : ESYSREPO_API ostream &operator<<(ostream &os, const esys::repo::Result &result)
264 : {
265 47 : auto resul_code_str = esys::repo::ResultCode::s_find_name(result.get_result_code());
266 :
267 47 : os << resul_code_str << "(" << result.get_result_code_int() << ") line " << result.get_line_number();
268 47 : if (result.ok()) return os;
269 :
270 88 : if (result.get_error_info() == nullptr) return os;
271 44 : os << std::endl;
272 44 : os << *result.get_error_info().get();
273 44 : return os;
274 47 : }
275 :
276 : } // namespace std
|