From 2785fa3e97722d0b92cd13aedc128426a8d7bd1d 2017-12-19 14:14:51 From: Alexandre Leroux Date: 2017-12-19 14:14:51 Subject: [PATCH] Wait for the end of an acquisition to validate an operation (1) Creating a synchronous waiting helper for sending a signal --- diff --git a/core/include/Common/SignalWaiter.h b/core/include/Common/SignalWaiter.h new file mode 100644 index 0000000..3dcc8f2 --- /dev/null +++ b/core/include/Common/SignalWaiter.h @@ -0,0 +1,38 @@ +#ifndef SCIQLOP_SIGNALWAITER_H +#define SCIQLOP_SIGNALWAITER_H + +#include "CoreGlobal.h" + +#include + +/** + * Class for synchronously waiting for the reception of a signal. The signal to wait is passed to + * the construction of the object. When starting the wait, a timeout can be set to exit if the + * signal has not been sent + */ +class SCIQLOP_CORE_EXPORT SignalWaiter : public QObject { + Q_OBJECT +public: + /** + * Ctor + * @param object the sender of the signal + * @param signal the signal to listen + */ + explicit SignalWaiter(QObject &sender, const char *signal); + + /** + * Starts the signal and leaves after the signal has been received, or after the timeout + * @param timeout the timeout set (if 0, uses a default timeout) + * @return true if the signal was sent, false if the timeout occured + */ + bool wait(int timeout); + +private: + bool m_Timeout; + QEventLoop m_EventLoop; + +private slots: + void timeout(); +}; + +#endif // SCIQLOP_SIGNALWAITER_H diff --git a/core/meson.build b/core/meson.build index cc49a89..8ce78de 100644 --- a/core/meson.build +++ b/core/meson.build @@ -5,6 +5,7 @@ catalogueapi_dep = dependency('CatalogueAPI', required : true, fallback:['Catalo core_moc_headers = [ 'include/Catalogue/CatalogueController.h', + 'include/Common/SignalWaiter.h', 'include/Data/IDataProvider.h', 'include/DataSource/DataSourceController.h', 'include/DataSource/DataSourceItemAction.h', @@ -24,6 +25,7 @@ core_moc_files = qt5.preprocess(moc_headers : core_moc_headers) core_sources = [ 'src/Common/DateUtils.cpp', + 'src/Common/SignalWaiter.cpp', 'src/Common/StringUtils.cpp', 'src/Common/MimeTypesDef.cpp', 'src/Catalogue/CatalogueController.cpp', diff --git a/core/src/Common/SignalWaiter.cpp b/core/src/Common/SignalWaiter.cpp new file mode 100644 index 0000000..73c1f59 --- /dev/null +++ b/core/src/Common/SignalWaiter.cpp @@ -0,0 +1,36 @@ +#include "Common/SignalWaiter.h" + +#include + +namespace { + +const auto DEFAULT_TIMEOUT = 30000; + +} // namespace + +SignalWaiter::SignalWaiter(QObject &sender, const char *signal) : m_Timeout{false} +{ + connect(&sender, signal, &m_EventLoop, SLOT(quit())); +} + +bool SignalWaiter::wait(int timeout) +{ + if (timeout == 0) { + timeout = DEFAULT_TIMEOUT; + } + + QTimer timer{}; + timer.setInterval(timeout); + timer.start(); + connect(&timer, &QTimer::timeout, this, &SignalWaiter::timeout); + + m_EventLoop.exec(); + + return !m_Timeout; +} + +void SignalWaiter::timeout() +{ + m_Timeout = true; + m_EventLoop.quit(); +} diff --git a/plugins/amda/tests/TestAmdaFuzzing.cpp b/plugins/amda/tests/TestAmdaFuzzing.cpp index 8d82b0b..92b751b 100644 --- a/plugins/amda/tests/TestAmdaFuzzing.cpp +++ b/plugins/amda/tests/TestAmdaFuzzing.cpp @@ -5,6 +5,7 @@ #include "AmdaProvider.h" +#include #include #include #include