expected.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #pragma once
  2. #include <type_traits>
  3. #ifdef __cpp_lib_expected
  4. #if __cpp_lib_expected >= 202211L
  5. #include <expected>
  6. namespace jvalidate::detail {
  7. using std::expected;
  8. using std::unexpected;
  9. inline constexpr std::unexpect_t unexpect{};
  10. }
  11. #endif
  12. #else
  13. #include <initializer_list>
  14. #include <optional>
  15. #include <utility>
  16. #include <variant>
  17. #include <jvalidate/detail/expect.h>
  18. namespace jvalidate::detail {
  19. inline constexpr struct unexpect_t {
  20. } unexpect{};
  21. template <typename E> class unexpected {
  22. public:
  23. template <typename Err = E>
  24. constexpr explicit unexpected(Err && e) : error_(std::forward<Err>(e)) {}
  25. template <typename... Args>
  26. constexpr explicit unexpected(std::in_place_t, Args &&... args)
  27. : error_{std::forward<Args>(args)...} {}
  28. template <typename U, typename... Args>
  29. constexpr explicit unexpected(std::in_place_t, std::initializer_list<U> il, Args &&... args)
  30. : error_{il, std::forward<Args>(args)...} {}
  31. constexpr const E & error() const & noexcept { return error_; }
  32. constexpr E & error() & noexcept { return error_; }
  33. constexpr E && error() && noexcept { return std::move(error_); }
  34. constexpr void swap(unexpected & other) noexcept(std::is_nothrow_swappable_v<E>) {
  35. using std::swap;
  36. swap(error(), other.error());
  37. }
  38. template <typename E2> friend constexpr bool operator==(unexpected & x, unexpected<E2> & y) {
  39. return x.error() == y.error();
  40. }
  41. friend constexpr void swap(unexpected & x, unexpected & y) noexcept(noexcept(x.swap(y))) {
  42. return x.swap(y);
  43. }
  44. private:
  45. E error_;
  46. };
  47. template <typename E> unexpected(E) -> unexpected<E>;
  48. template <typename E> class bad_expected_access;
  49. template <> class bad_expected_access<void> : public std::exception {
  50. public:
  51. char const * what() const noexcept override { return "unexpected"; }
  52. };
  53. template <typename E> class bad_expected_access : public bad_expected_access<void> {
  54. public:
  55. bad_expected_access(E error) : error_(std::move(error)) {}
  56. constexpr const E & error() const & noexcept { return error_; }
  57. constexpr E & error() & noexcept { return error_; }
  58. constexpr E && error() && noexcept { return std::move(error_); }
  59. private:
  60. E error_;
  61. };
  62. template <typename T, typename E> class expected {
  63. public:
  64. using value_type = T;
  65. using error_type = E;
  66. using unexpected_type = unexpected<E>;
  67. template <typename U> using rebind = expected<U, error_type>;
  68. public:
  69. constexpr expected() = default;
  70. template <typename U, typename G>
  71. constexpr explicit(!std::is_convertible_v<const U &, T> && !std::is_convertible_v<const G &, E>)
  72. expected(expected<U, G> const & other) {
  73. if (other.has_value()) {
  74. value_.template emplace<0>(*other);
  75. } else {
  76. value_.template emplace<1>(other.error());
  77. }
  78. }
  79. template <typename U, typename G>
  80. constexpr explicit(!std::is_convertible_v<U, T> && !std::is_convertible_v<G, E>)
  81. expected(expected<U, G> && other) {
  82. if (other.has_value()) {
  83. value_.template emplace<0>(*std::move(other));
  84. } else {
  85. value_.template emplace<1>(std::move(other).error());
  86. }
  87. }
  88. template <typename U = std::remove_cv_t<T>>
  89. constexpr explicit(!std::is_convertible_v<U, T>) expected(U && v)
  90. : value_(std::in_place_index<0>, std::forward<T>(v)) {}
  91. template <typename G>
  92. constexpr explicit(!std::is_convertible_v<const G &, E>) expected(unexpected<G> const & e)
  93. : value_(std::in_place_index<1>, e.error()) {}
  94. template <typename G>
  95. constexpr explicit(!std::is_convertible_v<G, E>) expected(unexpected<G> && e)
  96. : value_(std::in_place_index<1>, std::move(e).error()) {}
  97. template <typename... Args>
  98. constexpr explicit expected(std::in_place_t, Args &&... args)
  99. : value_(std::in_place_index<0>, std::forward<Args>(args)...) {}
  100. template <typename U, typename... Args>
  101. constexpr explicit expected(std::in_place_t, std::initializer_list<U> il, Args &&... args)
  102. : value_(std::in_place_index<0>, il, std::forward<Args>(args)...) {}
  103. template <typename... Args>
  104. constexpr explicit expected(unexpect_t, Args &&... args)
  105. : value_(std::in_place_index<1>, std::forward<Args>(args)...) {}
  106. template <typename U, typename... Args>
  107. constexpr explicit expected(unexpect_t, std::initializer_list<U> il, Args &&... args)
  108. : value_(std::in_place_index<1>, il, std::forward<Args>(args)...) {}
  109. constexpr explicit operator bool() const noexcept { return has_value(); }
  110. constexpr bool has_value() const noexcept { return value_.index() == 0; }
  111. constexpr const T * operator->() const noexcept { return std::get_if<0>(&value_); }
  112. constexpr T * operator->() noexcept { return std::get_if<0>(&value_); }
  113. constexpr const T & operator*() const & noexcept { return *std::get_if<0>(&value_); }
  114. constexpr T & operator*() & noexcept { return *std::get_if<0>(&value_); }
  115. constexpr T && operator*() && noexcept { return std::move(*std::get_if<0>(&value_)); }
  116. constexpr const T & value() const & {
  117. if (JVALIDATE_LIKELY(has_value())) {
  118. return operator*();
  119. }
  120. throw bad_expected_access(error());
  121. }
  122. constexpr T & value() & {
  123. if (JVALIDATE_LIKELY(has_value())) {
  124. return operator*();
  125. }
  126. throw bad_expected_access(std::as_const(error()));
  127. }
  128. constexpr T && value() && {
  129. if (JVALIDATE_LIKELY(has_value())) {
  130. return std::move(*this).operator*();
  131. }
  132. throw bad_expected_access(std::move(error()));
  133. }
  134. constexpr const E & error() const & noexcept { return *std::get_if<1>(&value_); }
  135. constexpr E & error() & noexcept { return *std::get_if<1>(&value_); }
  136. constexpr E && error() && noexcept { return std::move(*std::get_if<1>(&value_)); }
  137. template <typename F> constexpr auto transform(F && f) & {
  138. using G = std::invoke_result_t<F, value_type &>;
  139. if (has_value()) {
  140. return expected<G, error_type>(std::forward<F>(f)(**this));
  141. }
  142. return expected<G, error_type>(unexpect, error());
  143. }
  144. template <typename F> constexpr auto transform(F && f) const & {
  145. using G = std::invoke_result_t<F, value_type const &>;
  146. if (has_value()) {
  147. return expected<G, error_type>(std::forward<F>(f)(**this));
  148. }
  149. return expected<G, error_type>(unexpect, error());
  150. }
  151. template <typename F> constexpr auto transform(F && f) && {
  152. using G = std::invoke_result_t<F, value_type>;
  153. if (has_value()) {
  154. return expected<G, error_type>(std::forward<F>(f)(*std::move(*this)));
  155. }
  156. return expected<G, error_type>(unexpect, std::move(*this).error());
  157. }
  158. template <typename F> constexpr auto transform_error(F && f) & {
  159. using G = std::invoke_result_t<F, E &>;
  160. if (has_value()) {
  161. return expected<value_type, G>(**this);
  162. }
  163. return expected<value_type, G>(unexpect, std::forward<F>(f)(error()));
  164. }
  165. template <typename F> constexpr auto transform_error(F && f) const & {
  166. using G = std::invoke_result_t<F, E const &>;
  167. if (has_value()) {
  168. return expected<value_type, G>(**this);
  169. }
  170. return expected<value_type, G>(unexpect, std::forward<F>(f)(error()));
  171. }
  172. template <typename F> constexpr auto transform_error(F && f) && {
  173. using G = std::invoke_result_t<F, E>;
  174. if (has_value()) {
  175. return expected<value_type, G>(*std::move(*this));
  176. }
  177. return expected<value_type, G>(unexpect, std::forward<F>(f)(std::move(*this).error()));
  178. }
  179. private:
  180. std::variant<T, E> value_;
  181. };
  182. template <typename E> class expected<void, E> {
  183. public:
  184. using value_type = void;
  185. using error_type = E;
  186. using unexpected_type = unexpected<E>;
  187. template <typename U> using rebind = expected<U, error_type>;
  188. public:
  189. constexpr expected() = default;
  190. template <typename G>
  191. constexpr explicit(!std::is_convertible_v<const G &, E>)
  192. expected(expected<void, G> const & other) {
  193. if (not other.has_value()) {
  194. *this = other.error();
  195. }
  196. }
  197. template <typename G>
  198. constexpr explicit(!std::is_convertible_v<G, E>) expected(expected<void, G> && other) {
  199. if (not other.has_value()) {
  200. *this = std::move(other).error();
  201. }
  202. }
  203. template <typename G>
  204. constexpr explicit(!std::is_convertible_v<const G &, E>) expected(unexpected<G> const & e)
  205. : value_(e.error()) {}
  206. template <typename G>
  207. constexpr explicit(!std::is_convertible_v<G, E>) expected(unexpected<G> && e)
  208. : value_(std::move(e).error()) {}
  209. constexpr explicit expected(std::in_place_t) {}
  210. template <typename... Args>
  211. constexpr explicit expected(unexpect_t, Args &&... args)
  212. : value_(std::in_place, std::forward<Args>(args)...) {}
  213. template <typename U, typename... Args>
  214. constexpr explicit expected(unexpect_t, std::initializer_list<U> il, Args &&... args)
  215. : value_(std::in_place, il, std::forward<Args>(args)...) {}
  216. constexpr explicit operator bool() const noexcept { return has_value(); }
  217. constexpr bool has_value() const noexcept { return not value_.has_value(); }
  218. constexpr void operator*() const noexcept {}
  219. void value() const & noexcept {
  220. if (JVALIDATE_LIKELY(has_value())) {
  221. return operator*();
  222. }
  223. throw bad_expected_access(error());
  224. }
  225. void value() && noexcept {
  226. if (JVALIDATE_LIKELY(has_value())) {
  227. return operator*();
  228. }
  229. throw bad_expected_access(std::move(error()));
  230. }
  231. constexpr const E & error() const & noexcept { return *value_; }
  232. constexpr E & error() & noexcept { return *value_; }
  233. constexpr E && error() && noexcept { return std::move(*value_); }
  234. template <typename F> constexpr auto transform(F && f) & {
  235. using G = std::invoke_result_t<F>;
  236. if (has_value()) {
  237. return expected<G, error_type>(std::forward<F>(f)());
  238. }
  239. return expected<G, error_type>(unexpect, error());
  240. }
  241. template <typename F> constexpr auto transform(F && f) const & {
  242. using G = std::invoke_result_t<F>;
  243. if (has_value()) {
  244. return expected<G, error_type>(std::forward<F>(f)());
  245. }
  246. return expected<G, error_type>(unexpect, error());
  247. }
  248. template <typename F> constexpr auto transform(F && f) && {
  249. using G = std::invoke_result_t<F>;
  250. if (has_value()) {
  251. return expected<G, error_type>(std::forward<F>(f)());
  252. }
  253. return expected<G, error_type>(unexpect, std::move(*this).error());
  254. }
  255. template <typename F> constexpr auto transform_error(F && f) & {
  256. using G = std::invoke_result_t<F, E &>;
  257. if (has_value()) {
  258. return expected<value_type, G>(**this);
  259. }
  260. return expected<value_type, G>(unexpect, std::forward<F>(f)(error()));
  261. }
  262. template <typename F> constexpr auto transform_error(F && f) const & {
  263. using G = std::invoke_result_t<F, E const &>;
  264. if (has_value()) {
  265. return expected<value_type, G>(**this);
  266. }
  267. return expected<value_type, G>(unexpect, std::forward<F>(f)(error()));
  268. }
  269. template <typename F> constexpr auto transform_error(F && f) && {
  270. using G = std::invoke_result_t<F, E>;
  271. if (has_value()) {
  272. return expected<value_type, G>(*std::move(*this));
  273. }
  274. return expected<value_type, G>(unexpect, std::forward<F>(f)(std::move(*this).error()));
  275. }
  276. private:
  277. std::optional<E> value_;
  278. };
  279. }
  280. #endif
  281. namespace jvalidate::detail {
  282. inline std::string to_message(std::errc ec) { return std::make_error_code(ec).message(); }
  283. }