在Qt应用程序及其插件中使用Singleton类 [英] Using a Singleton Class across a Qt Application and its Plugins

查看:199
本文介绍了在Qt应用程序及其插件中使用Singleton类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的Qt应用程序中使用Singleton类(其程序范围的调试记录器称为"PrisLog").该程序还具有插件.我想让我的单例类可用于这些插件,但这不起作用.据我所知,尝试在插件中使用该类会导致创建另一个实例.

I'm trying to use a Singleton Class (its a program-wide debug logger called 'PrisLog') across my Qt Application. The program also has plugins. I want to make my singleton class available to those plugins, but this doesn't work. From what I can tell, trying to use the class in the plugin results in another instance being created.

-singleton类只是一个* .cpp和* .h文件,仅此而已.我已将我的主应用程序和插件分别链接到这些文件...这是正确的方法吗?

-The singleton class is just a *.cpp and *.h file, nothing else. I've linked both my main application and the plugin to these files individually... is this the right way to do it?

-我在下面附加了我的单例类的代码,尽管我认为我已经正确创建了该类.如果我在主应用程序的单独类中使用它,它将按预期运行(一个实例).

-I've attached my singleton class's code below, though I think I've created the class correctly. If I use it from within separate classes in my main application, it works as expected (one instance).

将应用程序和插件都链接到同一个静态库(单例类)即可.这是我的qmake * .pro文件的外观:

Linking both the application and plugin to the same static lib (the singleton class) works. Here's how my qmake *.pro files looked:

MySingletonLib.pro

MySingletonLib.pro

TEMPLATE = lib

TEMPLATE = lib

CONFIG + = staticlib

CONFIG += staticlib

HEADERS + = \ mysingletonlib.h

HEADERS += \ mysingletonlib.h

来源+ = \ mysingletonlib.cpp

SOURCES += \ mysingletonlib.cpp

MyPlugin.pro(在myplugin.h中还包括#include mysingletonlib.h)

MyPlugin.pro (also incl #include mysingletonlib.h in myplugin.h)

INCLUDEPATH + = path/to/MySingletonLib

INCLUDEPATH += path/to/MySingletonLib

LIBS + = -Lpath/to/MySingletonLib -lMySingletonLib

LIBS += -Lpath/to/MySingletonLib -lMySingletonLib

MyPlugin.pro(还包括myapp.h中的#include mysingletonlib.h)

MyPlugin.pro (also incl #include mysingletonlib.h in myapp.h)

INCLUDEPATH + = path/to/MySingletonLib

INCLUDEPATH += path/to/MySingletonLib

LIBS + = -Lpath/to/MySingletonLib -lMySingletonLib

LIBS += -Lpath/to/MySingletonLib -lMySingletonLib

原始代码:

#ifndef PRISLOG_H
#define PRISLOG_H

#include <QFile>
#include <QDir>
#include <QString>
#include <QMutex>
#include <QDebug>
#include <QMutexLocker>
#include <QTextStream>
#include <QDateTime>

// PrisLog (singleton) class definition
class PrisLog
{

public:
    static PrisLog* Instance();

    void SetLogsPath(QString);
    QString GetLogsPath();

    void SetDebugDestination(QString);
    void SetElmRxDestination(QString);
    void SetElmTxDestination(QString);
    void SetDlgDestination(QString);

    QTextStream* GetDebugStream();
    QTextStream* GetElmRxStream();
    QTextStream* GetElmTxStream();
    QTextStream* GetDlgStream();

    QMutex* GetDebugMutex();

private:
    PrisLog();                          // private constructor
    PrisLog(const PrisLog&);            // prevent copy constructor
    PrisLog& operator=(const PrisLog&); // prevent assignment

    static PrisLog* m_Instance;
    static bool m_InitFlag;

    QString m_appPath;

    QFile m_DebugFile;
    QTextStream m_DebugStream;
    QMutex m_DebugMutex;

    QFile m_ElmRxFile;
    QTextStream m_ElmRxStream;

    QFile m_ElmTxFile;
    QTextStream m_ElmTxStream;

    QFile m_DlgFile;
    QTextStream m_DlgStream;

};

// thread-UNSAFE writer, but less expensive
// use: single stream <--> single thread!
class PrisLogWriter
{

public:
    PrisLogWriter(QTextStream*);
    ~PrisLogWriter();

    QTextStream* m_stream;
};

// thread-UNSAFE writer, but less expensive
// this version does not include any formatting
// use: single stream <--> single thread!
class PrisLogRawWriter
{

public:
    PrisLogRawWriter(QTextStream*);
    ~PrisLogRawWriter();

    QTextStream* m_stream;
};

// thread-safe writer
// use: single stream <--> many threads
class PrisLogSafeWriter
{

public:
    PrisLogSafeWriter(QTextStream*, QMutex*);
    ~PrisLogSafeWriter();

    QTextStream* m_stream;

private:
    QMutex* m_mutex;
};


#define PRISLOGDEBUG (*(PrisLogSafeWriter(PrisLog::Instance()->GetDebugStream(), PrisLog::Instance()->GetDebugMutex()).m_stream))
#define PRISLOGELMRX (*(PrisLogWriter(PrisLog::Instance()->GetElmRxStream()).m_stream))
#define PRISLOGELMTX (*(PrisLogWriter(PrisLog::Instance()->GetElmTxStream()).m_stream))
#define PRISLOGDLG (*(PrisLogRawWriter(PrisLog::Instance()->GetDlgStream()).m_stream))


#endif // PRISLOG_H

推荐答案

我认为您应该将此类带到静态链接但分隔开的共享dll/so中.

I think you should take this class to a statically linked, but separeted shared dll/so.

如果您的主机应用程序不使用此类,则链接器将不会将其链接到二进制文件中,而您的插件将无法使用它.此外:您的二进制文件没有库类接口.

If your host application does not use this class, the linker simply won't link it into the binary and your plugin could not use it. Additionally: your binary has no library class interface.

这篇关于在Qt应用程序及其插件中使用Singleton类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