]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/outcome/iostream_support.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / outcome / iostream_support.hpp
CommitLineData
92f5a8d4 1/* iostream specialisations for result and outcome
1e59de90 2(C) 2017-2022 Niall Douglas <http://www.nedproductions.biz/> (21 commits)
92f5a8d4
TL
3File Created: July 2017
4
5
6Boost Software License - Version 1.0 - August 17th, 2003
7
8Permission is hereby granted, free of charge, to any person or organization
9obtaining a copy of the software and accompanying documentation covered by
10this license (the "Software") to use, reproduce, display, distribute,
11execute, and transmit the Software, and to prepare derivative works of the
12Software, and to permit third-parties to whom the Software is furnished to
13do so, all subject to the following:
14
15The copyright notices in the Software and this entire statement, including
16the above license grant, this restriction and the following disclaimer,
17must be included in all copies of the Software, in whole or in part, and
18all derivative works of the Software, unless such copies or derivative
19works are solely in the form of machine-executable object code generated by
20a source language processor.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
25SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
26FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
27ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28DEALINGS 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
39BOOST_OUTCOME_V2_NAMESPACE_BEGIN
40
41namespace 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
142SIGNATURE NOT RECOGNISED
143*/
144BOOST_OUTCOME_TEMPLATE(class R, class S, class P)
145BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<R>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<S>()))
146inline 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
156SIGNATURE NOT RECOGNISED
157*/
158BOOST_OUTCOME_TEMPLATE(class R, class S, class P)
159BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<R>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<S>()))
160inline 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
170SIGNATURE NOT RECOGNISED
171*/
172template <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
186SIGNATURE NOT RECOGNISED
187*/
188template <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
202SIGNATURE NOT RECOGNISED
203*/
204template <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
218SIGNATURE NOT RECOGNISED
219*/
220template <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
235SIGNATURE NOT RECOGNISED
236*/
237BOOST_OUTCOME_TEMPLATE(class R, class S, class P, class N)
238BOOST_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>()))
239inline 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
253SIGNATURE NOT RECOGNISED
254*/
255BOOST_OUTCOME_TEMPLATE(class R, class S, class P, class N)
256BOOST_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>()))
257inline 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
271SIGNATURE NOT RECOGNISED
272*/
273template <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}
313BOOST_OUTCOME_V2_NAMESPACE_END
314
315#endif