]>
Commit | Line | Data |
---|---|---|
92f5a8d4 | 1 | /* iostream specialisations for result and outcome |
1e59de90 | 2 | (C) 2017-2022 Niall Douglas <http://www.nedproductions.biz/> (21 commits) |
92f5a8d4 TL |
3 | File Created: July 2017 |
4 | ||
5 | ||
6 | Boost Software License - Version 1.0 - August 17th, 2003 | |
7 | ||
8 | Permission is hereby granted, free of charge, to any person or organization | |
9 | obtaining a copy of the software and accompanying documentation covered by | |
10 | this license (the "Software") to use, reproduce, display, distribute, | |
11 | execute, and transmit the Software, and to prepare derivative works of the | |
12 | Software, and to permit third-parties to whom the Software is furnished to | |
13 | do so, all subject to the following: | |
14 | ||
15 | The copyright notices in the Software and this entire statement, including | |
16 | the above license grant, this restriction and the following disclaimer, | |
17 | must be included in all copies of the Software, in whole or in part, and | |
18 | all derivative works of the Software, unless such copies or derivative | |
19 | works are solely in the form of machine-executable object code generated by | |
20 | a source language processor. | |
21 | ||
22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
24 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT | |
25 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE | |
26 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, | |
27 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
28 | DEALINGS IN THE SOFTWARE. | |
29 | */ | |
30 | ||
31 | #ifndef BOOST_OUTCOME_IOSTREAM_SUPPORT_HPP | |
32 | #define BOOST_OUTCOME_IOSTREAM_SUPPORT_HPP | |
33 | ||
34 | #include "outcome.hpp" | |
35 | ||
36 | #include <iostream> | |
37 | #include <sstream> | |
38 | ||
39 | BOOST_OUTCOME_V2_NAMESPACE_BEGIN | |
40 | ||
41 | namespace detail | |
42 | { | |
43 | template <class T> typename std::add_lvalue_reference<T>::type lvalueref() noexcept; | |
44 | ||
1e59de90 | 45 | template <template <class, class> class ValueStorage, class T, class E> inline std::ostream &value_storage_out(std::ostream &s, const ValueStorage<T, E> &v) |
92f5a8d4 | 46 | { |
f67539c2 TL |
47 | s << static_cast<uint16_t>(v._status.status_value) << " " << v._status.spare_storage_value << " "; |
48 | if(v._status.have_value()) | |
92f5a8d4 TL |
49 | { |
50 | s << v._value; // NOLINT | |
51 | } | |
1e59de90 TL |
52 | if(v._status.have_error()) |
53 | { | |
54 | s << v._error; // NOLINT | |
55 | } | |
92f5a8d4 TL |
56 | return s; |
57 | } | |
1e59de90 | 58 | template <template <class, class> class ValueStorage, class E> inline std::ostream &value_storage_out(std::ostream &s, const ValueStorage<void, E> &v) |
92f5a8d4 | 59 | { |
f67539c2 | 60 | s << static_cast<uint16_t>(v._status.status_value) << " " << v._status.spare_storage_value << " "; |
1e59de90 TL |
61 | if(v._status.have_error()) |
62 | { | |
63 | s << v._error; // NOLINT | |
64 | } | |
92f5a8d4 TL |
65 | return s; |
66 | } | |
1e59de90 | 67 | template <template <class, class> class ValueStorage, class T> inline std::ostream &value_storage_out(std::ostream &s, const ValueStorage<T, void> &v) |
92f5a8d4 | 68 | { |
f67539c2 TL |
69 | s << static_cast<uint16_t>(v._status.status_value) << " " << v._status.spare_storage_value << " "; |
70 | if(v._status.have_value()) | |
92f5a8d4 TL |
71 | { |
72 | s << v._value; // NOLINT | |
73 | } | |
74 | return s; | |
75 | } | |
1e59de90 TL |
76 | |
77 | template <class T, class E> inline std::ostream &operator<<(std::ostream &s, const value_storage_trivial<T, E> &v) { return value_storage_out(s, v); } | |
78 | template <class T, class E> inline std::ostream &operator<<(std::ostream &s, const value_storage_nontrivial<T, E> &v) { return value_storage_out(s, v); } | |
79 | ||
80 | template <template <class, class> class ValueStorage, class T, class E> inline std::istream &value_storage_in(std::istream &s, ValueStorage<T, E> &v) | |
92f5a8d4 | 81 | { |
1e59de90 TL |
82 | using type = ValueStorage<T, E>; |
83 | v.~type(); | |
84 | new(&v) type; | |
f67539c2 TL |
85 | uint16_t x, y; |
86 | s >> x >> y; | |
87 | v._status.status_value = static_cast<detail::status>(x); | |
88 | v._status.spare_storage_value = y; | |
89 | if(v._status.have_value()) | |
92f5a8d4 TL |
90 | { |
91 | new(&v._value) decltype(v._value)(); // NOLINT | |
92 | s >> v._value; // NOLINT | |
93 | } | |
1e59de90 TL |
94 | if(v._status.have_error()) |
95 | { | |
96 | new(&v._error) decltype(v._error)(); // NOLINT | |
97 | s >> v._error; // NOLINT | |
98 | } | |
92f5a8d4 TL |
99 | return s; |
100 | } | |
1e59de90 | 101 | template <template <class, class> class ValueStorage, class E> inline std::istream &value_storage_in(std::istream &s, ValueStorage<void, E> &v) |
92f5a8d4 | 102 | { |
1e59de90 TL |
103 | using type = ValueStorage<void, E>; |
104 | v.~type(); | |
105 | new(&v) type; | |
f67539c2 TL |
106 | uint16_t x, y; |
107 | s >> x >> y; | |
108 | v._status.status_value = static_cast<detail::status>(x); | |
109 | v._status.spare_storage_value = y; | |
1e59de90 TL |
110 | if(v._status.have_error()) |
111 | { | |
112 | new(&v._error) decltype(v._error)(); // NOLINT | |
113 | s >> v._error; // NOLINT | |
114 | } | |
92f5a8d4 TL |
115 | return s; |
116 | } | |
1e59de90 | 117 | template <template <class, class> class ValueStorage, class T> inline std::istream &value_storage_in(std::istream &s, ValueStorage<T, void> &v) |
92f5a8d4 | 118 | { |
1e59de90 TL |
119 | using type = ValueStorage<T, void>; |
120 | v.~type(); | |
121 | new(&v) type; | |
f67539c2 TL |
122 | uint16_t x, y; |
123 | s >> x >> y; | |
124 | v._status.status_value = static_cast<detail::status>(x); | |
125 | v._status.spare_storage_value = y; | |
126 | if(v._status.have_value()) | |
92f5a8d4 TL |
127 | { |
128 | new(&v._value) decltype(v._value)(); // NOLINT | |
129 | s >> v._value; // NOLINT | |
130 | } | |
131 | return s; | |
132 | } | |
1e59de90 TL |
133 | template <class T, class E> inline std::istream &operator>>(std::istream &s, value_storage_trivial<T, E> &v) { return value_storage_in(s, v); } |
134 | template <class T, class E> inline std::istream &operator>>(std::istream &s, value_storage_nontrivial<T, E> &v) { return value_storage_in(s, v); } | |
92f5a8d4 TL |
135 | BOOST_OUTCOME_TEMPLATE(class T) |
136 | BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_constructible<std::error_code, T>::value)) | |
137 | inline std::string safe_message(T && /*unused*/) { return {}; } | |
138 | inline std::string safe_message(const std::error_code &ec) { return " (" + ec.message() + ")"; } | |
139 | } // namespace detail | |
140 | ||
141 | /*! AWAITING HUGO JSON CONVERSION TOOL | |
142 | SIGNATURE NOT RECOGNISED | |
143 | */ | |
144 | BOOST_OUTCOME_TEMPLATE(class R, class S, class P) | |
145 | BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<R>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<S>())) | |
146 | inline std::istream &operator>>(std::istream &s, basic_result<R, S, P> &v) | |
147 | { | |
148 | s >> v._iostreams_state(); | |
149 | if(v.has_error()) | |
150 | { | |
151 | s >> v.assume_error(); | |
152 | } | |
153 | return s; | |
154 | } | |
155 | /*! AWAITING HUGO JSON CONVERSION TOOL | |
156 | SIGNATURE NOT RECOGNISED | |
157 | */ | |
158 | BOOST_OUTCOME_TEMPLATE(class R, class S, class P) | |
159 | BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<R>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<S>())) | |
160 | inline std::ostream &operator<<(std::ostream &s, const basic_result<R, S, P> &v) | |
161 | { | |
162 | s << v._iostreams_state(); | |
163 | if(v.has_error()) | |
164 | { | |
165 | s << v.assume_error(); | |
166 | } | |
167 | return s; | |
168 | } | |
169 | /*! AWAITING HUGO JSON CONVERSION TOOL | |
170 | SIGNATURE NOT RECOGNISED | |
171 | */ | |
172 | template <class R, class S, class P> inline std::string print(const basic_result<R, S, P> &v) | |
173 | { | |
174 | std::stringstream s; | |
175 | if(v.has_value()) | |
176 | { | |
177 | s << v.value(); | |
178 | } | |
179 | if(v.has_error()) | |
180 | { | |
181 | s << v.error() << detail::safe_message(v.error()); | |
182 | } | |
183 | return s.str(); | |
184 | } | |
185 | /*! AWAITING HUGO JSON CONVERSION TOOL | |
186 | SIGNATURE NOT RECOGNISED | |
187 | */ | |
188 | template <class S, class P> inline std::string print(const basic_result<void, S, P> &v) | |
189 | { | |
190 | std::stringstream s; | |
191 | if(v.has_value()) | |
192 | { | |
193 | s << "(+void)"; | |
194 | } | |
195 | if(v.has_error()) | |
196 | { | |
197 | s << v.error() << detail::safe_message(v.error()); | |
198 | } | |
199 | return s.str(); | |
200 | } | |
201 | /*! AWAITING HUGO JSON CONVERSION TOOL | |
202 | SIGNATURE NOT RECOGNISED | |
203 | */ | |
204 | template <class R, class P> inline std::string print(const basic_result<R, void, P> &v) | |
205 | { | |
206 | std::stringstream s; | |
207 | if(v.has_value()) | |
208 | { | |
209 | s << v.value(); | |
210 | } | |
211 | if(v.has_error()) | |
212 | { | |
213 | s << "(-void)"; | |
214 | } | |
215 | return s.str(); | |
216 | } | |
217 | /*! AWAITING HUGO JSON CONVERSION TOOL | |
218 | SIGNATURE NOT RECOGNISED | |
219 | */ | |
220 | template <class P> inline std::string print(const basic_result<void, void, P> &v) | |
221 | { | |
222 | std::stringstream s; | |
223 | if(v.has_value()) | |
224 | { | |
225 | s << "(+void)"; | |
226 | } | |
227 | if(v.has_error()) | |
228 | { | |
229 | s << "(-void)"; | |
230 | } | |
231 | return s.str(); | |
232 | } | |
233 | ||
234 | /*! AWAITING HUGO JSON CONVERSION TOOL | |
235 | SIGNATURE NOT RECOGNISED | |
236 | */ | |
237 | BOOST_OUTCOME_TEMPLATE(class R, class S, class P, class N) | |
238 | BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<R>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<S>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<P>())) | |
239 | inline std::istream &operator>>(std::istream &s, outcome<R, S, P, N> &v) | |
240 | { | |
241 | s >> v._iostreams_state(); | |
242 | if(v.has_error()) | |
243 | { | |
244 | s >> v.assume_error(); | |
245 | } | |
246 | if(v.has_exception()) | |
247 | { | |
248 | s >> v.assume_exception(); | |
249 | } | |
250 | return s; | |
251 | } | |
252 | /*! AWAITING HUGO JSON CONVERSION TOOL | |
253 | SIGNATURE NOT RECOGNISED | |
254 | */ | |
255 | BOOST_OUTCOME_TEMPLATE(class R, class S, class P, class N) | |
256 | BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<R>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<S>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<P>())) | |
257 | inline std::ostream &operator<<(std::ostream &s, const outcome<R, S, P, N> &v) | |
258 | { | |
259 | s << v._iostreams_state(); | |
260 | if(v.has_error()) | |
261 | { | |
262 | s << v.assume_error(); | |
263 | } | |
264 | if(v.has_exception()) | |
265 | { | |
266 | s << v.assume_exception(); | |
267 | } | |
268 | return s; | |
269 | } | |
270 | /*! AWAITING HUGO JSON CONVERSION TOOL | |
271 | SIGNATURE NOT RECOGNISED | |
272 | */ | |
273 | template <class R, class S, class P, class N> inline std::string print(const outcome<R, S, P, N> &v) | |
274 | { | |
275 | std::stringstream s; | |
276 | int total = static_cast<int>(v.has_value()) + static_cast<int>(v.has_error()) + static_cast<int>(v.has_exception()); | |
277 | if(total > 1) | |
278 | { | |
279 | s << "{ "; | |
280 | } | |
281 | s << print(static_cast<const basic_result<R, S, N> &>(static_cast<const detail::basic_result_final<R, S, N> &>(v))); // NOLINT | |
282 | if(total > 1) | |
283 | { | |
284 | s << ", "; | |
285 | } | |
286 | if(v.has_exception()) | |
287 | { | |
288 | #ifndef BOOST_NO_EXCEPTIONS | |
289 | try | |
290 | { | |
291 | rethrow_exception(v.exception()); | |
292 | } | |
293 | catch(const std::system_error &e) | |
294 | { | |
295 | s << "std::system_error code " << e.code() << ": " << e.what(); | |
296 | } | |
297 | catch(const std::exception &e) | |
298 | { | |
299 | s << "std::exception: " << e.what(); | |
300 | } | |
301 | catch(...) | |
302 | #endif | |
303 | { | |
304 | s << "unknown exception"; | |
305 | } | |
306 | } | |
307 | if(total > 1) | |
308 | { | |
309 | s << " }"; | |
310 | } | |
311 | return s.str(); | |
312 | } | |
313 | BOOST_OUTCOME_V2_NAMESPACE_END | |
314 | ||
315 | #endif |