iostream_support.hpp 7.9 KB

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