Generalize undecorators with an undecorator function

This commit is contained in:
Denis Blank 2016-10-13 00:48:25 +02:00
parent ec15592bca
commit 591592385a

View File

@ -352,7 +352,8 @@ void invokeContinuation(Data data) {
} }
} }
/// /// Holds the effective data for the continuable, like the current owner status,
/// the continuation object and the dispatcher.
template<typename ContinuationType, template<typename ContinuationType,
typename DispatcherType> typename DispatcherType>
struct ContinuableData { struct ContinuableData {
@ -396,29 +397,40 @@ struct ContinuableData {
Dispatcher dispatcher; Dispatcher dispatcher;
}; };
/// The DefaultDecoration is a container for already materialized /// An undecorator that undecorates nothing
/// ContinuableData which can be accessed instantly. struct UndecorateNone {
template<typename Data> template<typename /*Target*/, typename T>
class DefaultDecoration { static auto undecorate(T&& data) -> std::decay_t<T> {
return std::forward<T>(data);
}
};
/// The Undecorateable is a container for unmaterialized
/// ContinuableData which can be accessed on demand.
template<typename Data,
typename Undecorator = UndecorateNone>
class Undecorateable {
public: public:
explicit DefaultDecoration(Data data_) explicit Undecorateable(Data data_)
: data(std::move(data_)) { } : data(std::move(data_)) { }
/// Return a r-value reference to the data /// Return a r-value reference to the data
template<typename Callback> template<typename Target>
Data&& undecorate()&& { auto undecorate()&& {
return std::move(data); return Undecorator::template undecorate<Target>(std::move(data));
} }
/// Return a copy of the data /// Return a copy of the data
template<typename Callback> template<typename Target>
Data undecorate() const& { auto undecorate() const& {
return data; return Undecorator::template undecorate<Target>(data);
} }
private: private:
Data data; Data data;
}; };
template<typename... TargetArgs, typename... CombinedData> template<typename... TargetArgs, typename... CombinedData>
auto undecorateCombined(Identity<TargetArgs...>, auto undecorateCombined(Identity<TargetArgs...>,
std::tuple<CombinedData...> combined) { std::tuple<CombinedData...> combined) {
@ -431,6 +443,7 @@ auto undecorateCombined(std::tuple<CombinedData...> combined) {
// return undecorateCombined(TargetArgs{}, std::move(combined)); // return undecorateCombined(TargetArgs{}, std::move(combined));
} }
// FIXME
template<typename Combined> template<typename Combined>
class LazyCombineDecoration { class LazyCombineDecoration {
public: public:
@ -477,7 +490,7 @@ private:
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 {
using Decoration = DefaultDecoration<ContinuableData< using Decoration = Undecorateable<ContinuableData<
std::decay_t<Continuation>, std::decay_t<Continuation>,
std::decay_t<Dispatcher> std::decay_t<Dispatcher>
>>; >>;
@ -491,7 +504,7 @@ template<typename Undecorated, typename Callback>
auto thenImpl(Undecorated undecorated, Callback&& callback) { auto thenImpl(Undecorated undecorated, Callback&& callback) {
auto next = appendCallback(std::move(undecorated.continuation), auto next = appendCallback(std::move(undecorated.continuation),
std::forward<Callback>(callback)); std::forward<Callback>(callback));
using Decoration = DefaultDecoration< using Decoration = Undecorateable<
typename Undecorated::template ChangeContinuationTo<decltype(next)> typename Undecorated::template ChangeContinuationTo<decltype(next)>
>; >;
return ContinuableBase<Decoration>(Decoration({ return ContinuableBase<Decoration>(Decoration({
@ -503,7 +516,7 @@ auto thenImpl(Undecorated undecorated, Callback&& callback) {
template<typename Undecorated, typename NewDispatcher> template<typename Undecorated, typename NewDispatcher>
auto postImpl(Undecorated undecorated, NewDispatcher&& newDispatcher) { auto postImpl(Undecorated undecorated, NewDispatcher&& newDispatcher) {
using Decoration = DefaultDecoration< using Decoration = Undecorateable<
typename Undecorated::template typename Undecorated::template
ChangeDispatcherTo<std::decay_t<NewDispatcher>> ChangeDispatcherTo<std::decay_t<NewDispatcher>>
>; >;