mirror of https://github.com/procxx/kepka.git
Simplified usage of Observer, new event types can be added easier.
This commit is contained in:
parent
46ad43bb1e
commit
1c13556b8d
|
@ -22,50 +22,69 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
||||||
#include "core/observer.h"
|
#include "core/observer.h"
|
||||||
|
|
||||||
namespace Notify {
|
namespace Notify {
|
||||||
|
namespace internal {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
using StartCallbacksList = QVector<StartObservedEventCallback>;
|
struct StartCallbackData {
|
||||||
using FinishCallbacksList = QVector<FinishObservedEventCallback>;
|
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<StartCallbacksList> StartCallbacks;
|
||||||
NeverFreedPointer<FinishCallbacksList> FinishCallbacks;
|
NeverFreedPointer<FinishCallbacksList> FinishCallbacks;
|
||||||
UnregisterObserverCallback UnregisterCallbacks[256]/* = { nullptr }*/;
|
UnregisterCallbackData UnregisterCallbacks[256]/* = { nullptr }*/;
|
||||||
|
|
||||||
ObservedEvent LastRegisteredEvent/* = 0*/;
|
ObservedEvent LastRegisteredEvent/* = 0*/;
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
} // namespace internal
|
||||||
|
|
||||||
void startObservers() {
|
void startObservers() {
|
||||||
if (!StartCallbacks) return;
|
if (!internal::StartCallbacks) return;
|
||||||
|
|
||||||
for (auto &callback : *StartCallbacks) {
|
for (auto &callback : *internal::StartCallbacks) {
|
||||||
callback();
|
callback.call(callback.that);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void finishObservers() {
|
void finishObservers() {
|
||||||
if (!FinishCallbacks) return;
|
if (!internal::FinishCallbacks) return;
|
||||||
|
|
||||||
for (auto &callback : *FinishCallbacks) {
|
for (auto &callback : *internal::FinishCallbacks) {
|
||||||
callback();
|
callback.call(callback.that);
|
||||||
}
|
}
|
||||||
StartCallbacks.clear();
|
internal::StartCallbacks.clear();
|
||||||
FinishCallbacks.clear();
|
internal::FinishCallbacks.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
ObservedEventRegistrator::ObservedEventRegistrator(StartObservedEventCallback startCallback
|
namespace internal {
|
||||||
, FinishObservedEventCallback finishCallback
|
|
||||||
, UnregisterObserverCallback unregisterCallback) {
|
BaseObservedEventRegistrator::BaseObservedEventRegistrator(void *that
|
||||||
|
, StartCallback startCallback
|
||||||
|
, FinishCallback finishCallback
|
||||||
|
, UnregisterCallback unregisterCallback) {
|
||||||
_event = LastRegisteredEvent++;
|
_event = LastRegisteredEvent++;
|
||||||
|
|
||||||
StartCallbacks.makeIfNull();
|
StartCallbacks.makeIfNull();
|
||||||
StartCallbacks->push_back(startCallback);
|
StartCallbacks->push_back({ that, startCallback });
|
||||||
|
|
||||||
FinishCallbacks.makeIfNull();
|
FinishCallbacks.makeIfNull();
|
||||||
FinishCallbacks->push_back(finishCallback);
|
FinishCallbacks->push_back({ that, finishCallback });
|
||||||
|
|
||||||
UnregisterCallbacks[_event] = unregisterCallback;
|
UnregisterCallbacks[_event] = { that, unregisterCallback };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace internal
|
||||||
|
|
||||||
// Observer base interface.
|
// Observer base interface.
|
||||||
Observer::~Observer() {
|
Observer::~Observer() {
|
||||||
for_const (auto connection, _connections) {
|
for_const (auto connection, _connections) {
|
||||||
|
@ -78,10 +97,11 @@ void Observer::observerRegistered(ConnectionId connection) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void unregisterObserver(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;
|
auto connectionIndex = int(connection & 0x00FFFFFFU) - 1;
|
||||||
if (connectionIndex >= 0 && UnregisterCallbacks[event]) {
|
auto &callback = internal::UnregisterCallbacks[event];
|
||||||
UnregisterCallbacks[event](connectionIndex);
|
if (connectionIndex >= 0 && callback.call && callback.that) {
|
||||||
|
callback.call(callback.that, connectionIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,6 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
||||||
|
|
||||||
namespace Notify {
|
namespace Notify {
|
||||||
|
|
||||||
using ObservedEvent = uchar;
|
|
||||||
using ConnectionId = uint32;
|
using ConnectionId = uint32;
|
||||||
|
|
||||||
// startObservers() must be called after main() started (not in a global variable constructor).
|
// startObservers() must be called after main() started (not in a global variable constructor).
|
||||||
|
@ -33,19 +32,23 @@ void startObservers();
|
||||||
void finishObservers();
|
void finishObservers();
|
||||||
|
|
||||||
using StartObservedEventCallback = void(*)();
|
using StartObservedEventCallback = void(*)();
|
||||||
using UnregisterObserverCallback = void(*)(int connectionIndex);
|
|
||||||
using FinishObservedEventCallback = void(*)();
|
using FinishObservedEventCallback = void(*)();
|
||||||
|
|
||||||
// Objects of this class should be constructed in global scope.
|
namespace internal {
|
||||||
// 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);
|
|
||||||
|
|
||||||
|
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 {
|
inline ObservedEvent event() const {
|
||||||
return _event;
|
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.
|
// Each observer type should have observerRegistered(Notify::ConnectionId connection) method.
|
||||||
// Usually it is done by deriving the type from the Notify::Observer base class.
|
// 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.
|
// 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 {
|
namespace internal {
|
||||||
|
|
||||||
template <typename ObserverType, int>
|
template <typename ObserverType, int>
|
||||||
|
|
|
@ -1100,38 +1100,18 @@ namespace {
|
||||||
|
|
||||||
using internal::ImageLoadedHandler;
|
using internal::ImageLoadedHandler;
|
||||||
|
|
||||||
using ImageLoadedObserversList = Notify::ObserversList<int, ImageLoadedHandler>;
|
Notify::SimpleObservedEventRegistrator<ImageLoadedHandler> creator(nullptr, nullptr);
|
||||||
NeverFreedPointer<ImageLoadedObserversList> ImageLoadedObservers;
|
|
||||||
|
|
||||||
void StartCallback() {
|
|
||||||
ImageLoadedObservers.makeIfNull();
|
|
||||||
}
|
|
||||||
void FinishCallback() {
|
|
||||||
ImageLoadedObservers.clear();
|
|
||||||
}
|
|
||||||
void UnregisterCallback(int connectionIndex) {
|
|
||||||
t_assert(!ImageLoadedObservers.isNull());
|
|
||||||
Notify::unregisterObserver(*ImageLoadedObservers, connectionIndex);
|
|
||||||
}
|
|
||||||
Notify::ObservedEventRegistrator creator(StartCallback, FinishCallback, UnregisterCallback);
|
|
||||||
|
|
||||||
bool Started() {
|
|
||||||
return !ImageLoadedObservers.isNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
Notify::ConnectionId plainRegisterImageLoadedObserver(ImageLoadedHandler &&handler) {
|
Notify::ConnectionId plainRegisterImageLoadedObserver(ImageLoadedHandler &&handler) {
|
||||||
t_assert(Started());
|
return creator.registerObserver(std_::forward<ImageLoadedHandler>(handler));
|
||||||
auto connectionId = Notify::registerObserver(creator.event(), *ImageLoadedObservers
|
|
||||||
, Notify::UniversalFlag, std_::forward<ImageLoadedHandler>(handler));
|
|
||||||
return connectionId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void notifyImageLoaded() {
|
void notifyImageLoaded() {
|
||||||
Notify::notifyObservers(*ImageLoadedObservers, Notify::UniversalFlag);
|
creator.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
|
@ -29,11 +29,9 @@ void emitPeerUpdated();
|
||||||
} // namespace App
|
} // namespace App
|
||||||
|
|
||||||
namespace Notify {
|
namespace Notify {
|
||||||
namespace internal {
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
using PeerObserversList = ObserversList<PeerUpdateFlags, PeerUpdateHandler>;
|
using internal::PeerUpdateHandler;
|
||||||
NeverFreedPointer<PeerObserversList> PeerUpdateObservers;
|
|
||||||
|
|
||||||
using SmallUpdatesList = QVector<PeerUpdate>;
|
using SmallUpdatesList = QVector<PeerUpdate>;
|
||||||
NeverFreedPointer<SmallUpdatesList> SmallUpdates;
|
NeverFreedPointer<SmallUpdatesList> SmallUpdates;
|
||||||
|
@ -41,34 +39,25 @@ using AllUpdatesList = QMap<PeerData*, PeerUpdate>;
|
||||||
NeverFreedPointer<AllUpdatesList> AllUpdates;
|
NeverFreedPointer<AllUpdatesList> AllUpdates;
|
||||||
|
|
||||||
void StartCallback() {
|
void StartCallback() {
|
||||||
PeerUpdateObservers.makeIfNull();
|
|
||||||
SmallUpdates.makeIfNull();
|
SmallUpdates.makeIfNull();
|
||||||
AllUpdates.makeIfNull();
|
AllUpdates.makeIfNull();
|
||||||
}
|
}
|
||||||
void FinishCallback() {
|
void FinishCallback() {
|
||||||
PeerUpdateObservers.clear();
|
|
||||||
SmallUpdates.clear();
|
SmallUpdates.clear();
|
||||||
AllUpdates.clear();
|
AllUpdates.clear();
|
||||||
}
|
}
|
||||||
void UnregisterCallback(int connectionIndex) {
|
ObservedEventRegistrator<PeerUpdateFlags, PeerUpdateHandler> creator(StartCallback, FinishCallback);
|
||||||
t_assert(!PeerUpdateObservers.isNull());
|
|
||||||
unregisterObserver(*PeerUpdateObservers, connectionIndex);
|
|
||||||
}
|
|
||||||
ObservedEventRegistrator creator(StartCallback, FinishCallback, UnregisterCallback);
|
|
||||||
|
|
||||||
bool Started() {
|
|
||||||
return !PeerUpdateObservers.isNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
namespace internal {
|
||||||
|
|
||||||
ConnectionId plainRegisterPeerObserver(PeerUpdateFlags events, PeerUpdateHandler &&handler) {
|
ConnectionId plainRegisterPeerObserver(PeerUpdateFlags events, PeerUpdateHandler &&handler) {
|
||||||
t_assert(Started());
|
return creator.registerObserver(events, std_::forward<PeerUpdateHandler>(handler));
|
||||||
auto connectionId = registerObserver(creator.event(), *PeerUpdateObservers
|
|
||||||
, events, std_::forward<PeerUpdateHandler>(handler));
|
|
||||||
return connectionId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace internal
|
||||||
|
|
||||||
void mergePeerUpdate(PeerUpdate &mergeTo, const PeerUpdate &mergeFrom) {
|
void mergePeerUpdate(PeerUpdate &mergeTo, const PeerUpdate &mergeFrom) {
|
||||||
if (!(mergeTo.flags & PeerUpdateFlag::NameChanged)) {
|
if (!(mergeTo.flags & PeerUpdateFlag::NameChanged)) {
|
||||||
if (mergeFrom.flags & PeerUpdateFlag::NameChanged) {
|
if (mergeFrom.flags & PeerUpdateFlag::NameChanged) {
|
||||||
|
@ -79,53 +68,51 @@ void mergePeerUpdate(PeerUpdate &mergeTo, const PeerUpdate &mergeFrom) {
|
||||||
mergeTo.flags |= mergeFrom.flags;
|
mergeTo.flags |= mergeFrom.flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace internal
|
|
||||||
|
|
||||||
void peerUpdatedDelayed(const PeerUpdate &update) {
|
void peerUpdatedDelayed(const PeerUpdate &update) {
|
||||||
t_assert(internal::Started());
|
t_assert(creator.started());
|
||||||
|
|
||||||
int existingUpdatesCount = internal::SmallUpdates->size();
|
int existingUpdatesCount = SmallUpdates->size();
|
||||||
for (int i = 0; i < existingUpdatesCount; ++i) {
|
for (int i = 0; i < existingUpdatesCount; ++i) {
|
||||||
auto &existingUpdate = (*internal::SmallUpdates)[i];
|
auto &existingUpdate = (*SmallUpdates)[i];
|
||||||
if (existingUpdate.peer == update.peer) {
|
if (existingUpdate.peer == update.peer) {
|
||||||
internal::mergePeerUpdate(existingUpdate, update);
|
mergePeerUpdate(existingUpdate, update);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (internal::AllUpdates->isEmpty()) {
|
if (AllUpdates->isEmpty()) {
|
||||||
if (existingUpdatesCount < 5) {
|
if (existingUpdatesCount < 5) {
|
||||||
internal::SmallUpdates->push_back(update);
|
SmallUpdates->push_back(update);
|
||||||
} else {
|
} else {
|
||||||
internal::AllUpdates->insert(update.peer, update);
|
AllUpdates->insert(update.peer, update);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
auto it = internal::AllUpdates->find(update.peer);
|
auto it = AllUpdates->find(update.peer);
|
||||||
if (it != internal::AllUpdates->cend()) {
|
if (it != AllUpdates->cend()) {
|
||||||
internal::mergePeerUpdate(it.value(), update);
|
mergePeerUpdate(it.value(), update);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
internal::AllUpdates->insert(update.peer, update);
|
AllUpdates->insert(update.peer, update);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void peerUpdatedSendDelayed() {
|
void peerUpdatedSendDelayed() {
|
||||||
App::emitPeerUpdated();
|
App::emitPeerUpdated();
|
||||||
|
|
||||||
t_assert(internal::Started());
|
t_assert(creator.started());
|
||||||
|
|
||||||
if (internal::SmallUpdates->isEmpty()) return;
|
if (SmallUpdates->isEmpty()) return;
|
||||||
|
|
||||||
auto smallList = createAndSwap(*internal::SmallUpdates);
|
auto smallList = createAndSwap(*SmallUpdates);
|
||||||
auto allList = createAndSwap(*internal::AllUpdates);
|
auto allList = createAndSwap(*AllUpdates);
|
||||||
for_const (auto &update, smallList) {
|
for_const (auto &update, smallList) {
|
||||||
notifyObservers(*internal::PeerUpdateObservers, update.flags, update);
|
creator.notify(update.flags, update);
|
||||||
}
|
}
|
||||||
for_const (auto &update, allList) {
|
for_const (auto &update, allList) {
|
||||||
notifyObservers(*internal::PeerUpdateObservers, update.flags, update);
|
creator.notify(update.flags, update);
|
||||||
}
|
}
|
||||||
if (internal::SmallUpdates->isEmpty()) {
|
if (SmallUpdates->isEmpty()) {
|
||||||
std::swap(smallList, *internal::SmallUpdates);
|
std::swap(smallList, *SmallUpdates);
|
||||||
internal::SmallUpdates->resize(0);
|
SmallUpdates->resize(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -240,9 +240,6 @@ namespace {
|
||||||
|
|
||||||
using internal::QueryUpdateHandler;
|
using internal::QueryUpdateHandler;
|
||||||
|
|
||||||
using QueryObserversList = Notify::ObserversList<int, QueryUpdateHandler>;
|
|
||||||
NeverFreedPointer<QueryObserversList> QueryUpdateObservers;
|
|
||||||
|
|
||||||
struct Query {
|
struct Query {
|
||||||
enum class Type {
|
enum class Type {
|
||||||
ReadFile,
|
ReadFile,
|
||||||
|
@ -268,55 +265,51 @@ using QueryList = QList<Query>;
|
||||||
NeverFreedPointer<QueryList> Queries;
|
NeverFreedPointer<QueryList> Queries;
|
||||||
|
|
||||||
void StartCallback() {
|
void StartCallback() {
|
||||||
QueryUpdateObservers.makeIfNull();
|
|
||||||
Queries.makeIfNull();
|
Queries.makeIfNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
void FinishCallback() {
|
void FinishCallback() {
|
||||||
QueryUpdateObservers.clear();
|
|
||||||
Queries.clear();
|
Queries.clear();
|
||||||
}
|
}
|
||||||
void UnregisterCallback(int connectionIndex) {
|
|
||||||
t_assert(!QueryUpdateObservers.isNull());
|
|
||||||
Notify::unregisterObserver(*QueryUpdateObservers, connectionIndex);
|
|
||||||
}
|
|
||||||
Notify::ObservedEventRegistrator creator(StartCallback, FinishCallback, UnregisterCallback);
|
|
||||||
|
|
||||||
bool Started() {
|
Notify::SimpleObservedEventRegistrator<QueryUpdateHandler> creator(StartCallback, FinishCallback);
|
||||||
return !QueryUpdateObservers.isNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
QueryId queryReadFile(const QString &caption, const QString &filter) {
|
QueryId queryReadFile(const QString &caption, const QString &filter) {
|
||||||
t_assert(Started());
|
t_assert(creator.started());
|
||||||
|
|
||||||
Queries->push_back(Query(Query::Type::ReadFile, caption, filter));
|
Queries->push_back(Query(Query::Type::ReadFile, caption, filter));
|
||||||
Global::RefHandleFileDialogQueue().call();
|
Global::RefHandleFileDialogQueue().call();
|
||||||
return Queries->back().id;
|
return Queries->back().id;
|
||||||
}
|
}
|
||||||
|
|
||||||
QueryId queryReadFiles(const QString &caption, const QString &filter) {
|
QueryId queryReadFiles(const QString &caption, const QString &filter) {
|
||||||
t_assert(Started());
|
t_assert(creator.started());
|
||||||
|
|
||||||
Queries->push_back(Query(Query::Type::ReadFiles, caption, filter));
|
Queries->push_back(Query(Query::Type::ReadFiles, caption, filter));
|
||||||
Global::RefHandleFileDialogQueue().call();
|
Global::RefHandleFileDialogQueue().call();
|
||||||
return Queries->back().id;
|
return Queries->back().id;
|
||||||
}
|
}
|
||||||
|
|
||||||
QueryId queryWriteFile(const QString &caption, const QString &filter, const QString &filePath) {
|
QueryId queryWriteFile(const QString &caption, const QString &filter, const QString &filePath) {
|
||||||
t_assert(Started());
|
t_assert(creator.started());
|
||||||
|
|
||||||
Queries->push_back(Query(Query::Type::WriteFile, caption, filter, filePath));
|
Queries->push_back(Query(Query::Type::WriteFile, caption, filter, filePath));
|
||||||
Global::RefHandleFileDialogQueue().call();
|
Global::RefHandleFileDialogQueue().call();
|
||||||
return Queries->back().id;
|
return Queries->back().id;
|
||||||
}
|
}
|
||||||
|
|
||||||
QueryId queryReadFolder(const QString &caption) {
|
QueryId queryReadFolder(const QString &caption) {
|
||||||
t_assert(Started());
|
t_assert(creator.started());
|
||||||
|
|
||||||
Queries->push_back(Query(Query::Type::ReadFolder, caption));
|
Queries->push_back(Query(Query::Type::ReadFolder, caption));
|
||||||
Global::RefHandleFileDialogQueue().call();
|
Global::RefHandleFileDialogQueue().call();
|
||||||
return Queries->back().id;
|
return Queries->back().id;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool processQuery() {
|
bool processQuery() {
|
||||||
if (!Started() || !Global::started() || Queries->isEmpty()) return false;
|
if (!creator.started() || !Global::started() || Queries->isEmpty()) return false;
|
||||||
|
|
||||||
auto query = Queries->front();
|
auto query = Queries->front();
|
||||||
Queries->pop_front();
|
Queries->pop_front();
|
||||||
|
@ -364,19 +357,16 @@ bool processQuery() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// No one know what happened during filedialogGet*() call in the event loop.
|
// No one know what happened during filedialogGet*() call in the event loop.
|
||||||
if (!Started() || !Global::started()) return false;
|
if (!creator.started() || !Global::started()) return false;
|
||||||
|
|
||||||
Notify::notifyObservers(*QueryUpdateObservers, Notify::UniversalFlag, update);
|
creator.notify(update);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
Notify::ConnectionId plainRegisterObserver(QueryUpdateHandler &&handler) {
|
Notify::ConnectionId plainRegisterObserver(QueryUpdateHandler &&handler) {
|
||||||
t_assert(Started());
|
return creator.registerObserver(std_::forward<QueryUpdateHandler>(handler));
|
||||||
auto connectionId = Notify::registerObserver(creator.event(), *QueryUpdateObservers
|
|
||||||
, Notify::UniversalFlag, std_::forward<QueryUpdateHandler>(handler));
|
|
||||||
return connectionId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
Loading…
Reference in New Issue