Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion qrutils/imagesCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImagesCache>
{
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.
Expand Down
1 change: 1 addition & 0 deletions qrutils/qrutils.pri
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ SOURCES += \
$$PWD/generator/abstractGenerator.cpp \
$$PWD/widgetFinder.cpp \
$$PWD/imagesCache.cpp \
$$PWD/singleton.cpp \

FORMS += \
$$PWD/watchListWindow.ui
Expand Down
20 changes: 20 additions & 0 deletions qrutils/singleton.cpp
Original file line number Diff line number Diff line change
@@ -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<const void *, QWeakPointer<QObject>> SingletonImpl::staticTypeToObject;
QMutex SingletonImpl::staticMutex;
33 changes: 21 additions & 12 deletions qrutils/singleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,38 @@

#include <QMutex>
#include <QSharedPointer>

#pragma once

namespace utils {

class SingletonImpl
{
public:
template <typename T> static QSharedPointer<T> instance(const std::function<T*()>& factory) {
QMutexLocker lock(&staticMutex);
auto &instance = staticTypeToObject[&T::staticMetaObject];
if (auto result = instance.lock()) {
return qSharedPointerCast<T, QObject>(result);
}
auto result = QSharedPointer<T>(factory());
staticTypeToObject[&T::staticMetaObject] = qSharedPointerCast<QObject, T>(result);
return result;
}
private:
static QHash<const void *, QWeakPointer<QObject>> staticTypeToObject;
static QMutex staticMutex;
};


/// Instantiates and provides to all callers single instance of the some type.
template<typename T>
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<T> instance()
{
static QMutex m;
QMutexLocker lock(&m);
static QWeakPointer<T> instance;
if (auto result = instance.lock()) {
return result;
} else {
result = QSharedPointer<T>(new T());
instance = result;
return result;
}
return SingletonImpl::instance<T>([]() -> T* {return new T();});
}
};

Expand Down
Loading