minor tweaks

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-11-03 13:52:57 +08:00
parent c75712d3f1
commit 9715bd9d4b
3 changed files with 21 additions and 15 deletions

View File

@ -25,8 +25,6 @@
#pragma once #pragma once
#include "framelesshelpercore_global.h" #include "framelesshelpercore_global.h"
#include <QtCore/qhash.h>
#include <QtCore/qmutex.h>
FRAMELESSHELPER_BEGIN_NAMESPACE FRAMELESSHELPER_BEGIN_NAMESPACE
@ -54,10 +52,6 @@ public:
{ {
return reinterpret_cast<T>(get(function)); return reinterpret_cast<T>(get(function));
} }
private:
static inline QMutex m_mutex{};
static inline QHash<QString, std::optional<QFunctionPointer>> m_functionCache = {};
}; };
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE

View File

@ -365,7 +365,9 @@ void FramelessManagerPrivate::initialize()
flagSet = true; flagSet = true;
// Set a global flag so that people can check whether FramelessHelper is being // Set a global flag so that people can check whether FramelessHelper is being
// used without actually accessing the FramelessHelper interface. // used without actually accessing the FramelessHelper interface.
qApp->setProperty(kGlobalFlagVarName, FramelessHelper::Core::version().version); const int ver = FramelessHelper::Core::version().version;
qputenv(kGlobalFlagVarName, QByteArray::number(ver));
qApp->setProperty(kGlobalFlagVarName, ver);
} }
} }

View File

@ -23,6 +23,8 @@
*/ */
#include "sysapiloader_p.h" #include "sysapiloader_p.h"
#include <QtCore/qhash.h>
#include <QtCore/qmutex.h>
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
# include <QtCore/private/qsystemlibrary_p.h> # include <QtCore/private/qsystemlibrary_p.h>
#else #else
@ -37,6 +39,14 @@ Q_LOGGING_CATEGORY(lcSysApiLoader, "wangwenx190.framelesshelper.core.sysapiloade
#define WARNING qCWarning(lcSysApiLoader) #define WARNING qCWarning(lcSysApiLoader)
#define CRITICAL qCCritical(lcSysApiLoader) #define CRITICAL qCCritical(lcSysApiLoader)
struct SysApiLoaderData
{
QMutex mutex;
QHash<QString, std::optional<QFunctionPointer>> functionCache = {};
};
Q_GLOBAL_STATIC(SysApiLoaderData, g_loaderData)
Q_GLOBAL_STATIC(SysApiLoader, g_sysApiLoader) Q_GLOBAL_STATIC(SysApiLoader, g_sysApiLoader)
SysApiLoader::SysApiLoader(QObject *parent) : QObject(parent) SysApiLoader::SysApiLoader(QObject *parent) : QObject(parent)
@ -91,17 +101,17 @@ bool SysApiLoader::isAvailable(const QString &library, const QString &function)
if (library.isEmpty() || function.isEmpty()) { if (library.isEmpty() || function.isEmpty()) {
return false; return false;
} }
const QMutexLocker locker(&m_mutex); const QMutexLocker locker(&g_loaderData()->mutex);
if (m_functionCache.contains(function)) { if (g_loaderData()->functionCache.contains(function)) {
return m_functionCache.value(function).has_value(); return g_loaderData()->functionCache.value(function).has_value();
} }
const QFunctionPointer symbol = SysApiLoader::resolve(library, function); const QFunctionPointer symbol = SysApiLoader::resolve(library, function);
if (symbol) { if (symbol) {
m_functionCache.insert(function, symbol); g_loaderData()->functionCache.insert(function, symbol);
DEBUG << "Successfully loaded" << function << "from" << library; DEBUG << "Successfully loaded" << function << "from" << library;
return true; return true;
} }
m_functionCache.insert(function, std::nullopt); g_loaderData()->functionCache.insert(function, std::nullopt);
WARNING << "Failed to load" << function << "from" << library; WARNING << "Failed to load" << function << "from" << library;
return false; return false;
} }
@ -112,9 +122,9 @@ QFunctionPointer SysApiLoader::get(const QString &function)
if (function.isEmpty()) { if (function.isEmpty()) {
return nullptr; return nullptr;
} }
const QMutexLocker locker(&m_mutex); const QMutexLocker locker(&g_loaderData()->mutex);
if (m_functionCache.contains(function)) { if (g_loaderData()->functionCache.contains(function)) {
return m_functionCache.value(function).value_or(nullptr); return g_loaderData()->functionCache.value(function).value_or(nullptr);
} }
return nullptr; return nullptr;
} }