refactor: allow exit event to provide an exit code
This commit is contained in:
parent
71c25b3ab9
commit
9025ab8fb7
10 changed files with 45 additions and 15 deletions
|
|
@ -21,6 +21,20 @@ public:
|
|||
virtual ~EventData() = default;
|
||||
};
|
||||
|
||||
class ExitEventData : public EventData
|
||||
{
|
||||
public:
|
||||
ExitEventData(int exitCode) : m_exitCode(exitCode) {};
|
||||
virtual ~ExitEventData() = default;
|
||||
int exitCode()
|
||||
{
|
||||
return m_exitCode;
|
||||
}
|
||||
|
||||
private:
|
||||
int m_exitCode;
|
||||
};
|
||||
|
||||
//! Event
|
||||
/*!
|
||||
\c Event holds an event type and a pointer to event data. It is movable, but not copyable
|
||||
|
|
@ -77,7 +91,6 @@ public:
|
|||
switch (event.getType()) {
|
||||
using enum EventTypes;
|
||||
case Unknown:
|
||||
case Quit:
|
||||
case System:
|
||||
case Timer:
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@
|
|||
#include "base/EventQueueTimer.h"
|
||||
#include "base/Log.h"
|
||||
#include "base/SimpleEventQueueBuffer.h"
|
||||
#include "common/ExitCodes.h"
|
||||
#include "mt/Lock.h"
|
||||
#include "mt/Mutex.h"
|
||||
|
||||
|
|
@ -44,7 +45,7 @@ EventQueue::~EventQueue()
|
|||
ARCH->setSignalHandler(Arch::ThreadSignal::Terminate, nullptr, nullptr);
|
||||
}
|
||||
|
||||
void EventQueue::loop()
|
||||
int EventQueue::loop()
|
||||
{
|
||||
m_buffer->init();
|
||||
{
|
||||
|
|
@ -67,6 +68,12 @@ void EventQueue::loop()
|
|||
Event::deleteData(event);
|
||||
getEvent(event);
|
||||
}
|
||||
int exitCode = s_exitSuccess;
|
||||
auto *exitEvent = dynamic_cast<ExitEventData *>(event.getDataObject());
|
||||
if (exitEvent != nullptr) {
|
||||
exitCode = exitEvent->exitCode();
|
||||
}
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
void EventQueue::adoptBuffer(IEventQueueBuffer *buffer)
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ public:
|
|||
EventQueue &operator=(EventQueue &&) = delete;
|
||||
|
||||
// IEventQueue overrides
|
||||
void loop() override;
|
||||
int loop() override;
|
||||
void adoptBuffer(IEventQueueBuffer *) override;
|
||||
bool getEvent(Event &event, double timeout = -1.0) override;
|
||||
bool dispatchEvent(const Event &event) override;
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ public:
|
|||
/*!
|
||||
Dequeues and dispatches events until the kQuit event is found.
|
||||
*/
|
||||
virtual void loop() = 0;
|
||||
virtual int loop() = 0;
|
||||
|
||||
//! Set the buffer
|
||||
/*!
|
||||
|
|
|
|||
|
|
@ -12,9 +12,9 @@
|
|||
#include "base/Log.h"
|
||||
#include "base/LogOutputters.h"
|
||||
#include "common/ExitCodes.h"
|
||||
#include "common/PlatformInfo.h"
|
||||
#include "common/Settings.h"
|
||||
#include "deskflow/DeskflowException.h"
|
||||
#include "mt/ThreadException.h"
|
||||
|
||||
#if defined(Q_OS_WIN)
|
||||
#include "base/IEventQueue.h"
|
||||
|
|
@ -170,5 +170,8 @@ void App::handleScreenError() const
|
|||
|
||||
void App::runEventsLoop(const void *)
|
||||
{
|
||||
m_events->loop();
|
||||
int exitCode = m_events->loop();
|
||||
if (exitCode != s_exitSuccess) {
|
||||
throw ThreadExitException(new LoopErrorCode(exitCode));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -121,6 +121,14 @@ public:
|
|||
protected:
|
||||
void runEventsLoop(const void *);
|
||||
|
||||
struct LoopErrorCode
|
||||
{
|
||||
int m_errorCode;
|
||||
LoopErrorCode(int errorCode) : m_errorCode(errorCode)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
void (*m_bye)(int);
|
||||
IEventQueue *m_events = nullptr;
|
||||
|
|
|
|||
|
|
@ -327,15 +327,14 @@ int ClientApp::mainLoop()
|
|||
// run event loop. if startClient() failed we're supposed to retry
|
||||
// later. the timer installed by startClient() will take care of
|
||||
// that.
|
||||
|
||||
getEvents()->loop();
|
||||
int exitCode = getEvents()->loop();
|
||||
|
||||
// close down
|
||||
LOG_DEBUG("stopping client");
|
||||
stopClient();
|
||||
LOG_NOTE("stopped client");
|
||||
|
||||
return s_exitSuccess;
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
int ClientApp::start()
|
||||
|
|
|
|||
|
|
@ -539,8 +539,7 @@ int ServerApp::mainLoop()
|
|||
// run event loop. if startServer() failed we're supposed to retry
|
||||
// later. the timer installed by startServer() will take care of
|
||||
// that.
|
||||
|
||||
getEvents()->loop();
|
||||
int exitCode = getEvents()->loop();
|
||||
|
||||
// close down
|
||||
LOG_DEBUG("stopping server");
|
||||
|
|
@ -549,7 +548,7 @@ int ServerApp::mainLoop()
|
|||
cleanupServer();
|
||||
LOG_NOTE("stopped server");
|
||||
|
||||
return s_exitSuccess;
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
void ServerApp::resetServer()
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "base/IEventQueue.h"
|
||||
#include "common/ExitCodes.h"
|
||||
|
||||
class MockEventQueue : public IEventQueue
|
||||
{
|
||||
|
|
@ -27,9 +28,9 @@ public:
|
|||
return true;
|
||||
}
|
||||
|
||||
void loop() override
|
||||
int loop() override
|
||||
{
|
||||
// do nothing
|
||||
return s_exitSuccess;
|
||||
}
|
||||
|
||||
void adoptBuffer(IEventQueueBuffer *) override
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@
|
|||
class MockEventQueue : public IEventQueue
|
||||
{
|
||||
public:
|
||||
MOCK_METHOD(void, loop, (), (override));
|
||||
MOCK_METHOD(int, loop, (), (override));
|
||||
MOCK_METHOD(EventQueueTimer *, newOneShotTimer, (double, void *), (override));
|
||||
MOCK_METHOD(EventQueueTimer *, newTimer, (double, void *), (override));
|
||||
MOCK_METHOD(bool, getEvent, (Event &, double), (override));
|
||||
|
|
|
|||
Loading…
Reference in a new issue