expected.h 11 KB

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