2016-05-24 16:13:07 +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
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2016-05-24 16:13:07 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-02-21 13:45:56 +00:00
|
|
|
#include <vector>
|
2017-02-21 14:37:53 +00:00
|
|
|
#include <deque>
|
2016-10-14 17:10:15 +00:00
|
|
|
#include "core/type_traits.h"
|
2016-05-24 16:13:07 +00:00
|
|
|
|
2016-08-18 19:27:43 +00:00
|
|
|
namespace base {
|
|
|
|
namespace internal {
|
|
|
|
|
2016-11-20 12:54:07 +00:00
|
|
|
using ObservableCallHandlers = base::lambda<void()>;
|
2016-08-18 19:27:43 +00:00
|
|
|
void RegisterPendingObservable(ObservableCallHandlers *handlers);
|
|
|
|
void UnregisterActiveObservable(ObservableCallHandlers *handlers);
|
|
|
|
void UnregisterObservable(ObservableCallHandlers *handlers);
|
|
|
|
|
|
|
|
template <typename EventType>
|
|
|
|
struct SubscriptionHandlerHelper {
|
2017-01-14 18:50:16 +00:00
|
|
|
using type = base::lambda<void(parameter_type<EventType>)>;
|
2016-08-18 19:27:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct SubscriptionHandlerHelper<void> {
|
2016-11-20 12:54:07 +00:00
|
|
|
using type = base::lambda<void()>;
|
2016-08-18 19:27:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename EventType>
|
|
|
|
using SubscriptionHandler = typename SubscriptionHandlerHelper<EventType>::type;
|
|
|
|
|
|
|
|
// Required because QShared/WeakPointer can't point to void.
|
|
|
|
class BaseObservableData {
|
|
|
|
};
|
|
|
|
|
2016-09-26 14:50:24 +00:00
|
|
|
template <typename EventType, typename Handler>
|
2016-08-18 19:27:43 +00:00
|
|
|
class CommonObservableData;
|
|
|
|
|
2016-09-26 14:50:24 +00:00
|
|
|
template <typename EventType, typename Handler>
|
2016-08-18 19:27:43 +00:00
|
|
|
class ObservableData;
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
class Subscription {
|
|
|
|
public:
|
|
|
|
Subscription() = default;
|
|
|
|
Subscription(const Subscription &) = delete;
|
|
|
|
Subscription &operator=(const Subscription &) = delete;
|
2016-10-07 16:45:45 +00:00
|
|
|
Subscription(Subscription &&other) : _node(base::take(other._node)), _removeMethod(other._removeMethod) {
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
|
|
|
Subscription &operator=(Subscription &&other) {
|
|
|
|
qSwap(_node, other._node);
|
|
|
|
qSwap(_removeMethod, other._removeMethod);
|
|
|
|
return *this;
|
|
|
|
}
|
2016-10-14 17:10:15 +00:00
|
|
|
explicit operator bool() const {
|
|
|
|
return (_node != nullptr);
|
|
|
|
}
|
2016-08-18 19:27:43 +00:00
|
|
|
void destroy() {
|
|
|
|
if (_node) {
|
|
|
|
(*_removeMethod)(_node);
|
|
|
|
delete _node;
|
|
|
|
_node = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~Subscription() {
|
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Node {
|
|
|
|
Node(const QSharedPointer<internal::BaseObservableData> &observable) : observable(observable) {
|
|
|
|
}
|
|
|
|
Node *next = nullptr;
|
|
|
|
Node *prev = nullptr;
|
|
|
|
QWeakPointer<internal::BaseObservableData> observable;
|
|
|
|
};
|
|
|
|
using RemoveMethod = void(*)(Node*);
|
|
|
|
Subscription(Node *node, RemoveMethod removeMethod) : _node(node), _removeMethod(removeMethod) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *_node = nullptr;
|
|
|
|
RemoveMethod _removeMethod;
|
|
|
|
|
2016-09-26 14:50:24 +00:00
|
|
|
template <typename EventType, typename Handler>
|
2016-08-18 19:27:43 +00:00
|
|
|
friend class internal::CommonObservableData;
|
|
|
|
|
2016-09-26 14:50:24 +00:00
|
|
|
template <typename EventType, typename Handler>
|
2016-08-18 19:27:43 +00:00
|
|
|
friend class internal::ObservableData;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
2016-10-14 18:27:42 +00:00
|
|
|
template <typename EventType, typename Handler, bool EventTypeIsSimple>
|
|
|
|
class BaseObservable;
|
|
|
|
|
2016-09-26 14:50:24 +00:00
|
|
|
template <typename EventType, typename Handler>
|
2016-08-18 19:27:43 +00:00
|
|
|
class CommonObservable {
|
|
|
|
public:
|
2016-09-23 16:04:26 +00:00
|
|
|
Subscription add_subscription(Handler &&handler) {
|
2016-08-27 04:49:18 +00:00
|
|
|
if (!_data) {
|
2016-09-26 14:50:24 +00:00
|
|
|
_data = MakeShared<ObservableData<EventType, Handler>>(this);
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
2017-02-21 13:45:56 +00:00
|
|
|
return _data->append(std::move(handler));
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-09-26 14:50:24 +00:00
|
|
|
QSharedPointer<ObservableData<EventType, Handler>> _data;
|
2016-08-18 19:27:43 +00:00
|
|
|
|
2016-09-26 14:50:24 +00:00
|
|
|
friend class CommonObservableData<EventType, Handler>;
|
2016-10-14 18:27:42 +00:00
|
|
|
friend class BaseObservable<EventType, Handler, base::type_traits<EventType>::is_fast_copy_type::value>;
|
2016-08-18 19:27:43 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-10-14 18:27:42 +00:00
|
|
|
template <typename EventType, typename Handler>
|
|
|
|
class BaseObservable<EventType, Handler, true> : public internal::CommonObservable<EventType, Handler> {
|
|
|
|
public:
|
|
|
|
void notify(EventType event, bool sync = false) {
|
|
|
|
if (this->_data) {
|
2017-02-21 13:45:56 +00:00
|
|
|
this->_data->notify(std::move(event), sync);
|
2016-10-14 18:27:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-18 19:27:43 +00:00
|
|
|
|
2016-10-14 18:27:42 +00:00
|
|
|
};
|
2016-10-14 17:10:15 +00:00
|
|
|
|
2016-10-14 18:27:42 +00:00
|
|
|
template <typename EventType, typename Handler>
|
|
|
|
class BaseObservable<EventType, Handler, false> : public internal::CommonObservable<EventType, Handler> {
|
2016-08-18 19:27:43 +00:00
|
|
|
public:
|
2016-08-27 04:49:18 +00:00
|
|
|
void notify(EventType &&event, bool sync = false) {
|
2016-08-28 20:23:32 +00:00
|
|
|
if (this->_data) {
|
2017-02-21 13:45:56 +00:00
|
|
|
this->_data->notify(std::move(event), sync);
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-23 16:04:26 +00:00
|
|
|
void notify(const EventType &event, bool sync = false) {
|
2016-10-14 17:10:15 +00:00
|
|
|
if (this->_data) {
|
2017-03-08 10:46:04 +00:00
|
|
|
auto event_copy = event;
|
|
|
|
this->_data->notify(std::move(event_copy), sync);
|
2016-10-14 17:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-18 19:27:43 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-10-14 18:27:42 +00:00
|
|
|
} // namespace internal
|
|
|
|
|
2016-08-18 19:27:43 +00:00
|
|
|
namespace internal {
|
|
|
|
|
2016-09-26 14:50:24 +00:00
|
|
|
template <typename EventType, typename Handler>
|
2016-08-18 19:27:43 +00:00
|
|
|
class CommonObservableData : public BaseObservableData {
|
|
|
|
public:
|
2016-09-26 14:50:24 +00:00
|
|
|
CommonObservableData(CommonObservable<EventType, Handler> *observable) : _observable(observable) {
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
|
|
|
|
2016-08-27 04:49:18 +00:00
|
|
|
Subscription append(Handler &&handler) {
|
2017-02-21 13:45:56 +00:00
|
|
|
auto node = new Node(_observable->_data, std::move(handler));
|
2016-08-27 04:49:18 +00:00
|
|
|
if (_begin) {
|
|
|
|
_end->next = node;
|
|
|
|
node->prev = _end;
|
|
|
|
_end = node;
|
|
|
|
} else {
|
|
|
|
_begin = _end = node;
|
|
|
|
}
|
2016-08-18 19:27:43 +00:00
|
|
|
return { _end, &CommonObservableData::destroyNode };
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return !_begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Node : public Subscription::Node {
|
2017-02-21 13:45:56 +00:00
|
|
|
Node(const QSharedPointer<BaseObservableData> &observer, Handler &&handler) : Subscription::Node(observer), handler(std::move(handler)) {
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
|
|
|
Handler handler;
|
|
|
|
};
|
|
|
|
|
|
|
|
void remove(Subscription::Node *node) {
|
|
|
|
if (node->prev) {
|
|
|
|
node->prev->next = node->next;
|
|
|
|
}
|
|
|
|
if (node->next) {
|
|
|
|
node->next->prev = node->prev;
|
|
|
|
}
|
|
|
|
if (_begin == node) {
|
|
|
|
_begin = static_cast<Node*>(node->next);
|
|
|
|
}
|
|
|
|
if (_end == node) {
|
|
|
|
_end = static_cast<Node*>(node->prev);
|
|
|
|
}
|
|
|
|
if (_current == node) {
|
|
|
|
_current = static_cast<Node*>(node->prev);
|
|
|
|
} else if (!_begin) {
|
|
|
|
_observable->_data.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroyNode(Subscription::Node *node) {
|
2016-09-02 17:01:33 +00:00
|
|
|
if (auto that = node->observable.toStrongRef()) {
|
2016-08-18 19:27:43 +00:00
|
|
|
static_cast<CommonObservableData*>(that.data())->remove(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename CallCurrent>
|
|
|
|
void notifyEnumerate(CallCurrent callCurrent) {
|
|
|
|
_current = _begin;
|
|
|
|
do {
|
|
|
|
callCurrent();
|
|
|
|
if (_current) {
|
|
|
|
_current = static_cast<Node*>(_current->next);
|
|
|
|
} else if (_begin) {
|
|
|
|
_current = _begin;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (_current);
|
|
|
|
|
2016-08-27 04:49:18 +00:00
|
|
|
if (empty()) {
|
2016-08-18 19:27:43 +00:00
|
|
|
_observable->_data.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 14:50:24 +00:00
|
|
|
CommonObservable<EventType, Handler> *_observable = nullptr;
|
2016-08-27 04:49:18 +00:00
|
|
|
Node *_begin = nullptr;
|
2016-08-18 19:27:43 +00:00
|
|
|
Node *_current = nullptr;
|
2016-08-27 04:49:18 +00:00
|
|
|
Node *_end = nullptr;
|
2016-08-18 19:27:43 +00:00
|
|
|
ObservableCallHandlers _callHandlers;
|
|
|
|
|
2016-09-26 14:50:24 +00:00
|
|
|
friend class ObservableData<EventType, Handler>;
|
2016-08-18 19:27:43 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-09-26 14:50:24 +00:00
|
|
|
template <typename EventType, typename Handler>
|
|
|
|
class ObservableData : public CommonObservableData<EventType, Handler> {
|
2016-08-18 19:27:43 +00:00
|
|
|
public:
|
2016-09-26 14:50:24 +00:00
|
|
|
using CommonObservableData<EventType, Handler>::CommonObservableData;
|
2016-08-18 19:27:43 +00:00
|
|
|
|
2016-08-27 04:49:18 +00:00
|
|
|
void notify(EventType &&event, bool sync) {
|
|
|
|
if (_handling) {
|
|
|
|
sync = false;
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
2016-08-27 04:49:18 +00:00
|
|
|
if (sync) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_events.push_back(std::move(event));
|
2016-08-27 04:49:18 +00:00
|
|
|
callHandlers();
|
|
|
|
} else {
|
2016-08-28 20:23:32 +00:00
|
|
|
if (!this->_callHandlers) {
|
|
|
|
this->_callHandlers = [this]() {
|
2016-08-27 04:49:18 +00:00
|
|
|
callHandlers();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (_events.empty()) {
|
2016-08-28 20:23:32 +00:00
|
|
|
RegisterPendingObservable(&this->_callHandlers);
|
2016-08-27 04:49:18 +00:00
|
|
|
}
|
2017-02-21 13:45:56 +00:00
|
|
|
_events.push_back(std::move(event));
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~ObservableData() {
|
2016-08-28 20:23:32 +00:00
|
|
|
UnregisterObservable(&this->_callHandlers);
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void callHandlers() {
|
2016-08-27 04:49:18 +00:00
|
|
|
_handling = true;
|
2016-10-07 16:45:45 +00:00
|
|
|
auto events = base::take(_events);
|
2016-08-18 19:27:43 +00:00
|
|
|
for (auto &event : events) {
|
2016-08-28 20:23:32 +00:00
|
|
|
this->notifyEnumerate([this, &event]() {
|
|
|
|
this->_current->handler(event);
|
2016-08-18 19:27:43 +00:00
|
|
|
});
|
|
|
|
}
|
2016-08-27 04:49:18 +00:00
|
|
|
_handling = false;
|
2016-08-28 20:23:32 +00:00
|
|
|
UnregisterActiveObservable(&this->_callHandlers);
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 14:37:53 +00:00
|
|
|
std::deque<EventType> _events;
|
2016-08-27 04:49:18 +00:00
|
|
|
bool _handling = false;
|
2016-08-18 19:27:43 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-09-26 14:50:24 +00:00
|
|
|
template <class Handler>
|
|
|
|
class ObservableData<void, Handler> : public CommonObservableData<void, Handler> {
|
2016-08-18 19:27:43 +00:00
|
|
|
public:
|
2016-09-26 14:50:24 +00:00
|
|
|
using CommonObservableData<void, Handler>::CommonObservableData;
|
2016-08-18 19:27:43 +00:00
|
|
|
|
2016-08-27 04:49:18 +00:00
|
|
|
void notify(bool sync) {
|
|
|
|
if (_handling) {
|
|
|
|
sync = false;
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
2016-08-27 04:49:18 +00:00
|
|
|
if (sync) {
|
|
|
|
++_eventsCount;
|
|
|
|
callHandlers();
|
|
|
|
} else {
|
2016-09-26 18:17:36 +00:00
|
|
|
if (!this->_callHandlers) {
|
|
|
|
this->_callHandlers = [this]() {
|
2016-08-27 04:49:18 +00:00
|
|
|
callHandlers();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (!_eventsCount) {
|
2016-09-26 18:17:36 +00:00
|
|
|
RegisterPendingObservable(&this->_callHandlers);
|
2016-08-27 04:49:18 +00:00
|
|
|
}
|
|
|
|
++_eventsCount;
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~ObservableData() {
|
2016-09-26 18:17:36 +00:00
|
|
|
UnregisterObservable(&this->_callHandlers);
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void callHandlers() {
|
2016-08-27 04:49:18 +00:00
|
|
|
_handling = true;
|
2016-10-07 16:45:45 +00:00
|
|
|
auto eventsCount = base::take(_eventsCount);
|
2016-08-18 19:27:43 +00:00
|
|
|
for (int i = 0; i != eventsCount; ++i) {
|
2016-09-26 18:17:36 +00:00
|
|
|
this->notifyEnumerate([this]() {
|
|
|
|
this->_current->handler();
|
2016-08-18 19:27:43 +00:00
|
|
|
});
|
|
|
|
}
|
2016-08-27 04:49:18 +00:00
|
|
|
_handling = false;
|
2016-09-26 18:17:36 +00:00
|
|
|
UnregisterActiveObservable(&this->_callHandlers);
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int _eventsCount = 0;
|
2016-08-27 04:49:18 +00:00
|
|
|
bool _handling = false;
|
2016-08-18 19:27:43 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-09-26 14:50:24 +00:00
|
|
|
template <typename Handler>
|
2016-10-14 18:27:42 +00:00
|
|
|
class BaseObservable<void, Handler, base::type_traits<void>::is_fast_copy_type::value> : public internal::CommonObservable<void, Handler> {
|
2016-08-18 19:27:43 +00:00
|
|
|
public:
|
2016-08-27 04:49:18 +00:00
|
|
|
void notify(bool sync = false) {
|
2016-09-26 18:17:36 +00:00
|
|
|
if (this->_data) {
|
|
|
|
this->_data->notify(sync);
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-10-14 18:27:42 +00:00
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
template <typename EventType, typename Handler = internal::SubscriptionHandler<EventType>>
|
|
|
|
class Observable : public internal::BaseObservable<EventType, Handler, base::type_traits<EventType>::is_fast_copy_type::value> {
|
2017-01-14 18:50:16 +00:00
|
|
|
public:
|
|
|
|
Observable() = default;
|
|
|
|
Observable(const Observable &other) = delete;
|
|
|
|
Observable(Observable &&other) = default;
|
|
|
|
Observable &operator=(const Observable &other) = delete;
|
|
|
|
Observable &operator=(Observable &&other) = default;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
class Variable {
|
|
|
|
public:
|
|
|
|
Variable(parameter_type<Type> startValue = Type()) : _value(startValue) {
|
|
|
|
}
|
|
|
|
Variable(Variable &&other) = default;
|
|
|
|
Variable &operator=(Variable &&other) = default;
|
|
|
|
|
|
|
|
parameter_type<Type> value() const {
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setForced(parameter_type<Type> newValue, bool sync = false) {
|
|
|
|
_value = newValue;
|
2017-03-04 19:36:59 +00:00
|
|
|
changed().notify(_value, sync);
|
2017-01-14 18:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void set(parameter_type<Type> newValue, bool sync = false) {
|
|
|
|
if (_value != newValue) {
|
|
|
|
setForced(newValue, sync);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Callback>
|
|
|
|
void process(Callback callback, bool sync = false) {
|
|
|
|
callback(_value);
|
2017-03-04 19:36:59 +00:00
|
|
|
changed().notify(_value, sync);
|
2017-01-14 18:50:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 14:03:07 +00:00
|
|
|
Observable<Type> &changed() const {
|
2017-03-04 19:36:59 +00:00
|
|
|
return _changed;
|
2017-01-14 18:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Type _value;
|
2017-03-07 14:03:07 +00:00
|
|
|
mutable Observable<Type> _changed;
|
2017-01-14 18:50:16 +00:00
|
|
|
|
2016-10-14 18:27:42 +00:00
|
|
|
};
|
|
|
|
|
2016-08-18 19:27:43 +00:00
|
|
|
class Subscriber {
|
|
|
|
protected:
|
2016-09-26 14:50:24 +00:00
|
|
|
template <typename EventType, typename Handler, typename Lambda>
|
|
|
|
int subscribe(base::Observable<EventType, Handler> &observable, Lambda &&handler) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_subscriptions.push_back(observable.add_subscription(std::forward<Lambda>(handler)));
|
2016-10-14 17:10:15 +00:00
|
|
|
return _subscriptions.size();
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
|
|
|
|
2016-09-26 14:50:24 +00:00
|
|
|
template <typename EventType, typename Handler, typename Lambda>
|
|
|
|
int subscribe(base::Observable<EventType, Handler> *observable, Lambda &&handler) {
|
2017-02-21 13:45:56 +00:00
|
|
|
return subscribe(*observable, std::forward<Lambda>(handler));
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
|
|
|
|
2017-01-14 18:50:16 +00:00
|
|
|
template <typename Type, typename Lambda>
|
2017-03-07 14:03:07 +00:00
|
|
|
int subscribe(const base::Variable<Type> &variable, Lambda &&handler) {
|
2017-03-04 19:36:59 +00:00
|
|
|
return subscribe(variable.changed(), std::forward<Lambda>(handler));
|
2017-01-14 18:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Type, typename Lambda>
|
2017-03-07 14:03:07 +00:00
|
|
|
int subscribe(const base::Variable<Type> *variable, Lambda &&handler) {
|
2017-03-04 19:36:59 +00:00
|
|
|
return subscribe(variable->changed(), std::forward<Lambda>(handler));
|
2017-01-14 18:50:16 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 19:27:43 +00:00
|
|
|
void unsubscribe(int index) {
|
2016-10-14 17:10:15 +00:00
|
|
|
if (!index) return;
|
2017-02-21 14:37:53 +00:00
|
|
|
auto count = static_cast<int>(_subscriptions.size());
|
|
|
|
t_assert(index > 0 && index <= count);
|
2016-10-14 17:10:15 +00:00
|
|
|
_subscriptions[index - 1].destroy();
|
2017-02-21 14:37:53 +00:00
|
|
|
if (index == count) {
|
2016-10-14 17:10:15 +00:00
|
|
|
while (index > 0 && !_subscriptions[--index]) {
|
|
|
|
_subscriptions.pop_back();
|
|
|
|
}
|
|
|
|
}
|
2016-08-18 19:27:43 +00:00
|
|
|
}
|
|
|
|
|
2016-08-27 04:49:18 +00:00
|
|
|
~Subscriber() {
|
2016-10-07 16:45:45 +00:00
|
|
|
auto subscriptions = base::take(_subscriptions);
|
2016-08-27 04:49:18 +00:00
|
|
|
for (auto &subscription : subscriptions) {
|
|
|
|
subscription.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-18 19:27:43 +00:00
|
|
|
private:
|
2017-02-21 13:45:56 +00:00
|
|
|
std::vector<base::Subscription> _subscriptions;
|
2016-08-18 19:27:43 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
void HandleObservables();
|
|
|
|
|
|
|
|
} // namespace base
|