/* This file is part of Telegram Desktop, the official desktop version of Telegram messaging app, see https://telegram.org Telegram Desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. In addition, as a special exception, the copyright holders give permission to link the code of portions of this program with the OpenSSL library. Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org */ #pragma once #include "base/lambda.h" #include #include namespace rpl { namespace details { template class mutable_lambda_wrap { public: mutable_lambda_wrap(Lambda &&lambda) : _lambda(std::move(lambda)) { } template auto operator()(Args&&... args) const { return (const_cast(this)->_lambda)( std::forward(args)...); } private: Lambda _lambda; }; // Type-erased copyable mutable lambda using base::lambda. template class mutable_lambda; template class mutable_lambda { public: // Copy / move construct / assign from an arbitrary type. template < typename Lambda, typename = std::enable_if_t()( std::declval()...)), Return >::value>> mutable_lambda(Lambda other) : _implementation( mutable_lambda_wrap(std::move(other))) { } template < typename ...OtherArgs, typename = std::enable_if_t< (sizeof...(Args) == sizeof...(OtherArgs))>> Return operator()(OtherArgs&&... args) { return _implementation(std::forward(args)...); } private: base::lambda _implementation; }; } // namespace details template class producer { public: using value_type = Value; using error_type = Error; using consumer_type = consumer; template < typename Generator, typename = std::enable_if()( std::declval())), lifetime>::value>> producer(Generator &&generator); template < typename OnNext, typename OnError, typename OnDone, typename = std::enable_if_t< details::is_callable_v && details::is_callable_v && details::is_callable_v>> lifetime start( OnNext &&next, OnError &&error, OnDone &&done) &&; template < typename OnNext, typename OnError, typename OnDone, typename = std::enable_if_t< details::is_callable_v && details::is_callable_v && details::is_callable_v>> lifetime start_copy( OnNext &&next, OnError &&error, OnDone &&done) const &; lifetime start_existing(const consumer_type &consumer) &&; private: details::mutable_lambda< lifetime(const consumer_type &)> _generator; }; template template inline producer::producer(Generator &&generator) : _generator(std::forward(generator)) { } template template < typename OnNext, typename OnError, typename OnDone, typename> inline lifetime producer::start( OnNext &&next, OnError &&error, OnDone &&done) && { return std::move(*this).start_existing(consumer( std::forward(next), std::forward(error), std::forward(done))); } template template < typename OnNext, typename OnError, typename OnDone, typename> inline lifetime producer::start_copy( OnNext &&next, OnError &&error, OnDone &&done) const & { auto copy = *this; return std::move(copy).start( std::forward(next), std::forward(error), std::forward(done)); } template inline lifetime producer::start_existing( const consumer_type &consumer) && { consumer.add_lifetime(std::move(_generator)(consumer)); return [consumer] { consumer.terminate(); }; } template inline producer duplicate( const producer &value) { return value; } template < typename Value, typename Error, typename Method, typename = decltype(std::declval()( std::declval>()))> inline auto operator|(producer &&value, Method &&method) { return std::forward(method)(std::move(value)); } template inline auto bind_on_next(OnNext &&handler) { return [handler = std::forward(handler)]( auto &&existing) mutable { using error_type = typename std::decay_t::error_type; return producer([ existing = std::move(existing), handler = std::move(handler) ](const consumer &consumer) mutable { return std::move(existing).start( std::move(handler), [consumer](error_type &&error) { consumer.put_error(std::move(error)); }, [consumer] { consumer.put_done(); }); }); }; } template inline auto bind_on_error(OnError &&handler) { return [handler = std::forward(handler)](auto &&existing) mutable { using value_type = typename std::decay_t::value_type; return producer([ existing = std::move(existing), handler = std::move(handler) ](const consumer &consumer) mutable { return std::move(existing).start( [consumer](value_type &&value) { consumer.put_next(std::move(value)); }, std::move(handler), [consumer] { consumer.put_done(); }); }); }; } template inline auto bind_on_done(OnDone &&handler) { return [handler = std::forward(handler)](auto &&existing) mutable { using value_type = typename std::decay_t::value_type; using error_type = typename std::decay_t::error_type; return producer([ existing = std::move(existing), handler = std::move(handler) ](const consumer &consumer) mutable { return std::move(existing).start( [consumer](value_type &&value) { consumer.put_next(std::move(value)); }, [consumer](error_type &&value) { consumer.put_error(std::move(value)); }, std::move(handler)); }); }; } namespace details { template struct next_holder { OnNext next; }; template struct error_holder { OnError error; }; template struct done_holder { OnDone done; }; template < typename Value, typename Error, typename OnNext> struct producer_with_next { producer bound; OnNext next; }; template < typename Value, typename Error, typename OnError> struct producer_with_error { producer bound; OnError error; }; template < typename Value, typename Error, typename OnDone> struct producer_with_done { producer bound; OnDone done; }; template < typename Value, typename Error, typename OnNext, typename OnError> struct producer_with_next_error { producer bound; OnNext next; OnError error; }; template < typename Value, typename Error, typename OnNext, typename OnDone> struct producer_with_next_done { producer bound; OnNext next; OnDone done; }; template < typename Value, typename Error, typename OnError, typename OnDone> struct producer_with_error_done { producer bound; OnError error; OnDone done; }; template < typename Value, typename Error, typename OnNext, typename OnError, typename OnDone> struct producer_with_next_error_done { producer bound; OnNext next; OnError error; OnDone done; }; struct lifetime_holder { lifetime &alive_while; }; template struct lifetime_holder_1 { Callback1 callback1; lifetime &alive_while; }; template struct lifetime_holder_2 { Callback1 callback1; Callback2 callback2; lifetime &alive_while; }; template struct lifetime_holder_3 { Callback1 callback1; Callback2 callback2; Callback3 callback3; lifetime &alive_while; }; enum class CallbackType { Next, Error, Done, Invalid, }; template inline auto on_next_dispatch( Callback &&callback, std::integral_constant) { return [](const auto&) {}; } template inline decltype(auto) on_next_dispatch( Callback &&callback, std::integral_constant) { return std::forward(callback); } template inline auto on_error_dispatch( Callback &&callback, std::integral_constant) { return [](const auto&) {}; } template inline decltype(auto) on_error_dispatch( Callback &&callback, std::integral_constant) { return std::forward(callback); } template inline auto on_done_dispatch( Callback &&callback, std::integral_constant) { return [] {}; } template inline decltype(auto) on_done_dispatch( Callback &&callback, std::integral_constant) { return std::forward(callback); } enum class CallbacksType { NextError, NextDone, ErrorDone, Invalid, }; template inline auto on_next_dispatch( Callback1 &&callback1, std::integral_constant) { return [](const auto&) {}; } template inline decltype(auto) on_next_dispatch( Callback1 &&callback1, std::integral_constant) { return std::forward(callback1); } template inline decltype(auto) on_next_dispatch( Callback1 &&callback1, std::integral_constant) { return std::forward(callback1); } template inline auto on_error_dispatch( Callback1 &&callback1, Callback2 &&callback2, std::integral_constant) { return [](const auto&) {}; } template inline decltype(auto) on_error_dispatch( Callback1 &&callback1, Callback2 &&callback2, std::integral_constant) { return std::forward(callback2); } template inline decltype(auto) on_error_dispatch( Callback1 &&callback1, Callback2 &&callback2, std::integral_constant) { return std::forward(callback1); } template inline auto on_done_dispatch( Callback2 &&callback2, std::integral_constant) { return [] {}; } template inline decltype(auto) on_done_dispatch( Callback2 &&callback2, std::integral_constant) { return std::forward(callback2); } template inline decltype(auto) on_done_dispatch( Callback2 &&callback2, std::integral_constant) { return std::forward(callback2); } } // namespace details template inline details::next_holder> on_next(OnNext &&handler) { return { std::forward(handler) }; } template inline details::error_holder> on_error(OnError &&handler) { return { std::forward(handler) }; } template inline details::done_holder> on_done(OnDone &&handler) { return { std::forward(handler) }; } inline details::lifetime_holder start(lifetime &alive_while) { return { alive_while }; } template inline details::lifetime_holder_1< std::decay_t> start( Callback1 &&callback1, lifetime &alive_while) { return { std::forward(callback1), alive_while }; } template inline details::lifetime_holder_2< std::decay_t, std::decay_t> start( Callback1 &&callback1, Callback2 &&callback2, lifetime &alive_while) { return { std::forward(callback1), std::forward(callback2), alive_while }; } template inline details::lifetime_holder_3< std::decay_t, std::decay_t, std::decay_t> start( Callback1 &&callback1, Callback2 &&callback2, Callback3 &&callback3, lifetime &alive_while) { return { std::forward(callback1), std::forward(callback2), std::forward(callback3), alive_while }; } namespace details { template < typename Value, typename Error, typename OnNext, typename = std::enable_if_t< is_callable_v>> inline producer_with_next operator|( producer &&value, next_holder &&handler) { return { std::move(value), std::move(handler.next) }; } template < typename Value, typename Error, typename OnError, typename = std::enable_if_t< is_callable_v>> inline producer_with_error operator|( producer &&value, error_holder &&handler) { return { std::move(value), std::move(handler.error) }; } template < typename Value, typename Error, typename OnDone, typename = std::enable_if_t< is_callable_v>> inline producer_with_done operator|( producer &&value, done_holder &&handler) { return { std::move(value), std::move(handler.done) }; } template < typename Value, typename Error, typename OnNext, typename OnError, typename = std::enable_if_t< is_callable_v>, typename = std::enable_if_t< is_callable_v>> inline producer_with_next_error operator|( producer_with_next &&producer_with_next, error_holder &&handler) { return { std::move(producer_with_next.bound), std::move(producer_with_next.next), std::move(handler.error) }; } template < typename Value, typename Error, typename OnNext, typename OnError, typename = std::enable_if_t< is_callable_v>, typename = std::enable_if_t< is_callable_v>> inline producer_with_next_error operator|( producer_with_error &&producer_with_error, next_holder &&handler) { return { std::move(producer_with_error.bound), std::move(handler.next), std::move(producer_with_error.error) }; } template < typename Value, typename Error, typename OnNext, typename OnDone, typename = std::enable_if_t< is_callable_v && is_callable_v>> inline producer_with_next_done operator|( producer_with_next &&producer_with_next, done_holder &&handler) { return { std::move(producer_with_next.bound), std::move(producer_with_next.next), std::move(handler.done) }; } template < typename Value, typename Error, typename OnNext, typename OnDone, typename = std::enable_if_t< is_callable_v && is_callable_v>> inline producer_with_next_done operator|( producer_with_done &&producer_with_done, next_holder &&handler) { return { std::move(producer_with_done.bound), std::move(handler.next), std::move(producer_with_done.done) }; } template < typename Value, typename Error, typename OnError, typename OnDone, typename = std::enable_if_t< is_callable_v && is_callable_v>> inline producer_with_error_done operator|( producer_with_error &&producer_with_error, done_holder &&handler) { return { std::move(producer_with_error.bound), std::move(producer_with_error.error), std::move(handler.done) }; } template < typename Value, typename Error, typename OnError, typename OnDone, typename = std::enable_if_t< is_callable_v && is_callable_v>> inline producer_with_error_done operator|( producer_with_done &&producer_with_done, error_holder &&handler) { return { std::move(producer_with_done.bound), std::move(handler.error), std::move(producer_with_done.done) }; } template < typename Value, typename Error, typename OnNext, typename OnError, typename OnDone, typename = std::enable_if_t< is_callable_v && is_callable_v && is_callable_v>> inline producer_with_next_error_done< Value, Error, OnNext, OnError, OnDone> operator|( producer_with_next_error< Value, Error, OnNext, OnError> &&producer_with_next_error, done_holder &&handler) { return { std::move(producer_with_next_error.bound), std::move(producer_with_next_error.next), std::move(producer_with_next_error.error), std::move(handler.done) }; } template < typename Value, typename Error, typename OnNext, typename OnError, typename OnDone, typename = std::enable_if_t< is_callable_v && is_callable_v && is_callable_v>> inline producer_with_next_error_done< Value, Error, OnNext, OnError, OnDone> operator|( producer_with_next_done< Value, Error, OnNext, OnDone> &&producer_with_next_done, error_holder &&handler) { return { std::move(producer_with_next_done.bound), std::move(producer_with_next_done.next), std::move(handler.error), std::move(producer_with_next_done.done) }; } template < typename Value, typename Error, typename OnNext, typename OnError, typename OnDone, typename = std::enable_if_t< is_callable_v && is_callable_v && is_callable_v>> inline producer_with_next_error_done< Value, Error, OnNext, OnError, OnDone> operator|( producer_with_error_done< Value, Error, OnError, OnDone> &&producer_with_error_done, next_holder &&handler) { return { std::move(producer_with_error_done.bound), std::move(handler.next), std::move(producer_with_error_done.error), std::move(producer_with_error_done.done) }; } template < typename Value, typename Error, typename OnNext, typename OnError, typename OnDone> inline void operator|( producer_with_next_error_done< Value, Error, OnNext, OnError, OnDone> &&producer_with_next_error_done, lifetime_holder &&lifetime) { lifetime.alive_while.add( std::move(producer_with_next_error_done.bound).start( std::move(producer_with_next_error_done.next), std::move(producer_with_next_error_done.error), std::move(producer_with_next_error_done.done))); } template inline void operator|( producer &&value, lifetime_holder &&start_with_lifetime) { return std::move(value) | on_next([](const Value&) {}) | on_error([](const Error&) {}) | on_done([] {}) | std::move(start_with_lifetime); } template inline void operator|( producer_with_next< Value, Error, OnNext> &&producer_with_next, lifetime_holder &&start_with_lifetime) { return std::move(producer_with_next) | on_error([](const Error&) {}) | on_done([] {}) | std::move(start_with_lifetime); } template inline void operator|( producer_with_error< Value, Error, OnError> &&producer_with_error, lifetime_holder &&start_with_lifetime) { return std::move(producer_with_error) | on_next([](const Value&) {}) | on_done([] {}) | std::move(start_with_lifetime); } template inline void operator|( producer_with_done< Value, Error, OnDone> &&producer_with_done, lifetime_holder &&start_with_lifetime) { return std::move(producer_with_done) | on_next([](const Value&) {}) | on_error([](const Error&) {}) | std::move(start_with_lifetime); } template < typename Value, typename Error, typename OnNext, typename OnError> inline void operator|( producer_with_next_error< Value, Error, OnNext, OnError> &&producer_with_next_error, lifetime_holder &&start_with_lifetime) { return std::move(producer_with_next_error) | on_done([] {}) | std::move(start_with_lifetime); } template < typename Value, typename Error, typename OnNext, typename OnDone> inline void operator|( producer_with_next_done< Value, Error, OnNext, OnDone> &&producer_with_next_done, lifetime_holder &&start_with_lifetime) { return std::move(producer_with_next_done) | on_error([](const Error&) {}) | std::move(start_with_lifetime); } template < typename Value, typename Error, typename OnError, typename OnDone> inline void operator|( producer_with_error_done< Value, Error, OnError, OnDone> &&producer_with_error_done, lifetime_holder &&start_with_lifetime) { return std::move(producer_with_error_done) | on_next([](const Value&) {}) | std::move(start_with_lifetime); } template < typename Value, typename Error, typename Callback1, typename = std::enable_if_t< is_callable_v // || is_callable_v || is_callable_v >> inline void operator|( producer &&value, lifetime_holder_1 &&start_with_lifetime) { using callback1_type = std::integral_constant ? CallbackType::Next : // is_callable_v ? CallbackType::Error : is_callable_v ? CallbackType::Done : CallbackType::Invalid>; start_with_lifetime.alive_while.add( std::move(value).start( on_next_dispatch(std::move(start_with_lifetime.callback1), callback1_type{}), on_error_dispatch(std::move(start_with_lifetime.callback1), callback1_type{}), on_done_dispatch(std::move(start_with_lifetime.callback1), callback1_type{}))); } template < typename Value, typename Error, typename OnNext, typename Callback1, typename = std::enable_if_t< is_callable_v || is_callable_v >> inline void operator|( producer_with_next &&producer_with_next, lifetime_holder_1 &&start_with_lifetime) { using callback1_type = std::integral_constant ? CallbackType::Error : is_callable_v ? CallbackType::Done : CallbackType::Invalid>; start_with_lifetime.alive_while.add( std::move(producer_with_next.bound).start( std::move(producer_with_next.next), on_error_dispatch(std::move(start_with_lifetime.callback1), callback1_type{}), on_done_dispatch(std::move(start_with_lifetime.callback1), callback1_type{}))); } template < typename Value, typename Error, typename OnError, typename Callback1, typename = std::enable_if_t< is_callable_v || is_callable_v >> inline void operator|( producer_with_error &&producer_with_error, lifetime_holder_1 &&start_with_lifetime) { using callback1_type = std::integral_constant ? CallbackType::Next : is_callable_v ? CallbackType::Done : CallbackType::Invalid>; start_with_lifetime.alive_while.add( std::move(producer_with_error.bound).start( on_next_dispatch(std::move(start_with_lifetime.callback1), callback1_type{}), std::move(producer_with_error.error), on_done_dispatch(std::move(start_with_lifetime.callback1), callback1_type{}))); } template < typename Value, typename Error, typename OnDone, typename Callback1, typename = std::enable_if_t< is_callable_v // || is_callable_v >> inline void operator|( producer_with_done &&producer_with_done, lifetime_holder_1 &&start_with_lifetime) { using callback1_type = std::integral_constant ? CallbackType::Next : // is_callable_v ? CallbackType::Error : CallbackType::Invalid>; start_with_lifetime.alive_while.add( std::move(producer_with_done.bound).start( on_next_dispatch(std::move(start_with_lifetime.callback1), callback1_type{}), on_error_dispatch(std::move(start_with_lifetime.callback1), callback1_type{}), std::move(producer_with_done.done))); } template < typename Value, typename Error, typename OnNext, typename OnError, typename Callback1, typename = std::enable_if_t< is_callable_v>> inline void operator|( producer_with_next_error &&producer_with_next_error, lifetime_holder_1 &&start_with_lifetime) { start_with_lifetime.alive_while.add( std::move(producer_with_next_error.bound).start( std::move(producer_with_next_error.next), std::move(producer_with_next_error.error), std::move(start_with_lifetime.callback1))); } template < typename Value, typename Error, typename OnNext, typename OnDone, typename Callback1, typename = std::enable_if_t< is_callable_v>> inline void operator|( producer_with_next_done &&producer_with_next_done, lifetime_holder_1 &&start_with_lifetime) { start_with_lifetime.alive_while.add( std::move(producer_with_next_done.bound).start( std::move(producer_with_next_done.next), std::move(start_with_lifetime.callback1), std::move(producer_with_next_done.done))); } template < typename Value, typename Error, typename OnError, typename OnDone, typename Callback1, typename = std::enable_if_t< is_callable_v>> inline void operator|( producer_with_error_done &&producer_with_error_done, lifetime_holder_1 &&start_with_lifetime) { start_with_lifetime.alive_while.add( std::move(producer_with_error_done.bound).start( std::move(start_with_lifetime.callback1), std::move(producer_with_error_done.error), std::move(producer_with_error_done.done))); } template < typename Value, typename Error, typename Callback1, typename Callback2, typename = std::enable_if_t< (is_callable_v && is_callable_v) || (is_callable_v && is_callable_v) // || (is_callable_v && is_callable_v) >> inline void operator|( producer &&value, lifetime_holder_2 &&start_with_lifetime) { using callbacks_type = std::integral_constant && is_callable_v) ? CallbacksType::NextError : (is_callable_v && is_callable_v) ? CallbacksType::NextDone : // (is_callable_v && is_callable_v) ? CallbacksType::ErrorDone : CallbacksType::Invalid>; start_with_lifetime.alive_while.add( std::move(value).start( on_next_dispatch(std::move(start_with_lifetime.callback1), callbacks_type{}), on_error_dispatch(std::move(start_with_lifetime.callback1), std::move(start_with_lifetime.callback2), callbacks_type{}), on_done_dispatch(std::move(start_with_lifetime.callback2), callbacks_type{}))); } template < typename Value, typename Error, typename OnNext, typename Callback1, typename Callback2, typename = std::enable_if_t< is_callable_v>, typename = std::enable_if_t< is_callable_v>> inline void operator|( producer_with_next &&producer_with_next, lifetime_holder_2 &&start_with_lifetime) { start_with_lifetime.alive_while.add( std::move(producer_with_next.bound).start( std::move(producer_with_next.next), std::move(start_with_lifetime.callback1), std::move(start_with_lifetime.callback2))); } template < typename Value, typename Error, typename OnError, typename Callback1, typename Callback2, typename = std::enable_if_t< is_callable_v>, typename = std::enable_if_t< is_callable_v>> inline void operator|( producer_with_error &&producer_with_error, lifetime_holder_2 &&start_with_lifetime) { start_with_lifetime.alive_while.add( std::move(producer_with_error.bound).start( std::move(start_with_lifetime.callback1), std::move(producer_with_error.error), std::move(start_with_lifetime.callback2))); } template < typename Value, typename Error, typename OnDone, typename Callback1, typename Callback2, typename = std::enable_if_t< is_callable_v>, typename = std::enable_if_t< is_callable_v>> inline void operator|( producer_with_done &&producer_with_done, lifetime_holder_2 &&start_with_lifetime) { start_with_lifetime.alive_while.add( std::move(producer_with_done.bound).start( std::move(start_with_lifetime.callback1), std::move(start_with_lifetime.callback2), std::move(producer_with_done.done))); } template < typename Value, typename Error, typename Callback1, typename Callback2, typename Callback3, typename = std::enable_if_t< is_callable_v>, typename = std::enable_if_t< is_callable_v>, typename = std::enable_if_t< is_callable_v>> inline void operator|( producer &&value, lifetime_holder_3 &&start_with_lifetime) { start_with_lifetime.alive_while.add( std::move(value).start( std::move(start_with_lifetime.callback1), std::move(start_with_lifetime.callback2), std::move(start_with_lifetime.callback3))); } } // namespace details } // namespace rpl