2017-10-01 09:39:07 +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-10-01 09:39:07 +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-10-01 09:39:07 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
namespace rpl {
|
|
|
|
namespace details {
|
|
|
|
|
|
|
|
class take_helper {
|
|
|
|
public:
|
|
|
|
take_helper(int count) : _count(count) {
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Error,
|
|
|
|
typename Generator>
|
|
|
|
auto operator()(producer<Value, Error, Generator> &&initial) {
|
|
|
|
return make_producer<Value, Error>([
|
|
|
|
initial = std::move(initial),
|
|
|
|
limit = _count
|
|
|
|
](const auto &consumer) mutable {
|
|
|
|
auto count = consumer.template make_state<int>(limit);
|
2017-10-13 16:33:38 +00:00
|
|
|
auto initial_consumer = make_consumer<Value, Error>(
|
2017-10-01 09:39:07 +00:00
|
|
|
[consumer, count](auto &&value) {
|
|
|
|
auto left = (*count)--;
|
|
|
|
if (left) {
|
|
|
|
consumer.put_next_forward(
|
|
|
|
std::forward<decltype(value)>(value));
|
|
|
|
--left;
|
|
|
|
}
|
|
|
|
if (!left) {
|
|
|
|
consumer.put_done();
|
|
|
|
}
|
|
|
|
}, [consumer](auto &&error) {
|
|
|
|
consumer.put_error_forward(
|
|
|
|
std::forward<decltype(error)>(error));
|
|
|
|
}, [consumer] {
|
|
|
|
consumer.put_done();
|
|
|
|
});
|
2017-10-13 16:33:38 +00:00
|
|
|
consumer.add_lifetime(initial_consumer.terminator());
|
|
|
|
return std::move(initial).start_existing(initial_consumer);
|
2017-10-01 09:39:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int _count = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace details
|
2018-05-21 21:31:46 +00:00
|
|
|
|
2017-10-01 09:39:07 +00:00
|
|
|
inline auto take(int count)
|
|
|
|
-> details::take_helper {
|
|
|
|
Expects(count >= 0);
|
2018-05-21 21:31:46 +00:00
|
|
|
|
2017-10-01 09:39:07 +00:00
|
|
|
return details::take_helper(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace rpl
|