-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
132 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright (c) Sergey Kovalevich <inndie@gmail.com> | ||
// SPDX-License-Identifier: AGPL-3.0 | ||
|
||
#include <stdexcept> | ||
#include <type_traits> | ||
#include <variant> | ||
|
||
namespace turboq::detail { | ||
|
||
struct BadExpectedAccess : public std::runtime_error { | ||
template <typename... Args> | ||
BadExpectedAccess(Args&&... args) : std::runtime_error(std::forward<Args>(args)...) {} | ||
}; | ||
|
||
/// @brief Required tagging type for cases where expected and unexpected type are identical. | ||
template <typename E> | ||
class Unexpected { | ||
public: | ||
constexpr Unexpected(Unexpected const&) = default; | ||
constexpr Unexpected(Unexpected&&) = default; | ||
|
||
constexpr explicit Unexpected(E const& rhs) : storage_(rhs) {} | ||
constexpr explicit Unexpected(E&& rhs) : storage_(std::move(rhs)) {} | ||
|
||
constexpr E const& error() const& noexcept { | ||
return storage_; | ||
} | ||
constexpr E&& error() && noexcept { | ||
return std::move(storage_); | ||
} | ||
|
||
private: | ||
E storage_; | ||
}; | ||
|
||
template <typename E> | ||
Unexpected(E) -> Unexpected<E>; | ||
|
||
/// @brief A stripped-down version of std::expected from C++23. | ||
template <typename T, typename E> | ||
class Expected { | ||
private: | ||
std::variant<T, Unexpected<E>> storage_; | ||
|
||
public: | ||
constexpr Expected() : storage_(std::in_place_index<0>) {} | ||
|
||
template <typename... Args> | ||
constexpr Expected(Args&&... args) : storage_(std::forward<Args>(args)...) {} | ||
|
||
constexpr Expected(Expected const&) = default; | ||
constexpr Expected(Expected&&) = default; | ||
|
||
constexpr bool has_value() const noexcept { | ||
return storage_.index() == 0; | ||
} | ||
|
||
constexpr explicit operator bool() const noexcept { | ||
return has_value(); | ||
} | ||
|
||
template <class X> | ||
constexpr T value_or(X&& v) const& noexcept { | ||
return has_value() ? std::get<T>(storage_) : static_cast<T>(std::forward<X>(v)); | ||
} | ||
|
||
constexpr T const& value() const& { | ||
if (!has_value()) | ||
throw BadExpectedAccess("Attempt to access value() when unexpected"); | ||
return std::get<T>(storage_); | ||
} | ||
|
||
constexpr T value() && { | ||
if (!has_value()) | ||
throw BadExpectedAccess("Attempt to access value() when unexpected"); | ||
return std::move(std::get<T>(storage_)); | ||
} | ||
|
||
constexpr E const& error() const& noexcept { | ||
return std::get_if<Unexpected<E>>(&storage_)->error(); | ||
} | ||
|
||
constexpr E error() && noexcept { | ||
return std::move(*std::get_if<Unexpected<E>>(&storage_)).error(); | ||
} | ||
|
||
constexpr T const& operator*() const noexcept { | ||
return *std::get_if<T>(&storage_); | ||
} | ||
|
||
constexpr T const* operator->() const noexcept { | ||
return std::get_if<T>(&storage_); | ||
} | ||
}; | ||
|
||
/// \overload | ||
template <typename E> | ||
class Expected<void, E> { | ||
private: | ||
std::variant<std::monostate, Unexpected<E>> storage_; | ||
|
||
public: | ||
constexpr Expected() noexcept : storage_(std::in_place_index<0>) {} | ||
|
||
template <typename... Args> | ||
constexpr Expected(Args&&... args) : storage_(std::forward<Args>(args)...) {} | ||
|
||
constexpr Expected(Expected const&) = default; | ||
constexpr Expected(Expected&&) = default; | ||
|
||
constexpr bool has_value() const noexcept { | ||
return storage_.index() == 0; | ||
} | ||
|
||
constexpr explicit operator bool() const noexcept { | ||
return has_value(); | ||
} | ||
|
||
constexpr E const& error() const& noexcept { | ||
return std::get_if<Unexpected<E>>(&storage_)->error(); | ||
} | ||
|
||
constexpr E error() && noexcept { | ||
return std::move(*std::get_if<Unexpected<E>>(&storage_)).error(); | ||
} | ||
}; | ||
|
||
} // namespace turboq::detail |