2017-09-26 11:49:16 +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/event_stream.h>
|
|
|
|
|
|
|
|
namespace Data {
|
|
|
|
|
2017-09-26 20:14:56 +00:00
|
|
|
template <typename FlagsType>
|
|
|
|
using FlagsUnderlying = typename FlagsType::Type;
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
template <
|
|
|
|
typename FlagsType,
|
2017-09-26 20:14:56 +00:00
|
|
|
FlagsUnderlying<FlagsType> kEssential = FlagsUnderlying<FlagsType>(-1)>
|
2017-09-26 11:49:16 +00:00
|
|
|
class Flags {
|
|
|
|
public:
|
2017-09-26 13:53:51 +00:00
|
|
|
using Type = FlagsType;
|
|
|
|
using Enum = typename Type::Enum;
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
struct Change {
|
2017-09-26 13:53:51 +00:00
|
|
|
using Type = FlagsType;
|
|
|
|
using Enum = typename Type::Enum;
|
|
|
|
|
|
|
|
Change(Type diff, Type value)
|
2017-09-26 11:49:16 +00:00
|
|
|
: diff(diff)
|
|
|
|
, value(value) {
|
|
|
|
}
|
2017-09-26 13:53:51 +00:00
|
|
|
Type diff = 0;
|
|
|
|
Type value = 0;
|
2017-09-26 11:49:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Flags() = default;
|
2017-09-26 13:53:51 +00:00
|
|
|
Flags(Type value) : _value(value) {
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 13:53:51 +00:00
|
|
|
void set(Type which) {
|
2017-09-26 11:49:16 +00:00
|
|
|
if (auto diff = which ^ _value) {
|
|
|
|
_value = which;
|
|
|
|
updated(diff);
|
|
|
|
}
|
|
|
|
}
|
2017-09-26 13:53:51 +00:00
|
|
|
void add(Type which) {
|
2017-09-26 11:49:16 +00:00
|
|
|
if (auto diff = which & ~_value) {
|
|
|
|
_value |= which;
|
|
|
|
updated(diff);
|
|
|
|
}
|
|
|
|
}
|
2017-09-26 13:53:51 +00:00
|
|
|
void remove(Type which) {
|
2017-09-26 11:49:16 +00:00
|
|
|
if (auto diff = which & _value) {
|
|
|
|
_value &= ~which;
|
|
|
|
updated(diff);
|
|
|
|
}
|
|
|
|
}
|
2017-09-27 12:04:19 +00:00
|
|
|
auto current() const {
|
2017-09-26 11:49:16 +00:00
|
|
|
return _value;
|
|
|
|
}
|
2017-09-27 12:04:19 +00:00
|
|
|
auto changes() const {
|
2017-09-26 11:49:16 +00:00
|
|
|
return _changes.events();
|
|
|
|
}
|
2017-09-27 12:04:19 +00:00
|
|
|
auto value() const {
|
2017-09-26 11:49:16 +00:00
|
|
|
return _changes.events_starting_with({
|
2017-09-26 13:53:51 +00:00
|
|
|
Type::from_raw(kEssential),
|
2017-09-26 11:49:16 +00:00
|
|
|
_value });
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-09-26 13:53:51 +00:00
|
|
|
void updated(Type diff) {
|
|
|
|
if ((diff &= Type::from_raw(kEssential))) {
|
2017-09-26 11:49:16 +00:00
|
|
|
_changes.fire({ diff, _value });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 13:53:51 +00:00
|
|
|
Type _value = 0;
|
2017-09-26 11:49:16 +00:00
|
|
|
rpl::event_stream<Change> _changes;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Data
|