diff --git a/qrutils/imagesCache.h b/qrutils/imagesCache.h index 8e140b25a4..d109ba5187 100644 --- a/qrutils/imagesCache.h +++ b/qrutils/imagesCache.h @@ -28,8 +28,9 @@ namespace utils { /// Pixmaps and svg images are contained separately as they are rendered differently. class QRUTILS_EXPORT ImagesCache: public utils::Singleton { + Q_OBJECT public: - ~ImagesCache() = default; + ~ImagesCache() override {}; /// Draws image with given file name with a given painter in given rectangle. Note that actual file, from which /// an image will be loaded may be different from fileName, as described in selectBestImageFile. diff --git a/qrutils/qrutils.pri b/qrutils/qrutils.pri index 0cf085f25b..99fe2ac789 100644 --- a/qrutils/qrutils.pri +++ b/qrutils/qrutils.pri @@ -54,6 +54,7 @@ SOURCES += \ $$PWD/generator/abstractGenerator.cpp \ $$PWD/widgetFinder.cpp \ $$PWD/imagesCache.cpp \ + $$PWD/singleton.cpp \ FORMS += \ $$PWD/watchListWindow.ui diff --git a/qrutils/singleton.cpp b/qrutils/singleton.cpp new file mode 100644 index 0000000000..b526229b87 --- /dev/null +++ b/qrutils/singleton.cpp @@ -0,0 +1,20 @@ +/* Copyright 2025 CyberTech Labs Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ + +#include "singleton.h" + +using namespace utils; + +QHash> SingletonImpl::staticTypeToObject; +QMutex SingletonImpl::staticMutex; diff --git a/qrutils/singleton.h b/qrutils/singleton.h index 0ef2a4a28f..607d7c7324 100644 --- a/qrutils/singleton.h +++ b/qrutils/singleton.h @@ -14,29 +14,38 @@ #include #include - #pragma once namespace utils { +class SingletonImpl +{ +public: + template static QSharedPointer instance(const std::function& factory) { + QMutexLocker lock(&staticMutex); + auto &instance = staticTypeToObject[&T::staticMetaObject]; + if (auto result = instance.lock()) { + return qSharedPointerCast(result); + } + auto result = QSharedPointer(factory()); + staticTypeToObject[&T::staticMetaObject] = qSharedPointerCast(result); + return result; + } +private: + static QHash> staticTypeToObject; + static QMutex staticMutex; +}; + + /// Instantiates and provides to all callers single instance of the some type. template -class Singleton +class Singleton: public QObject { public: /// Creates single instance of some type (given in class template) if it does not exist and returns it. static QSharedPointer instance() { - static QMutex m; - QMutexLocker lock(&m); - static QWeakPointer instance; - if (auto result = instance.lock()) { - return result; - } else { - result = QSharedPointer(new T()); - instance = result; - return result; - } + return SingletonImpl::instance([]() -> T* {return new T();}); } };