Remove Any wrapper (#1336)

Fixes #1174
This commit is contained in:
Alex Kremer
2024-04-10 19:14:53 +01:00
committed by GitHub
parent 7fcd3e48bd
commit 230212213b
10 changed files with 117 additions and 176 deletions

View File

@@ -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>
@@ -66,9 +65,9 @@ public:
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 {};
@@ -90,18 +89,16 @@ public:
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)));
}
}));
}
/**
@@ -117,10 +114,10 @@ public:
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 {};
@@ -145,12 +142,12 @@ public:
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 {};
@@ -175,12 +172,12 @@ public:
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 {};
@@ -211,14 +208,14 @@ private:
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;
};
@@ -232,25 +229,25 @@ private:
}
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));
}

View File

@@ -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>

View File

@@ -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>
@@ -64,10 +63,10 @@ public:
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 {};
@@ -88,10 +87,10 @@ public:
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 {};
@@ -113,11 +112,11 @@ public:
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 {};
@@ -136,10 +135,10 @@ private:
[[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>
@@ -153,13 +152,13 @@ private:
}
[[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));
}

View File

@@ -1,53 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2024, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#pragma once
#include <any>
#include <type_traits>
// Will be removed after the migration to std::expected is complete (#1173)
// Issue to track this removal can be found here: https://github.com/XRPLF/clio/issues/1174
namespace util::async::impl {
/**
* @brief A wrapper for std::any to workaround issues with boost.outcome
*/
class Any {
std::any value_;
public:
Any() = default;
Any(Any const&) = default;
Any(Any&&) = default;
// note: this needs to be `auto` instead of `std::any` because of a bug in gcc 11.4
Any(auto&& v)
requires(std::is_same_v<std::decay_t<decltype(v)>, std::any>)
: value_{std::forward<decltype(v)>(v)}
{
}
operator std::any&() noexcept
{
return value_;
}
};
} // namespace util::async::impl

View File

@@ -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>
@@ -55,7 +55,7 @@ public:
pimpl_->wait();
}
std::expected<Any, ExecutionError>
std::expected<std::any, ExecutionError>
get()
{
return pimpl_->get();
@@ -87,7 +87,7 @@ private:
virtual void
wait() noexcept = 0;
virtual std::expected<Any, ExecutionError>
virtual std::expected<std::any, ExecutionError>
get() = 0;
virtual void
requestStop() = 0;
@@ -111,10 +111,10 @@ private:
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();
}

View File

@@ -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>
@@ -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));

View File

@@ -22,10 +22,10 @@
#include "util/MockOperation.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>
@@ -41,23 +41,23 @@ struct MockStrand {
template <typename T>
using StoppableOperation = MockStoppableOperation<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::function<std::any(util::async::AnyStopToken)>),
(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)
);
};

View File

