mirror of
https://github.com/telegramdesktop/tdesktop
synced 2025-01-22 07:14:09 +00:00
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"
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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>
|
||||
|
@ -1100,38 +1100,18 @@ namespace {
|
||||
|
||||
using internal::ImageLoadedHandler;
|
||||
|
||||
using ImageLoadedObserversList = Notify::ObserversList<int, ImageLoadedHandler>;
|
||||
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();
|
||||
}
|
||||
Notify::SimpleObservedEventRegistrator<ImageLoadedHandler> creator(nullptr, nullptr);
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace internal {
|
||||
|
||||
Notify::ConnectionId plainRegisterImageLoadedObserver(ImageLoadedHandler &&handler) {
|
||||
t_assert(Started());
|
||||
auto connectionId = Notify::registerObserver(creator.event(), *ImageLoadedObservers
|
||||
, Notify::UniversalFlag, std_::forward<ImageLoadedHandler>(handler));
|
||||
return connectionId;
|
||||
return creator.registerObserver(std_::forward<ImageLoadedHandler>(handler));
|
||||
}
|
||||
|
||||
void notifyImageLoaded() {
|
||||
Notify::notifyObservers(*ImageLoadedObservers, Notify::UniversalFlag);
|
||||
creator.notify();
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
@ -29,11 +29,9 @@ void emitPeerUpdated();
|
||||
} // namespace App
|
||||
|
||||
namespace Notify {
|
||||
namespace internal {
|
||||
namespace {
|
||||
|
||||
using PeerObserversList = ObserversList<PeerUpdateFlags, PeerUpdateHandler>;
|
||||
NeverFreedPointer<PeerObserversList> PeerUpdateObservers;
|
||||
using internal::PeerUpdateHandler;
|
||||
|
||||
using SmallUpdatesList = QVector<PeerUpdate>;
|
||||
NeverFreedPointer<SmallUpdatesList> SmallUpdates;
|
||||
@ -41,34 +39,25 @@ using AllUpdatesList = QMap<PeerData*, PeerUpdate>;
|
||||
NeverFreedPointer<AllUpdatesList> AllUpdates;
|
||||
|
||||
void StartCallback() {
|
||||
PeerUpdateObservers.makeIfNull();
|
||||
SmallUpdates.makeIfNull();
|
||||
AllUpdates.makeIfNull();
|
||||
}
|
||||
void FinishCallback() {
|
||||
PeerUpdateObservers.clear();
|
||||
SmallUpdates.clear();
|
||||
AllUpdates.clear();
|
||||
}
|
||||
void UnregisterCallback(int connectionIndex) {
|
||||
t_assert(!PeerUpdateObservers.isNull());
|
||||
unregisterObserver(*PeerUpdateObservers, connectionIndex);
|
||||
}
|
||||
ObservedEventRegistrator creator(StartCallback, FinishCallback, UnregisterCallback);
|
||||
|
||||
bool Started() {
|
||||
return !PeerUpdateObservers.isNull();
|
||||
}
|
||||
ObservedEventRegistrator<PeerUpdateFlags, PeerUpdateHandler> creator(StartCallback, FinishCallback);
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace internal {
|
||||
|
||||
ConnectionId plainRegisterPeerObserver(PeerUpdateFlags events, PeerUpdateHandler &&handler) {
|
||||
t_assert(Started());
|
||||
auto connectionId = registerObserver(creator.event(), *PeerUpdateObservers
|
||||
, events, std_::forward<PeerUpdateHandler>(handler));
|
||||
return connectionId;
|
||||
return creator.registerObserver(events, std_::forward<PeerUpdateHandler>(handler));
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
void mergePeerUpdate(PeerUpdate &mergeTo, const PeerUpdate &mergeFrom) {
|
||||
if (!(mergeTo.flags & PeerUpdateFlag::NameChanged)) {
|
||||
if (mergeFrom.flags & PeerUpdateFlag::NameChanged) {
|
||||
@ -79,53 +68,51 @@ void mergePeerUpdate(PeerUpdate &mergeTo, const PeerUpdate &mergeFrom) {
|
||||
mergeTo.flags |= mergeFrom.flags;
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
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) {
|
||||
auto &existingUpdate = (*internal::SmallUpdates)[i];
|
||||
auto &existingUpdate = (*SmallUpdates)[i];
|
||||
if (existingUpdate.peer == update.peer) {
|
||||
internal::mergePeerUpdate(existingUpdate, update);
|
||||
mergePeerUpdate(existingUpdate, update);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (internal::AllUpdates->isEmpty()) {
|
||||
if (AllUpdates->isEmpty()) {
|
||||
if (existingUpdatesCount < 5) {
|
||||
internal::SmallUpdates->push_back(update);
|
||||
SmallUpdates->push_back(update);
|
||||
} else {
|
||||
internal::AllUpdates->insert(update.peer, update);
|
||||
AllUpdates->insert(update.peer, update);
|
||||
}
|
||||
} else {
|
||||
auto it = internal::AllUpdates->find(update.peer);
|
||||
if (it != internal::AllUpdates->cend()) {
|
||||
internal::mergePeerUpdate(it.value(), update);
|
||||
auto it = AllUpdates->find(update.peer);
|
||||
if (it != AllUpdates->cend()) {
|
||||
mergePeerUpdate(it.value(), update);
|
||||
return;
|
||||
}
|
||||
internal::AllUpdates->insert(update.peer, update);
|
||||
AllUpdates->insert(update.peer, update);
|
||||
}
|
||||
}
|
||||
|
||||
void peerUpdatedSendDelayed() {
|
||||
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 allList = createAndSwap(*internal::AllUpdates);
|
||||
auto smallList = createAndSwap(*SmallUpdates);
|
||||
auto allList = createAndSwap(*AllUpdates);
|
||||
for_const (auto &update, smallList) {
|
||||
notifyObservers(*internal::PeerUpdateObservers, update.flags, update);
|
||||
creator.notify(update.flags, update);
|
||||
}
|
||||
for_const (auto &update, allList) {
|
||||
notifyObservers(*internal::PeerUpdateObservers, update.flags, update);
|
||||
creator.notify(update.flags, update);
|
||||
}
|
||||
if (internal::SmallUpdates->isEmpty()) {
|
||||
std::swap(smallList, *internal::SmallUpdates);
|
||||
internal::SmallUpdates->resize(0);
|
||||
if (SmallUpdates->isEmpty()) {
|
||||
std::swap(smallList, *SmallUpdates);
|
||||
SmallUpdates->resize(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -240,9 +240,6 @@ namespace {
|
||||
|
||||
using internal::QueryUpdateHandler;
|
||||
|
||||
using QueryObserversList = Notify::ObserversList<int, QueryUpdateHandler>;
|
||||
NeverFreedPointer<QueryObserversList> QueryUpdateObservers;
|
||||
|
||||
struct Query {
|
||||
enum class Type {
|
||||
ReadFile,
|
||||
@ -268,55 +265,51 @@ using QueryList = QList<Query>;
|
||||
NeverFreedPointer<QueryList> Queries;
|
||||
|
||||
void StartCallback() {
|
||||
QueryUpdateObservers.makeIfNull();
|
||||
Queries.makeIfNull();
|
||||
}
|
||||
|
||||
void FinishCallback() {
|
||||
QueryUpdateObservers.clear();
|
||||
Queries.clear();
|
||||
}
|
||||
void UnregisterCallback(int connectionIndex) {
|
||||
t_assert(!QueryUpdateObservers.isNull());
|
||||
Notify::unregisterObserver(*QueryUpdateObservers, connectionIndex);
|
||||
}
|
||||
Notify::ObservedEventRegistrator creator(StartCallback, FinishCallback, UnregisterCallback);
|
||||
|
||||
bool Started() {
|
||||
return !QueryUpdateObservers.isNull();
|
||||
}
|
||||
Notify::SimpleObservedEventRegistrator<QueryUpdateHandler> creator(StartCallback, FinishCallback);
|
||||
|
||||
} // namespace
|
||||
|
||||
QueryId queryReadFile(const QString &caption, const QString &filter) {
|
||||
t_assert(Started());
|
||||
t_assert(creator.started());
|
||||
|
||||
Queries->push_back(Query(Query::Type::ReadFile, caption, filter));
|
||||
Global::RefHandleFileDialogQueue().call();
|
||||
return Queries->back().id;
|
||||
}
|
||||
|
||||
QueryId queryReadFiles(const QString &caption, const QString &filter) {
|
||||
t_assert(Started());
|
||||
t_assert(creator.started());
|
||||
|
||||
Queries->push_back(Query(Query::Type::ReadFiles, caption, filter));
|
||||
Global::RefHandleFileDialogQueue().call();
|
||||
return Queries->back().id;
|
||||
}
|
||||
|
||||
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));
|
||||
Global::RefHandleFileDialogQueue().call();
|
||||
return Queries->back().id;
|
||||
}
|
||||
|
||||
QueryId queryReadFolder(const QString &caption) {
|
||||
t_assert(Started());
|
||||
t_assert(creator.started());
|
||||
|
||||
Queries->push_back(Query(Query::Type::ReadFolder, caption));
|
||||
Global::RefHandleFileDialogQueue().call();
|
||||
return Queries->back().id;
|
||||
}
|
||||
|
||||
bool processQuery() {
|
||||
if (!Started() || !Global::started() || Queries->isEmpty()) return false;
|
||||
if (!creator.started() || !Global::started() || Queries->isEmpty()) return false;
|
||||
|
||||
auto query = Queries->front();
|
||||
Queries->pop_front();
|
||||
@ -364,19 +357,16 @@ bool processQuery() {
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
namespace internal {
|
||||
|
||||
Notify::ConnectionId plainRegisterObserver(QueryUpdateHandler &&handler) {
|
||||
t_assert(Started());
|
||||
auto connectionId = Notify::registerObserver(creator.event(), *QueryUpdateObservers
|
||||
, Notify::UniversalFlag, std_::forward<QueryUpdateHandler>(handler));
|
||||
return connectionId;
|
||||
return creator.registerObserver(std_::forward<QueryUpdateHandler>(handler));
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
Loading…
Reference in New Issue
Block a user