Skip to content

Commit

Permalink
Remove Any wrapper (#1336)
Browse files Browse the repository at this point in the history
Fixes #1174
  • Loading branch information
godexsoft authored Apr 10, 2024
1 parent 7fcd3e4 commit 2302122
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 176 deletions.
53 changes: 25 additions & 28 deletions src/util/async/AnyExecutionContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "util/async/AnyStopToken.hpp"
#include "util/async/AnyStrand.hpp"
#include "util/async/Concepts.hpp"
#include "util/async/impl/Any.hpp"
#include "util/async/impl/ErasedOperation.hpp"

#include <any>
Expand Down Expand Up @@ -66,9 +65,9 @@ class AnyExecutionContext {
execute(SomeHandlerWithoutStopToken auto&& fn)
{
using RetType = std::decay_t<decltype(fn())>;
static_assert(not std::is_same_v<RetType, impl::Any>);
static_assert(not std::is_same_v<RetType, std::any>);

return AnyOperation<RetType>(pimpl_->execute([fn = std::forward<decltype(fn)>(fn)]() -> impl::Any {
return AnyOperation<RetType>(pimpl_->execute([fn = std::forward<decltype(fn)>(fn)]() -> std::any {
if constexpr (std::is_void_v<RetType>) {
fn();
return {};
Expand All @@ -90,18 +89,16 @@ class AnyExecutionContext {
execute(SomeHandlerWith<AnyStopToken> auto&& fn)
{
using RetType = std::decay_t<decltype(fn(std::declval<AnyStopToken>()))>;
static_assert(not std::is_same_v<RetType, impl::Any>);
static_assert(not std::is_same_v<RetType, std::any>);

return AnyOperation<RetType>(
pimpl_->execute([fn = std::forward<decltype(fn)>(fn)](auto stopToken) -> impl::Any {
if constexpr (std::is_void_v<RetType>) {
fn(std::move(stopToken));
return {};
} else {
return std::make_any<RetType>(fn(std::move(stopToken)));
}
})
);
return AnyOperation<RetType>(pimpl_->execute([fn = std::forward<decltype(fn)>(fn)](auto stopToken) -> std::any {
if constexpr (std::is_void_v<RetType>) {
fn(std::move(stopToken));
return {};
} else {
return std::make_any<RetType>(fn(std::move(stopToken)));
}
}));
}

/**
Expand All @@ -117,10 +114,10 @@ class AnyExecutionContext {
execute(SomeHandlerWith<AnyStopToken> auto&& fn, SomeStdDuration auto timeout)
{
using RetType = std::decay_t<decltype(fn(std::declval<AnyStopToken>()))>;
static_assert(not std::is_same_v<RetType, impl::Any>);
static_assert(not std::is_same_v<RetType, std::any>);

return AnyOperation<RetType>(pimpl_->execute(
[fn = std::forward<decltype(fn)>(fn)](auto stopToken) -> impl::Any {
[fn = std::forward<decltype(fn)>(fn)](auto stopToken) -> std::any {
if constexpr (std::is_void_v<RetType>) {
fn(std::move(stopToken));
return {};
Expand All @@ -145,12 +142,12 @@ class AnyExecutionContext {
scheduleAfter(SomeStdDuration auto delay, SomeHandlerWith<AnyStopToken> auto&& fn)
{
using RetType = std::decay_t<decltype(fn(std::declval<AnyStopToken>()))>;
static_assert(not std::is_same_v<RetType, impl::Any>);
static_assert(not std::is_same_v<RetType, std::any>);

auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(delay);
return AnyOperation<RetType>(pimpl_->scheduleAfter(
millis,
[fn = std::forward<decltype(fn)>(fn)](auto stopToken) -> impl::Any {
[fn = std::forward<decltype(fn)>(fn)](auto stopToken) -> std::any {
if constexpr (std::is_void_v<RetType>) {
fn(std::move(stopToken));
return {};
Expand All @@ -175,12 +172,12 @@ class AnyExecutionContext {
scheduleAfter(SomeStdDuration auto delay, SomeHandlerWith<AnyStopToken, bool> auto&& fn)
{
using RetType = std::decay_t<decltype(fn(std::declval<AnyStopToken>(), true))>;
static_assert(not std::is_same_v<RetType, impl::Any>);
static_assert(not std::is_same_v<RetType, std::any>);

auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(delay);
return AnyOperation<RetType>(pimpl_->scheduleAfter(
millis,
[fn = std::forward<decltype(fn)>(fn)](auto stopToken, auto cancelled) -> impl::Any {
[fn = std::forward<decltype(fn)>(fn)](auto stopToken, auto cancelled) -> std::any {
if constexpr (std::is_void_v<RetType>) {
fn(std::move(stopToken), cancelled);
return {};
Expand Down Expand Up @@ -211,14 +208,14 @@ class AnyExecutionContext {

virtual impl::ErasedOperation
execute(
std::function<impl::Any(AnyStopToken)>,
std::function<std::any(AnyStopToken)>,
std::optional<std::chrono::milliseconds> timeout = std::nullopt
) = 0;
virtual impl::ErasedOperation execute(std::function<impl::Any()>) = 0;
virtual impl::ErasedOperation execute(std::function<std::any()>) = 0;
virtual impl::ErasedOperation
scheduleAfter(std::chrono::milliseconds, std::function<impl::Any(AnyStopToken)>) = 0;
scheduleAfter(std::chrono::milliseconds, std::function<std::any(AnyStopToken)>) = 0;
virtual impl::ErasedOperation
scheduleAfter(std::chrono::milliseconds, std::function<impl::Any(AnyStopToken, bool)>) = 0;
scheduleAfter(std::chrono::milliseconds, std::function<std::any(AnyStopToken, bool)>) = 0;
virtual AnyStrand
makeStrand() = 0;
};
Expand All @@ -232,25 +229,25 @@ class AnyExecutionContext {
}

impl::ErasedOperation
execute(std::function<impl::Any(AnyStopToken)> fn, std::optional<std::chrono::milliseconds> timeout) override
execute(std::function<std::any(AnyStopToken)> fn, std::optional<std::chrono::milliseconds> timeout) override
{
return ctx.get().execute(std::move(fn), timeout);
}

impl::ErasedOperation
execute(std::function<impl::Any()> fn) override
execute(std::function<std::any()> fn) override
{
return ctx.get().execute(std::move(fn));
}

impl::ErasedOperation
scheduleAfter(std::chrono::milliseconds delay, std::function<impl::Any(AnyStopToken)> fn) override
scheduleAfter(std::chrono::milliseconds delay, std::function<std::any(AnyStopToken)> fn) override
{
return ctx.get().scheduleAfter(delay, std::move(fn));
}

impl::ErasedOperation
scheduleAfter(std::chrono::milliseconds delay, std::function<impl::Any(AnyStopToken, bool)> fn) override
scheduleAfter(std::chrono::milliseconds delay, std::function<std::any(AnyStopToken, bool)> fn) override
{
return ctx.get().scheduleAfter(delay, std::move(fn));
}
Expand Down
1 change: 0 additions & 1 deletion src/util/async/AnyOperation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include "util/async/Concepts.hpp"
#include "util/async/Error.hpp"
#include "util/async/impl/Any.hpp"
#include "util/async/impl/ErasedOperation.hpp"

#include <fmt/core.h>
Expand Down
21 changes: 10 additions & 11 deletions src/util/async/AnyStrand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include "util/async/AnyStopToken.hpp"
#include "util/async/Concepts.hpp"
#include "util/async/impl/Any.hpp"
#include "util/async/impl/ErasedOperation.hpp"

#include <any>
Expand Down Expand Up @@ -64,10 +63,10 @@ class AnyStrand {
execute(SomeHandlerWithoutStopToken auto&& fn)
{
using RetType = std::decay_t<decltype(fn())>;
static_assert(not std::is_same_v<RetType, impl::Any>);
static_assert(not std::is_same_v<RetType, std::any>);

return AnyOperation<RetType>( //
pimpl_->execute([fn = std::forward<decltype(fn)>(fn)]() -> impl::Any {
pimpl_->execute([fn = std::forward<decltype(fn)>(fn)]() -> std::any {
if constexpr (std::is_void_v<RetType>) {
fn();
return {};
Expand All @@ -88,10 +87,10 @@ class AnyStrand {
execute(SomeHandlerWith<AnyStopToken> auto&& fn)
{
using RetType = std::decay_t<decltype(fn(std::declval<AnyStopToken>()))>;
static_assert(not std::is_same_v<RetType, impl::Any>);
static_assert(not std::is_same_v<RetType, std::any>);

return AnyOperation<RetType>( //
pimpl_->execute([fn = std::forward<decltype(fn)>(fn)](auto stopToken) -> impl::Any {
pimpl_->execute([fn = std::forward<decltype(fn)>(fn)](auto stopToken) -> std::any {
if constexpr (std::is_void_v<RetType>) {
fn(std::move(stopToken));
return {};
Expand All @@ -113,11 +112,11 @@ class AnyStrand {
execute(SomeHandlerWith<AnyStopToken> auto&& fn, SomeStdDuration auto timeout)
{
using RetType = std::decay_t<decltype(fn(std::declval<AnyStopToken>()))>;
static_assert(not std::is_same_v<RetType, impl::Any>);
static_assert(not std::is_same_v<RetType, std::any>);

return AnyOperation<RetType>( //
pimpl_->execute(
[fn = std::forward<decltype(fn)>(fn)](auto stopToken) -> impl::Any {
[fn = std::forward<decltype(fn)>(fn)](auto stopToken) -> std::any {
if constexpr (std::is_void_v<RetType>) {
fn(std::move(stopToken));
return {};
Expand All @@ -136,10 +135,10 @@ class AnyStrand {

[[nodiscard]] virtual impl::ErasedOperation
execute(
std::function<impl::Any(AnyStopToken)>,
std::function<std::any(AnyStopToken)>,
std::optional<std::chrono::milliseconds> timeout = std::nullopt
) = 0;
[[nodiscard]] virtual impl::ErasedOperation execute(std::function<impl::Any()>) = 0;
[[nodiscard]] virtual impl::ErasedOperation execute(std::function<std::any()>) = 0;
};

template <typename StrandType>
Expand All @@ -153,13 +152,13 @@ class AnyStrand {
}

[[nodiscard]] impl::ErasedOperation
execute(std::function<impl::Any(AnyStopToken)> fn, std::optional<std::chrono::milliseconds> timeout) override
execute(std::function<std::any(AnyStopToken)> fn, std::optional<std::chrono::milliseconds> timeout) override
{
return strand.execute(std::move(fn), timeout);
}

[[nodiscard]] impl::ErasedOperation
execute(std::function<impl::Any()> fn) override
execute(std::function<std::any()> fn) override
{
return strand.execute(std::move(fn));
}
Expand Down
53 changes: 0 additions & 53 deletions src/util/async/impl/Any.hpp

This file was deleted.

10 changes: 5 additions & 5 deletions src/util/async/impl/ErasedOperation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#include "util/Assert.hpp"
#include "util/async/Concepts.hpp"
#include "util/async/Error.hpp"
#include "util/async/impl/Any.hpp"

#include <any>
#include <expected>
#include <memory>
#include <type_traits>
Expand Down Expand Up @@ -55,7 +55,7 @@ class ErasedOperation {
pimpl_->wait();
}

std::expected<Any, ExecutionError>
std::expected<std::any, ExecutionError>
get()
{
return pimpl_->get();
Expand Down Expand Up @@ -87,7 +87,7 @@ class ErasedOperation {

virtual void
wait() noexcept = 0;
virtual std::expected<Any, ExecutionError>
virtual std::expected<std::any, ExecutionError>
get() = 0;
virtual void
requestStop() = 0;
Expand All @@ -111,10 +111,10 @@ class ErasedOperation {
return operation.wait();
}

std::expected<Any, ExecutionError>
std::expected<std::any, ExecutionError>
get() override
{
// Note: return type of the operation was already wrapped to impl::Any by AnyExecutionContext
// Note: return type of the operation was already wrapped to std::any by AnyExecutionContext
return operation.get();
}

Expand Down
20 changes: 10 additions & 10 deletions unittests/util/MockExecutionContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
#include "util/MockStrand.hpp"
#include "util/async/AnyStopToken.hpp"
#include "util/async/Error.hpp"
#include "util/async/impl/Any.hpp"

#include <gmock/gmock.h>

#include <any>
#include <chrono>
#include <expected>
#include <functional>
Expand All @@ -50,29 +50,29 @@ struct MockExecutionContext {
template <typename T>
using ScheduledOperation = MockScheduledOperation<T>;

MOCK_METHOD(Operation<util::async::impl::Any> const&, execute, (std::function<util::async::impl::Any()>), (const));
MOCK_METHOD(Operation<std::any> const&, execute, (std::function<std::any()>), (const));
MOCK_METHOD(
Operation<util::async::impl::Any> const&,
Operation<std::any> const&,
execute,
(std::function<util::async::impl::Any()>, std::optional<std::chrono::milliseconds>),
(std::function<std::any()>, std::optional<std::chrono::milliseconds>),
(const)
);
MOCK_METHOD(
StoppableOperation<util::async::impl::Any> const&,
StoppableOperation<std::any> const&,
execute,
(std::function<util::async::impl::Any(util::async::AnyStopToken)>, std::optional<std::chrono::milliseconds>),
(std::function<std::any(util::async::AnyStopToken)>, std::optional<std::chrono::milliseconds>),
(const)
);
MOCK_METHOD(
ScheduledOperation<util::async::impl::Any> const&,
ScheduledOperation<std::any> const&,
scheduleAfter,
(std::chrono::milliseconds, std::function<util::async::impl::Any(util::async::AnyStopToken)>),
(std::chrono::milliseconds, std::function<std::any(util::async::AnyStopToken)>),
(const)
);
MOCK_METHOD(
ScheduledOperation<util::async::impl::Any> const&,
ScheduledOperation<std::any> const&,
scheduleAfter,
(std::chrono::milliseconds, std::function<util::async::impl::Any(util::async::AnyStopToken, bool)>),
(std::chrono::milliseconds, std::function<std::any(util::async::AnyStopToken, bool)>),
(const)
);
MOCK_METHOD(MockStrand const&, makeStrand, (), (const));
Expand Down
Loading

0 comments on commit 2302122

Please sign in to comment.