Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ksergey committed Jul 9, 2024
1 parent 8f6c6c8 commit 823a803
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion code/turboq/BoundedMPSCRawQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class BoundedMPSCRawQueueProducer {
std::size_t const totalSize = size + sizeof(MessageHeader);
if (totalSize > header_->maxMessageSize) [[unlikely]] {
throw std::runtime_error(
std::format("buffer exceed max message size ({} > {})", totalSize, header_->maxMessageSize));
fmt::format("buffer exceed max message size ({} > {})", totalSize, header_->maxMessageSize));
}

std::size_t currentProducerPos = std::atomic_ref(header_->producerPos).load(std::memory_order_acquire);
Expand Down
62 changes: 62 additions & 0 deletions code/turboq/detail/Expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: AGPL-3.0

#include <stdexcept>
#include <system_error>
#include <type_traits>
#include <variant>

Expand All @@ -17,7 +18,9 @@ template <typename E>
class Unexpected {
public:
constexpr Unexpected(Unexpected const&) = default;
constexpr Unexpected& operator=(Unexpected const&) = default;
constexpr Unexpected(Unexpected&&) = default;
constexpr Unexpected& operator=(Unexpected&&) = default;

constexpr explicit Unexpected(E const& rhs) : storage_(rhs) {}
constexpr explicit Unexpected(E&& rhs) : storage_(std::move(rhs)) {}
Expand All @@ -29,6 +32,9 @@ class Unexpected {
return std::move(storage_);
}

friend constexpr bool operator==(Unexpected const&, Unexpected const&) = default;
friend constexpr auto operator<=>(Unexpected const&, Unexpected const&) = default;

private:
E storage_;
};
Expand All @@ -49,7 +55,40 @@ class Expected {
constexpr Expected(Args&&... args) : storage_(std::forward<Args>(args)...) {}

constexpr Expected(Expected const&) = default;
constexpr Expected& operator=(Expected const&) = default;
constexpr Expected(Expected&&) = default;
constexpr Expected& operator=(Expected&&) = default;

constexpr Expected& operator=(T&& value) {
storage_.template emplace<0>(std::move(value));
return *this;
}

constexpr Expected& operator=(T const& value) {
storage_.template emplace<0>(value);
return *this;
}

constexpr Expected& operator=(Unexpected<E>&& value) {
storage_.template emplace<1>(std::move(value));
return *this;
}

constexpr Expected& operator=(Unexpected<E> const& value) {
storage_.template emplace<1>(value);
return *this;
}

template <typename U = T, typename G = E>
requires(!std::is_same_v<Expected<U, G>, Expected> && std::is_convertible_v<U, T> && std::is_convertible_v<G, E>)
constexpr Expected& operator=(Expected<U, G> value) {
if (value.has_value()) {
storage_.template emplace<0>(std::move(static_cast<Expected<U, G>&&>(value).value()));
} else {
storage_.template emplace<1>(std::move(static_cast<Expected<U, G>&&>(value).error()));
}
return *this;
}

constexpr bool has_value() const noexcept {
return storage_.index() == 0;
Expand Down Expand Up @@ -106,7 +145,20 @@ class Expected<void, E> {
constexpr Expected(Args&&... args) : storage_(std::forward<Args>(args)...) {}

constexpr Expected(Expected const&) = default;
constexpr Expected& operator=(Expected const&) = default;
constexpr Expected(Expected&&) = default;
constexpr Expected& operator=(Expected&&) = default;

template <typename G = E>
requires std::is_convertible_v<G, E>
constexpr Expected& operator=(Expected<void, G> value) {
if (!value.has_value()) {
storage_.template emplace<1>(std::move(static_cast<Expected<void, G>&&>(value).error()));
} else {
storage_.template emplace<0>();
}
return *this;
}

constexpr bool has_value() const noexcept {
return storage_.index() == 0;
Expand All @@ -123,6 +175,16 @@ class Expected<void, E> {
constexpr E error() && noexcept {
return std::move(*std::get_if<Unexpected<E>>(&storage_)).error();
}

constexpr Expected& operator=(Unexpected<E>&& value) {
storage_.template emplace<1>(std::move(value));
return *this;
}

constexpr Expected& operator=(Unexpected<E> const& value) {
storage_.template emplace<1>(value);
return *this;
}
};

} // namespace turboq::detail

0 comments on commit 823a803

Please sign in to comment.