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