2017-09-19 14:51:01 +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-19 14:51:01 +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-19 14:51:01 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <rpl/producer.h>
|
|
|
|
#include <rpl/event_stream.h>
|
|
|
|
|
2018-06-02 14:29:21 +00:00
|
|
|
namespace mapbox {
|
|
|
|
namespace util {
|
|
|
|
|
|
|
|
template <typename ...Types>
|
|
|
|
class variant;
|
|
|
|
|
|
|
|
} // namespace util
|
|
|
|
} // namespace mapbox
|
|
|
|
|
2017-09-19 14:51:01 +00:00
|
|
|
namespace rpl {
|
2017-11-11 17:53:23 +00:00
|
|
|
namespace details {
|
|
|
|
|
|
|
|
template <typename A, typename B>
|
|
|
|
struct supports_equality_compare {
|
|
|
|
template <typename U, typename V>
|
|
|
|
static auto test(const U *u, const V *v)
|
2018-06-02 14:29:21 +00:00
|
|
|
-> decltype(*u == *v, true_t());
|
|
|
|
static false_t test(...);
|
2017-11-11 17:53:23 +00:00
|
|
|
static constexpr bool value
|
2018-06-02 14:29:21 +00:00
|
|
|
= (sizeof(test((const A*)nullptr, (const B*)nullptr))
|
|
|
|
== sizeof(true_t));
|
2017-11-11 17:53:23 +00:00
|
|
|
};
|
|
|
|
|
2018-06-02 14:29:21 +00:00
|
|
|
// Fix for MSVC expression SFINAE.
|
|
|
|
// It still doesn't work! :(
|
|
|
|
//
|
|
|
|
//template <typename Type1, typename ...Types1>
|
|
|
|
//struct supports_equality_compare<
|
|
|
|
// mapbox::util::variant<Type1, Types1...>,
|
|
|
|
// mapbox::util::variant<Type1, Types1...>> {
|
|
|
|
// static constexpr bool value
|
|
|
|
// = (supports_equality_compare<Type1, Type1>::value
|
|
|
|
// && supports_equality_compare<
|
|
|
|
// mapbox::util::variant<Types1...>,
|
|
|
|
// mapbox::util::variant<Types1...>>::value);
|
|
|
|
//
|
|
|
|
//};
|
|
|
|
//template <typename Type>
|
|
|
|
//struct supports_equality_compare<
|
|
|
|
// mapbox::util::variant<Type>,
|
|
|
|
// mapbox::util::variant<Type>> {
|
|
|
|
// static constexpr bool value = supports_equality_compare<Type, Type>::value;
|
|
|
|
//
|
|
|
|
//};
|
|
|
|
|
2017-11-11 17:53:23 +00:00
|
|
|
template <typename A, typename B>
|
|
|
|
constexpr bool supports_equality_compare_v
|
2018-06-02 14:29:21 +00:00
|
|
|
= supports_equality_compare<std::decay_t<A>, std::decay_t<B>>::value;
|
2017-11-11 17:53:23 +00:00
|
|
|
|
|
|
|
} // namespace details
|
2017-09-19 14:51:01 +00:00
|
|
|
|
2019-02-20 13:28:48 +00:00
|
|
|
template <typename Type, typename Error = no_error>
|
2017-10-22 12:07:57 +00:00
|
|
|
class variable final {
|
2017-09-19 14:51:01 +00:00
|
|
|
public:
|
|
|
|
variable() : _data{} {
|
|
|
|
}
|
2018-06-23 00:02:20 +00:00
|
|
|
variable(variable &&other) : _data(std::move(other._data)) {
|
|
|
|
}
|
2018-06-22 16:44:45 +00:00
|
|
|
variable &operator=(variable &&other) {
|
|
|
|
return (*this = std::move(other._data));
|
|
|
|
}
|
|
|
|
|
2017-09-19 14:51:01 +00:00
|
|
|
template <
|
|
|
|
typename OtherType,
|
|
|
|
typename = std::enable_if_t<
|
2017-10-22 12:07:57 +00:00
|
|
|
std::is_constructible_v<Type, OtherType&&>>>
|
2017-09-19 14:51:01 +00:00
|
|
|
variable(OtherType &&data) : _data(std::forward<OtherType>(data)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename OtherType,
|
|
|
|
typename = std::enable_if_t<
|
2017-10-22 12:07:57 +00:00
|
|
|
std::is_assignable_v<Type&, OtherType&&>>>
|
2017-09-19 14:51:01 +00:00
|
|
|
variable &operator=(OtherType &&data) {
|
2017-10-22 12:07:57 +00:00
|
|
|
_lifetime.destroy();
|
2017-09-19 14:51:01 +00:00
|
|
|
return assign(std::forward<OtherType>(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename OtherType,
|
2019-02-27 11:36:19 +00:00
|
|
|
typename OtherError,
|
2017-09-27 11:16:05 +00:00
|
|
|
typename Generator,
|
2017-09-19 14:51:01 +00:00
|
|
|
typename = std::enable_if_t<
|
2017-10-22 12:07:57 +00:00
|
|
|
std::is_assignable_v<Type&, OtherType>>>
|
2019-02-27 11:36:19 +00:00
|
|
|
variable(producer<OtherType, OtherError, Generator> &&stream) {
|
2017-09-19 14:51:01 +00:00
|
|
|
std::move(stream)
|
2018-09-18 10:43:14 +00:00
|
|
|
| start_with_next([=](auto &&data) {
|
2017-10-22 12:07:57 +00:00
|
|
|
assign(std::forward<decltype(data)>(data));
|
2017-09-19 14:51:01 +00:00
|
|
|
}, _lifetime);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename OtherType,
|
2019-02-27 11:36:19 +00:00
|
|
|
typename OtherError,
|
2017-09-27 11:16:05 +00:00
|
|
|
typename Generator,
|
2017-09-19 14:51:01 +00:00
|
|
|
typename = std::enable_if_t<
|
2017-10-22 12:07:57 +00:00
|
|
|
std::is_assignable_v<Type&, OtherType>>>
|
2017-09-27 11:16:05 +00:00
|
|
|
variable &operator=(
|
2019-02-27 11:36:19 +00:00
|
|
|
producer<OtherType, OtherError, Generator> &&stream) {
|
2017-09-19 14:51:01 +00:00
|
|
|
_lifetime.destroy();
|
|
|
|
std::move(stream)
|
2018-09-18 10:43:14 +00:00
|
|
|
| start_with_next([=](auto &&data) {
|
2017-10-22 12:07:57 +00:00
|
|
|
assign(std::forward<decltype(data)>(data));
|
2017-09-19 14:51:01 +00:00
|
|
|
}, _lifetime);
|
2018-09-09 17:38:08 +00:00
|
|
|
return *this;
|
2017-09-19 14:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Type current() const {
|
|
|
|
return _data;
|
|
|
|
}
|
2017-09-27 11:16:05 +00:00
|
|
|
auto value() const {
|
2017-09-19 14:51:01 +00:00
|
|
|
return _changes.events_starting_with_copy(_data);
|
|
|
|
}
|
2017-10-31 18:25:22 +00:00
|
|
|
auto changes() const {
|
|
|
|
return _changes.events();
|
|
|
|
}
|
2017-09-19 14:51:01 +00:00
|
|
|
|
2019-02-20 13:28:48 +00:00
|
|
|
// Send 'done' to all subscribers and unsubscribe them.
|
|
|
|
template <
|
|
|
|
typename OtherType,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
std::is_assignable_v<Type&, OtherType>>>
|
|
|
|
void reset(OtherType &&data) {
|
|
|
|
_data = std::forward<OtherType>(data);
|
|
|
|
_changes = event_stream<Type, Error>();
|
|
|
|
}
|
|
|
|
void reset() {
|
|
|
|
reset(Type());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename OtherError,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
std::is_constructible_v<Error, OtherError&&>>>
|
|
|
|
void reset_with_error(OtherError &&error) {
|
|
|
|
_changes.fire_error(std::forward<OtherError>(error));
|
|
|
|
}
|
|
|
|
void reset_with_error() {
|
|
|
|
reset_with_error(Error());
|
|
|
|
}
|
|
|
|
|
2017-09-19 14:51:01 +00:00
|
|
|
private:
|
|
|
|
template <typename OtherType>
|
|
|
|
variable &assign(OtherType &&data) {
|
2017-11-11 17:53:23 +00:00
|
|
|
if constexpr (details::supports_equality_compare_v<Type, OtherType>) {
|
2017-10-22 12:07:57 +00:00
|
|
|
if (!(_data == data)) {
|
|
|
|
_data = std::forward<OtherType>(data);
|
|
|
|
_changes.fire_copy(_data);
|
|
|
|
}
|
2017-11-11 17:53:23 +00:00
|
|
|
} else if constexpr (details::supports_equality_compare_v<Type, Type>) {
|
2017-10-22 12:07:57 +00:00
|
|
|
auto old = std::move(_data);
|
|
|
|
_data = std::forward<OtherType>(data);
|
|
|
|
if (!(_data == old)) {
|
|
|
|
_changes.fire_copy(_data);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_data = std::forward<OtherType>(data);
|
|
|
|
_changes.fire_copy(_data);
|
|
|
|
}
|
2017-09-19 14:51:01 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type _data;
|
2019-02-20 13:28:48 +00:00
|
|
|
event_stream<Type, Error> _changes;
|
2017-09-27 11:16:05 +00:00
|
|
|
lifetime _lifetime;
|
2017-09-19 14:51:01 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rpl
|