/* 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 "rpl/consumer.h" #include "rpl/lifetime.h" namespace rpl { template class producer { public: using value_type = Value; using error_type = Error; template ()(std::declval>())), lifetime >::value>> producer(Generator &&generator); template < typename OnNext, typename OnError, typename OnDone, typename = decltype(std::declval()(std::declval())), typename = decltype(std::declval()(std::declval())), typename = decltype(std::declval()())> lifetime start( OnNext &&next, OnError &&error, OnDone &&done) const; private: base::lambda)> _generator; }; template template producer::producer(Generator &&generator) : _generator(std::forward(generator)) { } template template < typename OnNext, typename OnError, typename OnDone, typename, typename, typename> lifetime producer::start( OnNext &&next, OnError &&error, OnDone &&done) const { auto result = consumer( std::forward(next), std::forward(error), std::forward(done)); result.set_lifetime(_generator(result)); return [result] { result.terminate(); }; } template inline producer duplicate(const producer &producer) { return producer; } template < typename Value, typename Error, typename Method, typename = decltype(std::declval()(std::declval>()))> inline decltype(auto) operator|(producer &&producer, Method &&method) { return std::forward(method)(std::move(producer)); } template inline decltype(auto) bind_on_next(OnNext &&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::forward(handler) ](consumer consumer) { return existing.start([handler = std::decay_t(handler)]( value_type &&value) { handler(std::move(value)); }, [consumer](error_type &&error) { consumer.put_error(std::move(error)); }, [consumer] { consumer.put_done(); }); }); }; } template inline decltype(auto) bind_on_error(OnError &&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::forward(handler) ](consumer consumer) { return existing.start([consumer](value_type &&value) { consumer.put_next(std::move(value)); }, [handler = std::decay_t(handler)](error_type &&error) { handler(std::move(error)); }, [consumer] { consumer.put_done(); }); }); }; } template inline decltype(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::forward(handler) ](consumer consumer) { return existing.start([consumer](value_type &&value) { consumer.put_next(std::move(value)); }, [consumer](error_type &&value) { consumer.put_error(std::move(value)); }, [handler = std::decay_t(handler)] { 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 producer; OnNext next; }; template < typename Value, typename Error, typename OnError> struct producer_with_error { producer producer; OnError error; }; template < typename Value, typename Error, typename OnDone> struct producer_with_done { producer producer; OnDone done; }; template < typename Value, typename Error, typename OnNext, typename OnError> struct producer_with_next_error { producer producer; OnNext next; OnError error; }; template < typename Value, typename Error, typename OnNext, typename OnDone> struct producer_with_next_done { producer producer; OnNext next; OnDone done; }; template < typename Value, typename Error, typename OnError, typename OnDone> struct producer_with_error_done { producer producer; OnError error; OnDone done; }; template < typename Value, typename Error, typename OnNext, typename OnError, typename OnDone> struct producer_with_next_error_done { producer producer; OnNext next; OnError error; OnDone done; }; struct lifetime_holder { lifetime &alive_while; }; } // 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 }; } namespace details { template < typename Value, typename Error, typename OnNext, typename = decltype(std::declval()(std::declval()))> inline producer_with_next operator|( producer &&producer, next_holder &&handler) { return { std::move(producer), std::move(handler.next) }; } template < typename Value, typename Error, typename OnError, typename = decltype(std::declval()(std::declval()))> inline producer_with_error operator|( producer &&producer, error_holder &&handler) { return { std::move(producer), std::move(handler.error) }; } template < typename Value, typename Error, typename OnDone, typename = decltype(std::declval()())> inline producer_with_done operator|( producer &&producer, done_holder &&handler) { return { std::move(producer), std::move(handler.done) }; } template < typename Value, typename Error, typename OnNext, typename OnError, typename = decltype(std::declval()(std::declval())), typename = decltype(std::declval()(std::declval()))> inline producer_with_next_error operator|( producer_with_next &&producer_with_next, error_holder &&handler) { return { std::move(producer_with_next.producer), std::move(producer_with_next.next), std::move(handler.error) }; } template < typename Value, typename Error, typename OnNext, typename OnError, typename = decltype(std::declval()(std::declval())), typename = decltype(std::declval()(std::declval()))> inline producer_with_next_error operator|( producer_with_error &&producer_with_error, next_holder &&handler) { return { std::move(producer_with_error.producer), std::move(handler.next), std::move(producer_with_error.error) }; } template < typename Value, typename Error, typename OnNext, typename OnDone, typename = decltype(std::declval()(std::declval())), typename = decltype(std::declval()())> inline producer_with_next_done operator|( producer_with_next &&producer_with_next, done_holder &&handler) { return { std::move(producer_with_next.producer), std::move(producer_with_next.next), std::move(handler.done) }; } template < typename Value, typename Error, typename OnNext, typename OnDone, typename = decltype(std::declval()(std::declval())), typename = decltype(std::declval()())> inline producer_with_next_done operator|( producer_with_done &&producer_with_done, next_holder &&handler) { return { std::move(producer_with_done.producer), std::move(handler.next), std::move(producer_with_done.done) }; } template < typename Value, typename Error, typename OnError, typename OnDone, typename = decltype(std::declval()(std::declval())), typename = decltype(std::declval()())> inline producer_with_error_done operator|( producer_with_error &&producer_with_error, done_holder &&handler) { return { std::move(producer_with_error.producer), std::move(producer_with_error.error), std::move(handler.done) }; } template < typename Value, typename Error, typename OnError, typename OnDone, typename = decltype(std::declval()(std::declval())), typename = decltype(std::declval()())> inline producer_with_error_done operator|( producer_with_done &&producer_with_done, error_holder &&handler) { return { std::move(producer_with_done.producer), std::move(handler.error), std::move(producer_with_done.done) }; } template < typename Value, typename Error, typename OnNext, typename OnError, typename OnDone, typename = decltype(std::declval()(std::declval())), typename = decltype(std::declval()(std::declval())), typename = decltype(std::declval()())> 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.producer), 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 = decltype(std::declval()(std::declval())), typename = decltype(std::declval()(std::declval())), typename = decltype(std::declval()())> 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.producer), 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 = decltype(std::declval()(std::declval())), typename = decltype(std::declval()(std::declval())), typename = decltype(std::declval()())> 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.producer), 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(producer_with_next_error_done.producer.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 &&producer, lifetime_holder &&start_with_lifetime) { return std::move(producer) | on_next([](Value&&) {}) | on_error([](Error&&) {}) | on_done([] {}) | std::move(start_with_lifetime); } template inline void operator|( producer_with_next &&producer_with_next, lifetime_holder &&start_with_lifetime) { return std::move(producer_with_next) | on_error([](Error&&) {}) | on_done([] {}) | std::move(start_with_lifetime); } template inline void operator|( producer_with_error &&producer_with_error, lifetime_holder &&start_with_lifetime) { return std::move(producer_with_error) | on_next([](Value&&) {}) | on_done([] {}) | std::move(start_with_lifetime); } template inline void operator|( producer_with_done &&producer_with_done, lifetime_holder &&start_with_lifetime) { return std::move(producer_with_done) | on_next([](Value&&) {}) | on_error([](Error&&) {}) | std::move(start_with_lifetime); } template 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 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([](Error&&) {}) | std::move(start_with_lifetime); } template 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([](Value&&) {}) | std::move(start_with_lifetime); } } // namespace details } // namespace rpl