2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 06:35:14 +00:00

Simplified usage of Observer, new event types can be added easier.

This commit is contained in:
John Preston
2016-05-25 21:49:47 +03:00
parent 46ad43bb1e
commit 1c13556b8d
5 changed files with 230 additions and 178 deletions

View File

@@ -22,50 +22,69 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
#include "core/observer.h"
namespace Notify {
namespace internal {
namespace {
using StartCallbacksList = QVector<StartObservedEventCallback>;
using FinishCallbacksList = QVector<FinishObservedEventCallback>;
struct StartCallbackData {
void *that;
StartCallback call;
};
struct FinishCallbackData {
void *that;
FinishCallback call;
};
struct UnregisterCallbackData {
void *that;
UnregisterCallback call;
};
using StartCallbacksList = QVector<StartCallbackData>;
using FinishCallbacksList = QVector<FinishCallbackData>;
NeverFreedPointer<StartCallbacksList> StartCallbacks;
NeverFreedPointer<FinishCallbacksList> FinishCallbacks;
UnregisterObserverCallback UnregisterCallbacks[256]/* = { nullptr }*/;
UnregisterCallbackData UnregisterCallbacks[256]/* = { nullptr }*/;
ObservedEvent LastRegisteredEvent/* = 0*/;
} // namespace
} // namespace internal
void startObservers() {
if (!StartCallbacks) return;
if (!internal::StartCallbacks) return;
for (auto &callback : *StartCallbacks) {
callback();
for (auto &callback : *internal::StartCallbacks) {
callback.call(callback.that);
}
}
void finishObservers() {
if (!FinishCallbacks) return;
if (!internal::FinishCallbacks) return;
for (auto &callback : *FinishCallbacks) {
callback();
for (auto &callback : *internal::FinishCallbacks) {
callback.call(callback.that);
}
StartCallbacks.clear();
FinishCallbacks.clear();
internal::StartCallbacks.clear();
internal::FinishCallbacks.clear();
}
ObservedEventRegistrator::ObservedEventRegistrator(StartObservedEventCallback startCallback
, FinishObservedEventCallback finishCallback
, UnregisterObserverCallback unregisterCallback) {
namespace internal {
BaseObservedEventRegistrator::BaseObservedEventRegistrator(void *that
, StartCallback startCallback
, FinishCallback finishCallback
, UnregisterCallback unregisterCallback) {
_event = LastRegisteredEvent++;
StartCallbacks.makeIfNull();
StartCallbacks->push_back(startCallback);
StartCallbacks->push_back({ that, startCallback });
FinishCallbacks.makeIfNull();
FinishCallbacks->push_back(finishCallback);
FinishCallbacks->push_back({ that, finishCallback });
UnregisterCallbacks[_event] = unregisterCallback;
UnregisterCallbacks[_event] = { that, unregisterCallback };
}
} // namespace internal
// Observer base interface.
Observer::~Observer() {
for_const (auto connection, _connections) {
@@ -78,10 +97,11 @@ void Observer::observerRegistered(ConnectionId connection) {
}
void unregisterObserver(ConnectionId connection) {
auto event = static_cast<ObservedEvent>(connection >> 24);
auto event = static_cast<internal::ObservedEvent>(connection >> 24);
auto connectionIndex = int(connection & 0x00FFFFFFU) - 1;
if (connectionIndex >= 0 && UnregisterCallbacks[event]) {
UnregisterCallbacks[event](connectionIndex);
auto &callback = internal::UnregisterCallbacks[event];
if (connectionIndex >= 0 && callback.call && callback.that) {
callback.call(callback.that, connectionIndex);
}
}

View File

@@ -24,7 +24,6 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
namespace Notify {
using ObservedEvent = uchar;
using ConnectionId = uint32;
// startObservers() must be called after main() started (not in a global variable constructor).
@@ -33,19 +32,23 @@ void startObservers();
void finishObservers();
using StartObservedEventCallback = void(*)();
using UnregisterObserverCallback = void(*)(int connectionIndex);
using FinishObservedEventCallback = void(*)();
// Objects of this class should be constructed in global scope.
// startCallback will be called from Notify::startObservers().
// finishCallback will be called from Notify::finishObservers().
// unregisterCallback will be used to destroy connections.
class ObservedEventRegistrator {
public:
ObservedEventRegistrator(StartObservedEventCallback startCallback,
FinishObservedEventCallback finishCallback,
UnregisterObserverCallback unregisterCallback);
namespace internal {
using ObservedEvent = uchar;
using StartCallback = void(*)(void*);
using FinishCallback = void(*)(void*);
using UnregisterCallback = void(*)(void*,int connectionIndex);
class BaseObservedEventRegistrator {
public:
BaseObservedEventRegistrator(void *that
, StartCallback startCallback
, FinishCallback finishCallback
, UnregisterCallback unregisterCallback);
protected:
inline ObservedEvent event() const {
return _event;
}
@@ -55,6 +58,138 @@ private:
};
// Handler is one of Function<> instantiations.
template <typename Flags, typename Handler>
struct ObserversList {
struct Entry {
Flags flags;
Handler handler;
};
std_::vector_of_moveable<Entry> entries;
QVector<int> freeIndices;
};
// If no filtering by flags is done, you can use Flags=int and this value.
constexpr int UniversalFlag = 0x01;
} // namespace internal
// Objects of this class should be constructed in global scope.
// startCallback will be called from Notify::startObservers().
// finishCallback will be called from Notify::finishObservers().
template <typename Flags, typename Handler>
class ObservedEventRegistrator : public internal::BaseObservedEventRegistrator {
public:
ObservedEventRegistrator(StartObservedEventCallback startCallback,
FinishObservedEventCallback finishCallback) : internal::BaseObservedEventRegistrator(reinterpret_cast<void*>(this),
ObservedEventRegistrator<Flags, Handler>::start,
ObservedEventRegistrator<Flags, Handler>::finish,
ObservedEventRegistrator<Flags, Handler>::unregister)
, _startCallback(startCallback), _finishCallback(finishCallback) {
}
bool started() const {
return _list != nullptr;
}
ConnectionId registerObserver(Flags flags, Handler &&handler) {
t_assert(started());
int connectionIndex = doRegisterObserver(flags, std_::forward<Handler>(handler));
return (static_cast<uint32>(event()) << 24) | static_cast<uint32>(connectionIndex + 1);
}
template <typename... Args>
void notify(Flags flags, Args&&... args) {
t_assert(started());
for (auto &entry : _list->entries) {
if (!entry.handler.isNull() && (flags & entry.flags)) {
entry.handler.call(std_::forward<Args>(args)...);
}
}
}
private:
using Self = ObservedEventRegistrator<Flags, Handler>;
static void start(void *vthat) {
Self *that = reinterpret_cast<Self*>(vthat);
t_assert(!that->started());
if (that->_startCallback) that->_startCallback();
that->_list = new internal::ObserversList<Flags, Handler>();
}
static void finish(void *vthat) {
Self *that = reinterpret_cast<Self*>(vthat);
if (that->_finishCallback) that->_finishCallback();
delete that->_list;
that->_list = nullptr;
}
static void unregister(void *vthat, int connectionIndex) {
Self *that = reinterpret_cast<Self*>(vthat);
t_assert(that->started());
auto &entries(that->_list->entries);
if (entries.size() <= connectionIndex) return;
if (entries.size() == connectionIndex + 1) {
for (entries.pop_back(); !entries.isEmpty() && entries.back().handler.isNull();) {
entries.pop_back();
}
} else {
entries[connectionIndex].handler = Handler();
that->_list->freeIndices.push_back(connectionIndex);
}
}
int doRegisterObserver(Flags flags, Handler &&handler) {
while (!_list->freeIndices.isEmpty()) {
auto freeIndex = _list->freeIndices.back();
_list->freeIndices.pop_back();
if (freeIndex < _list->entries.size()) {
_list->entries[freeIndex] = { flags, std_::move(handler) };
return freeIndex;
}
}
_list->entries.push_back({ flags, std_::move(handler) });
return _list->entries.size() - 1;
}
StartObservedEventCallback _startCallback;
FinishObservedEventCallback _finishCallback;
internal::ObserversList<Flags, Handler> *_list = nullptr;
};
// If no filtering of notifications by Flags is intended use this class.
template <typename Handler>
class SimpleObservedEventRegistrator {
public:
SimpleObservedEventRegistrator(StartObservedEventCallback startCallback,
FinishObservedEventCallback finishCallback) : _implementation(startCallback, finishCallback) {
}
bool started() const {
return _implementation.started();
}
ConnectionId registerObserver(Handler &&handler) {
return _implementation.registerObserver(internal::UniversalFlag, std_::forward<Handler>(handler));
}
template <typename... Args>
void notify(Args&&... args) {
return _implementation.notify(internal::UniversalFlag, std_::forward<Args>(args)...);
}
private:
ObservedEventRegistrator<int, Handler> _implementation;
};
// Each observer type should have observerRegistered(Notify::ConnectionId connection) method.
// Usually it is done by deriving the type from the Notify::Observer base class.
// In destructor it should call Notify::unregisterObserver(connection) for all the connections.
@@ -78,66 +213,6 @@ private:
};
inline ConnectionId observerConnectionId(ObservedEvent event, int connectionIndex) {
t_assert(connectionIndex >= 0 && connectionIndex < 0x01000000);
return (static_cast<uint32>(event) << 24) | (connectionIndex + 1);
}
// Handler is one of Function<> instantiations.
template <typename Flags, typename Handler>
struct ObserversList {
struct Entry {
Flags flags;
Handler handler;
};
std_::vector_of_moveable<Entry> entries;
QVector<int> freeIndices;
};
// If no filtering by flags is done, you can use this value in both
// Notify::registerObserver() and Notify::notifyObservers()
constexpr int UniversalFlag = 0x01;
template <typename Flags, typename Handler>
ConnectionId registerObserver(ObservedEvent event, ObserversList<Flags, Handler> &list, Flags flags, Handler &&handler) {
while (!list.freeIndices.isEmpty()) {
auto freeIndex = list.freeIndices.back();
list.freeIndices.pop_back();
if (freeIndex < list.entries.size()) {
list.entries[freeIndex] = { flags, std_::move(handler) };
return freeIndex;
}
}
list.entries.push_back({ flags, std_::move(handler) });
int connectionIndex = list.entries.size() - 1;
return (static_cast<uint32>(event) << 24) | static_cast<uint32>(connectionIndex + 1);
}
template <typename Flags, typename Handler>
void unregisterObserver(ObserversList<Flags, Handler> &list, int connectionIndex) {
auto &entries(list.entries);
if (entries.size() <= connectionIndex) return;
if (entries.size() == connectionIndex + 1) {
for (entries.pop_back(); !entries.isEmpty() && entries.back().handler.isNull();) {
entries.pop_back();
}
} else {
entries[connectionIndex].handler = Handler();
list.freeIndices.push_back(connectionIndex);
}
}
template <typename Flags, typename Handler, typename... Args>
void notifyObservers(ObserversList<Flags, Handler> &list, Flags flags, Args&&... args) {
for (auto &entry : list.entries) {
if (!entry.handler.isNull() && (flags & entry.flags)) {
entry.handler.call(std_::forward<Args>(args)...);
}
}
}
namespace internal {
template <typename ObserverType, int>