iostream_support_result.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* iostream specialisations for result
  2. (C) 2017-2025 Niall Douglas <http://www.nedproductions.biz/> (21 commits)
  3. File Created: May 2025
  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_RESULT_HPP
  26. #define BOOST_OUTCOME_IOSTREAM_SUPPORT_RESULT_HPP
  27. #include "result.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)
  66. {
  67. return value_storage_out(s, v);
  68. }
  69. template <class T, class E> inline std::ostream &operator<<(std::ostream &s, const value_storage_nontrivial<T, E> &v)
  70. {
  71. return value_storage_out(s, v);
  72. }
  73. template <template <class, class> class ValueStorage, class T, class E> inline std::istream &value_storage_in(std::istream &s, ValueStorage<T, E> &v)
  74. {
  75. using type = ValueStorage<T, E>;
  76. v.~type();
  77. new(&v) type;
  78. uint16_t x, y;
  79. s >> x >> y;
  80. v._status.status_value = static_cast<detail::status>(x);
  81. v._status.spare_storage_value = y;
  82. if(v._status.have_value())
  83. {
  84. new(BOOST_OUTCOME_ADDRESS_OF(v._value)) decltype(v._value)(); // NOLINT
  85. s >> v._value; // NOLINT
  86. }
  87. if(v._status.have_error())
  88. {
  89. new(BOOST_OUTCOME_ADDRESS_OF(v._error)) decltype(v._error)(); // NOLINT
  90. s >> v._error; // NOLINT
  91. }
  92. return s;
  93. }
  94. template <template <class, class> class ValueStorage, class E> inline std::istream &value_storage_in(std::istream &s, ValueStorage<void, E> &v)
  95. {
  96. using type = ValueStorage<void, E>;
  97. v.~type();
  98. new(&v) type;
  99. uint16_t x, y;
  100. s >> x >> y;
  101. v._status.status_value = static_cast<detail::status>(x);
  102. v._status.spare_storage_value = y;
  103. if(v._status.have_error())
  104. {
  105. new(BOOST_OUTCOME_ADDRESS_OF(v._error)) decltype(v._error)(); // NOLINT
  106. s >> v._error; // NOLINT
  107. }
  108. return s;
  109. }
  110. template <template <class, class> class ValueStorage, class T> inline std::istream &value_storage_in(std::istream &s, ValueStorage<T, void> &v)
  111. {
  112. using type = ValueStorage<T, void>;
  113. v.~type();
  114. new(&v) type;
  115. uint16_t x, y;
  116. s >> x >> y;
  117. v._status.status_value = static_cast<detail::status>(x);
  118. v._status.spare_storage_value = y;
  119. if(v._status.have_value())
  120. {
  121. new(BOOST_OUTCOME_ADDRESS_OF(v._value)) decltype(v._value)(); // NOLINT
  122. s >> v._value; // NOLINT
  123. }
  124. return s;
  125. }
  126. template <class T, class E> inline std::istream &operator>>(std::istream &s, value_storage_trivial<T, E> &v)
  127. {
  128. return value_storage_in(s, v);
  129. }
  130. template <class T, class E> inline std::istream &operator>>(std::istream &s, value_storage_nontrivial<T, E> &v)
  131. {
  132. return value_storage_in(s, v);
  133. }
  134. BOOST_OUTCOME_TEMPLATE(class T)
  135. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_constructible<std::error_code, T>::value))
  136. inline std::string safe_message(T && /*unused*/)
  137. {
  138. return {};
  139. }
  140. inline std::string safe_message(const std::error_code &ec)
  141. {
  142. return " (" + ec.message() + ")";
  143. }
  144. } // namespace detail
  145. /*! AWAITING HUGO JSON CONVERSION TOOL
  146. SIGNATURE NOT RECOGNISED
  147. */
  148. BOOST_OUTCOME_TEMPLATE(class R, class S, class P)
  149. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<R>()),
  150. BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<S>()))
  151. inline std::istream &operator>>(std::istream &s, basic_result<R, S, P> &v)
  152. {
  153. s >> v._iostreams_state();
  154. if(v.has_error())
  155. {
  156. s >> v.assume_error();
  157. }
  158. return s;
  159. }
  160. /*! AWAITING HUGO JSON CONVERSION TOOL
  161. SIGNATURE NOT RECOGNISED
  162. */
  163. BOOST_OUTCOME_TEMPLATE(class R, class S, class P)
  164. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<R>()),
  165. BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<S>()))
  166. inline std::ostream &operator<<(std::ostream &s, const basic_result<R, S, P> &v)
  167. {
  168. s << v._iostreams_state();
  169. if(v.has_error())
  170. {
  171. s << v.assume_error();
  172. }
  173. return s;
  174. }
  175. /*! AWAITING HUGO JSON CONVERSION TOOL
  176. SIGNATURE NOT RECOGNISED
  177. */
  178. template <class R, class S, class P> inline std::string print(const basic_result<R, S, P> &v)
  179. {
  180. std::stringstream s;
  181. if(v.has_value())
  182. {
  183. s << v.value();
  184. }
  185. if(v.has_error())
  186. {
  187. s << v.error() << detail::safe_message(v.error());
  188. }
  189. return s.str();
  190. }
  191. /*! AWAITING HUGO JSON CONVERSION TOOL
  192. SIGNATURE NOT RECOGNISED
  193. */
  194. template <class S, class P> inline std::string print(const basic_result<void, S, P> &v)
  195. {
  196. std::stringstream s;
  197. if(v.has_value())
  198. {
  199. s << "(+void)";
  200. }
  201. if(v.has_error())
  202. {
  203. s << v.error() << detail::safe_message(v.error());
  204. }
  205. return s.str();
  206. }
  207. /*! AWAITING HUGO JSON CONVERSION TOOL
  208. SIGNATURE NOT RECOGNISED
  209. */
  210. template <class R, class P> inline std::string print(const basic_result<R, void, P> &v)
  211. {
  212. std::stringstream s;
  213. if(v.has_value())
  214. {
  215. s << v.value();
  216. }
  217. if(v.has_error())
  218. {
  219. s << "(-void)";
  220. }
  221. return s.str();
  222. }
  223. /*! AWAITING HUGO JSON CONVERSION TOOL
  224. SIGNATURE NOT RECOGNISED
  225. */
  226. template <class P> inline std::string print(const basic_result<void, void, P> &v)
  227. {
  228. std::stringstream s;
  229. if(v.has_value())
  230. {
  231. s << "(+void)";
  232. }
  233. if(v.has_error())
  234. {
  235. s << "(-void)";
  236. }
  237. return s.str();
  238. }
  239. BOOST_OUTCOME_V2_NAMESPACE_END
  240. #endif