2017-09-03 12:44:21 +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-03 12:44:21 +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-03 12:44:21 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-09-12 13:58:14 +00:00
|
|
|
#include <gsl/gsl_assert>
|
|
|
|
#include <rpl/lifetime.h>
|
2017-09-18 16:39:45 +00:00
|
|
|
#include <rpl/details/callable.h>
|
2017-09-03 12:44:21 +00:00
|
|
|
|
2017-11-21 09:20:56 +00:00
|
|
|
// GCC 7.2 can't handle not type-erased consumers.
|
|
|
|
// It eats up 4GB RAM + 16GB swap on the unittest and dies.
|
|
|
|
// Clang and Visual C++ both handle it without such problems.
|
|
|
|
#if defined _DEBUG || defined COMPILER_GCC
|
|
|
|
#define RPL_CONSUMER_TYPE_ERASED_ALWAYS
|
|
|
|
#endif // _DEBUG || COMPILER_GCC
|
|
|
|
|
2017-09-03 12:44:21 +00:00
|
|
|
namespace rpl {
|
2017-09-27 15:15:26 +00:00
|
|
|
namespace details {
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Error,
|
|
|
|
typename OnNext,
|
|
|
|
typename OnError,
|
|
|
|
typename OnDone>
|
|
|
|
class consumer_handlers;
|
|
|
|
|
|
|
|
template <typename Value, typename Error>
|
|
|
|
class type_erased_handlers {
|
|
|
|
public:
|
|
|
|
virtual bool put_next(Value &&value) = 0;
|
|
|
|
virtual bool put_next_copy(const Value &value) = 0;
|
|
|
|
virtual void put_error(Error &&error) = 0;
|
|
|
|
virtual void put_error_copy(const Error &error) = 0;
|
|
|
|
virtual void put_done() = 0;
|
|
|
|
|
2017-10-02 09:14:07 +00:00
|
|
|
bool add_lifetime(lifetime &&lifetime);
|
2017-09-27 15:15:26 +00:00
|
|
|
|
|
|
|
template <typename Type, typename... Args>
|
|
|
|
Type *make_state(Args&& ...args);
|
|
|
|
|
|
|
|
void terminate();
|
|
|
|
|
2017-10-27 08:51:11 +00:00
|
|
|
virtual ~type_erased_handlers() = default;
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
protected:
|
|
|
|
lifetime _lifetime;
|
|
|
|
bool _terminated = false;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Handlers>
|
|
|
|
struct is_type_erased_handlers
|
|
|
|
: std::false_type {
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Value, typename Error>
|
|
|
|
struct is_type_erased_handlers<type_erased_handlers<Value, Error>>
|
|
|
|
: std::true_type {
|
|
|
|
};
|
|
|
|
|
2017-09-29 10:55:25 +00:00
|
|
|
template <typename Handlers>
|
|
|
|
constexpr bool is_type_erased_handlers_v
|
|
|
|
= is_type_erased_handlers<Handlers>::value;
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <typename Value, typename Error, typename OnNext, typename OnError, typename OnDone>
|
|
|
|
class consumer_handlers final
|
|
|
|
: public type_erased_handlers<Value, Error> {
|
|
|
|
public:
|
|
|
|
template <
|
|
|
|
typename OnNextOther,
|
|
|
|
typename OnErrorOther,
|
|
|
|
typename OnDoneOther>
|
|
|
|
consumer_handlers(
|
|
|
|
OnNextOther &&next,
|
|
|
|
OnErrorOther &&error,
|
|
|
|
OnDoneOther &&done)
|
|
|
|
: _next(std::forward<OnNextOther>(next))
|
|
|
|
, _error(std::forward<OnErrorOther>(error))
|
|
|
|
, _done(std::forward<OnDoneOther>(done)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool put_next(Value &&value) final override;
|
|
|
|
bool put_next_copy(const Value &value) final override;
|
|
|
|
void put_error(Error &&error) final override;
|
|
|
|
void put_error_copy(const Error &error) final override;
|
|
|
|
void put_done() final override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
OnNext _next;
|
|
|
|
OnError _error;
|
|
|
|
OnDone _done;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Value, typename Error>
|
2017-10-02 09:14:07 +00:00
|
|
|
inline bool type_erased_handlers<Value, Error>::add_lifetime(
|
2017-09-27 15:15:26 +00:00
|
|
|
lifetime &&lifetime) {
|
|
|
|
if (_terminated) {
|
|
|
|
lifetime.destroy();
|
2017-10-02 09:14:07 +00:00
|
|
|
return false;
|
2017-09-27 15:15:26 +00:00
|
|
|
}
|
2017-10-02 09:14:07 +00:00
|
|
|
_lifetime.add(std::move(lifetime));
|
|
|
|
return true;
|
2017-09-27 15:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Value, typename Error>
|
|
|
|
template <typename Type, typename... Args>
|
|
|
|
inline Type *type_erased_handlers<Value, Error>::make_state(
|
|
|
|
Args&& ...args) {
|
2017-10-02 09:14:07 +00:00
|
|
|
if (_terminated) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-09-27 15:15:26 +00:00
|
|
|
return _lifetime.make_state<Type>(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Value, typename Error>
|
|
|
|
inline void type_erased_handlers<Value, Error>::terminate() {
|
|
|
|
if (!_terminated) {
|
|
|
|
_terminated = true;
|
2017-11-21 10:27:37 +00:00
|
|
|
_lifetime.destroy();
|
2017-09-27 15:15:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Error,
|
|
|
|
typename OnNext,
|
|
|
|
typename OnError,
|
|
|
|
typename OnDone>
|
|
|
|
bool consumer_handlers<
|
|
|
|
Value,
|
|
|
|
Error,
|
|
|
|
OnNext,
|
|
|
|
OnError,
|
|
|
|
OnDone
|
|
|
|
>::put_next(Value &&value) {
|
|
|
|
if (this->_terminated) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto handler = this->_next;
|
|
|
|
details::callable_invoke(std::move(handler), std::move(value));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Error,
|
|
|
|
typename OnNext,
|
|
|
|
typename OnError,
|
|
|
|
typename OnDone>
|
|
|
|
bool consumer_handlers<
|
|
|
|
Value,
|
|
|
|
Error,
|
|
|
|
OnNext,
|
|
|
|
OnError,
|
|
|
|
OnDone
|
|
|
|
>::put_next_copy(const Value &value) {
|
|
|
|
if (this->_terminated) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto handler = this->_next;
|
|
|
|
details::const_ref_call_invoke(std::move(handler), value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Error,
|
|
|
|
typename OnNext,
|
|
|
|
typename OnError,
|
|
|
|
typename OnDone>
|
|
|
|
void consumer_handlers<
|
|
|
|
Value,
|
|
|
|
Error,
|
|
|
|
OnNext,
|
|
|
|
OnError,
|
|
|
|
OnDone
|
|
|
|
>::put_error(Error &&error) {
|
|
|
|
if (!this->_terminated) {
|
|
|
|
details::callable_invoke(
|
|
|
|
std::move(this->_error),
|
|
|
|
std::move(error));
|
|
|
|
this->terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Error,
|
|
|
|
typename OnNext,
|
|
|
|
typename OnError,
|
|
|
|
typename OnDone>
|
|
|
|
void consumer_handlers<
|
|
|
|
Value,
|
|
|
|
Error,
|
|
|
|
OnNext,
|
|
|
|
OnError,
|
|
|
|
OnDone
|
|
|
|
>::put_error_copy(const Error &error) {
|
|
|
|
if (!this->_terminated) {
|
|
|
|
details::const_ref_call_invoke(
|
|
|
|
std::move(this->_error),
|
|
|
|
error);
|
|
|
|
this->terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Error,
|
|
|
|
typename OnNext,
|
|
|
|
typename OnError,
|
|
|
|
typename OnDone>
|
|
|
|
void consumer_handlers<
|
|
|
|
Value,
|
|
|
|
Error,
|
|
|
|
OnNext,
|
|
|
|
OnError,
|
|
|
|
OnDone
|
|
|
|
>::put_done() {
|
|
|
|
if (!this->_terminated) {
|
|
|
|
std::move(this->_done)();
|
|
|
|
this->terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace details
|
2017-09-03 12:44:21 +00:00
|
|
|
|
2017-09-05 08:22:06 +00:00
|
|
|
struct no_value {
|
|
|
|
no_value() = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct no_error {
|
|
|
|
no_error() = delete;
|
2017-09-03 12:44:21 +00:00
|
|
|
};
|
|
|
|
|
2017-09-04 11:24:35 +00:00
|
|
|
struct empty_value {
|
2017-09-03 12:44:21 +00:00
|
|
|
};
|
|
|
|
|
2017-09-12 13:58:14 +00:00
|
|
|
struct empty_error {
|
|
|
|
};
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <
|
|
|
|
typename Value = empty_value,
|
|
|
|
typename Error = no_error,
|
|
|
|
typename Handlers = details::type_erased_handlers<Value, Error>>
|
|
|
|
class consumer;
|
|
|
|
|
|
|
|
namespace details {
|
|
|
|
|
|
|
|
template <typename Value, typename Error, typename Handlers>
|
|
|
|
class consumer_base {
|
2017-09-29 10:55:25 +00:00
|
|
|
static constexpr bool is_type_erased
|
|
|
|
= is_type_erased_handlers_v<Handlers>;
|
2017-09-27 15:15:26 +00:00
|
|
|
|
2017-09-03 12:44:21 +00:00
|
|
|
public:
|
|
|
|
template <
|
|
|
|
typename OnNext,
|
|
|
|
typename OnError,
|
2017-09-27 15:15:26 +00:00
|
|
|
typename OnDone>
|
|
|
|
consumer_base(
|
2017-09-03 12:44:21 +00:00
|
|
|
OnNext &&next,
|
|
|
|
OnError &&error,
|
|
|
|
OnDone &&done);
|
|
|
|
|
2017-09-05 08:22:06 +00:00
|
|
|
bool put_next(Value &&value) const;
|
|
|
|
bool put_next_copy(const Value &value) const;
|
2017-09-14 19:27:41 +00:00
|
|
|
bool put_next_forward(Value &&value) const {
|
|
|
|
return put_next(std::move(value));
|
|
|
|
}
|
|
|
|
bool put_next_forward(const Value &value) const {
|
|
|
|
return put_next_copy(value);
|
|
|
|
}
|
2017-09-05 08:22:06 +00:00
|
|
|
void put_error(Error &&error) const;
|
|
|
|
void put_error_copy(const Error &error) const;
|
2017-09-14 19:27:41 +00:00
|
|
|
void put_error_forward(Error &&error) const {
|
|
|
|
return put_error(std::move(error));
|
|
|
|
}
|
|
|
|
void put_error_forward(const Error &error) const {
|
|
|
|
return put_error_copy(error);
|
|
|
|
}
|
2017-09-04 11:24:35 +00:00
|
|
|
void put_done() const;
|
2017-09-03 12:44:21 +00:00
|
|
|
|
2017-10-02 09:14:07 +00:00
|
|
|
bool add_lifetime(lifetime &&lifetime) const;
|
2017-09-12 13:58:14 +00:00
|
|
|
|
|
|
|
template <typename Type, typename... Args>
|
|
|
|
Type *make_state(Args&& ...args) const;
|
2017-09-14 19:27:41 +00:00
|
|
|
|
2017-09-03 12:44:21 +00:00
|
|
|
void terminate() const;
|
2017-10-13 16:33:38 +00:00
|
|
|
auto terminator() const {
|
|
|
|
return [self = *this] {
|
|
|
|
self.terminate();
|
|
|
|
};
|
|
|
|
}
|
2017-09-03 12:44:21 +00:00
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
const details::type_erased_handlers<Value, Error> *comparable() const {
|
|
|
|
return _handlers.get();
|
2017-09-03 15:57:51 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 12:44:21 +00:00
|
|
|
private:
|
2017-09-27 15:15:26 +00:00
|
|
|
template <
|
|
|
|
typename OtherHandlers,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
std::is_base_of_v<Handlers, OtherHandlers>>>
|
|
|
|
consumer_base(const std::shared_ptr<OtherHandlers> &handlers)
|
|
|
|
: _handlers(handlers) {
|
|
|
|
}
|
2017-09-12 13:58:14 +00:00
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <
|
|
|
|
typename OtherHandlers,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
std::is_base_of_v<Handlers, OtherHandlers>>>
|
|
|
|
consumer_base(std::shared_ptr<OtherHandlers> &&handlers)
|
|
|
|
: _handlers(std::move(handlers)) {
|
|
|
|
}
|
2017-09-12 13:58:14 +00:00
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
mutable std::shared_ptr<Handlers> _handlers;
|
2017-09-03 12:44:21 +00:00
|
|
|
|
2017-09-29 10:55:25 +00:00
|
|
|
bool handlers_put_next(Value &&value) const {
|
|
|
|
if constexpr (is_type_erased) {
|
|
|
|
return _handlers->put_next(std::move(value));
|
|
|
|
} else {
|
|
|
|
return _handlers->Handlers::put_next(std::move(value));
|
|
|
|
}
|
2017-09-27 15:15:26 +00:00
|
|
|
}
|
2017-09-29 10:55:25 +00:00
|
|
|
bool handlers_put_next_copy(const Value &value) const {
|
|
|
|
if constexpr (is_type_erased) {
|
|
|
|
return _handlers->put_next_copy(value);
|
|
|
|
} else {
|
|
|
|
return _handlers->Handlers::put_next_copy(value);
|
|
|
|
}
|
2017-09-27 15:15:26 +00:00
|
|
|
}
|
2017-09-29 10:55:25 +00:00
|
|
|
std::shared_ptr<Handlers> take_handlers() const {
|
|
|
|
return std::exchange(_handlers, nullptr);
|
2017-09-27 15:15:26 +00:00
|
|
|
}
|
2017-09-03 12:44:21 +00:00
|
|
|
|
2017-09-18 16:39:45 +00:00
|
|
|
template <
|
2017-09-27 15:15:26 +00:00
|
|
|
typename OtherValue,
|
|
|
|
typename OtherError,
|
|
|
|
typename OtherHandlers>
|
|
|
|
friend class ::rpl::consumer;
|
2017-09-03 12:44:21 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <typename Value, typename Error, typename Handlers>
|
2017-09-03 12:44:21 +00:00
|
|
|
template <typename OnNext, typename OnError, typename OnDone>
|
2017-09-27 15:15:26 +00:00
|
|
|
inline consumer_base<Value, Error, Handlers>::consumer_base(
|
2017-09-03 12:44:21 +00:00
|
|
|
OnNext &&next,
|
|
|
|
OnError &&error,
|
2017-10-27 08:51:11 +00:00
|
|
|
OnDone &&done)
|
|
|
|
: _handlers(std::make_shared<consumer_handlers<
|
|
|
|
Value,
|
|
|
|
Error,
|
|
|
|
std::decay_t<OnNext>,
|
|
|
|
std::decay_t<OnError>,
|
|
|
|
std::decay_t<OnDone>>>(
|
2017-09-03 12:44:21 +00:00
|
|
|
std::forward<OnNext>(next),
|
|
|
|
std::forward<OnError>(error),
|
|
|
|
std::forward<OnDone>(done))) {
|
|
|
|
}
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <typename Value, typename Error, typename Handlers>
|
|
|
|
inline bool consumer_base<Value, Error, Handlers>::put_next(
|
|
|
|
Value &&value) const {
|
|
|
|
if (_handlers) {
|
2017-09-29 10:55:25 +00:00
|
|
|
if (handlers_put_next(std::move(value))) {
|
2017-09-03 15:57:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-09-27 15:15:26 +00:00
|
|
|
_handlers = nullptr;
|
2017-09-03 15:57:51 +00:00
|
|
|
}
|
|
|
|
return false;
|
2017-09-03 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <typename Value, typename Error, typename Handlers>
|
|
|
|
inline bool consumer_base<Value, Error, Handlers>::put_next_copy(
|
2017-09-18 16:39:45 +00:00
|
|
|
const Value &value) const {
|
2017-09-27 15:15:26 +00:00
|
|
|
if (_handlers) {
|
2017-09-29 10:55:25 +00:00
|
|
|
if (handlers_put_next_copy(value)) {
|
2017-09-14 19:27:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-09-27 15:15:26 +00:00
|
|
|
_handlers = nullptr;
|
2017-09-14 19:27:41 +00:00
|
|
|
}
|
|
|
|
return false;
|
2017-09-05 08:22:06 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <typename Value, typename Error, typename Handlers>
|
|
|
|
inline void consumer_base<Value, Error, Handlers>::put_error(
|
|
|
|
Error &&error) const {
|
|
|
|
if (_handlers) {
|
2017-09-29 10:55:25 +00:00
|
|
|
if constexpr (is_type_erased) {
|
|
|
|
take_handlers()->put_error(std::move(error));
|
|
|
|
} else {
|
|
|
|
take_handlers()->Handlers::put_error(std::move(error));
|
|
|
|
}
|
2017-09-03 15:57:51 +00:00
|
|
|
}
|
2017-09-03 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <typename Value, typename Error, typename Handlers>
|
|
|
|
inline void consumer_base<Value, Error, Handlers>::put_error_copy(
|
2017-09-18 16:39:45 +00:00
|
|
|
const Error &error) const {
|
2017-09-27 15:15:26 +00:00
|
|
|
if (_handlers) {
|
2017-09-29 10:55:25 +00:00
|
|
|
if constexpr (is_type_erased) {
|
|
|
|
take_handlers()->put_error_copy(error);
|
|
|
|
} else {
|
|
|
|
take_handlers()->Handlers::put_error_copy(error);
|
|
|
|
}
|
2017-09-14 19:27:41 +00:00
|
|
|
}
|
2017-09-05 08:22:06 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <typename Value, typename Error, typename Handlers>
|
|
|
|
inline void consumer_base<Value, Error, Handlers>::put_done() const {
|
|
|
|
if (_handlers) {
|
2017-09-29 10:55:25 +00:00
|
|
|
if constexpr (is_type_erased) {
|
|
|
|
take_handlers()->put_done();
|
|
|
|
} else {
|
|
|
|
take_handlers()->Handlers::put_done();
|
|
|
|
}
|
2017-09-03 15:57:51 +00:00
|
|
|
}
|
2017-09-03 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <typename Value, typename Error, typename Handlers>
|
2017-10-02 09:14:07 +00:00
|
|
|
inline bool consumer_base<Value, Error, Handlers>::add_lifetime(
|
2017-09-27 15:15:26 +00:00
|
|
|
lifetime &&lifetime) const {
|
2017-10-02 09:14:07 +00:00
|
|
|
if (!_handlers) {
|
2017-09-03 15:57:51 +00:00
|
|
|
lifetime.destroy();
|
2017-10-02 09:14:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (_handlers->add_lifetime(std::move(lifetime))) {
|
|
|
|
return true;
|
2017-09-03 15:57:51 +00:00
|
|
|
}
|
2017-10-02 09:14:07 +00:00
|
|
|
_handlers = nullptr;
|
|
|
|
return false;
|
2017-09-03 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <typename Value, typename Error, typename Handlers>
|
2017-09-12 13:58:14 +00:00
|
|
|
template <typename Type, typename... Args>
|
2017-09-27 15:15:26 +00:00
|
|
|
inline Type *consumer_base<Value, Error, Handlers>::make_state(
|
|
|
|
Args&& ...args) const {
|
2017-10-02 09:14:07 +00:00
|
|
|
if (!_handlers) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (auto result = _handlers->template make_state<Type>(
|
|
|
|
std::forward<Args>(args)...)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
_handlers = nullptr;
|
|
|
|
return nullptr;
|
2017-09-12 13:58:14 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <typename Value, typename Error, typename Handlers>
|
|
|
|
inline void consumer_base<Value, Error, Handlers>::terminate() const {
|
|
|
|
if (_handlers) {
|
|
|
|
std::exchange(_handlers, nullptr)->terminate();
|
2017-09-03 15:57:51 +00:00
|
|
|
}
|
2017-09-03 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Value, typename Error>
|
2017-09-27 15:15:26 +00:00
|
|
|
using consumer_base_type_erased = consumer_base<
|
|
|
|
Value,
|
|
|
|
Error,
|
|
|
|
details::type_erased_handlers<Value, Error>>;
|
|
|
|
|
|
|
|
template <typename Value, typename Error, typename Handlers>
|
|
|
|
constexpr bool is_specific_handlers_v = !std::is_same_v<
|
|
|
|
details::type_erased_handlers<Value, Error>,
|
|
|
|
Handlers
|
|
|
|
> && std::is_base_of_v<
|
|
|
|
details::type_erased_handlers<Value, Error>,
|
|
|
|
Handlers
|
|
|
|
>;
|
|
|
|
|
|
|
|
} // namespace details
|
|
|
|
|
|
|
|
template <typename Value, typename Error, typename Handlers>
|
|
|
|
class consumer final
|
|
|
|
: public details::consumer_base<Value, Error, Handlers> {
|
|
|
|
using parent_type = details::consumer_base<
|
|
|
|
Value,
|
|
|
|
Error,
|
|
|
|
Handlers>;
|
2017-09-03 12:44:21 +00:00
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
public:
|
|
|
|
using parent_type::parent_type;
|
|
|
|
|
|
|
|
};
|
2017-09-12 13:58:14 +00:00
|
|
|
|
2017-09-03 12:44:21 +00:00
|
|
|
template <typename Value, typename Error>
|
2017-09-27 15:15:26 +00:00
|
|
|
class consumer<Value, Error, details::type_erased_handlers<Value, Error>> final
|
|
|
|
: public details::consumer_base_type_erased<Value, Error> {
|
|
|
|
using parent_type = details::consumer_base_type_erased<
|
|
|
|
Value,
|
|
|
|
Error>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using parent_type::parent_type;
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename Handlers,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
details::is_specific_handlers_v<Value, Error, Handlers>>>
|
|
|
|
consumer(const details::consumer_base<Value, Error, Handlers> &other)
|
|
|
|
: parent_type(other._handlers) {
|
2017-09-03 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <
|
|
|
|
typename Handlers,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
details::is_specific_handlers_v<Value, Error, Handlers>>>
|
|
|
|
consumer(details::consumer_base<Value, Error, Handlers> &&other)
|
|
|
|
: parent_type(std::move(other._handlers)) {
|
2017-09-03 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <
|
|
|
|
typename Handlers,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
details::is_specific_handlers_v<Value, Error, Handlers>>>
|
|
|
|
consumer &operator=(
|
|
|
|
const details::consumer_base<Value, Error, Handlers> &other) {
|
2017-09-27 18:45:26 +00:00
|
|
|
this->_handlers = other._handlers;
|
2017-09-27 15:15:26 +00:00
|
|
|
return *this;
|
2017-09-14 19:27:41 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <
|
|
|
|
typename Handlers,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
details::is_specific_handlers_v<Value, Error, Handlers>>>
|
|
|
|
consumer &operator=(
|
|
|
|
details::consumer_base<Value, Error, Handlers> &&other) {
|
2017-09-27 18:45:26 +00:00
|
|
|
this->_handlers = std::move(other._handlers);
|
2017-09-27 15:15:26 +00:00
|
|
|
return *this;
|
2017-09-03 12:44:21 +00:00
|
|
|
}
|
2017-09-27 15:15:26 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Error,
|
|
|
|
typename Handlers1,
|
|
|
|
typename Handlers2>
|
|
|
|
inline bool operator==(
|
|
|
|
const consumer<Value, Error, Handlers1> &a,
|
|
|
|
const consumer<Value, Error, Handlers2> &b) {
|
|
|
|
return a.comparable() == b.comparable();
|
2017-09-03 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Error,
|
|
|
|
typename Handlers1,
|
|
|
|
typename Handlers2>
|
|
|
|
inline bool operator<(
|
|
|
|
const consumer<Value, Error, Handlers1> &a,
|
|
|
|
const consumer<Value, Error, Handlers2> &b) {
|
|
|
|
return a.comparable() < b.comparable();
|
2017-09-14 19:27:41 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:15:26 +00:00
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Error,
|
|
|
|
typename Handlers1,
|
|
|
|
typename Handlers2>
|
|
|
|
inline bool operator!=(
|
|
|
|
const consumer<Value, Error, Handlers1> &a,
|
|
|
|
const consumer<Value, Error, Handlers2> &b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Error,
|
|
|
|
typename Handlers1,
|
|
|
|
typename Handlers2>
|
|
|
|
inline bool operator>(
|
|
|
|
const consumer<Value, Error, Handlers1> &a,
|
|
|
|
const consumer<Value, Error, Handlers2> &b) {
|
|
|
|
return b < a;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Error,
|
|
|
|
typename Handlers1,
|
|
|
|
typename Handlers2>
|
|
|
|
inline bool operator<=(
|
|
|
|
const consumer<Value, Error, Handlers1> &a,
|
|
|
|
const consumer<Value, Error, Handlers2> &b) {
|
|
|
|
return !(b < a);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Error,
|
|
|
|
typename Handlers1,
|
|
|
|
typename Handlers2>
|
|
|
|
inline bool operator>=(
|
|
|
|
const consumer<Value, Error, Handlers1> &a,
|
|
|
|
const consumer<Value, Error, Handlers2> &b) {
|
|
|
|
return !(a < b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename Error,
|
|
|
|
typename OnNext,
|
|
|
|
typename OnError,
|
|
|
|
typename OnDone,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
details::is_callable_v<OnNext, Value> &&
|
|
|
|
details::is_callable_v<OnError, Error> &&
|
|
|
|
details::is_callable_v<OnDone>>>
|
2017-11-21 09:20:56 +00:00
|
|
|
#ifdef RPL_CONSUMER_TYPE_ERASED_ALWAYS
|
2017-09-28 11:40:10 +00:00
|
|
|
inline consumer<Value, Error> make_consumer(
|
2017-11-21 09:20:56 +00:00
|
|
|
#else // RPL_CONSUMER_TYPE_ERASED_ALWAYS
|
2017-09-27 15:15:26 +00:00
|
|
|
inline auto make_consumer(
|
2017-11-21 09:20:56 +00:00
|
|
|
#endif // !RPL_CONSUMER_TYPE_ERASED_ALWAYS
|
2017-09-27 15:15:26 +00:00
|
|
|
OnNext &&next,
|
|
|
|
OnError &&error,
|
|
|
|
OnDone &&done) {
|
|
|
|
return consumer<Value, Error, details::consumer_handlers<
|
|
|
|
Value,
|
|
|
|
Error,
|
|
|
|
std::decay_t<OnNext>,
|
|
|
|
std::decay_t<OnError>,
|
|
|
|
std::decay_t<OnDone>>>(
|
|
|
|
std::forward<OnNext>(next),
|
|
|
|
std::forward<OnError>(error),
|
|
|
|
std::forward<OnDone>(done));
|
2017-09-03 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace rpl
|