#pragma once #include #include #include #include namespace async { /* * A result set specialization that provides a syncronous list. * * Only for testing purposes. * * WARNING: The nested eventloop can cause all sorts of trouble. Use only in testing code. */ template class SyncListResult : public QList { public: SyncListResult(const QSharedPointer > &emitter) :QList(), mComplete(false), mEmitter(emitter) { emitter->onAdded([this](const T &value) { this->append(value); }); emitter->onComplete([this]() { mComplete = true; if (eventLoopAborter) { eventLoopAborter(); //Be safe in case of a second invocation of the complete handler eventLoopAborter = std::function(); } }); emitter->onClear([this]() { this->clear(); }); } void exec() { QEventLoop eventLoop; eventLoopAborter = [&eventLoop]() { eventLoop.quit(); }; eventLoop.exec(); } private: bool mComplete; QSharedPointer > mEmitter; std::function eventLoopAborter; }; }