@@ -54,8 +54,8 @@ struct AnyExecutionContextTests : Test {
TEST_F(AnyExecutionContextTests, ExecuteWithoutTokenAndVoid)
{
auto mockOp = OperationType<impl::Any>{};
EXPECT_CALL(mockExecutionContext, execute(An<std::function<impl::Any()>>())).WillOnce(ReturnRef(mockOp));
auto mockOp = OperationType<std::any>{};
EXPECT_CALL(mockExecutionContext, execute(An<std::function<std::any()>>())).WillOnce(ReturnRef(mockOp));
EXPECT_CALL(mockOp, get());
auto op = ctx.execute([] { throw 0; });
@@ -66,17 +66,17 @@ TEST_F(AnyExecutionContextTests, ExecuteWithoutTokenAndVoid)
TEST_F(AnyExecutionContextTests, ExecuteWithoutTokenAndVoidThrowsException)
{
auto mockOp = OperationType<impl::Any>{};
EXPECT_CALL(mockExecutionContext, execute(An<std::function<impl::Any()>>()))
.WillOnce([](auto&&) -> OperationType<impl::Any> const& { throw 0; });
auto mockOp = OperationType<std::any>{};
EXPECT_CALL(mockExecutionContext, execute(An<std::function<std::any()>>()))
.WillOnce([](auto&&) -> OperationType<std::any> const& { throw 0; });
EXPECT_ANY_THROW([[maybe_unused]] auto unused = ctx.execute([] { throw 0; }));
}
TEST_F(AnyExecutionContextTests, ExecuteWithStopTokenAndVoid)
{
auto mockOp = StoppableOperationType<impl::Any>{};
EXPECT_CALL(mockExecutionContext, execute(An<std::function<impl::Any(AnyStopToken)>>(), _))
auto mockOp = StoppableOperationType<std::any>{};
EXPECT_CALL(mockExecutionContext, execute(An<std::function<std::any(AnyStopToken)>>(), _))
.WillOnce(ReturnRef(mockOp));
EXPECT_CALL(mockOp, get());
@@ -88,17 +88,17 @@ TEST_F(AnyExecutionContextTests, ExecuteWithStopTokenAndVoid)
TEST_F(AnyExecutionContextTests, ExecuteWithStopTokenAndVoidThrowsException)
{
EXPECT_CALL(mockExecutionContext, execute(An<std::function<impl::Any(AnyStopToken)>>(), _))
.WillOnce([](auto&&, auto) -> StoppableOperationType<impl::Any> const& { throw 0; });
EXPECT_CALL(mockExecutionContext, execute(An<std::function<std::any(AnyStopToken)>>(), _))
.WillOnce([](auto&&, auto) -> StoppableOperationType<std::any> const& { throw 0; });
EXPECT_ANY_THROW([[maybe_unused]] auto unused = ctx.execute([](auto) { throw 0; }));
}
TEST_F(AnyExecutionContextTests, ExecuteWithStopTokenAndReturnValue)
{
auto mockOp = StoppableOperationType<impl::Any>{};
auto mockOp = StoppableOperationType<std::any>{};
EXPECT_CALL(mockOp, get()).WillOnce(Return(std::make_any<int>(42)));
EXPECT_CALL(mockExecutionContext, execute(An<std::function<impl::Any(AnyStopToken)>>(), _))
EXPECT_CALL(mockExecutionContext, execute(An<std::function<std::any(AnyStopToken)>>(), _))
.WillOnce(ReturnRef(mockOp));
auto op = ctx.execute([](auto) -> int { throw 0; });
@@ -109,19 +109,19 @@ TEST_F(AnyExecutionContextTests, ExecuteWithStopTokenAndReturnValue)
TEST_F(AnyExecutionContextTests, ExecuteWithStopTokenAndReturnValueThrowsException)
{
EXPECT_CALL(mockExecutionContext, execute(An<std::function<impl::Any(AnyStopToken)>>(), _))
.WillOnce([](auto&&, auto) -> StoppableOperationType<impl::Any> const& { throw 0; });
EXPECT_CALL(mockExecutionContext, execute(An<std::function<std::any(AnyStopToken)>>(), _))
.WillOnce([](auto&&, auto) -> StoppableOperationType<std::any> const& { throw 0; });
EXPECT_ANY_THROW([[maybe_unused]] auto unused = ctx.execute([](auto) -> int { throw 0; }));
}
TEST_F(AnyExecutionContextTests, TimerCancellation)
{
auto mockScheduledOp = ScheduledOperationType<impl::Any>{};
auto mockScheduledOp = ScheduledOperationType<std::any>{};
EXPECT_CALL(mockScheduledOp, cancel());
EXPECT_CALL(
mockExecutionContext,
scheduleAfter(An<std::chrono::milliseconds>(), An<std::function<impl::Any(AnyStopToken)>>())
scheduleAfter(An<std::chrono::milliseconds>(), An<std::function<std::any(AnyStopToken)>>())
)
.WillOnce(ReturnRef(mockScheduledOp));
@@ -133,13 +133,13 @@ TEST_F(AnyExecutionContextTests, TimerCancellation)
TEST_F(AnyExecutionContextTests, TimerExecuted)
{
auto mockScheduledOp = ScheduledOperationType<impl::Any>{};
auto mockScheduledOp = ScheduledOperationType<std::any>{};
EXPECT_CALL(mockScheduledOp, get()).WillOnce(Return(std::make_any<int>(42)));
EXPECT_CALL(
mockExecutionContext,
scheduleAfter(An<std::chrono::milliseconds>(), An<std::function<impl::Any(AnyStopToken)>>())
scheduleAfter(An<std::chrono::milliseconds>(), An<std::function<std::any(AnyStopToken)>>())
)
.WillOnce([&mockScheduledOp](auto, auto&&) -> ScheduledOperationType<impl::Any> const& {
.WillOnce([&mockScheduledOp](auto, auto&&) -> ScheduledOperationType<std::any> const& {
return mockScheduledOp;
});
@@ -151,11 +151,11 @@ TEST_F(AnyExecutionContextTests, TimerExecuted)
TEST_F(AnyExecutionContextTests, TimerWithBoolHandlerCancellation)
{
auto mockScheduledOp = ScheduledOperationType<impl::Any>{};
auto mockScheduledOp = ScheduledOperationType<std::any>{};
EXPECT_CALL(mockScheduledOp, cancel());
EXPECT_CALL(
mockExecutionContext,
scheduleAfter(An<std::chrono::milliseconds>(), An<std::function<impl::Any(AnyStopToken, bool)>>())
scheduleAfter(An<std::chrono::milliseconds>(), An<std::function<std::any(AnyStopToken, bool)>>())
)
.WillOnce(ReturnRef(mockScheduledOp));
@@ -167,13 +167,13 @@ TEST_F(AnyExecutionContextTests, TimerWithBoolHandlerCancellation)
TEST_F(AnyExecutionContextTests, TimerWithBoolHandlerExecuted)
{
auto mockScheduledOp = ScheduledOperationType<impl::Any>{};
auto mockScheduledOp = ScheduledOperationType<std::any>{};
EXPECT_CALL(mockScheduledOp, get()).WillOnce(Return(std::make_any<int>(42)));
EXPECT_CALL(
mockExecutionContext,
scheduleAfter(An<std::chrono::milliseconds>(), An<std::function<impl::Any(AnyStopToken, bool)>>())
scheduleAfter(An<std::chrono::milliseconds>(), An<std::function<std::any(AnyStopToken, bool)>>())
)
.WillOnce([&mockScheduledOp](auto, auto&&) -> ScheduledOperationType<impl::Any> const& {
.WillOnce([&mockScheduledOp](auto, auto&&) -> ScheduledOperationType<std::any> const& {
return mockScheduledOp;
});
@@ -185,11 +185,11 @@ TEST_F(AnyExecutionContextTests, TimerWithBoolHandlerExecuted)
TEST_F(AnyExecutionContextTests, StrandExecuteWithVoid)
{
auto mockOp = OperationType<impl::Any>{};
auto mockOp = OperationType<std::any>{};
auto mockStrand = StrandType{};
EXPECT_CALL(mockOp, get());
EXPECT_CALL(mockExecutionContext, makeStrand()).WillOnce(ReturnRef(mockStrand));
EXPECT_CALL(mockStrand, execute(An<std::function<impl::Any()>>())).WillOnce(ReturnRef(mockOp));
EXPECT_CALL(mockStrand, execute(An<std::function<std::any()>>())).WillOnce(ReturnRef(mockOp));
auto strand = ctx.makeStrand();
static_assert(std::is_same_v<decltype(strand), AnyStrand>);
@@ -204,8 +204,8 @@ TEST_F(AnyExecutionContextTests, StrandExecuteWithVoidThrowsException)
{
auto mockStrand = StrandType{};
EXPECT_CALL(mockExecutionContext, makeStrand()).WillOnce(ReturnRef(mockStrand));
EXPECT_CALL(mockStrand, execute(An<std::function<impl::Any()>>()))
.WillOnce([](auto&&) -> OperationType<impl::Any> const& { throw 0; });
EXPECT_CALL(mockStrand, execute(An<std::function<std::any()>>()))
.WillOnce([](auto&&) -> OperationType<std::any> const& { throw 0; });
auto strand = ctx.makeStrand();
static_assert(std::is_same_v<decltype(strand), AnyStrand>);
@@ -215,11 +215,11 @@ TEST_F(AnyExecutionContextTests, StrandExecuteWithVoidThrowsException)
TEST_F(AnyExecutionContextTests, StrandExecuteWithReturnValue)
{
auto mockOp = OperationType<impl::Any>{};
auto mockOp = OperationType<std::any>{};
auto mockStrand = StrandType{};
EXPECT_CALL(mockOp, get()).WillOnce(Return(std::make_any<int>(42)));
EXPECT_CALL(mockExecutionContext, makeStrand()).WillOnce(ReturnRef(mockStrand));
EXPECT_CALL(mockStrand, execute(An<std::function<impl::Any()>>())).WillOnce(ReturnRef(mockOp));
EXPECT_CALL(mockStrand, execute(An<std::function<std::any()>>())).WillOnce(ReturnRef(mockOp));
auto strand = ctx.makeStrand();
static_assert(std::is_same_v<decltype(strand), AnyStrand>);
@@ -234,8 +234,8 @@ TEST_F(AnyExecutionContextTests, StrandExecuteWithReturnValueThrowsException)
{
auto mockStrand = StrandType{};
EXPECT_CALL(mockExecutionContext, makeStrand()).WillOnce(ReturnRef(mockStrand));
EXPECT_CALL(mockStrand, execute(An<std::function<impl::Any()>>()))
.WillOnce([](auto&&) -> OperationType<impl::Any> const& { throw 0; });
EXPECT_CALL(mockStrand, execute(An<std::function<std::any()>>()))
.WillOnce([](auto&&) -> OperationType<std::any> const& { throw 0; });
auto strand = ctx.makeStrand();
static_assert(std::is_same_v<decltype(strand), AnyStrand>);
@@ -245,11 +245,11 @@ TEST_F(AnyExecutionContextTests, StrandExecuteWithReturnValueThrowsException)
TEST_F(AnyExecutionContextTests, StrandExecuteWithStopTokenAndVoid)
{
auto mockOp = StoppableOperationType<impl::Any>{};
auto mockOp = StoppableOperationType<std::any>{};
auto mockStrand = StrandType{};
EXPECT_CALL(mockOp, get());
EXPECT_CALL(mockExecutionContext, makeStrand()).WillOnce(ReturnRef(mockStrand));
EXPECT_CALL(mockStrand, execute(An<std::function<impl::Any(AnyStopToken)>>(), _)).WillOnce(ReturnRef(mockOp));
EXPECT_CALL(mockStrand, execute(An<std::function<std::any(AnyStopToken)>>(), _)).WillOnce(ReturnRef(mockOp));
auto strand = ctx.makeStrand();
static_assert(std::is_same_v<decltype(strand), AnyStrand>);
@@ -264,8 +264,8 @@ TEST_F(AnyExecutionContextTests, StrandExecuteWithStopTokenAndVoidThrowsExceptio
{
auto mockStrand = StrandType{};
EXPECT_CALL(mockExecutionContext, makeStrand()).WillOnce(ReturnRef(mockStrand));
EXPECT_CALL(mockStrand, execute(An<std::function<impl::Any(AnyStopToken)>>(), _))
.WillOnce([](auto&&, auto) -> StoppableOperationType<impl::Any> const& { throw 0; });
EXPECT_CALL(mockStrand, execute(An<std::function<std::any(AnyStopToken)>>(), _))
.WillOnce([](auto&&, auto) -> StoppableOperationType<std::any> const& { throw 0; });
auto strand = ctx.makeStrand();
static_assert(std::is_same_v<decltype(strand), AnyStrand>);
@@ -275,11 +275,11 @@ TEST_F(AnyExecutionContextTests, StrandExecuteWithStopTokenAndVoidThrowsExceptio
TEST_F(AnyExecutionContextTests, StrandExecuteWithStopTokenAndReturnValue)
{
auto mockOp = StoppableOperationType<impl::Any>{};
auto mockOp = StoppableOperationType<std::any>{};
auto mockStrand = StrandType{};
EXPECT_CALL(mockOp, get()).WillOnce(Return(std::make_any<int>(42)));
EXPECT_CALL(mockExecutionContext, makeStrand()).WillOnce(ReturnRef(mockStrand));
EXPECT_CALL(mockStrand, execute(An<std::function<impl::Any(AnyStopToken)>>(), _)).WillOnce(ReturnRef(mockOp));
EXPECT_CALL(mockStrand, execute(An<std::function<std::any(AnyStopToken)>>(), _)).WillOnce(ReturnRef(mockOp));
auto strand = ctx.makeStrand();
static_assert(std::is_same_v<decltype(strand), AnyStrand>);
@@ -294,8 +294,8 @@ TEST_F(AnyExecutionContextTests, StrandExecuteWithStopTokenAndReturnValueThrowsE
{
auto mockStrand = StrandType{};
EXPECT_CALL(mockExecutionContext, makeStrand()).WillOnce(ReturnRef(mockStrand));
EXPECT_CALL(mockStrand, execute(An<std::function<impl::Any(AnyStopToken)>>(), _))
.WillOnce([](auto&&, auto) -> StoppableOperationType<impl::Any> const& { throw 0; });
EXPECT_CALL(mockStrand, execute(An<std::function<std::any(AnyStopToken)>>(), _))
.WillOnce([](auto&&, auto) -> StoppableOperationType<std::any> const& { throw 0; });
auto strand = ctx.makeStrand();
static_assert(std::is_same_v<decltype(strand), AnyStrand>);

View File

@@ -32,8 +32,8 @@ using namespace util::async;
using namespace ::testing;
struct AnyOperationTests : Test {
using OperationType = MockOperation<std::expected<impl::Any, ExecutionError>>;
using ScheduledOperationType = MockScheduledOperation<std::expected<impl::Any, ExecutionError>>;
using OperationType = MockOperation<std::expected<std::any, ExecutionError>>;
using ScheduledOperationType = MockScheduledOperation<std::expected<std::any, ExecutionError>>;
NaggyMock<OperationType> mockOp;
NaggyMock<ScheduledOperationType> mockScheduledOp;
@@ -46,8 +46,7 @@ using AnyOperationDeathTest = AnyOperationTests;
TEST_F(AnyOperationTests, VoidDataYieldsNoError)
{
auto const noError = std::expected<impl::Any, ExecutionError>(impl::Any{});
EXPECT_CALL(mockOp, get()).WillOnce(Return(noError));
EXPECT_CALL(mockOp, get()).WillOnce(Return(std::any{}));
auto res = voidOp.get();
ASSERT_TRUE(res);
}

View File

@@ -48,8 +48,8 @@ struct AnyStrandTests : ::testing::Test {
TEST_F(AnyStrandTests, ExecuteWithoutTokenAndVoid)
{
auto mockOp = OperationType<impl::Any>{};
EXPECT_CALL(mockStrand, execute(An<std::function<impl::Any()>>())).WillOnce(ReturnRef(mockOp));
auto mockOp = OperationType<std::any>{};
EXPECT_CALL(mockStrand, execute(An<std::function<std::any()>>())).WillOnce(ReturnRef(mockOp));
auto op = strand.execute([] {});
static_assert(std::is_same_v<decltype(op), AnyOperation<void>>);
@@ -59,17 +59,17 @@ TEST_F(AnyStrandTests, ExecuteWithoutTokenAndVoid)
TEST_F(AnyStrandTests, ExecuteWithoutTokenAndVoidThrowsException)
{
auto mockOp = OperationType<impl::Any>{};
EXPECT_CALL(mockStrand, execute(An<std::function<impl::Any()>>()))
.WillOnce([](auto&&) -> OperationType<impl::Any> const& { throw 0; });
auto mockOp = OperationType<std::any>{};
EXPECT_CALL(mockStrand, execute(An<std::function<std::any()>>()))
.WillOnce([](auto&&) -> OperationType<std::any> const& { throw 0; });
EXPECT_ANY_THROW([[maybe_unused]] auto unused = strand.execute([] {}));
}
TEST_F(AnyStrandTests, ExecuteWithStopTokenAndVoid)
{
auto mockOp = StoppableOperationType<impl::Any>{};
EXPECT_CALL(mockStrand, execute(An<std::function<impl::Any(AnyStopToken)>>(), _)).WillOnce(ReturnRef(mockOp));
auto mockOp = StoppableOperationType<std::any>{};
EXPECT_CALL(mockStrand, execute(An<std::function<std::any(AnyStopToken)>>(), _)).WillOnce(ReturnRef(mockOp));
auto op = strand.execute([](auto) {});
static_assert(std::is_same_v<decltype(op), AnyOperation<void>>);
@@ -79,17 +79,17 @@ TEST_F(AnyStrandTests, ExecuteWithStopTokenAndVoid)
TEST_F(AnyStrandTests, ExecuteWithStopTokenAndVoidThrowsException)
{
EXPECT_CALL(mockStrand, execute(An<std::function<impl::Any(AnyStopToken)>>(), _))
.WillOnce([](auto&&, auto) -> StoppableOperationType<impl::Any> const& { throw 0; });
EXPECT_CALL(mockStrand, execute(An<std::function<std::any(AnyStopToken)>>(), _))
.WillOnce([](auto&&, auto) -> StoppableOperationType<std::any> const& { throw 0; });
EXPECT_ANY_THROW([[maybe_unused]] auto unused = strand.execute([](auto) {}));
}
TEST_F(AnyStrandTests, ExecuteWithStopTokenAndReturnValue)
{
auto mockOp = StoppableOperationType<impl::Any>{};
auto mockOp = StoppableOperationType<std::any>{};
EXPECT_CALL(mockOp, get()).WillOnce(Return(std::make_any<int>(42)));
EXPECT_CALL(mockStrand, execute(An<std::function<impl::Any(AnyStopToken)>>(), _)).WillOnce(ReturnRef(mockOp));
EXPECT_CALL(mockStrand, execute(An<std::function<std::any(AnyStopToken)>>(), _)).WillOnce(ReturnRef(mockOp));
auto op = strand.execute([](auto) { return 42; });
static_assert(std::is_same_v<decltype(op), AnyOperation<int>>);
@@ -99,17 +99,17 @@ TEST_F(AnyStrandTests, ExecuteWithStopTokenAndReturnValue)
TEST_F(AnyStrandTests, ExecuteWithStopTokenAndReturnValueThrowsException)
{
EXPECT_CALL(mockStrand, execute(An<std::function<impl::Any(AnyStopToken)>>(), _))
.WillOnce([](auto&&, auto) -> StoppableOperationType<impl::Any> const& { throw 0; });
EXPECT_CALL(mockStrand, execute(An<std::function<std::any(AnyStopToken)>>(), _))
.WillOnce([](auto&&, auto) -> StoppableOperationType<std::any> const& { throw 0; });
EXPECT_ANY_THROW([[maybe_unused]] auto unused = strand.execute([](auto) { return 42; }));
}
TEST_F(AnyStrandTests, ExecuteWithTimeoutAndStopTokenAndReturnValue)
{
auto mockOp = StoppableOperationType<impl::Any>{};
auto mockOp = StoppableOperationType<std::any>{};
EXPECT_CALL(mockOp, get()).WillOnce(Return(std::make_any<int>(42)));
EXPECT_CALL(mockStrand, execute(An<std::function<impl::Any(AnyStopToken)>>(), _)).WillOnce(ReturnRef(mockOp));
EXPECT_CALL(mockStrand, execute(An<std::function<std::any(AnyStopToken)>>(), _)).WillOnce(ReturnRef(mockOp));
auto op = strand.execute([](auto) { return 42; }, std::chrono::milliseconds{1});
static_assert(std::is_same_v<decltype(op), AnyOperation<int>>);
@@ -119,8 +119,8 @@ TEST_F(AnyStrandTests, ExecuteWithTimeoutAndStopTokenAndReturnValue)
TEST_F(AnyStrandTests, ExecuteWithTimoutAndStopTokenAndReturnValueThrowsException)
{
EXPECT_CALL(mockStrand, execute(An<std::function<impl::Any(AnyStopToken)>>(), _))
.WillOnce([](auto&&, auto) -> StoppableOperationType<impl::Any> const& { throw 0; });
EXPECT_CALL(mockStrand, execute(An<std::function<std::any(AnyStopToken)>>(), _))
.WillOnce([](auto&&, auto) -> StoppableOperationType<std::any> const& { throw 0; });
EXPECT_ANY_THROW(
[[maybe_unused]] auto unused = strand.execute([](auto) { return 42; }, std::chrono::milliseconds{1})