/* 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)); } namespace details { struct lifetime_with_none { lifetime &alive_while; }; template struct lifetime_with_next { lifetime &alive_while; OnNext next; }; template struct lifetime_with_error { lifetime &alive_while; OnError error; }; template struct lifetime_with_done { lifetime &alive_while; OnDone done; }; template struct lifetime_with_next_error { lifetime &alive_while; OnNext next; OnError error; }; template struct lifetime_with_error_done { lifetime &alive_while; OnError error; OnDone done; }; template struct lifetime_with_next_done { lifetime &alive_while; OnNext next; OnDone done; }; template struct lifetime_with_next_error_done { lifetime &alive_while; OnNext next; OnError error; OnDone done; }; } // namespace details inline auto start(lifetime &alive_while) -> details::lifetime_with_none { return { alive_while }; } template inline auto start_with_next(OnNext &&next, lifetime &alive_while) -> details::lifetime_with_next> { return { alive_while, std::forward(next) }; } template inline auto start_with_error(OnError &&error, lifetime &alive_while) -> details::lifetime_with_error> { return { alive_while, std::forward(error) }; } template inline auto start_with_done(OnDone &&done, lifetime &alive_while) -> details::lifetime_with_done> { return { alive_while, std::forward(done) }; } template inline auto start_with_next_error( OnNext &&next, OnError &&error, lifetime &alive_while) -> details::lifetime_with_next_error< std::decay_t, std::decay_t> { return { alive_while, std::forward(next), std::forward(error) }; } template inline auto start_with_error_done( OnError &&error, OnDone &&done, lifetime &alive_while) -> details::lifetime_with_error_done< std::decay_t, std::decay_t> { return { alive_while, std::forward(error), std::forward(done) }; } template inline auto start_with_next_done( OnNext &&next, OnDone &&done, lifetime &alive_while) -> details::lifetime_with_next_done< std::decay_t, std::decay_t> { return { alive_while, std::forward(next), std::forward(done) }; } template inline auto start_with_next_error_done( OnNext &&next, OnError &&error, OnDone &&done, lifetime &alive_while) -> details::lifetime_with_next_error_done< std::decay_t, std::decay_t, std::decay_t> { return { alive_while, std::forward(next), std::forward(error), std::forward(done) }; } namespace details { template inline void operator|( producer &&value, lifetime_with_none &&lifetime) { lifetime.alive_while.add( std::move(value).start( [](const Value&) {}, [](const Error&) {}, [] {})); } template < typename Value, typename Error, typename OnNext, typename = std::enable_if_t>> inline void operator|( producer &&value, lifetime_with_next &&lifetime) { lifetime.alive_while.add( std::move(value).start( std::move(lifetime.next), [](const Error&) {}, [] {})); } template < typename Value, typename Error, typename OnError, typename = std::enable_if_t>> inline void operator|( producer &&value, lifetime_with_error &&lifetime) { lifetime.alive_while.add( std::move(value).start( [](const Value&) {}, std::move(lifetime.error), [] {})); } template < typename Value, typename Error, typename OnDone, typename = std::enable_if_t>> inline void operator|( producer &&value, lifetime_with_done &&lifetime) { lifetime.alive_while.add( std::move(value).start( [](const Value&) {}, [](const Error&) {}, std::move(lifetime.done))); } template < typename Value, typename Error, typename OnNext, typename OnError, typename = std::enable_if_t< is_callable_v && is_callable_v>> inline void operator|( producer &&value, lifetime_with_next_error &&lifetime) { lifetime.alive_while.add( std::move(value).start( std::move(lifetime.next), std::move(lifetime.error), [] {})); } template < typename Value, typename Error, typename OnError, typename OnDone, typename = std::enable_if_t< is_callable_v && is_callable_v>> inline void operator|( producer &&value, lifetime_with_error_done &&lifetime) { lifetime.alive_while.add( std::move(value).start( [](const Value&) {}, std::move(lifetime.error), std::move(lifetime.done))); } template < typename Value, typename Error, typename OnNext, typename OnDone, typename = std::enable_if_t< is_callable_v && is_callable_v>> inline void operator|( producer &&value, lifetime_with_next_done &&lifetime) { lifetime.alive_while.add( std::move(value).start( std::move(lifetime.next), [](const Error&) {}, std::move(lifetime.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 void operator|( producer &&value, lifetime_with_next_error_done< OnNext, OnError, OnDone> &&lifetime) { lifetime.alive_while.add( std::move(value).start( std::move(lifetime.next), std::move(lifetime.error), std::move(lifetime.done))); } } // namespace details } // namespace rpl