2017-09-12 13:58:14 +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-12 13:58:14 +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-12 13:58:14 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <rpl/producer.h>
|
2017-09-19 14:51:01 +00:00
|
|
|
#include <rpl/combine.h>
|
2017-09-27 11:16:05 +00:00
|
|
|
#include <rpl/mappers.h>
|
2017-09-21 19:21:33 +00:00
|
|
|
#include "base/optional.h"
|
2017-09-12 13:58:14 +00:00
|
|
|
|
|
|
|
namespace rpl {
|
|
|
|
namespace details {
|
|
|
|
|
|
|
|
template <typename Predicate>
|
|
|
|
class filter_helper {
|
|
|
|
public:
|
|
|
|
template <typename OtherPredicate>
|
|
|
|
filter_helper(OtherPredicate &&predicate)
|
|
|
|
: _predicate(std::forward<OtherPredicate>(predicate)) {
|
|
|
|
}
|
|
|
|
|
2017-09-19 14:51:01 +00:00
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Error,
|
2017-09-27 11:16:05 +00:00
|
|
|
typename Generator,
|
2017-09-19 14:51:01 +00:00
|
|
|
typename = std::enable_if_t<
|
|
|
|
details::is_callable_v<Predicate, Value>>>
|
2017-09-27 11:16:05 +00:00
|
|
|
auto operator()(producer<Value, Error, Generator> &&initial) {
|
|
|
|
return make_producer<Value, Error>([
|
|
|
|
initial = std::move(initial),
|
|
|
|
predicate = std::move(_predicate)
|
2017-09-27 15:15:26 +00:00
|
|
|
](const auto &consumer) mutable {
|
2017-09-12 13:58:14 +00:00
|
|
|
return std::move(initial).start(
|
|
|
|
[
|
|
|
|
consumer,
|
|
|
|
predicate = std::move(predicate)
|
2017-09-14 19:27:41 +00:00
|
|
|
](auto &&value) {
|
2017-09-12 13:58:14 +00:00
|
|
|
const auto &immutable = value;
|
2017-09-21 19:21:33 +00:00
|
|
|
if (details::callable_invoke(
|
|
|
|
predicate,
|
|
|
|
immutable)
|
|
|
|
) {
|
|
|
|
consumer.put_next_forward(
|
|
|
|
std::forward<decltype(value)>(value));
|
2017-09-12 13:58:14 +00:00
|
|
|
}
|
2017-09-14 19:27:41 +00:00
|
|
|
}, [consumer](auto &&error) {
|
2017-09-21 19:21:33 +00:00
|
|
|
consumer.put_error_forward(
|
|
|
|
std::forward<decltype(error)>(error));
|
2017-09-12 13:58:14 +00:00
|
|
|
}, [consumer] {
|
|
|
|
consumer.put_done();
|
|
|
|
});
|
2017-09-27 11:16:05 +00:00
|
|
|
});
|
2017-09-12 13:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Predicate _predicate;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace details
|
|
|
|
|
|
|
|
template <typename Predicate>
|
2017-09-18 16:39:45 +00:00
|
|
|
inline auto filter(Predicate &&predicate)
|
2017-09-12 13:58:14 +00:00
|
|
|
-> details::filter_helper<std::decay_t<Predicate>> {
|
|
|
|
return details::filter_helper<std::decay_t<Predicate>>(
|
|
|
|
std::forward<Predicate>(predicate));
|
|
|
|
}
|
|
|
|
|
2017-09-19 14:51:01 +00:00
|
|
|
namespace details {
|
|
|
|
|
2017-09-27 11:16:05 +00:00
|
|
|
template <typename FilterError, typename FilterGenerator>
|
|
|
|
class filter_helper<producer<bool, FilterError, FilterGenerator>> {
|
2017-09-19 14:51:01 +00:00
|
|
|
public:
|
2017-09-27 11:16:05 +00:00
|
|
|
filter_helper(
|
|
|
|
producer<bool, FilterError, FilterGenerator> &&filterer)
|
|
|
|
: _filterer(std::move(filterer)) {
|
2017-09-19 14:51:01 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 11:16:05 +00:00
|
|
|
template <typename Value, typename Error, typename Generator>
|
|
|
|
auto operator()(producer<Value, Error, Generator> &&initial) {
|
|
|
|
using namespace mappers;
|
2017-09-19 14:51:01 +00:00
|
|
|
return combine(std::move(initial), std::move(_filterer))
|
2017-11-20 12:32:55 +00:00
|
|
|
| filter(_2)
|
|
|
|
| map(_1_of_two);
|
2017-09-19 14:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-09-27 11:16:05 +00:00
|
|
|
producer<bool, FilterError, FilterGenerator> _filterer;
|
2017-09-19 14:51:01 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-09-21 19:21:33 +00:00
|
|
|
template <typename Value>
|
|
|
|
inline const Value &deref_optional_helper(
|
|
|
|
const base::optional<Value> &value) {
|
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Value>
|
|
|
|
inline Value &&deref_optional_helper(
|
|
|
|
base::optional<Value> &&value) {
|
|
|
|
return std::move(*value);
|
|
|
|
}
|
|
|
|
|
|
|
|
class filter_optional_helper {
|
|
|
|
public:
|
2017-09-27 11:16:05 +00:00
|
|
|
template <typename Value, typename Error, typename Generator>
|
|
|
|
auto operator()(producer<
|
|
|
|
base::optional<Value>,
|
|
|
|
Error,
|
|
|
|
Generator> &&initial) const {
|
|
|
|
return make_producer<Value, Error>([
|
|
|
|
initial = std::move(initial)
|
2017-09-27 15:15:26 +00:00
|
|
|
](const auto &consumer) mutable {
|
2017-09-21 19:21:33 +00:00
|
|
|
return std::move(initial).start(
|
|
|
|
[consumer](auto &&value) {
|
|
|
|
if (value) {
|
|
|
|
consumer.put_next_forward(
|
|
|
|
deref_optional_helper(
|
|
|
|
std::forward<decltype(value)>(
|
|
|
|
value)));
|
|
|
|
}
|
|
|
|
}, [consumer](auto &&error) {
|
|
|
|
consumer.put_error_forward(
|
|
|
|
std::forward<decltype(error)>(error));
|
|
|
|
}, [consumer] {
|
|
|
|
consumer.put_done();
|
|
|
|
});
|
2017-09-27 11:16:05 +00:00
|
|
|
});
|
2017-09-21 19:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-09-19 14:51:01 +00:00
|
|
|
} // namespace details
|
2017-09-21 19:21:33 +00:00
|
|
|
|
|
|
|
inline auto filter_optional()
|
|
|
|
-> details::filter_optional_helper {
|
|
|
|
return details::filter_optional_helper();
|
|
|
|
}
|
|
|
|
|
2017-09-12 13:58:14 +00:00
|
|
|
} // namespace rpl
|