Move some methods out of the attorney

* Code cleanup
This commit is contained in:
Denis Blank 2018-11-24 15:02:23 +01:00
parent 6969a9e392
commit 3a70356f16
7 changed files with 49 additions and 43 deletions

4
.gitignore vendored
View File

@ -48,4 +48,8 @@ bld/
# Visual Studo 2015 cache/options directory # Visual Studo 2015 cache/options directory
.vs/ .vs/
# VSCode
.vscode/ .vscode/
# TMP files generated from clang-format
*.TMP

View File

@ -92,6 +92,8 @@ template <typename Data, typename Annotation>
class continuable_base { class continuable_base {
/// \cond false /// \cond false
using ownership = detail::util::ownership;
template <typename, typename> template <typename, typename>
friend class continuable_base; friend class continuable_base;
friend struct detail::base::attorney; friend struct detail::base::attorney;
@ -104,11 +106,11 @@ class continuable_base {
// The continuation type or intermediate result // The continuation type or intermediate result
Data data_; Data data_;
// The transferable state which represents the validity of the object // The transferable state which represents the validity of the object
detail::util::ownership ownership_; ownership ownership_;
/// \endcond /// \endcond
/// Constructor accepting the data object while erasing the annotation /// Constructor accepting the data object while erasing the annotation
explicit continuable_base(Data data, detail::util::ownership ownership) explicit continuable_base(Data data, ownership ownership)
: data_(std::move(data)), ownership_(std::move(ownership)) { : data_(std::move(data)), ownership_(std::move(ownership)) {
} }
@ -121,7 +123,8 @@ public:
/// while erasing the annotation /// while erasing the annotation
template <typename OData, std::enable_if_t<std::is_convertible< template <typename OData, std::enable_if_t<std::is_convertible<
std::decay_t<OData>, Data>::value>* = nullptr> std::decay_t<OData>, Data>::value>* = nullptr>
continuable_base(OData&& data) : data_(std::forward<OData>(data)) { continuable_base(OData&& data) // NOLINT(misc-forwarding-reference-overload)
: data_(std::forward<OData>(data)) {
} }
/// Constructor taking the data of other continuable_base objects /// Constructor taking the data of other continuable_base objects
@ -131,7 +134,7 @@ public:
/// the continuable by any object which is useful for type-erasure. /// the continuable by any object which is useful for type-erasure.
template <typename OData, typename OAnnotation> template <typename OData, typename OAnnotation>
continuable_base(continuable_base<OData, OAnnotation>&& other) continuable_base(continuable_base<OData, OAnnotation>&& other)
: continuable_base(std::move(other).finish().consume_data()) { : continuable_base(std::move(other).finish().consume()) {
} }
/// \cond false /// \cond false
@ -710,7 +713,7 @@ private:
ownership_.release(); ownership_.release();
} }
Data&& consume_data() && { Data&& consume() && {
assert_acquired(); assert_acquired();
release(); release();
return std::move(data_); return std::move(data_);
@ -814,7 +817,7 @@ constexpr auto make_continuable(Continuation&& continuation) {
"use make_continuable<void>(...). Continuables with an exact " "use make_continuable<void>(...). Continuables with an exact "
"signature may be created through make_continuable<Args...>."); "signature may be created through make_continuable<Args...>.");
return detail::base::attorney::create( return detail::base::attorney::create_from(
std::forward<Continuation>(continuation), std::forward<Continuation>(continuation),
detail::hints::extract(detail::traits::identity<Args...>{}), detail::hints::extract(detail::traits::identity<Args...>{}),
detail::util::ownership{}); detail::util::ownership{});

View File

@ -162,7 +162,7 @@ struct connection_finalizer<connection_strategy_all_tag> {
auto signature = aggregated::hint_of_data<decltype(result)>(); auto signature = aggregated::hint_of_data<decltype(result)>();
return base::attorney::create( return base::attorney::create_from(
[result = std::move(result)](auto&& callback) mutable { [result = std::move(result)](auto&& callback) mutable {
using submitter_t = using submitter_t =

View File

@ -174,7 +174,7 @@ struct connection_finalizer<connection_strategy_any_tag> {
traversal::container_category_of_t<std::decay_t<Connection>>{}, traversal::container_category_of_t<std::decay_t<Connection>>{},
traits::identity<std::decay_t<Connection>>{})){}; traits::identity<std::decay_t<Connection>>{})){};
return base::attorney::create( return base::attorney::create_from(
[connection = [connection =
std::forward<Connection>(connection)](auto&& callback) mutable { std::forward<Connection>(connection)](auto&& callback) mutable {

View File

@ -142,7 +142,7 @@ struct connection_finalizer<connection_strategy_seq_tag> {
auto signature = aggregated::hint_of_data<decltype(result)>(); auto signature = aggregated::hint_of_data<decltype(result)>();
return base::attorney::create( return base::attorney::create_from(
[result = std::move(result)](auto&& callback) mutable { [result = std::move(result)](auto&& callback) mutable {
// The data from which the visitor is constructed in-place // The data from which the visitor is constructed in-place
using data_t = using data_t =

View File

@ -92,7 +92,7 @@ auto normalize(Strategy /*strategy*/,
continuable_base<Data, Strategy>&& continuation) { continuable_base<Data, Strategy>&& continuation) {
// If we are in the given strategy we can just use the data of the continuable // If we are in the given strategy we can just use the data of the continuable
return base::attorney::consume_data(std::move(continuation)); return base::attorney::consume(std::move(continuation));
} }
/// Entry function for connecting two continuables with a given strategy. /// Entry function for connecting two continuables with a given strategy.
@ -114,7 +114,7 @@ auto connect(Strategy strategy, continuable_base<LData, LAnnotation>&& left,
// Return a new continuable containing the tuple and holding // Return a new continuable containing the tuple and holding
// the current strategy as annotation. // the current strategy as annotation.
return base::attorney::create(std::move(data), strategy, ownership_); return base::attorney::create_from(std::move(data), strategy, ownership_);
} }
/// All strategies should specialize this class in order to provide: /// All strategies should specialize this class in order to provide:
@ -130,7 +130,7 @@ auto finalize_connection(continuable_base<Data, Strategy>&& continuation) {
using finalizer = connection_finalizer<Strategy>; using finalizer = connection_finalizer<Strategy>;
util::ownership ownership = base::attorney::ownership_of(continuation); util::ownership ownership = base::attorney::ownership_of(continuation);
auto connection = base::attorney::consume_data(std::move(continuation)); auto connection = base::attorney::consume(std::move(continuation));
// Return a new continuable which // Return a new continuable which
return finalizer::finalize(std::move(connection), std::move(ownership)); return finalizer::finalize(std::move(connection), std::move(ownership));

View File

@ -52,7 +52,7 @@ namespace detail {
/// ///
/// Important methods are: /// Important methods are:
/// - Creating a continuation from a callback taking functional /// - Creating a continuation from a callback taking functional
/// base::attorney::create(auto&& callback) /// base::attorney::create_from(auto&& callback)
/// -> base::continuation<auto> /// -> base::continuation<auto>
/// - Chaining a continuation together with a callback /// - Chaining a continuation together with a callback
/// base::chain_continuation(base::continuation<auto> continuation, /// base::chain_continuation(base::continuation<auto> continuation,
@ -67,38 +67,38 @@ struct is_continuable : std::false_type {};
template <typename Data, typename Annotation> template <typename Data, typename Annotation>
struct is_continuable<continuable_base<Data, Annotation>> : std::true_type {}; struct is_continuable<continuable_base<Data, Annotation>> : std::true_type {};
/// Helper class to access private methods and members of
/// the continuable_base class.
struct attorney { struct attorney {
/// Makes a continuation wrapper from the given argument /// Creates a continuable_base from the given continuation, annotation
template <typename T, typename A> /// and ownership.
static auto create(T&& continuation, A /*hint*/, util::ownership ownership_) { template <
return continuable_base<std::decay_t<T>, std::decay_t<A>>( typename T, typename A,
std::forward<T>(continuation), ownership_); typename Continuable = continuable_base<std::decay_t<T>, std::decay_t<A>>>
} static auto create_from(T&& continuation, A annotation,
util::ownership ownership) {
/// Invokes a continuation object in a reference correct way (void)annotation;
template <typename Data, typename Annotation, typename Callback> return Continuable(std::forward<T>(continuation), ownership);
static auto
invoke_continuation(continuable_base<Data, Annotation>&& continuation,
Callback&& callback) noexcept {
auto materialized = std::move(continuation).finish();
materialized.release();
return materialized.data_(std::forward<Callback>(callback));
}
template <typename Data, typename Annotation>
static Data&&
consume_data(continuable_base<Data, Annotation>&& continuation) {
return std::move(continuation).consume_data();
} }
/// Returns the ownership of the given continuable_base
template <typename Continuable> template <typename Continuable>
static util::ownership ownership_of(Continuable&& continuation) noexcept { static util::ownership ownership_of(Continuable&& continuation) noexcept {
return continuation.ownership_; return continuation.ownership_;
} }
template <typename Data, typename Annotation>
static Data&& consume(continuable_base<Data, Annotation>&& continuation) {
return std::move(continuation).consume();
}
}; };
/// Invokes a continuation object in a reference correct way
template <typename Data, typename Annotation, typename Callback>
void invoke_continuation(continuable_base<Data, Annotation>&& continuation,
Callback&& callback) noexcept {
util::invoke(attorney::consume(std::move(continuation).finish()),
std::forward<Callback>(callback));
}
// Returns the invoker of a callback, the next callback // Returns the invoker of a callback, the next callback
// and the arguments of the previous continuation. // and the arguments of the previous continuation.
// //
@ -174,7 +174,7 @@ invoker_of(traits::identity<continuable_base<Data, Annotation>>) {
util::partial_invoke(std::forward<decltype(callback)>(callback), util::partial_invoke(std::forward<decltype(callback)>(callback),
std::forward<decltype(args)>(args)...); std::forward<decltype(args)>(args)...);
attorney::invoke_continuation( invoke_continuation(
std::move(continuation_), std::move(continuation_),
std::forward<decltype(next_callback)>(next_callback)); std::forward<decltype(next_callback)>(next_callback));
CONTINUABLE_BLOCK_TRY_END CONTINUABLE_BLOCK_TRY_END
@ -480,7 +480,7 @@ struct final_callback : util::non_copyable {
} }
void set_exception(exception_t error) { void set_exception(exception_t error) {
// NOLINTNEXTLINE(hicpp-move-const-arg) // 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(error));
} }
}; };
@ -533,7 +533,7 @@ auto chain_continuation(Continuation&& continuation, Callback&& callback,
auto ownership_ = attorney::ownership_of(continuation); auto ownership_ = attorney::ownership_of(continuation);
continuation.freeze(); continuation.freeze();
return attorney::create( return attorney::create_from(
[continuation = std::forward<Continuation>(continuation), [continuation = std::forward<Continuation>(continuation),
callback = std::forward<Callback>(callback), callback = std::forward<Callback>(callback),
executor = executor =
@ -554,8 +554,7 @@ auto chain_continuation(Continuation&& continuation, Callback&& callback,
// Invoke the continuation with a proxy callback. // Invoke the continuation with a proxy callback.
// The proxy callback is responsible for passing // The proxy callback is responsible for passing
// the result to the callback as well as decorating it. // the result to the callback as well as decorating it.
attorney::invoke_continuation(std::move(continuation), invoke_continuation(std::move(continuation), std::move(proxy));
std::move(proxy));
}, },
next_hint, ownership_); next_hint, ownership_);
} }
@ -566,8 +565,8 @@ auto chain_continuation(Continuation&& continuation, Callback&& callback,
/// - Continuation: continuation<[](auto&& callback) { callback("hi"); }> /// - Continuation: continuation<[](auto&& callback) { callback("hi"); }>
template <typename Continuation> template <typename Continuation>
void finalize_continuation(Continuation&& continuation) { void finalize_continuation(Continuation&& continuation) {
attorney::invoke_continuation(std::forward<Continuation>(continuation), invoke_continuation(std::forward<Continuation>(continuation),
callbacks::final_callback{}); callbacks::final_callback{});
} }
/// Workaround for GCC bug: /// Workaround for GCC bug: