#pragma once /* * Created by Brett on 06/02/24. * Licensed under GNU General Public License V3.0 * See LICENSE file for license detail */ #ifndef BLT_EXPECTED_H #define BLT_EXPECTED_H #include #include #include #include namespace blt { struct unexpect_t { explicit unexpect_t() = default; }; inline constexpr unexpect_t unexpect{}; template using remove_cvref_t = std::remove_reference_t>; template class unexpected { private: E e; public: constexpr unexpected(const unexpected&) = default; constexpr unexpected(unexpected&&) = default; template, unexpected> && !std::is_same_v, std::in_place_t> && std::is_constructible_v, bool> = true> constexpr explicit unexpected(Err&& e): e(std::forward(e)) {} template, bool> = true> constexpr explicit unexpected(std::in_place_t, Args&& ... args): e(std::forward(args)...) {} template&, Args...>, bool> = true> constexpr explicit unexpected(std::in_place_t, std::initializer_list il, Args&& ... args): e(il, std::forward(args)...) {} constexpr const E& error() const& noexcept { return e; } constexpr E& error()& noexcept { return e; } constexpr const E&& error() const&& noexcept { return e; } constexpr E&& error()&& noexcept { return e; } constexpr void swap(unexpected& other) noexcept(std::is_nothrow_swappable_v) { std::swap(error(), other.error()); } template inline friend constexpr bool operator==(const unexpected& x, const unexpected& y) { return x.error() == y.error(); } friend constexpr void swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y))) {} }; template unexpected(E) -> unexpected; template class bad_expected_access : public std::exception { private: E e; public: explicit bad_expected_access(E e): e(std::move(e)) {} const E& error() const& noexcept { return e; } E& error()& noexcept { return e; } const E&& error() const&& noexcept { return e; } E&& error()&& noexcept { return e; } [[nodiscard]] const char* what() const noexcept override { return "blt::expected does not contain a value!"; } }; template> class expected { protected: std::variant v; template inline static constexpr bool eight_insanity_v = std::is_constructible_v&> || std::is_constructible_v> || std::is_constructible_v&> || std::is_constructible_v> || std::is_convertible_v&, T> || std::is_convertible_v, T> || std::is_convertible_v&, T> || std::is_convertible_v, T>; template inline static constexpr bool four_insanity_v = std::is_constructible_v, expected&> || std::is_constructible_v, expected> || std::is_constructible_v, const expected&> || std::is_constructible_v, const expected>; public: template && std::is_convertible_v, bool> = true> constexpr expected(): v(G{}) {} // template && std::is_default_constructible_v && std::is_convertible_v, bool> = true> // constexpr expected(): v(H{}) // {} // constexpr expected(const expected& copy) = delete; constexpr expected(const expected& copy): expected::v(copy.v) // NOLINT {} expected& operator=(const expected& copy) { v = copy.v; return *this; } constexpr expected(expected&& move) noexcept: v(std::move(move.v)) {} expected& operator=(expected&& move) { std::swap(v, move.v); return *this; } /* * (4)...(5) */ template, class GF = const G&, std::enable_if_t< (!std::is_convertible_v || !std::is_convertible_v) && (std::is_constructible_v || std::is_void_v) && std::is_constructible_v && !eight_insanity_v && !four_insanity_v, bool> = true> constexpr explicit expected(const expected& other): v(other.has_value() ? std::forward(*other) : std::forward(other.error())) {} template || !std::is_convertible_v) && (std::is_constructible_v || std::is_void_v) && std::is_constructible_v && !eight_insanity_v && !four_insanity_v, bool> = true> constexpr explicit expected(expected&& other): v(other.has_value() ? std::forward(*other) : std::forward(other.error())) {} template, class GF = const G&, std::enable_if_t< (std::is_convertible_v && std::is_convertible_v) && (std::is_constructible_v || std::is_void_v) && std::is_constructible_v && !eight_insanity_v && !four_insanity_v, bool> = true> constexpr expected(const expected& other): // NOLINT v(other.has_value() ? std::forward(*other) : std::forward(other.error())) {} template && std::is_convertible_v) && (std::is_constructible_v || std::is_void_v) && std::is_constructible_v && !eight_insanity_v && !four_insanity_v, bool> = true> constexpr expected(expected&& other): // NOLINT v(other.has_value() ? std::forward(*other) : std::forward(other.error())) {} /* * (6) */ template && !std::is_same_v, void> && !std::is_same_v, std::in_place_t> && !std::is_same_v> && std::is_constructible_v && !std::is_same_v, unexpected> && !std::is_same_v, expected>, bool> = true> constexpr explicit expected(U&& v): v(T(std::forward(v))) {} template && !std::is_same_v, void> && !std::is_same_v, std::in_place_t> && !std::is_same_v> && std::is_constructible_v && !std::is_same_v, unexpected> && !std::is_same_v, expected>, bool> = true> constexpr expected(U&& v): v(T(std::forward(v))) // NOLINT {} /* * (7) */ template>, std::enable_if_t< !std::is_convertible_v && std::is_constructible_v, bool> = true> constexpr explicit expected(const unexpected& e): v(std::forward(e.error())) {} template>, std::enable_if_t< std::is_convertible_v && std::is_constructible_v, bool> = true> constexpr expected(const unexpected& e): v(std::forward(e.error())) // NOLINT {} /* * (8) */ template>, std::enable_if_t< !std::is_convertible_v && std::is_constructible_v, bool> = true> constexpr explicit expected(unexpected&& e): v(std::forward(e.error())) {} template>, std::enable_if_t< std::is_convertible_v && std::is_constructible_v, bool> = true> constexpr expected(unexpected&& e): v(std::forward(e.error())) // NOLINT {} /* * (9)...(13) */ template, bool> = true> constexpr explicit expected(std::in_place_t, Args&& ... args): v(T(std::forward(args)...)) {} template&, Args...>, bool> = true> constexpr explicit expected(std::in_place_t, std::initializer_list il, Args&& ... args): v(T(il, std::forward(args)...)) {} // template, void>, bool> = true> // constexpr explicit expected(std::in_place_t) noexcept: v(T()) // {} template, bool> = true> constexpr explicit expected(unexpect_t, Args&& ... args): v(E(std::forward(args)...)) {} template&, Args...>, bool> = true> constexpr explicit expected(unexpect_t, std::initializer_list il, Args&& ... args): v(E(il, std::forward(args)...)) {} [[nodiscard]] constexpr explicit operator bool() const noexcept { return std::holds_alternative(v); } [[nodiscard]] constexpr inline bool has_value() const noexcept { return std::holds_alternative(v); } constexpr T& value()& { if (*this) return std::get(v); else throw bad_expected_access(std::as_const(error())); } constexpr const T& value() const& { if (*this) return std::get(v); else throw bad_expected_access(std::as_const(error())); } constexpr T&& value()&& { if (*this) return std::get(v); else throw bad_expected_access(std::move(error())); } constexpr const T&& value() const&& { if (*this) return std::get(v); else throw bad_expected_access(std::move(error())); } constexpr const E& error() const& noexcept { return std::get(v); } constexpr E& error()& noexcept { return std::get(v); } constexpr const E&& error() const&& noexcept { return std::get(v); } constexpr E&& error()&& noexcept { return std::get(v); } template && std::is_copy_constructible_v, bool> = true> constexpr T value_or(U&& default_value) const& { return bool(*this) ? **this : static_cast(std::forward(default_value)); } template && std::is_move_constructible_v, bool> = true> constexpr T value_or(U&& default_value)&& { return bool(*this) ? std::move(**this) : static_cast(std::forward(default_value)); } constexpr inline const T* operator->() const noexcept { return &std::get(v); } constexpr inline T* operator->() noexcept { return &std::get(v); } constexpr inline const T& operator*() const& noexcept { return std::get(v); } constexpr inline T& operator*()& noexcept { return std::get(v); } constexpr inline const T&& operator*() const&& noexcept { return std::move(std::get(v)); } constexpr inline T&& operator*()&& noexcept { return std::move(std::get(v)); } }; template class expected { public: using expected::expected; constexpr expected(const expected& copy) = delete; expected& operator=(const expected& copy) = delete; }; } #endif //BLT_EXPECTED_H