mirror of
https://github.com/Naios/continuable.git
synced 2025-12-06 08:46:44 +08:00
Use a promise<> for work rather than a dedicated work_base
This commit is contained in:
parent
389002e918
commit
a2fdfdfceb
@ -35,7 +35,6 @@
|
||||
#include <continuable/continuable-base.hpp>
|
||||
#include <continuable/continuable-primitives.hpp>
|
||||
#include <continuable/continuable-promise-base.hpp>
|
||||
#include <continuable/continuable-work-base.hpp>
|
||||
#include <continuable/detail/other/erasure.hpp>
|
||||
|
||||
namespace cti {
|
||||
@ -83,8 +82,11 @@ using promise = promise_base<detail::erasure::callback<Args...>, //
|
||||
signature_arg_t<Args...>>;
|
||||
|
||||
/// Defines a non-copyable type erasure which is capable of carrying
|
||||
/// callable objects passed to executors. Additionally the outstanding work
|
||||
/// can be resolved through an exception.
|
||||
/// callable objects passed to executors.
|
||||
///
|
||||
/// The work behaves like a `promise<>` but the work type erasure uses extra
|
||||
/// stack space for small object optimization.
|
||||
/// Additionally the outstanding work can be resolved through an exception.
|
||||
///
|
||||
/// \note You can always define your own cancelable_work with a type erasure of
|
||||
/// choice, the type erasure wrapper just needs to accept a
|
||||
@ -92,7 +94,8 @@ using promise = promise_base<detail::erasure::callback<Args...>, //
|
||||
/// `void(exception_arg_t, exception_t)` signature.
|
||||
///
|
||||
/// \since 4.0.0
|
||||
using work = work_base<detail::erasure::work>;
|
||||
using work = promise_base<detail::erasure::work, //
|
||||
signature_arg_t<>>;
|
||||
/// \}
|
||||
} // namespace cti
|
||||
|
||||
|
||||
@ -1,135 +0,0 @@
|
||||
|
||||
/*
|
||||
|
||||
/~` _ _ _|_. _ _ |_ | _
|
||||
\_,(_)| | | || ||_|(_||_)|(/_
|
||||
|
||||
https://github.com/Naios/continuable
|
||||
v4.0.0
|
||||
|
||||
Copyright(c) 2015 - 2019 Denis Blank <denis.blank at outlook dot com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files(the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions :
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
**/
|
||||
|
||||
#ifndef CONTINUABLE_WORK_BASE_HPP_INCLUDED
|
||||
#define CONTINUABLE_WORK_BASE_HPP_INCLUDED
|
||||
|
||||
#include <cassert>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <continuable/continuable-primitives.hpp>
|
||||
#include <continuable/detail/core/annotation.hpp>
|
||||
#include <continuable/detail/core/types.hpp>
|
||||
#include <continuable/detail/utility/traits.hpp>
|
||||
#include <continuable/detail/utility/util.hpp>
|
||||
|
||||
namespace cti {
|
||||
/// \defgroup Base Base
|
||||
/// provides classes and functions to create continuable_base objects.
|
||||
/// \{
|
||||
|
||||
/// The work_base makes it possible to resolve an asynchronous
|
||||
/// work on a different execution context than the current one.
|
||||
///
|
||||
/// A work compatible object is passed to any executor that is passed to
|
||||
/// \see continuable_base::then or \see async_on.
|
||||
///
|
||||
/// \since 4.0.0
|
||||
template <typename Data>
|
||||
class work_base
|
||||
/// \cond false
|
||||
: detail::util::non_copyable
|
||||
/// \endcond
|
||||
{ // clang-format on
|
||||
|
||||
/// \cond false
|
||||
// The work type
|
||||
Data data_;
|
||||
/// \endcond
|
||||
|
||||
public:
|
||||
/// Constructor accepting the data object
|
||||
explicit work_base(Data data) : data_(std::move(data)) {
|
||||
}
|
||||
|
||||
/// \cond false
|
||||
work_base(work_base&&) = default;
|
||||
work_base(work_base const&) = delete;
|
||||
|
||||
work_base& operator=(work_base&&) = default;
|
||||
work_base& operator=(work_base const&) = delete;
|
||||
/// \endcond
|
||||
|
||||
/// Constructor accepting any object convertible to the data object
|
||||
template <typename OData,
|
||||
std::enable_if_t<std::is_convertible<
|
||||
detail::traits::unrefcv_t<OData>, Data>::value>* = nullptr>
|
||||
/* implicit */ work_base(OData&& data) : data_(std::forward<OData>(data)) {
|
||||
}
|
||||
|
||||
/// Assignment operator accepting any object convertible to the data object
|
||||
template <typename OData,
|
||||
std::enable_if_t<std::is_convertible<
|
||||
detail::traits::unrefcv_t<OData>, Data>::value>* = nullptr>
|
||||
work_base& operator=(OData&& data) {
|
||||
data_ = std::forward<OData>(data);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Invokes the underlying work
|
||||
///
|
||||
/// \throws This method never throws an exception.
|
||||
///
|
||||
/// \attention This method may only be called once!
|
||||
///
|
||||
/// \since 4.0.0
|
||||
void set_value() noexcept {
|
||||
std::move(data_)();
|
||||
data_ = nullptr;
|
||||
}
|
||||
|
||||
/// Passes an exception to the underlying work
|
||||
///
|
||||
/// \throws This method never throws an exception.
|
||||
///
|
||||
/// \attention This method may only be called once!
|
||||
///
|
||||
/// \since 4.0.0
|
||||
void set_exception(exception_t exception) noexcept {
|
||||
std::move(data_)(exception_arg_t{}, std::move(exception));
|
||||
data_ = nullptr;
|
||||
}
|
||||
|
||||
/// \copydoc set_value
|
||||
void operator()() && noexcept {
|
||||
std::move(data_)();
|
||||
data_ = nullptr;
|
||||
}
|
||||
|
||||
/// \copydoc set_exception
|
||||
void operator()(exception_arg_t tag, exception_t exception) && noexcept {
|
||||
std::move(data_)(tag, std::move(exception));
|
||||
data_ = nullptr;
|
||||
}
|
||||
};
|
||||
/// \}
|
||||
} // namespace cti
|
||||
|
||||
#endif // CONTINUABLE_WORK_BASE_HPP_INCLUDED
|
||||
@ -57,6 +57,5 @@ namespace cti {}
|
||||
#include <continuable/continuable-traverse-async.hpp>
|
||||
#include <continuable/continuable-traverse.hpp>
|
||||
#include <continuable/continuable-types.hpp>
|
||||
#include <continuable/continuable-work-base.hpp>
|
||||
|
||||
#endif // CONTINUABLE_HPP_INCLUDED
|
||||
|
||||
@ -523,6 +523,10 @@ public:
|
||||
std::move(next_callback_)(exception_arg_t{}, std::move(exception));
|
||||
}
|
||||
|
||||
explicit operator bool() const noexcept {
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
Invoker invoker_;
|
||||
Callback callback_;
|
||||
@ -671,12 +675,12 @@ struct callback_base<identity<Args...>, HandleResults, HandleErrors, Callback,
|
||||
Executor, NextCallback>>::operator();
|
||||
|
||||
/// Resolves the continuation with the given values
|
||||
void set_value(Args... args) {
|
||||
void set_value(Args... args) noexcept {
|
||||
std::move (*this)(std::move(args)...);
|
||||
}
|
||||
|
||||
/// Resolves the continuation with the given error variable.
|
||||
void set_exception(exception_t error) {
|
||||
void set_exception(exception_t error) noexcept {
|
||||
std::move (*this)(exception_arg_t{}, std::move(error));
|
||||
}
|
||||
|
||||
@ -725,13 +729,13 @@ struct final_callback : util::non_copyable {
|
||||
#endif // CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS
|
||||
}
|
||||
|
||||
void set_value(Args... args) {
|
||||
void set_value(Args... args) noexcept {
|
||||
std::move (*this)(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
void set_exception(exception_t error) {
|
||||
void set_exception(exception_t exception) noexcept {
|
||||
// NOLINTNEXTLINE(hicpp-move-const-arg, performance-move-const-arg)
|
||||
std::move (*this)(exception_arg_t{}, std::move(error));
|
||||
std::move (*this)(exception_arg_t{}, std::move(exception));
|
||||
}
|
||||
|
||||
explicit operator bool() const noexcept {
|
||||
|
||||
@ -85,11 +85,11 @@ public:
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void set_value(Args... args) {
|
||||
void set_value(Args... args) noexcept {
|
||||
std::move (*this)(std::move(args)...);
|
||||
}
|
||||
|
||||
void set_exception(exception_t error) {
|
||||
void set_exception(exception_t error) noexcept {
|
||||
std::move (*this)(exception_arg_t{}, std::move(error));
|
||||
}
|
||||
|
||||
|
||||
@ -87,11 +87,12 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
void operator()(Args... args) && {
|
||||
void operator()(Args... args) && noexcept {
|
||||
std::move(erasure_)(std::move(args)...);
|
||||
}
|
||||
|
||||
void operator()(exception_arg_t exception_arg, exception_t exception) && {
|
||||
void operator()(exception_arg_t exception_arg, exception_t exception) &&
|
||||
noexcept {
|
||||
std::move(erasure_)(exception_arg, std::move(exception));
|
||||
}
|
||||
|
||||
@ -101,6 +102,62 @@ public:
|
||||
};
|
||||
#endif
|
||||
|
||||
using work_erasure_t =
|
||||
fu2::function_base<true, false, fu2::capacity_fixed<32UL>, true, false,
|
||||
void()&&, void(exception_arg_t, exception_t) &&>;
|
||||
|
||||
#ifdef CONTINUABLE_HAS_IMMEDIATE_TYPES
|
||||
using work = work_erasure_t;
|
||||
#else
|
||||
class work;
|
||||
|
||||
template <typename T>
|
||||
struct is_work : std::false_type {};
|
||||
template <>
|
||||
struct is_work<work> : std::true_type {};
|
||||
|
||||
class work {
|
||||
using erasure_t = work_erasure_t;
|
||||
erasure_t erasure_;
|
||||
|
||||
public:
|
||||
work() = default;
|
||||
~work() = default;
|
||||
work(work const&) = delete;
|
||||
work(work&&) = default;
|
||||
work& operator=(work const&) = delete;
|
||||
work& operator=(work&&) = default;
|
||||
|
||||
template <
|
||||
typename T,
|
||||
std::enable_if_t<std::is_convertible<T, erasure_t>::value>* = nullptr,
|
||||
std::enable_if_t<!is_work<traits::unrefcv_t<T>>::value>* = nullptr>
|
||||
/* implicit */ work(T&& callable) : erasure_(std::forward<T>(callable)) {
|
||||
}
|
||||
|
||||
template <
|
||||
typename T,
|
||||
std::enable_if_t<std::is_assignable<erasure_t, T>::value>* = nullptr,
|
||||
std::enable_if_t<!is_work<traits::unrefcv_t<T>>::value>* = nullptr>
|
||||
work& operator=(T&& callable) {
|
||||
erasure_ = std::forward<T>(callable);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void operator()() && noexcept {
|
||||
std::move(erasure_)();
|
||||
}
|
||||
|
||||
void operator()(exception_arg_t, exception_t exception) && noexcept {
|
||||
std::move(erasure_)(exception_arg_t{}, std::move(exception));
|
||||
}
|
||||
|
||||
explicit operator bool() const noexcept {
|
||||
return bool(erasure_);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
template <typename... Args>
|
||||
struct continuation_capacity {
|
||||
using type = union {
|
||||
@ -177,58 +234,6 @@ public:
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
using work_erasure_t =
|
||||
fu2::function_base<true, false, fu2::capacity_fixed<32UL>, true, false,
|
||||
void()&&, void(exception_arg_t, exception_t) &&>;
|
||||
|
||||
#ifdef CONTINUABLE_HAS_IMMEDIATE_TYPES
|
||||
using work = work_erasure_t;
|
||||
#else
|
||||
class work;
|
||||
|
||||
template <typename T>
|
||||
struct is_work : std::false_type {};
|
||||
template <>
|
||||
struct is_work<work> : std::true_type {};
|
||||
|
||||
class work {
|
||||
using erasure_t = work_erasure_t;
|
||||
erasure_t erasure_;
|
||||
|
||||
public:
|
||||
work() = default;
|
||||
~work() = default;
|
||||
work(work const&) = delete;
|
||||
work(work&&) = default;
|
||||
work& operator=(work const&) = delete;
|
||||
work& operator=(work&&) = default;
|
||||
|
||||
template <
|
||||
typename T,
|
||||
std::enable_if_t<std::is_convertible<T, erasure_t>::value>* = nullptr,
|
||||
std::enable_if_t<!is_work<traits::unrefcv_t<T>>::value>* = nullptr>
|
||||
/* implicit */ work(T&& callable) : erasure_(std::forward<T>(callable)) {
|
||||
}
|
||||
|
||||
template <
|
||||
typename T,
|
||||
std::enable_if_t<std::is_assignable<erasure_t, T>::value>* = nullptr,
|
||||
std::enable_if_t<!is_work<traits::unrefcv_t<T>>::value>* = nullptr>
|
||||
work& operator=(T&& callable) {
|
||||
erasure_ = std::forward<T>(callable);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void operator()() && {
|
||||
std::move(erasure_)();
|
||||
}
|
||||
|
||||
void operator()(exception_arg_t, exception_t e) && {
|
||||
std::move(erasure_)(exception_arg_t{}, std::move(e));
|
||||
}
|
||||
};
|
||||
#endif
|
||||
} // namespace erasure
|
||||
} // namespace detail
|
||||
} // namespace cti
|
||||
|
||||
@ -48,8 +48,28 @@ TYPED_TEST(single_dimension_tests, are_executor_dispatchable) {
|
||||
}
|
||||
|
||||
TYPED_TEST(single_dimension_tests, are_executor_exception_resolveable) {
|
||||
auto executor = [&](work work) {
|
||||
work.set_exception(supply_test_exception()); //
|
||||
auto executor = [&](auto&& work) {
|
||||
std::forward<decltype(work)>(work).set_exception(supply_test_exception());
|
||||
};
|
||||
|
||||
ASSERT_ASYNC_EXCEPTION_RESULT(async_on(
|
||||
[] {
|
||||
FAIL(); //
|
||||
},
|
||||
executor),
|
||||
get_test_exception_proto());
|
||||
|
||||
ASSERT_ASYNC_EXCEPTION_RESULT(this->supply().then(
|
||||
[] {
|
||||
FAIL(); //
|
||||
},
|
||||
executor),
|
||||
get_test_exception_proto());
|
||||
}
|
||||
|
||||
TYPED_TEST(single_dimension_tests, are_executor_exception_resolveable_erased) {
|
||||
auto executor = [&](work work) {
|
||||
std::move(work).set_exception(supply_test_exception()); //
|
||||
};
|
||||
|
||||
ASSERT_ASYNC_EXCEPTION_RESULT(async_on(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user