Index: sources/canbus/frameinterface.cpp =================================================================== diff -u -re1605219ac2baf49ef21d0889f845ac53d59c3c1 -r56d00a82669a7a2c00ab90109a89dbec8db27527 --- sources/canbus/frameinterface.cpp (.../frameinterface.cpp) (revision e1605219ac2baf49ef21d0889f845ac53d59c3c1) +++ sources/canbus/frameinterface.cpp (.../frameinterface.cpp) (revision 56d00a82669a7a2c00ab90109a89dbec8db27527) @@ -14,16 +14,13 @@ #include "frameinterface.h" // Qt -#include -#include +#include #include // Project #include "logger.h" -#include "maintimer.h" #include "messagedispatcher.h" #include "caninterface.h" -#include "format.h" // namespace using namespace Can; @@ -39,32 +36,39 @@ */ bool FrameInterface::init() { - // This is required for Signal/Slots in threading. - qRegisterMetaType("Can_Id"); + if ( _init ) return false; + _init = true; - // runs in main thread - Q_ASSERT_X(QThread::currentThread() == qApp->thread() , "_FrameInterface::init", "The Class initialization must be done in Main Thread" ); - _CanFrame_Thread.setObjectName("Can Frame Thread"); - connect(qApp, &QApplication::aboutToQuit, this, &FrameInterface::quit); - _CanFrame_Thread.start(); - moveToThread(&_CanFrame_Thread); - - // runs in USB Watcher thread initConnections(); - LOG_EVENT(QObject::tr("FrameInterface Initialized")); + LOG_EVENT(QObject::tr("%1 Initialized").arg(metaObject()->className())); return true; } -void FrameInterface::quit() +/*! + * \brief FrameInterface::init + * \details Initialized the Class by calling the init() method first + * And initializes the thread vThread by calling initThread + * on success init(). + * \param vThread - the thread + * \return returns the return value of the init() method + */ +bool FrameInterface::init(QThread &vThread) { - // runs in Logger thread - moveToThread(qApp->thread()); + if ( init() ) return false; + initThread(vThread); + return true; +} - // runs in main thread - _CanFrame_Thread.quit(); - _CanFrame_Thread.wait(); +/*! + * \brief FrameInterface::quit + * \details quits the class + * Calls quitThread + */ +void FrameInterface::quit() +{ + quitThread(); } /*! @@ -84,6 +88,37 @@ } /*! + * \brief ApplicationController::initThread + * \details Moves this object into the thread vThread. + * And checks that this method is called from main thread. + * Also connects quitThread to application aboutToQuit. + * \param vThread - the thread + */ +void FrameInterface::initThread(QThread &vThread) +{ + // runs in main thread + Q_ASSERT_X(QThread::currentThread() == qApp->thread() , __func__, "The Class initialization must be done in Main Thread" ); + _thread = &vThread; + _thread->setObjectName(QString("%1_Thread").arg(metaObject()->className())); + connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(quit())); + _thread->start(); + moveToThread(_thread); +} + +/*! + * \brief FrameInterface::quitThread + * \details Moves this object to main thread to be handled by QApplicaiton + * And to be destroyed there. + */ +void FrameInterface::quitThread() +{ + if ( ! _thread ) return; + + // runs in thread + moveToThread(qApp->thread()); +} + +/*! * \brief FrameInterface::transmitFrame * \details Prepares a frame to be transmitted * and emit signal didFrameTransmit with the frame as its argument