Commit 5ba60c0a authored by Arjen Hiemstra's avatar Arjen Hiemstra
Browse files

Use a d pointer for all classes in the systemstats lib

parent fbb35156
......@@ -75,44 +75,56 @@ QVariant addVariants(const QVariant &first, const QVariant &second)
return result;
}
class Q_DECL_HIDDEN AggregateSensor::Private
{
public:
QRegularExpression matchObjects;
QString matchProperty;
QHash<QString, QPointer<SensorProperty>> sensors;
bool dataChangeQueued = false;
int dataCompressionDuration = 100;
SensorContainer *subsystem = nullptr;
std::function<QVariant(QVariant, QVariant)> aggregateFunction;
};
AggregateSensor::AggregateSensor(SensorObject *provider, const QString &id, const QString &name)
: SensorProperty(id, name, provider)
, m_subsystem(qobject_cast<SensorContainer *>(provider->parent()))
, d(std::make_unique<Private>())
{
m_aggregateFunction = addVariants;
connect(m_subsystem, &SensorContainer::objectAdded, this, &AggregateSensor::updateSensors);
connect(m_subsystem, &SensorContainer::objectRemoved, this, &AggregateSensor::updateSensors);
d->subsystem = qobject_cast<SensorContainer *>(provider->parent());
d->aggregateFunction = addVariants;
connect(d->subsystem, &SensorContainer::objectAdded, this, &AggregateSensor::updateSensors);
connect(d->subsystem, &SensorContainer::objectRemoved, this, &AggregateSensor::updateSensors);
}
AggregateSensor::~AggregateSensor()
{
}
AggregateSensor::~AggregateSensor() = default;
QRegularExpression AggregateSensor::matchSensors() const
{
return m_matchObjects;
return d->matchObjects;
}
QVariant AggregateSensor::value() const
{
if (m_sensors.isEmpty()) {
if (d->sensors.isEmpty()) {
return QVariant();
}
auto it = m_sensors.constBegin();
while (!it.value() && it != m_sensors.constEnd()) {
auto it = d->sensors.constBegin();
while (!it.value() && it != d->sensors.constEnd()) {
it++;
}
if (it == m_sensors.constEnd()) {
if (it == d->sensors.constEnd()) {
return QVariant{};
}
QVariant result = it.value()->value();
it++;
for (; it != m_sensors.constEnd(); it++) {
for (; it != d->sensors.constEnd(); it++) {
if (it.value()) {
result = m_aggregateFunction(result, it.value()->value());
result = d->aggregateFunction(result, it.value()->value());
}
}
return result;
......@@ -123,7 +135,7 @@ void AggregateSensor::subscribe()
bool wasSubscribed = SensorProperty::isSubscribed();
SensorProperty::subscribe();
if (!wasSubscribed && isSubscribed()) {
for (auto sensor : qAsConst(m_sensors)) {
for (auto sensor : qAsConst(d->sensors)) {
if (sensor) {
sensor->subscribe();
}
......@@ -136,7 +148,7 @@ void AggregateSensor::unsubscribe()
bool wasSubscribed = SensorProperty::isSubscribed();
SensorProperty::unsubscribe();
if (wasSubscribed && !isSubscribed()) {
for (auto sensor : qAsConst(m_sensors)) {
for (auto sensor : qAsConst(d->sensors)) {
if (sensor) {
sensor->unsubscribe();
}
......@@ -146,28 +158,28 @@ void AggregateSensor::unsubscribe()
void AggregateSensor::setMatchSensors(const QRegularExpression &objectIds, const QString &propertyName)
{
if (objectIds == m_matchObjects && propertyName == m_matchProperty) {
if (objectIds == d->matchObjects && propertyName == d->matchProperty) {
return;
}
m_matchProperty = propertyName;
m_matchObjects = objectIds;
d->matchProperty = propertyName;
d->matchObjects = objectIds;
updateSensors();
}
std::function<QVariant(QVariant, QVariant)> AggregateSensor::aggregateFunction() const
{
return m_aggregateFunction;
return d->aggregateFunction;
}
void AggregateSensor::setAggregateFunction(const std::function<QVariant(QVariant, QVariant)> &newAggregateFunction)
{
m_aggregateFunction = newAggregateFunction;
d->aggregateFunction = newAggregateFunction;
}
void AggregateSensor::addSensor(SensorProperty *sensor)
{
if (!sensor || sensor->path() == path() || m_sensors.contains(sensor->path())) {
if (!sensor || sensor->path() == path() || d->sensors.contains(sensor->path())) {
return;
}
......@@ -178,12 +190,12 @@ void AggregateSensor::addSensor(SensorProperty *sensor)
connect(sensor, &SensorProperty::valueChanged, this, [this, sensor]() {
sensorDataChanged(sensor);
});
m_sensors.insert(sensor->path(), sensor);
d->sensors.insert(sensor->path(), sensor);
}
void AggregateSensor::removeSensor(const QString &sensorPath)
{
auto sensor = m_sensors.take(sensorPath);
auto sensor = d->sensors.take(sensorPath);
sensor->disconnect(this);
if (isSubscribed()) {
sensor->unsubscribe();
......@@ -192,27 +204,27 @@ void AggregateSensor::removeSensor(const QString &sensorPath)
int AggregateSensor::matchCount() const
{
return m_sensors.size();
return d->sensors.size();
}
void AggregateSensor::updateSensors()
{
if (!m_matchObjects.isValid()) {
if (!d->matchObjects.isValid()) {
return;
}
for (auto obj : m_subsystem->objects()) {
if (m_matchObjects.match(obj->id()).hasMatch()) {
auto sensor = obj->sensor(m_matchProperty);
for (auto obj : d->subsystem->objects()) {
if (d->matchObjects.match(obj->id()).hasMatch()) {
auto sensor = obj->sensor(d->matchProperty);
if (sensor) {
addSensor(sensor);
}
}
}
auto itr = m_sensors.begin();
while (itr != m_sensors.end()) {
auto itr = d->sensors.begin();
while (itr != d->sensors.end()) {
if (!itr.value()) {
itr = m_sensors.erase(itr);
itr = d->sensors.erase(itr);
} else {
++itr;
}
......@@ -229,51 +241,56 @@ void AggregateSensor::sensorDataChanged(SensorProperty *sensor)
void AggregateSensor::delayedEmitDataChanged()
{
if (!m_dataChangeQueued) {
m_dataChangeQueued = true;
QTimer::singleShot(m_dataCompressionDuration, [this]() {
if (!d->dataChangeQueued) {
d->dataChangeQueued = true;
QTimer::singleShot(d->dataCompressionDuration, [this]() {
Q_EMIT valueChanged();
m_dataChangeQueued = false;
d->dataChangeQueued = false;
});
}
}
class Q_DECL_HIDDEN PercentageSensor::Private
{
public:
SensorProperty *sensor = nullptr;
};
PercentageSensor::PercentageSensor(SensorObject *provider, const QString &id, const QString &name)
: SensorProperty(id, name, provider)
, d(std::make_unique<Private>())
{
setUnit(KSysGuard::UnitPercent);
setMax(100);
}
PercentageSensor::~PercentageSensor()
{
}
PercentageSensor::~PercentageSensor() = default;
void PercentageSensor::setBaseSensor(SensorProperty *property)
{
m_sensor = property;
d->sensor = property;
connect(property, &SensorProperty::valueChanged, this, &PercentageSensor::valueChanged);
connect(property, &SensorProperty::sensorInfoChanged, this, &PercentageSensor::valueChanged);
}
QVariant PercentageSensor::value() const
{
if (!m_sensor) {
if (!d->sensor) {
return QVariant();
}
QVariant value = m_sensor->value();
QVariant value = d->sensor->value();
if (!value.isValid()) {
return QVariant();
}
return (value.toReal() / m_sensor->info().max) * 100.0;
return (value.toReal() / d->sensor->info().max) * 100.0;
}
void PercentageSensor::subscribe()
{
m_sensor->subscribe();
d->sensor->subscribe();
}
void PercentageSensor::unsubscribe()
{
m_sensor->unsubscribe();
d->sensor->unsubscribe();
}
......@@ -21,6 +21,7 @@
#pragma once
#include <functional>
#include <memory>
#include <QRegularExpression>
#include <QVariant>
......@@ -66,14 +67,8 @@ private:
void sensorDataChanged(SensorProperty *sensor);
void delayedEmitDataChanged();
QRegularExpression m_matchObjects;
QString m_matchProperty;
QHash<QString, QPointer<SensorProperty>> m_sensors;
bool m_dataChangeQueued = false;
int m_dataCompressionDuration = 100;
SensorContainer *m_subsystem;
std::function<QVariant(QVariant, QVariant)> m_aggregateFunction;
class Private;
const std::unique_ptr<Private> d;
};
class Q_DECL_EXPORT PercentageSensor : public SensorProperty
......@@ -90,7 +85,8 @@ public:
void setBaseSensor(SensorProperty *sensor);
private:
SensorProperty *m_sensor;
class Private;
const std::unique_ptr<Private> d;
};
} // namespace KSysGuard
......@@ -23,36 +23,43 @@
using namespace KSysGuard;
class Q_DECL_HIDDEN SensorContainer::Private
{
public:
QString id;
QString name;
QHash<QString, SensorObject *> sensorObjects;
};
SensorContainer::SensorContainer(const QString &id, const QString &name, SensorPlugin *parent)
: QObject(parent)
, m_id(id)
, m_name(name)
, d(std::make_unique<Private>())
{
d->id = id;
d->name = name;
parent->addContainer(this);
}
SensorContainer::~SensorContainer()
{
}
SensorContainer::~SensorContainer() = default;
QString SensorContainer::id() const
{
return m_id;
return d->id;
}
QString SensorContainer::name() const
{
return m_name;
return d->name;
}
QList<SensorObject *> SensorContainer::objects()
{
return m_sensorObjects.values();
return d->sensorObjects.values();
}
SensorObject *SensorContainer::object(const QString &id) const
{
return m_sensorObjects.value(id);
return d->sensorObjects.value(id);
}
void SensorContainer::addObject(SensorObject *object)
......@@ -60,8 +67,8 @@ void SensorContainer::addObject(SensorObject *object)
object->setParentContainer(this);
const QString id = object->id();
Q_ASSERT(!m_sensorObjects.contains(id));
m_sensorObjects[id] = object;
Q_ASSERT(!d->sensorObjects.contains(id));
d->sensorObjects[id] = object;
Q_EMIT objectAdded(object);
connect(object, &SensorObject::aboutToBeRemoved, this, [this, object]() {
......@@ -71,11 +78,11 @@ void SensorContainer::addObject(SensorObject *object)
void SensorContainer::removeObject(SensorObject *object)
{
if (!m_sensorObjects.contains(object->id())) {
if (!d->sensorObjects.contains(object->id())) {
return;
}
object->setParentContainer(nullptr);
m_sensorObjects.remove(object->id());
d->sensorObjects.remove(object->id());
Q_EMIT objectRemoved(object);
}
......@@ -19,6 +19,8 @@
#pragma once
#include <memory>
#include <QObject>
#include "SensorPlugin.h"
......@@ -83,10 +85,8 @@ Q_SIGNALS:
void objectRemoved(SensorObject *object);
private:
QString m_id;
QString m_name;
QHash<QString, SensorObject *> m_sensorObjects;
friend class SensorObject;
class Private;
const std::unique_ptr<Private> d;
};
} // namespace KSysGuard
......@@ -23,6 +23,16 @@
using namespace KSysGuard;
class Q_DECL_HIDDEN SensorObject::Private
{
public:
SensorContainer *parent = nullptr;
QString id;
QString name;
QHash<QString, SensorProperty *> sensors;
};
SensorObject::SensorObject(const QString &id, SensorContainer *parent)
: SensorObject(id, QString(), parent)
{
......@@ -30,69 +40,69 @@ SensorObject::SensorObject(const QString &id, SensorContainer *parent)
SensorObject::SensorObject(const QString &id, const QString &name, SensorContainer *parent)
: QObject(parent)
, m_parent(parent)
, m_id(id)
, m_name(name)
, d(std::make_unique<Private>())
{
d->parent = parent;
d->id = id;
d->name = name;
if (parent) {
parent->addObject(this);
parent->addObject(this);
}
}
SensorObject::~SensorObject()
{
}
SensorObject::~SensorObject() = default;
QString SensorObject::id() const
{
return m_id;
return d->id;
}
QString SensorObject::name() const
{
return m_name;
return d->name;
}
QString SensorObject::path() const
{
if (!m_parent) {
if (!d->parent) {
return QString{};
}
return m_parent->id() % QLatin1Char('/') % m_id;
return d->parent->id() % QLatin1Char('/') % d->id;
}
void SensorObject::setName(const QString& newName)
{
if (newName == m_name) {
if (newName == d->name) {
return;
}
m_name = newName;
d->name = newName;
Q_EMIT nameChanged();
}
void SensorObject::setParentContainer(SensorContainer* parent)
{
m_parent = parent;
d->parent = parent;
}
QList<SensorProperty *> SensorObject::sensors() const
{
return m_sensors.values();
return d->sensors.values();
}
SensorProperty *SensorObject::sensor(const QString &sensorId) const
{
return m_sensors.value(sensorId);
return d->sensors.value(sensorId);
}
void SensorObject::addProperty(SensorProperty *property)
{
m_sensors[property->id()] = property;
d->sensors[property->id()] = property;
connect(property, &SensorProperty::subscribedChanged, this, [=]() {
uint count = std::count_if(m_sensors.constBegin(), m_sensors.constEnd(), [](const SensorProperty *prop) {
uint count = std::count_if(d->sensors.constBegin(), d->sensors.constEnd(), [](const SensorProperty *prop) {
return prop->isSubscribed();
});
if (count == 1) {
......@@ -105,7 +115,7 @@ void SensorObject::addProperty(SensorProperty *property)
bool SensorObject::isSubscribed() const
{
return std::any_of(m_sensors.constBegin(), m_sensors.constEnd(), [](const SensorProperty *prop) {
return std::any_of(d->sensors.constBegin(), d->sensors.constEnd(), [](const SensorProperty *prop) {
return prop->isSubscribed();
});
}
......@@ -19,6 +19,8 @@
#pragma once
#include <memory>
#include <QObject>
#include "SensorPlugin.h"
......@@ -76,10 +78,8 @@ Q_SIGNALS:
void nameChanged();
private:
SensorContainer *m_parent = nullptr;
QString m_id;
QString m_name;
QHash<QString, SensorProperty *> m_sensors;
class Private;
const std::unique_ptr<Private> d;
};
} // namespace KSysGuard
......@@ -21,15 +21,24 @@
using namespace KSysGuard;
class Q_DECL_HIDDEN SensorPlugin::Private
{
public:
QList<SensorContainer *> containers;
};
SensorPlugin::SensorPlugin(QObject *parent, const QVariantList &args)
: QObject(parent)
, d(std::make_unique<Private>())
{
Q_UNUSED(args)
}
SensorPlugin::~SensorPlugin() = default;
QList<SensorContainer *> SensorPlugin::containers() const
{
return m_containers;
return d->containers;
}
QString SensorPlugin::providerName() const
......@@ -43,5 +52,5 @@ void SensorPlugin::update()
void SensorPlugin::addContainer(SensorContainer *container)
{
m_containers << container;
d->containers << container;
}
......@@ -19,6 +19,8 @@
#pragma once
#include <memory>
#include <QObject>
#include <QVariant>
......@@ -38,7 +40,7 @@ class SYSTEMSTATS_EXPORT SensorPlugin : public QObject
Q_OBJECT
public:
SensorPlugin(QObject *parent, const QVariantList &args);
~SensorPlugin() = default;
~SensorPlugin();
/**
A list of all containers provided by this plugin
......@@ -65,7 +67,8 @@ public:
void addContainer(SensorContainer *container);
private:
QList<SensorContainer *> m_containers;
class Private;
const std::unique_ptr<Private> d;
};
} // namespace KSysGuard
......@@ -22,6 +22,19 @@
using namespace KSysGuard;
class Q_DECL_HIDDEN SensorProperty::Private
{
public:
SensorObject *parent = nullptr;
SensorInfo info;
QString id;
QString name;
QString prefix;
QVariant value;
int subscribers = 0;
};
SensorProperty::SensorProperty(const QString &id, SensorObject *parent)
: SensorProperty(id, QString(), parent)
{
......@@ -34,9 +47,10 @@ SensorProperty::SensorProperty(const QString &id, const QString &name, SensorObj
SensorProperty::SensorProperty(const QString &id, const QString &name, const QVariant &initalValue, SensorObject *parent)
: QObject(parent)
, m_parent(parent)
, m_id(id)
, d(std::make_unique<Private>())
{
d->id = id;
d->parent = parent;
setName(name);
if (initalValue.isValid()) {
setValue(initalValue);
......@@ -50,78 +64,78 @@ SensorProperty::~SensorProperty()
SensorInfo SensorProperty::info() const
{
return m_info;
return d->info;
}
QString SensorProperty::id() const
{
return m_id;