| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- #pragma once
- #include <type_traits>
- #ifdef __cpp_lib_expected
- #if __cpp_lib_expected >= 202211L
- #include <expected>
- namespace jvalidate::detail {
- using std::expected;
- using std::unexpected;
- inline constexpr std::unexpect_t unexpect{};
- }
- #endif
- #else
- #include <initializer_list>
- #include <optional>
- #include <utility>
- #include <variant>
- #include <jvalidate/detail/expect.h>
- namespace jvalidate::detail {
- inline constexpr struct unexpect_t {
- } unexpect{};
- template <typename E> class unexpected {
- public:
- template <typename Err = E>
- constexpr explicit unexpected(Err && e) : error_(std::forward<Err>(e)) {}
- template <typename... Args>
- constexpr explicit unexpected(std::in_place_t, Args &&... args)
- : error_{std::forward<Args>(args)...} {}
- template <typename U, typename... Args>
- constexpr explicit unexpected(std::in_place_t, std::initializer_list<U> il, Args &&... args)
- : error_{il, std::forward<Args>(args)...} {}
- constexpr const E & error() const & noexcept { return error_; }
- constexpr E & error() & noexcept { return error_; }
- constexpr E && error() && noexcept { return std::move(error_); }
- constexpr void swap(unexpected & other) noexcept(std::is_nothrow_swappable_v<E>) {
- using std::swap;
- swap(error(), other.error());
- }
- template <typename E2> friend constexpr bool operator==(unexpected & x, unexpected<E2> & y) {
- return x.error() == y.error();
- }
- friend constexpr void swap(unexpected & x, unexpected & y) noexcept(noexcept(x.swap(y))) {
- return x.swap(y);
- }
- private:
- E error_;
- };
- template <typename E> unexpected(E) -> unexpected<E>;
- template <typename E> class bad_expected_access;
- template <> class bad_expected_access<void> : public std::exception {
- public:
- char const * what() const noexcept override { return "unexpected"; }
- };
- template <typename E> class bad_expected_access : public bad_expected_access<void> {
- public:
- bad_expected_access(E error) : error_(std::move(error)) {}
- char const * what() const noexcept override { return error_.c_str(); }
- constexpr const E & error() const & noexcept { return error_; }
- constexpr E & error() & noexcept { return error_; }
- constexpr E && error() && noexcept { return std::move(error_); }
- private:
- E error_;
- };
- template <typename T, typename E> class expected {
- public:
- using value_type = T;
- using error_type = E;
- using unexpected_type = unexpected<E>;
- template <typename U> using rebind = expected<U, error_type>;
- public:
- constexpr expected() = default;
- template <typename U, typename G>
- constexpr explicit(!std::is_convertible_v<const U &, T> && !std::is_convertible_v<const G &, E>)
- expected(expected<U, G> const & other) {
- if (other.has_value()) {
- value_.template emplace<0>(*other);
- } else {
- value_.template emplace<1>(other.error());
- }
- }
- template <typename U, typename G>
- constexpr explicit(!std::is_convertible_v<U, T> && !std::is_convertible_v<G, E>)
- expected(expected<U, G> && other) {
- if (other.has_value()) {
- value_.template emplace<0>(*std::move(other));
- } else {
- value_.template emplace<1>(std::move(other).error());
- }
- }
- template <typename U = std::remove_cv_t<T>>
- constexpr explicit(!std::is_convertible_v<U, T>) expected(U && v)
- : value_(std::in_place_index<0>, std::forward<T>(v)) {}
- template <typename G>
- constexpr explicit(!std::is_convertible_v<const G &, E>) expected(unexpected<G> const & e)
- : value_(std::in_place_index<1>, e.error()) {}
- template <typename G>
- constexpr explicit(!std::is_convertible_v<G, E>) expected(unexpected<G> && e)
- : value_(std::in_place_index<1>, std::move(e).error()) {}
- template <typename... Args>
- constexpr explicit expected(std::in_place_t, Args &&... args)
- : value_(std::in_place_index<0>, std::forward<Args>(args)...) {}
- template <typename U, typename... Args>
- constexpr explicit expected(std::in_place_t, std::initializer_list<U> il, Args &&... args)
- : value_(std::in_place_index<0>, il, std::forward<Args>(args)...) {}
- template <typename... Args>
- constexpr explicit expected(unexpect_t, Args &&... args)
- : value_(std::in_place_index<1>, std::forward<Args>(args)...) {}
- template <typename U, typename... Args>
- constexpr explicit expected(unexpect_t, std::initializer_list<U> il, Args &&... args)
- : value_(std::in_place_index<1>, il, std::forward<Args>(args)...) {}
- constexpr explicit operator bool() const noexcept { return has_value(); }
- constexpr bool has_value() const noexcept { return value_.index() == 0; }
- constexpr const T * operator->() const noexcept { return std::get_if<0>(&value_); }
- constexpr T * operator->() noexcept { return std::get_if<0>(&value_); }
- constexpr const T & operator*() const & noexcept { return *std::get_if<0>(&value_); }
- constexpr T & operator*() & noexcept { return *std::get_if<0>(&value_); }
- constexpr T && operator*() && noexcept { return std::move(*std::get_if<0>(&value_)); }
- constexpr const T & value() const & noexcept {
- if (JVALIDATE_LIKELY(has_value())) {
- return operator*();
- }
- throw bad_expected_access(error());
- }
- constexpr T & value() & noexcept {
- if (JVALIDATE_LIKELY(has_value())) {
- return operator*();
- }
- throw bad_expected_access(std::as_const(error()));
- }
- constexpr T && value() && noexcept {
- if (JVALIDATE_LIKELY(has_value())) {
- return std::move(*this).operator*();
- }
- throw bad_expected_access(std::move(error()));
- }
- constexpr const E & error() const & noexcept { return *std::get_if<1>(&value_); }
- constexpr E & error() & noexcept { return *std::get_if<1>(&value_); }
- constexpr E && error() && noexcept { return std::move(*std::get_if<1>(&value_)); }
- template <typename F> constexpr auto transform(F && f) & {
- using G = std::invoke_result_t<F, value_type &>;
- if (has_value()) {
- return expected<G, error_type>(std::forward<F>(f)(**this));
- }
- return expected<G, error_type>(unexpect, error());
- }
- template <typename F> constexpr auto transform(F && f) const & {
- using G = std::invoke_result_t<F, value_type const &>;
- if (has_value()) {
- return expected<G, error_type>(std::forward<F>(f)(**this));
- }
- return expected<G, error_type>(unexpect, error());
- }
- template <typename F> constexpr auto transform(F && f) && {
- using G = std::invoke_result_t<F, value_type>;
- if (has_value()) {
- return expected<G, error_type>(std::forward<F>(f)(*std::move(*this)));
- }
- return expected<G, error_type>(unexpect, std::move(*this).error());
- }
- template <typename F> constexpr auto transform_error(F && f) & {
- using G = std::invoke_result_t<F, E &>;
- if (has_value()) {
- return expected<value_type, G>(**this);
- }
- return expected<value_type, G>(unexpect, std::forward<F>(f)(error()));
- }
- template <typename F> constexpr auto transform_error(F && f) const & {
- using G = std::invoke_result_t<F, E const &>;
- if (has_value()) {
- return expected<value_type, G>(**this);
- }
- return expected<value_type, G>(unexpect, std::forward<F>(f)(error()));
- }
- template <typename F> constexpr auto transform_error(F && f) && {
- using G = std::invoke_result_t<F, E>;
- if (has_value()) {
- return expected<value_type, G>(*std::move(*this));
- }
- return expected<value_type, G>(unexpect, std::forward<F>(f)(std::move(*this).error()));
- }
- private:
- std::variant<T, E> value_;
- };
- template <typename E> class expected<void, E> {
- public:
- using value_type = void;
- using error_type = E;
- using unexpected_type = unexpected<E>;
- template <typename U> using rebind = expected<U, error_type>;
- public:
- constexpr expected() = default;
- template <typename G>
- constexpr explicit(!std::is_convertible_v<const G &, E>)
- expected(expected<void, G> const & other) {
- if (not other.has_value()) {
- *this = other.error();
- }
- }
- template <typename G>
- constexpr explicit(!std::is_convertible_v<G, E>) expected(expected<void, G> && other) {
- if (not other.has_value()) {
- *this = std::move(other).error();
- }
- }
- template <typename G>
- constexpr explicit(!std::is_convertible_v<const G &, E>) expected(unexpected<G> const & e)
- : value_(std::in_place_index<1>, e.error()) {}
- template <typename G>
- constexpr explicit(!std::is_convertible_v<G, E>) expected(unexpected<G> && e)
- : value_(std::in_place_index<1>, std::move(e).error()) {}
- constexpr explicit expected(std::in_place_t) {}
- template <typename... Args>
- constexpr explicit expected(unexpect_t, Args &&... args)
- : value_(std::in_place, std::forward<Args>(args)...) {}
- template <typename U, typename... Args>
- constexpr explicit expected(unexpect_t, std::initializer_list<U> il, Args &&... args)
- : value_(std::in_place, il, std::forward<Args>(args)...) {}
- constexpr explicit operator bool() const noexcept { return has_value(); }
- constexpr bool has_value() const noexcept { return not value_.has_value(); }
- constexpr void operator*() const noexcept {}
- void value() const & noexcept {
- if (JVALIDATE_LIKELY(has_value())) {
- return operator*();
- }
- throw bad_expected_access(error());
- }
- void value() && noexcept {
- if (JVALIDATE_LIKELY(has_value())) {
- return operator*();
- }
- throw bad_expected_access(std::move(error()));
- }
- constexpr const E & error() const & noexcept { return *value_; }
- constexpr E & error() & noexcept { return *value_; }
- constexpr E && error() && noexcept { return std::move(*value_); }
- template <typename F> constexpr auto transform(F && f) & {
- using G = std::invoke_result_t<F>;
- if (has_value()) {
- return expected<G, error_type>(std::forward<F>(f)());
- }
- return expected<G, error_type>(unexpect, error());
- }
- template <typename F> constexpr auto transform(F && f) const & {
- using G = std::invoke_result_t<F>;
- if (has_value()) {
- return expected<G, error_type>(std::forward<F>(f)());
- }
- return expected<G, error_type>(unexpect, error());
- }
- template <typename F> constexpr auto transform(F && f) && {
- using G = std::invoke_result_t<F>;
- if (has_value()) {
- return expected<G, error_type>(std::forward<F>(f)());
- }
- return expected<G, error_type>(unexpect, std::move(*this).error());
- }
- template <typename F> constexpr auto transform_error(F && f) & {
- using G = std::invoke_result_t<F, E &>;
- if (has_value()) {
- return expected<value_type, G>(**this);
- }
- return expected<value_type, G>(unexpect, std::forward<F>(f)(error()));
- }
- template <typename F> constexpr auto transform_error(F && f) const & {
- using G = std::invoke_result_t<F, E const &>;
- if (has_value()) {
- return expected<value_type, G>(**this);
- }
- return expected<value_type, G>(unexpect, std::forward<F>(f)(error()));
- }
- template <typename F> constexpr auto transform_error(F && f) && {
- using G = std::invoke_result_t<F, E>;
- if (has_value()) {
- return expected<value_type, G>(*std::move(*this));
- }
- return expected<value_type, G>(unexpect, std::forward<F>(f)(std::move(*this).error()));
- }
- private:
- std::optional<E> value_;
- };
- }
- #endif
- namespace jvalidate::detail {
- inline std::string to_message(std::errc ec) { return std::make_error_code(ec).message(); }
- }
|