iostream_support.hpp 9.9 KB

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