/*! * * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. * \copyright * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * \file singleton.h * \author (last) Behrouz NematiPour * \date (last) 02-Jan-2020 * \author (original) Behrouz NematiPour * \date (original) 02-Jan-2020 * */ #pragma once // Qt #include #include #include #include // Project #define SINGLETON(vCLASS) \ private: \ explicit vCLASS(QObject *parent = nullptr); \ vCLASS(vCLASS const &); \ vCLASS & operator = (vCLASS const &) = delete; \ public: \ static vCLASS &I() { \ static vCLASS _instance; \ return _instance; \ } class Singleton : public QObject { Q_OBJECT static QVector _threads; Singleton (Singleton const &); Singleton & operator = (Singleton const &) = delete; protected: bool _initialized = false; explicit Singleton(QObject *parent = nullptr) : QObject(parent) {} virtual ~Singleton() { _quit_(); } static Singleton &I() { static Singleton _instance; return _instance; } void initThread(QThread *vThread) { qDebug() << this->metaObject()->className(); if (! vThread) return; vThread->setObjectName(QString("%1_Thread").arg(this->metaObject()->className())); moveToThread(vThread); QObject::connect(qApp, &QApplication::aboutToQuit, vThread, [vThread]() { vThread->quit(); vThread->wait(); }); QObject::connect(vThread, &QThread::finished, this, [this](){ moveToThread(qApp->thread()); }); vThread->start(); } virtual bool _init_() { return true; } virtual void _quit_() { _initialized = false; } virtual void initConnections() { } public: virtual bool init(QThread *thread = nullptr) final { _init_(); initConnections(); initThread(thread); _initialized = true; return true; } };