]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/outcome/iostream_support.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / outcome / iostream_support.hpp
1 /* iostream specialisations for result and outcome
2 (C) 2017-2019 Niall Douglas <http://www.nedproductions.biz/> (21 commits)
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
45 template <class T> inline std::ostream &operator<<(std::ostream &s, const value_storage_trivial<T> &v)
46 {
47 s << v._status << " ";
48 if((v._status & status_have_value) != 0)
49 {
50 s << v._value; // NOLINT
51 }
52 return s;
53 }
54 inline std::ostream &operator<<(std::ostream &s, const value_storage_trivial<void> &v)
55 {
56 s << v._status << " ";
57 return s;
58 }
59 template <class T> inline std::ostream &operator<<(std::ostream &s, const value_storage_nontrivial<T> &v)
60 {
61 s << v._status << " ";
62 if((v._status & status_have_value) != 0)
63 {
64 s << v._value; // NOLINT
65 }
66 return s;
67 }
68 template <class T> inline std::istream &operator>>(std::istream &s, value_storage_trivial<T> &v)
69 {
70 v = value_storage_trivial<T>();
71 s >> v._status;
72 if((v._status & status_have_value) != 0)
73 {
74 new(&v._value) decltype(v._value)(); // NOLINT
75 s >> v._value; // NOLINT
76 }
77 return s;
78 }
79 inline std::istream &operator>>(std::istream &s, value_storage_trivial<devoid<void>> &v)
80 {
81 v = value_storage_trivial<devoid<void>>();
82 s >> v._status;
83 return s;
84 }
85 template <class T> inline std::istream &operator>>(std::istream &s, value_storage_nontrivial<T> &v)
86 {
87 v = value_storage_nontrivial<T>();
88 s >> v._status;
89 if((v._status & status_have_value) != 0)
90 {
91 new(&v._value) decltype(v._value)(); // NOLINT
92 s >> v._value; // NOLINT
93 }
94 return s;
95 }
96 BOOST_OUTCOME_TEMPLATE(class T)
97 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_constructible<std::error_code, T>::value))
98 inline std::string safe_message(T && /*unused*/) { return {}; }
99 inline std::string safe_message(const std::error_code &ec) { return " (" + ec.message() + ")"; }
100 } // namespace detail
101
102 /*! AWAITING HUGO JSON CONVERSION TOOL
103 SIGNATURE NOT RECOGNISED
104 */
105 BOOST_OUTCOME_TEMPLATE(class R, class S, class P)
106 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<R>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<S>()))
107 inline std::istream &operator>>(std::istream &s, basic_result<R, S, P> &v)
108 {
109 s >> v._iostreams_state();
110 if(v.has_error())
111 {
112 s >> v.assume_error();
113 }
114 return s;
115 }
116 /*! AWAITING HUGO JSON CONVERSION TOOL
117 SIGNATURE NOT RECOGNISED
118 */
119 BOOST_OUTCOME_TEMPLATE(class R, class S, class P)
120 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<R>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<S>()))
121 inline std::ostream &operator<<(std::ostream &s, const basic_result<R, S, P> &v)
122 {
123 s << v._iostreams_state();
124 if(v.has_error())
125 {
126 s << v.assume_error();
127 }
128 return s;
129 }
130 /*! AWAITING HUGO JSON CONVERSION TOOL
131 SIGNATURE NOT RECOGNISED
132 */
133 template <class R, class S, class P> inline std::string print(const basic_result<R, S, P> &v)
134 {
135 std::stringstream s;
136 if(v.has_value())
137 {
138 s << v.value();
139 }
140 if(v.has_error())
141 {
142 s << v.error() << detail::safe_message(v.error());
143 }
144 return s.str();
145 }
146 /*! AWAITING HUGO JSON CONVERSION TOOL
147 SIGNATURE NOT RECOGNISED
148 */
149 template <class S, class P> inline std::string print(const basic_result<void, S, P> &v)
150 {
151 std::stringstream s;
152 if(v.has_value())
153 {
154 s << "(+void)";
155 }
156 if(v.has_error())
157 {
158 s << v.error() << detail::safe_message(v.error());
159 }
160 return s.str();
161 }
162 /*! AWAITING HUGO JSON CONVERSION TOOL
163 SIGNATURE NOT RECOGNISED
164 */
165 template <class R, class P> inline std::string print(const basic_result<R, void, P> &v)
166 {
167 std::stringstream s;
168 if(v.has_value())
169 {
170 s << v.value();
171 }
172 if(v.has_error())
173 {
174 s << "(-void)";
175 }
176 return s.str();
177 }
178 /*! AWAITING HUGO JSON CONVERSION TOOL
179 SIGNATURE NOT RECOGNISED
180 */
181 template <class P> inline std::string print(const basic_result<void, void, P> &v)
182 {
183 std::stringstream s;
184 if(v.has_value())
185 {
186 s << "(+void)";
187 }
188 if(v.has_error())
189 {
190 s << "(-void)";
191 }
192 return s.str();
193 }
194
195 /*! AWAITING HUGO JSON CONVERSION TOOL
196 SIGNATURE NOT RECOGNISED
197 */
198 BOOST_OUTCOME_TEMPLATE(class R, class S, class P, class N)
199 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>()))
200 inline std::istream &operator>>(std::istream &s, outcome<R, S, P, N> &v)
201 {
202 s >> v._iostreams_state();
203 if(v.has_error())
204 {
205 s >> v.assume_error();
206 }
207 if(v.has_exception())
208 {
209 s >> v.assume_exception();
210 }
211 return s;
212 }
213 /*! AWAITING HUGO JSON CONVERSION TOOL
214 SIGNATURE NOT RECOGNISED
215 */
216 BOOST_OUTCOME_TEMPLATE(class R, class S, class P, class N)
217 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>()))
218 inline std::ostream &operator<<(std::ostream &s, const outcome<R, S, P, N> &v)
219 {
220 s << v._iostreams_state();
221 if(v.has_error())
222 {
223 s << v.assume_error();
224 }
225 if(v.has_exception())
226 {
227 s << v.assume_exception();
228 }
229 return s;
230 }
231 /*! AWAITING HUGO JSON CONVERSION TOOL
232 SIGNATURE NOT RECOGNISED
233 */
234 template <class R, class S, class P, class N> inline std::string print(const outcome<R, S, P, N> &v)
235 {
236 std::stringstream s;
237 int total = static_cast<int>(v.has_value()) + static_cast<int>(v.has_error()) + static_cast<int>(v.has_exception());
238 if(total > 1)
239 {
240 s << "{ ";
241 }
242 s << print(static_cast<const basic_result<R, S, N> &>(static_cast<const detail::basic_result_final<R, S, N> &>(v))); // NOLINT
243 if(total > 1)
244 {
245 s << ", ";
246 }
247 if(v.has_exception())
248 {
249 #ifndef BOOST_NO_EXCEPTIONS
250 try
251 {
252 rethrow_exception(v.exception());
253 }
254 catch(const std::system_error &e)
255 {
256 s << "std::system_error code " << e.code() << ": " << e.what();
257 }
258 catch(const std::exception &e)
259 {
260 s << "std::exception: " << e.what();
261 }
262 catch(...)
263 #endif
264 {
265 s << "unknown exception";
266 }
267 }
268 if(total > 1)
269 {
270 s << " }";
271 }
272 return s.str();
273 }
274 BOOST_OUTCOME_V2_NAMESPACE_END
275
276 #endif