mirror of
https://github.com/Naios/continuable.git
synced 2025-12-06 16:56:44 +08:00
more
This commit is contained in:
parent
b57d14caa9
commit
3182c4d258
174
NextGen.cpp
174
NextGen.cpp
@ -115,6 +115,16 @@ auto appendHandlerToContinuation(Continuation&& cont, Handler&& handler) {
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include "Continuable.h"
|
||||||
|
|
||||||
|
// Equivalent to C++17's std::void_t which is targets a bug in GCC,
|
||||||
|
// that prevents correct SFINAE behavior.
|
||||||
|
// See http://stackoverflow.com/questions/35753920 for details.
|
||||||
|
template<typename...>
|
||||||
|
struct deduce_to_void : std::common_type<void> { };
|
||||||
|
|
||||||
|
template<typename... T>
|
||||||
|
using always_void_t = typename deduce_to_void<T...>::type;
|
||||||
|
|
||||||
struct SelfDispatcher {
|
struct SelfDispatcher {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -123,6 +133,9 @@ struct SelfDispatcher {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct Identity { };
|
||||||
|
|
||||||
template<typename Config>
|
template<typename Config>
|
||||||
class ContinuableBase;
|
class ContinuableBase;
|
||||||
|
|
||||||
@ -139,6 +152,24 @@ auto applyTuple(std::integer_sequence<S, I...>, T&& tuple, F&& function) {
|
|||||||
return std::forward<F>(function)(std::get<I>(std::forward<T>(tuple))...);
|
return std::forward<F>(function)(std::get<I>(std::forward<T>(tuple))...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<unsigned... LeftI, unsigned... RightI,
|
||||||
|
typename... Left, typename... Right>
|
||||||
|
auto tupleMerge(std::integer_sequence<unsigned, LeftI...>,
|
||||||
|
std::integer_sequence<unsigned, RightI...>,
|
||||||
|
std::tuple<Left...>&& left, std::tuple<Right...>&& right) {
|
||||||
|
return std::make_tuple(std::get<LeftI>(std::move(left))...,
|
||||||
|
std::get<RightI>(std::move(right))...);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Merges the left and the right tuple together in one.
|
||||||
|
template<typename... Left, typename... Right>
|
||||||
|
auto tupleMerge(std::tuple<Left...>&& left,
|
||||||
|
std::tuple<Right...>&& right) {
|
||||||
|
return tupleMerge(std::make_integer_sequence<unsigned, sizeof...(Left)>{},
|
||||||
|
std::make_integer_sequence<unsigned, sizeof...(Right)>{},
|
||||||
|
std::move(left), std::move(right));
|
||||||
|
}
|
||||||
|
|
||||||
class Ownership {
|
class Ownership {
|
||||||
public:
|
public:
|
||||||
Ownership() { }
|
Ownership() { }
|
||||||
@ -166,6 +197,61 @@ private:
|
|||||||
bool isOwning{ true };
|
bool isOwning{ true };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename Function>
|
||||||
|
struct undecorate_function;
|
||||||
|
|
||||||
|
template<typename ReturnType, typename... Args>
|
||||||
|
struct undecorate_function<ReturnType(Args...)> {
|
||||||
|
/// The return type of the function.
|
||||||
|
typedef ReturnType return_type;
|
||||||
|
/// The argument types of the function as pack in Identity.
|
||||||
|
typedef Identity<Args...> argument_type;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Mutable function pointers
|
||||||
|
template<typename ReturnType, typename... Args>
|
||||||
|
struct undecorate_function<ReturnType(*)(Args...)>
|
||||||
|
: undecorate_function<ReturnType(Args...)> { };
|
||||||
|
|
||||||
|
/// Const function pointers
|
||||||
|
template<typename ReturnType, typename... Args>
|
||||||
|
struct undecorate_function<ReturnType(*const)(Args...)>
|
||||||
|
: undecorate_function<ReturnType(Args...)> { };
|
||||||
|
|
||||||
|
/// Mutable class method pointers
|
||||||
|
template<typename ClassType, typename ReturnType, typename... Args>
|
||||||
|
struct undecorate_function<ReturnType(ClassType::*)(Args...)>
|
||||||
|
: undecorate_function<ReturnType(Args...)> { };
|
||||||
|
|
||||||
|
/// Const class method pointers
|
||||||
|
template<typename ClassType, typename ReturnType, typename... Args>
|
||||||
|
struct undecorate_function<ReturnType(ClassType::*)(Args...) const>
|
||||||
|
: undecorate_function<ReturnType(Args...)> { };
|
||||||
|
|
||||||
|
/// Mutable volatile class method pointers
|
||||||
|
template<typename ClassType, typename ReturnType, typename... Args>
|
||||||
|
struct undecorate_function<ReturnType(ClassType::*)(Args...) volatile>
|
||||||
|
: undecorate_function<ReturnType(Args...)> { };
|
||||||
|
|
||||||
|
/// Const volatile class method pointers
|
||||||
|
template<typename ClassType, typename ReturnType, typename... Args>
|
||||||
|
struct undecorate_function<ReturnType(ClassType::*)(Args...) const volatile>
|
||||||
|
: undecorate_function<ReturnType(Args...)> { };
|
||||||
|
|
||||||
|
template<typename Function>
|
||||||
|
using do_undecorate = std::conditional_t<
|
||||||
|
std::is_class<Function>::value,
|
||||||
|
decltype(&Function::operator()),
|
||||||
|
Function>;
|
||||||
|
|
||||||
|
template<typename Function, typename = always_void_t<>>
|
||||||
|
struct is_undecorateable : std::false_type { };
|
||||||
|
|
||||||
|
template<typename Function>
|
||||||
|
struct is_undecorateable<Function, always_void_t<
|
||||||
|
typename do_undecorate<Function>::return_type
|
||||||
|
>> : std::true_type { };
|
||||||
|
|
||||||
/// Decorates single values
|
/// Decorates single values
|
||||||
template<typename Value>
|
template<typename Value>
|
||||||
struct CallbackResultDecorator {
|
struct CallbackResultDecorator {
|
||||||
@ -322,42 +408,61 @@ private:
|
|||||||
Data data;
|
Data data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename... TargetArgs, typename... CombinedData>
|
||||||
|
auto undecorateCombined(Identity<TargetArgs...>,
|
||||||
|
std::tuple<CombinedData...> combined) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Callback, typename... CombinedData>
|
||||||
|
auto undecorateCombined(std::tuple<CombinedData...> combined) {
|
||||||
|
// using TargetArgs = typename do_undecorate<Callback>::argument_type;
|
||||||
|
// return undecorateCombined(TargetArgs{}, std::move(combined));
|
||||||
|
}
|
||||||
|
|
||||||
template<typename Combined>
|
template<typename Combined>
|
||||||
class LazyCombineDecoration {
|
class LazyCombineDecoration {
|
||||||
public:
|
public:
|
||||||
// TODO
|
// TODO
|
||||||
explicit LazyCombineDecoration(Ownership ownership_, Combined combined_)
|
explicit LazyCombineDecoration(Combined combined_)
|
||||||
: ownership(std::move(ownership_)), combined(std::move(combined_)) { }
|
: combined(std::move(combined_)) { }
|
||||||
|
|
||||||
// using Config = typename Data::Config;
|
// using Config = typename Data::Config;
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
static void requiresUndecorateable() {
|
||||||
|
static_assert(is_undecorateable<Callback>::value,
|
||||||
|
"Can't retrieve the signature of the given callback. "
|
||||||
|
"Consider to pass an untemplated function or functor "
|
||||||
|
"to the `then` method invocation to fix this.");
|
||||||
|
}
|
||||||
|
|
||||||
/// Return a r-value reference to the data
|
/// Return a r-value reference to the data
|
||||||
template<typename Callback>
|
template<typename Callback>
|
||||||
void undecorate()&& {
|
void undecorate()&& {
|
||||||
|
requiresUndecorateable<Callback>();
|
||||||
|
return undecorateCombined<Callback>(std::move(combined));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Callback>
|
template<typename Callback>
|
||||||
/// Return a copy of the data
|
/// Return a copy of the data
|
||||||
void undecorate() const& {
|
void undecorate() const& {
|
||||||
|
requiresUndecorateable<Callback>();
|
||||||
|
return undecorateCombined<Callback>(combined);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename RightLazyCombine>
|
template<typename RightLazyCombine>
|
||||||
auto merge(RightLazyCombine&& right) {
|
auto merge(RightLazyCombine right) && {
|
||||||
|
auto merged = tupleMerge(std::move(combined), std::move(right.combined));
|
||||||
|
return ContinuableBase<LazyCombineDecoration<decltype(merged)>> {
|
||||||
|
LazyCombineDecoration<decltype(merged)>{std::move(merged)}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ownership ownership;
|
|
||||||
Combined combined;
|
Combined combined;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename>
|
|
||||||
struct is_lazy_combined
|
|
||||||
: std::false_type { };
|
|
||||||
|
|
||||||
template<typename Data>
|
|
||||||
struct is_lazy_combined<LazyCombineDecoration<Data>>
|
|
||||||
: std::true_type { };
|
|
||||||
|
|
||||||
template<typename Continuation, typename Dispatcher = SelfDispatcher>
|
template<typename Continuation, typename Dispatcher = SelfDispatcher>
|
||||||
auto make_continuable(Continuation&& continuation,
|
auto make_continuable(Continuation&& continuation,
|
||||||
Dispatcher&& dispatcher = SelfDispatcher{}) noexcept {
|
Dispatcher&& dispatcher = SelfDispatcher{}) noexcept {
|
||||||
@ -398,25 +503,22 @@ auto postImpl(Data data ,NewDispatcher&& newDispatcher) {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Decoration>
|
template<typename Combined>
|
||||||
auto convertToLazyCombine(std::true_type /*is_lazy_combined*/,
|
auto toLazyCombined(LazyCombineDecoration<Combined> decoration) {
|
||||||
Decoration&& decoration) {
|
return std::move(decoration);
|
||||||
return std::forward<Decoration>(decoration);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Decoration>
|
template<typename Decoration>
|
||||||
auto convertToLazyCombine(std::false_type /*is_lazy_combined*/,
|
auto toLazyCombined(Decoration&& decoration) {
|
||||||
Decoration&& decoration) {
|
auto data = std::forward<Decoration>(decoration).template undecorate<void>();
|
||||||
return LazyCombineDecoration<std::tuple<>>{};
|
return LazyCombineDecoration<std::tuple<decltype(data)>>(std::move(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename LeftDecoration, typename RightDecoration>
|
template<typename LeftDecoration, typename RightDecoration>
|
||||||
auto combineImpl(LeftDecoration&& leftDecoration,
|
auto combineImpl(LeftDecoration&& leftDecoration,
|
||||||
RightDecoration&& rightDecoration) {
|
RightDecoration&& rightDecoration) {
|
||||||
return convertToLazyCombine(is_lazy_combined<std::decay_t<LeftDecoration>>{},
|
return toLazyCombined(std::forward<LeftDecoration>(leftDecoration))
|
||||||
std::forward<LeftDecoration>(leftDecoration))
|
.merge(toLazyCombined(std::forward<RightDecoration>(rightDecoration)));
|
||||||
.add(convertToLazyCombine(is_lazy_combined<std::decay_t<RightDecoration>>{},
|
|
||||||
std::forward<RightDecoration>(rightDecoration)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Decoration>
|
template<typename Decoration>
|
||||||
@ -430,7 +532,7 @@ public:
|
|||||||
|
|
||||||
~ContinuableBase() {
|
~ContinuableBase() {
|
||||||
// Undecorate/materialize the decoration
|
// Undecorate/materialize the decoration
|
||||||
invokeContinuation(std::move(decoration).template undecorate<void(*)()>());
|
// invokeContinuation(std::move(decoration).template undecorate<void>());
|
||||||
}
|
}
|
||||||
ContinuableBase(ContinuableBase&&) = default;
|
ContinuableBase(ContinuableBase&&) = default;
|
||||||
ContinuableBase(ContinuableBase const&) = default;
|
ContinuableBase(ContinuableBase const&) = default;
|
||||||
@ -443,7 +545,7 @@ public:
|
|||||||
|
|
||||||
template<typename Callback>
|
template<typename Callback>
|
||||||
auto then(Callback&& callback) const& {
|
auto then(Callback&& callback) const& {
|
||||||
return thenImpl(decoration.undecorate<Callback>(),
|
return thenImpl(decoration.template undecorate<Callback>(),
|
||||||
std::forward<Callback>(callback));
|
std::forward<Callback>(callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -479,11 +581,18 @@ public:
|
|||||||
return combineImpl(decoration, right.decoration);
|
return combineImpl(decoration, right.decoration);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
auto undecorateFor(Callback&&) {
|
||||||
|
return decoration.template undecorate<Callback>();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// The Decoration represents the possible lazy materialized
|
/// The Decoration represents the possible lazy materialized
|
||||||
/// data of the continuable.
|
/// data of the continuable.
|
||||||
/// The decoration pattern is used to make it possible to allow lazy chaining
|
/// The decoration pattern is used to make it possible to allow lazy chaining
|
||||||
/// of operators on Continuables like the and expression `&&`.
|
/// of operators on Continuables like the and expression `&&`,
|
||||||
|
/// that requires lazy evaluation and the signature of the callback chained
|
||||||
|
/// with ContinuableBase::then.
|
||||||
Decoration decoration;
|
Decoration decoration;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -510,16 +619,17 @@ struct FailIfWrongArgs {
|
|||||||
int main(int, char**) {
|
int main(int, char**) {
|
||||||
auto dispatcher = SelfDispatcher{};
|
auto dispatcher = SelfDispatcher{};
|
||||||
|
|
||||||
auto unwrap = [](auto&& callback) {
|
(makeTestContinuation() && makeTestContinuation())
|
||||||
|
.undecorateFor([]()
|
||||||
|
{
|
||||||
|
|
||||||
callback("47");
|
});
|
||||||
};
|
|
||||||
|
|
||||||
/*auto unwrapper = [](auto&&... args) {
|
/*auto unwrapper = [](auto&&... args) {
|
||||||
return std::common_type<std::tuple<decltype(args)...>>{};
|
return std::common_type<std::tuple<decltype(args)...>>{};
|
||||||
};*/
|
};*/
|
||||||
|
|
||||||
using T = decltype(unwrap(FailIfWrongArgs<0>{}));
|
// using T = decltype(unwrap(FailIfWrongArgs<0>{}));
|
||||||
|
|
||||||
// using T = decltype(unwrap(std::declval<Inspector>()));
|
// using T = decltype(unwrap(std::declval<Inspector>()));
|
||||||
// T t{};
|
// T t{};
|
||||||
@ -528,7 +638,7 @@ int main(int, char**) {
|
|||||||
|
|
||||||
int res = 0;
|
int res = 0;
|
||||||
makeTestContinuation()
|
makeTestContinuation()
|
||||||
.then([](std::string /*str*/) {
|
.then([](std::string) {
|
||||||
return std::make_tuple(47, 46, 45);
|
return std::make_tuple(47, 46, 45);
|
||||||
})
|
})
|
||||||
// .post(dispatcher)
|
// .post(dispatcher)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user