expected.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. char const * what() const noexcept override { return error_.c_str(); }
  57. constexpr const E & error() const & noexcept { return error_; }
  58. constexpr E & error() & noexcept { return error_; }
  59. constexpr E && error() && noexcept { return std::move(error_); }
  60. private:
  61. E error_;
  62. };
  63. template <typename T, typename E> class expected {
  64. public:
  65. using value_type = T;
  66. using error_type = E;
  67. using unexpected_type = unexpected<E>;
  68. template <typename U> using rebind = expected<U, error_type>;
  69. public:
  70. constexpr expected() = default;
  71. template <typename U, typename G>
  72. constexpr explicit(!std::is_convertible_v<const U &, T> && !std::is_convertible_v<const G &, E>)
  73. expected(expected<U, G> const & other) {
  74. if (other.has_value()) {
  75. value_.template emplace<0>(*other);
  76. } else {
  77. value_.template emplace<1>(other.error());
  78. }
  79. }
  80. template <typename U, typename G>
  81. constexpr explicit(!std::is_convertible_v<U, T> && !std::is_convertible_v<G, E>)
  82. expected(expected<U, G> && other) {
  83. if (other.has_value()) {
  84. value_.template emplace<0>(*std::move(other));
  85. } else {
  86. value_.template emplace<1>(std::move(other).error());
  87. }
  88. }
  89. template <typename U = std::remove_cv_t<T>>
  90. constexpr explicit(!std::is_convertible_v<U, T>) expected(U && v)
  91. : value_(std::in_place_index<0>, std::forward<T>(v)) {}
  92. template <typename G>
  93. constexpr explicit(!std::is_convertible_v<const G &, E>) expected(unexpected<G> const & e)
  94. : value_(std::in_place_index<1>, e.error()) {}
  95. template <typename G>
  96. constexpr explicit(!std::is_convertible_v<G, E>) expected(unexpected<G> && e)
  97. : value_(std::in_place_index<1>, std::move(e).error()) {}
  98. template <typename... Args>
  99. constexpr explicit expected(std::in_place_t, Args &&... args)
  100. : value_(std::in_place_index<0>, std::forward<Args>(args)...) {}
  101. template <typename U, typename... Args>
  102. constexpr explicit expected(std::in_place_t, std::initializer_list<U> il, Args &&... args)
  103. : value_(std::in_place_index<0>, il, std::forward<Args>(args)...) {}
  104. template <typename... Args>
  105. constexpr explicit expected(unexpect_t, Args &&... args)
  106. : value_(std::in_place_index<1>, std::forward<Args>(args)...) {}
  107. template <typename U, typename... Args>
  108. constexpr explicit expected(unexpect_t, std::initializer_list<U> il, Args &&... args)
  109. : value_(std::in_place_index<1>, il, std::forward<Args>(args)...) {}
  110. constexpr explicit operator bool() const noexcept { return has_value(); }
  111. constexpr bool has_value() const noexcept { return value_.index() == 0; }
  112. constexpr const T * operator->() const noexcept { return std::get_if<0>(&value_); }
  113. constexpr T * operator->() noexcept { return std::get_if<0>(&value_); }
  114. constexpr const T & operator*() const & noexcept { return *std::get_if<0>(&value_); }
  115. constexpr T & operator*() & noexcept { return *std::get_if<0>(&value_); }
  116. constexpr T && operator*() && noexcept { return std::move(*std::get_if<0>(&value_)); }
  117. constexpr const T & value() const & noexcept {
  118. if (JVALIDATE_LIKELY(has_value())) {
  119. return operator*();
  120. }
  121. throw bad_expected_access(error());
  122. }
  123. constexpr T & value() & noexcept {
  124. if (JVALIDATE_LIKELY(has_value())) {
  125. return operator*();
  126. }
  127. throw bad_expected_access(std::as_const(error()));
  128. }
  129. constexpr T && value() && noexcept {
  130. if (JVALIDATE_LIKELY(has_value())) {
  131. return std::move(*this).operator*();
  132. }
  133. throw bad_expected_access(std::move(error()));
  134. }
  135. constexpr const E & error() const & noexcept { return *std::get_if<1>(&value_); }
  136. constexpr E & error() & noexcept { return *std::get_if<1>(&value_); }
  137. constexpr E && error() && noexcept { return std::move(*std::get_if<1>(&value_)); }
  138. template <typename F> constexpr auto transform(F && f) & {
  139. using G = std::invoke_result_t<F, value_type &>;
  140. if (has_value()) {
  141. return expected<G, error_type>(std::forward<F>(f)(**this));
  142. }
  143. return expected<G, error_type>(unexpect, error());
  144. }
  145. template <typename F> constexpr auto transform(F && f) const & {
  146. using G = std::invoke_result_t<F, value_type const &>;
  147. if (has_value()) {
  148. return expected<G, error_type>(std::forward<F>(f)(**this));
  149. }
  150. return expected<G, error_type>(unexpect, error());
  151. }
  152. template <typename F> constexpr auto transform(F && f) && {
  153. using G = std::invoke_result_t<F, value_type>;
  154. if (has_value()) {
  155. return expected<G, error_type>(std::forward<F>(f)(*std::move(*this)));
  156. }
  157. return expected<G, error_type>(unexpect, std::move(*this).error());
  158. }
  159. template <typename F> constexpr auto transform_error(F && f) & {
  160. using G = std::invoke_result_t<F, E &>;
  161. if (has_value()) {
  162. return expected<value_type, G>(**this);
  163. }
  164. return expected<value_type, G>(unexpect, std::forward<F>(f)(error()));
  165. }
  166. template <typename F> constexpr auto transform_error(F && f) const & {
  167. using G = std::invoke_result_t<F, E const &>;
  168. if (has_value()) {
  169. return expected<value_type, G>(**this);
  170. }
  171. return expected<value_type, G>(unexpect, std::forward<F>(f)(error()));
  172. }
  173. template <typename F> constexpr auto transform_error(F && f) && {
  174. using G = std::invoke_result_t<F, E>;
  175. if (has_value()) {
  176. return expected<value_type, G>(*std::move(*this));
  177. }
  178. return expected<value_type, G>(unexpect, std::forward<F>(f)(std::move(*this).error()));
  179. }
  180. private:
  181. std::variant<T, E> value_;
  182. };
  183. template <typename E> class expected<void, E> {
  184. public:
  185. using value_type = void;
  186. using error_type = E;
  187. using unexpected_type = unexpected<E>;
  188. template <typename U> using rebind = expected<U, error_type>;
  189. public:
  190. constexpr expected() = default;
  191. template <typename G>
  192. constexpr explicit(!std::is_convertible_v<const G &, E>)
  193. expected(expected<void, G> const & other) {
  194. if (not other.has_value()) {
  195. *this = other.error();
  196. }
  197. }
  198. template <typename G>
  199. constexpr explicit(!std::is_convertible_v<G, E>) expected(expected<void, G> && other) {
  200. if (not other.has_value()) {
  201. *this = std::move(other).error();
  202. }
  203. }
  204. template <typename G>
  205. constexpr explicit(!std::is_convertible_v<const G &, E>) expected(unexpected<G> const & e)
  206. : value_(std::in_place_index<1>, e.error()) {}
  207. template <typename G>
  208. constexpr explicit(!std::is_convertible_v<G, E>) expected(unexpected<G> && e)
  209. : value_(std::in_place_index<1>, std::move(e).error()) {}
  210. constexpr explicit expected(std::in_place_t) {}
  211. template <typename... Args>
  212. constexpr explicit expected(unexpect_t, Args &&... args)
  213. : value_(std::in_place, std::forward<Args>(args)...) {}
  214. template <typename U, typename... Args>
  215. constexpr explicit expected(unexpect_t, std::initializer_list<U> il, Args &&... args)
  216. : value_(std::in_place, il, std::forward<Args>(args)...) {}
  217. constexpr explicit operator bool() const noexcept { return has_value(); }
  218. constexpr bool has_value() const noexcept { return not value_.has_value(); }
  219. constexpr void operator*() const noexcept {}
  220. void value() const & noexcept {
  221. if (JVALIDATE_LIKELY(has_value())) {
  222. return operator*();
  223. }
  224. throw bad_expected_access(error());
  225. }
  226. void value() && noexcept {
  227. if (JVALIDATE_LIKELY(has_value())) {
  228. return operator*();
  229. }
  230. throw bad_expected_access(std::move(error()));
  231. }
  232. constexpr const E & error() const & noexcept { return *value_; }
  233. constexpr E & error() & noexcept { return *value_; }
  234. constexpr E && error() && noexcept { return std::move(*value_); }
  235. template <typename F> constexpr auto transform(F && f) & {
  236. using G = std::invoke_result_t<F>;
  237. if (has_value()) {
  238. return expected<G, error_type>(std::forward<F>(f)());
  239. }
  240. return expected<G, error_type>(unexpect, error());
  241. }
  242. template <typename F> constexpr auto transform(F && f) const & {
  243. using G = std::invoke_result_t<F>;
  244. if (has_value()) {
  245. return expected<G, error_type>(std::forward<F>(f)());
  246. }
  247. return expected<G, error_type>(unexpect, error());
  248. }
  249. template <typename F> constexpr auto transform(F && f) && {
  250. using G = std::invoke_result_t<F>;
  251. if (has_value()) {
  252. return expected<G, error_type>(std::forward<F>(f)());
  253. }
  254. return expected<G, error_type>(unexpect, std::move(*this).error());
  255. }
  256. template <typename F> constexpr auto transform_error(F && f) & {
  257. using G = std::invoke_result_t<F, E &>;
  258. if (has_value()) {
  259. return expected<value_type, G>(**this);
  260. }
  261. return expected<value_type, G>(unexpect, std::forward<F>(f)(error()));
  262. }
  263. template <typename F> constexpr auto transform_error(F && f) const & {
  264. using G = std::invoke_result_t<F, E const &>;
  265. if (has_value()) {
  266. return expected<value_type, G>(**this);
  267. }
  268. return expected<value_type, G>(unexpect, std::forward<F>(f)(error()));
  269. }
  270. template <typename F> constexpr auto transform_error(F && f) && {
  271. using G = std::invoke_result_t<F, E>;
  272. if (has_value()) {
  273. return expected<value_type, G>(*std::move(*this));
  274. }
  275. return expected<value_type, G>(unexpect, std::forward<F>(f)(std::move(*this).error()));
  276. }
  277. private:
  278. std::optional<E> value_;
  279. };
  280. }
  281. #endif
  282. namespace jvalidate::detail {
  283. inline std::string to_message(std::errc ec) { return std::make_error_code(ec).message(); }
  284. }