2017-09-19 14:51:01 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <rpl/producer.h>
|
|
|
|
#include <rpl/event_stream.h>
|
|
|
|
|
|
|
|
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)
|
|
|
|
-> decltype(*u == *v, details::true_t());
|
|
|
|
static details::false_t test(...);
|
|
|
|
static constexpr bool value
|
|
|
|
= (sizeof(test(
|
|
|
|
(std::decay_t<A>*)nullptr,
|
|
|
|
(std::decay_t<B>*)nullptr
|
|
|
|
)) == sizeof(details::true_t));
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename A, typename B>
|
|
|
|
constexpr bool supports_equality_compare_v
|
|
|
|
= supports_equality_compare<A, B>::value;
|
|
|
|
|
|
|
|
} // namespace details
|
2017-09-19 14:51:01 +00:00
|
|
|
|
|
|
|
template <typename Type>
|
2017-10-22 12:07:57 +00:00
|
|
|
class variable final {
|
2017-09-19 14:51:01 +00:00
|
|
|
public:
|
|
|
|
variable() : _data{} {
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2017-09-27 11:16:05 +00:00
|
|
|
typename Error,
|
|
|
|
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(producer<OtherType, Error, Generator> &&stream) {
|
2017-09-19 14:51:01 +00:00
|
|
|
std::move(stream)
|
2017-09-27 08:43:35 +00:00
|
|
|
| start_with_next([this](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,
|
2017-09-27 11:16:05 +00:00
|
|
|
typename Error,
|
|
|
|
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=(
|
|
|
|
producer<OtherType, Error, Generator> &&stream) {
|
2017-09-19 14:51:01 +00:00
|
|
|
_lifetime.destroy();
|
|
|
|
std::move(stream)
|
2017-09-27 08:43:35 +00:00
|
|
|
| start_with_next([this](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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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;
|
2017-09-27 11:16:05 +00:00
|
|
|
event_stream<Type> _changes;
|
|
|
|
lifetime _lifetime;
|
2017-09-19 14:51:01 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rpl
|