2017-09-18 16:39:45 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-09-18 16:39:45 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-09-18 16:39:45 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-09-19 14:51:01 +00:00
|
|
|
#include "base/optional.h"
|
2018-06-13 17:10:12 +00:00
|
|
|
#include "base/variant.h"
|
2017-09-26 20:14:56 +00:00
|
|
|
#include <rpl/map.h>
|
2017-09-18 16:39:45 +00:00
|
|
|
#include <rpl/producer.h>
|
|
|
|
#include <rpl/details/type_list.h>
|
|
|
|
#include <rpl/details/callable.h>
|
|
|
|
#include <rpl/mappers.h>
|
|
|
|
#include <rpl/complete.h>
|
|
|
|
|
|
|
|
namespace rpl {
|
|
|
|
namespace details {
|
|
|
|
|
|
|
|
template <typename ...Values>
|
|
|
|
struct combine_state {
|
2018-09-21 16:28:46 +00:00
|
|
|
combine_state() : accumulated(std::tuple<std::optional<Values>...>()) {
|
2017-09-18 16:39:45 +00:00
|
|
|
}
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<std::tuple<std::optional<Values>...>> accumulated;
|
|
|
|
std::optional<std::tuple<Values...>> latest;
|
2017-09-18 16:39:45 +00:00
|
|
|
int invalid = sizeof...(Values);
|
|
|
|
int working = sizeof...(Values);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ...Values, std::size_t ...I>
|
|
|
|
inline std::tuple<Values...> combine_make_first(
|
2018-09-21 16:28:46 +00:00
|
|
|
std::tuple<std::optional<Values>...> &&accumulated,
|
2017-09-18 16:39:45 +00:00
|
|
|
std::index_sequence<I...>) {
|
|
|
|
return std::make_tuple(std::move(*std::get<I>(accumulated))...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t Index, typename consumer_type, typename ...Values>
|
|
|
|
class combine_subscribe_one {
|
|
|
|
public:
|
|
|
|
combine_subscribe_one(
|
|
|
|
const consumer_type &consumer,
|
|
|
|
combine_state<Values...> *state)
|
|
|
|
: _consumer(consumer)
|
|
|
|
, _state(state) {
|
|
|
|
}
|
|
|
|
|
2017-09-27 11:16:05 +00:00
|
|
|
template <typename Value, typename Error, typename Generator>
|
|
|
|
void subscribe(producer<Value, Error, Generator> &&producer) {
|
2017-09-18 16:39:45 +00:00
|
|
|
_consumer.add_lifetime(std::move(producer).start(
|
|
|
|
[consumer = _consumer, state = _state](Value &&value) {
|
|
|
|
if (!state->accumulated) {
|
|
|
|
std::get<Index>(*state->latest) = std::move(value);
|
|
|
|
consumer.put_next_copy(*state->latest);
|
|
|
|
} else {
|
|
|
|
auto &accumulated = std::get<Index>(
|
|
|
|
*state->accumulated);
|
|
|
|
if (accumulated) {
|
|
|
|
accumulated = std::move(value);
|
|
|
|
} else {
|
|
|
|
accumulated = std::move(value);
|
|
|
|
if (!--state->invalid) {
|
|
|
|
constexpr auto kArity = sizeof...(Values);
|
|
|
|
state->latest = combine_make_first(
|
|
|
|
std::move(*state->accumulated),
|
|
|
|
std::make_index_sequence<kArity>());
|
2018-09-21 16:28:46 +00:00
|
|
|
state->accumulated = std::nullopt;
|
2017-09-18 16:39:45 +00:00
|
|
|
consumer.put_next_copy(*state->latest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-20 16:18:26 +00:00
|
|
|
}, [consumer = _consumer](auto &&error) {
|
|
|
|
consumer.put_error_forward(
|
|
|
|
std::forward<decltype(error)>(error));
|
2017-09-18 16:39:45 +00:00
|
|
|
}, [consumer = _consumer, state = _state] {
|
|
|
|
if (!--state->working) {
|
|
|
|
consumer.put_done();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const consumer_type &_consumer;
|
|
|
|
combine_state<Values...> *_state = nullptr;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename consumer_type,
|
|
|
|
typename ...Values,
|
|
|
|
typename ...Errors,
|
2017-09-27 11:16:05 +00:00
|
|
|
typename ...Generators,
|
2017-09-18 16:39:45 +00:00
|
|
|
std::size_t ...I>
|
|
|
|
inline void combine_subscribe(
|
|
|
|
const consumer_type &consumer,
|
|
|
|
combine_state<Values...> *state,
|
|
|
|
std::index_sequence<I...>,
|
2017-09-27 15:15:26 +00:00
|
|
|
std::tuple<producer<Values, Errors, Generators>...> &&saved) {
|
2017-09-18 16:39:45 +00:00
|
|
|
auto consume = { (
|
2017-10-20 16:18:26 +00:00
|
|
|
combine_subscribe_one<I, consumer_type, Values...>(
|
2017-09-18 16:39:45 +00:00
|
|
|
consumer,
|
|
|
|
state
|
2017-09-27 15:15:26 +00:00
|
|
|
).subscribe(std::get<I>(std::move(saved))), 0)... };
|
2017-09-18 16:39:45 +00:00
|
|
|
(void)consume;
|
|
|
|
}
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <typename ...Producers>
|
|
|
|
class combine_implementation_helper;
|
|
|
|
|
|
|
|
template <typename ...Producers>
|
|
|
|
combine_implementation_helper<std::decay_t<Producers>...>
|
|
|
|
make_combine_implementation_helper(Producers &&...producers) {
|
|
|
|
return combine_implementation_helper<std::decay_t<Producers>...>(
|
|
|
|
std::forward<Producers>(producers)...);
|
|
|
|
}
|
|
|
|
|
2017-09-27 11:16:05 +00:00
|
|
|
template <
|
|
|
|
typename ...Values,
|
|
|
|
typename ...Errors,
|
|
|
|
typename ...Generators>
|
2017-09-27 15:15:26 +00:00
|
|
|
class combine_implementation_helper<producer<Values, Errors, Generators>...> {
|
|
|
|
public:
|
2017-09-27 11:16:05 +00:00
|
|
|
using CombinedValue = std::tuple<Values...>;
|
2018-06-13 17:10:12 +00:00
|
|
|
using CombinedError = base::normalized_variant_t<Errors...>;
|
2017-09-27 15:15:26 +00:00
|
|
|
|
|
|
|
combine_implementation_helper(
|
|
|
|
producer<Values, Errors, Generators> &&...producers)
|
|
|
|
: _saved(std::make_tuple(std::move(producers)...)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Handlers>
|
|
|
|
lifetime operator()(const consumer<CombinedValue, CombinedError, Handlers> &consumer) {
|
2017-09-18 16:39:45 +00:00
|
|
|
auto state = consumer.template make_state<
|
2017-10-20 16:18:26 +00:00
|
|
|
combine_state<Values...>>();
|
2017-09-18 16:39:45 +00:00
|
|
|
constexpr auto kArity = sizeof...(Values);
|
2017-10-20 16:18:26 +00:00
|
|
|
combine_subscribe(
|
2017-09-18 16:39:45 +00:00
|
|
|
consumer,
|
|
|
|
state,
|
|
|
|
std::make_index_sequence<kArity>(),
|
2017-09-27 15:15:26 +00:00
|
|
|
std::move(_saved));
|
2017-09-18 16:39:45 +00:00
|
|
|
|
|
|
|
return lifetime();
|
2017-09-27 15:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::tuple<producer<Values, Errors, Generators>...> _saved;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename ...Values,
|
|
|
|
typename ...Errors,
|
|
|
|
typename ...Generators>
|
|
|
|
inline auto combine_implementation(
|
|
|
|
producer<Values, Errors, Generators> &&...producers) {
|
|
|
|
using CombinedValue = std::tuple<Values...>;
|
2018-06-13 17:10:12 +00:00
|
|
|
using CombinedError = base::normalized_variant_t<Errors...>;
|
2017-09-27 15:15:26 +00:00
|
|
|
|
|
|
|
return make_producer<CombinedValue, CombinedError>(
|
|
|
|
make_combine_implementation_helper(std::move(producers)...));
|
2017-09-18 16:39:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ...Args>
|
|
|
|
struct combine_just_producers : std::false_type {
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ...Args>
|
|
|
|
constexpr bool combine_just_producers_v
|
|
|
|
= combine_just_producers<Args...>::value;
|
|
|
|
|
2017-09-27 11:16:05 +00:00
|
|
|
template <
|
|
|
|
typename ...Values,
|
|
|
|
typename ...Errors,
|
|
|
|
typename ...Generators>
|
|
|
|
struct combine_just_producers<
|
|
|
|
producer<Values, Errors, Generators>...>
|
2017-09-18 16:39:45 +00:00
|
|
|
: std::true_type {
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ArgsList>
|
|
|
|
struct combine_just_producers_list
|
|
|
|
: type_list::extract_to_t<ArgsList, combine_just_producers> {
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ...Args>
|
|
|
|
struct combine_result_type;
|
|
|
|
|
|
|
|
template <typename ...Args>
|
|
|
|
using combine_result_type_t
|
|
|
|
= typename combine_result_type<Args...>::type;
|
|
|
|
|
2017-09-27 11:16:05 +00:00
|
|
|
template <
|
|
|
|
typename ...Values,
|
|
|
|
typename ...Errors,
|
|
|
|
typename ...Generators>
|
|
|
|
struct combine_result_type<producer<Values, Errors, Generators>...> {
|
2017-09-18 16:39:45 +00:00
|
|
|
using type = std::tuple<Values...>;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ArgsList>
|
|
|
|
struct combine_result_type_list
|
|
|
|
: type_list::extract_to_t<ArgsList, combine_result_type> {
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ArgsList>
|
|
|
|
using combine_result_type_list_t
|
|
|
|
= typename combine_result_type_list<ArgsList>::type;
|
|
|
|
|
|
|
|
template <typename ArgsList>
|
|
|
|
using combine_producers_no_mapper_t
|
|
|
|
= type_list::chop_last_t<ArgsList>;
|
|
|
|
|
|
|
|
template <typename ArgsList>
|
|
|
|
constexpr bool combine_is_good_mapper(std::true_type) {
|
|
|
|
return is_callable_v<
|
|
|
|
type_list::last_t<ArgsList>,
|
|
|
|
combine_result_type_list_t<
|
|
|
|
combine_producers_no_mapper_t<ArgsList>
|
|
|
|
>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ArgsList>
|
|
|
|
constexpr bool combine_is_good_mapper(std::false_type) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ArgsList>
|
|
|
|
struct combine_producers_with_mapper_list : std::bool_constant<
|
|
|
|
combine_is_good_mapper<ArgsList>(
|
|
|
|
combine_just_producers_list<
|
|
|
|
combine_producers_no_mapper_t<ArgsList>
|
|
|
|
>())> {
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ...Args>
|
|
|
|
struct combine_producers_with_mapper
|
|
|
|
: combine_producers_with_mapper_list<type_list::list<Args...>> {
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ...Args>
|
|
|
|
constexpr bool combine_producers_with_mapper_v
|
|
|
|
= combine_producers_with_mapper<Args...>::value;
|
|
|
|
|
|
|
|
template <typename ...Producers, std::size_t ...I>
|
|
|
|
inline decltype(auto) combine_call(
|
|
|
|
std::index_sequence<I...>,
|
|
|
|
Producers &&...producers) {
|
|
|
|
return combine_implementation(
|
|
|
|
argument_mapper<I>::call(std::move(producers)...)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace details
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename ...Args,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
details::combine_just_producers_v<Args...>
|
|
|
|
|| details::combine_producers_with_mapper_v<Args...>>>
|
|
|
|
inline decltype(auto) combine(Args &&...args) {
|
2017-09-29 10:55:25 +00:00
|
|
|
if constexpr (details::combine_just_producers_v<Args...>) {
|
|
|
|
return details::combine_implementation(std::move(args)...);
|
|
|
|
} else if constexpr (details::combine_producers_with_mapper_v<Args...>) {
|
|
|
|
constexpr auto kProducersCount = sizeof...(Args) - 1;
|
|
|
|
return details::combine_call(
|
|
|
|
std::make_index_sequence<kProducersCount>(),
|
|
|
|
std::forward<Args>(args)...)
|
|
|
|
| map(details::argument_mapper<kProducersCount>::call(
|
|
|
|
std::forward<Args>(args)...));
|
|
|
|
} else {
|
|
|
|
static_assert(false_(args...), "Bad combine() call.");
|
|
|
|
}
|
2017-09-18 16:39:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace details {
|
|
|
|
|
|
|
|
template <typename Value>
|
|
|
|
struct combine_vector_state {
|
2018-09-21 16:28:46 +00:00
|
|
|
std::vector<std::optional<Value>> accumulated;
|
2017-09-18 16:39:45 +00:00
|
|
|
std::vector<Value> latest;
|
|
|
|
int invalid = 0;
|
|
|
|
int working = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace details
|
|
|
|
|
2017-09-27 11:16:05 +00:00
|
|
|
template <typename Value, typename Error, typename Generator>
|
|
|
|
inline auto combine(
|
|
|
|
std::vector<producer<Value, Error, Generator>> &&producers) {
|
2017-09-18 16:39:45 +00:00
|
|
|
using state_type = details::combine_vector_state<Value>;
|
2017-09-27 11:16:05 +00:00
|
|
|
return make_producer<std::vector<Value>, Error>([
|
|
|
|
producers = std::move(producers)
|
2017-09-27 15:15:26 +00:00
|
|
|
](const auto &consumer) mutable {
|
2017-09-18 16:39:45 +00:00
|
|
|
auto count = producers.size();
|
|
|
|
auto state = consumer.template make_state<state_type>();
|
|
|
|
state->accumulated.resize(count);
|
|
|
|
state->invalid = count;
|
|
|
|
state->working = count;
|
|
|
|
for (auto index = 0; index != count; ++index) {
|
|
|
|
auto &producer = producers[index];
|
|
|
|
consumer.add_lifetime(std::move(producer).start(
|
|
|
|
[consumer, state, index](Value &&value) {
|
|
|
|
if (state->accumulated.empty()) {
|
|
|
|
state->latest[index] = std::move(value);
|
|
|
|
consumer.put_next_copy(state->latest);
|
|
|
|
} else if (state->accumulated[index]) {
|
|
|
|
state->accumulated[index] = std::move(value);
|
|
|
|
} else {
|
|
|
|
state->accumulated[index] = std::move(value);
|
|
|
|
if (!--state->invalid) {
|
|
|
|
state->latest.reserve(
|
|
|
|
state->accumulated.size());
|
|
|
|
for (auto &&value : state->accumulated) {
|
|
|
|
state->latest.push_back(
|
|
|
|
std::move(*value));
|
|
|
|
}
|
2017-11-20 12:23:20 +00:00
|
|
|
details::take(state->accumulated);
|
2017-09-18 16:39:45 +00:00
|
|
|
consumer.put_next_copy(state->latest);
|
|
|
|
}
|
|
|
|
}
|
2017-10-20 16:18:26 +00:00
|
|
|
}, [consumer](auto &&error) {
|
|
|
|
consumer.put_error_forward(
|
|
|
|
std::forward<decltype(error)>(error));
|
2017-09-18 16:39:45 +00:00
|
|
|
}, [consumer, state] {
|
|
|
|
if (!--state->working) {
|
|
|
|
consumer.put_done();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2017-09-27 11:16:05 +00:00
|
|
|
if (!count) {
|
|
|
|
consumer.put_done();
|
|
|
|
}
|
2017-09-18 16:39:45 +00:00
|
|
|
return lifetime();
|
2017-09-27 11:16:05 +00:00
|
|
|
});
|
2017-09-18 16:39:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 11:16:05 +00:00
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Error,
|
|
|
|
typename Generator,
|
|
|
|
typename Mapper>
|
2017-09-18 16:39:45 +00:00
|
|
|
inline auto combine(
|
2017-09-27 11:16:05 +00:00
|
|
|
std::vector<producer<Value, Error, Generator>> &&producers,
|
2017-09-18 16:39:45 +00:00
|
|
|
Mapper &&mapper) {
|
|
|
|
return combine(std::move(producers))
|
|
|
|
| map(std::forward<Mapper>(mapper));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace rpl
|