Commit ce572362 authored by David Redondo's avatar David Redondo 🏎

It is not possible to make virtual function calls from the constructor

parent f2509279
......@@ -24,8 +24,6 @@
BaseCpuObject::BaseCpuObject(const QString &id, const QString &name, SensorContainer *parent)
: SensorObject(id, name, parent)
{
makeSensors();
initialize();
}
void BaseCpuObject::makeSensors()
......@@ -41,6 +39,7 @@ void BaseCpuObject::makeSensors()
void BaseCpuObject::initialize()
{
makeSensors();
m_usage->setPrefix(name());
m_usage->setName(i18nc("@title", "Total Usage"));
......@@ -104,21 +103,23 @@ void CpuObject::initialize()
}
AllCpusObject::AllCpusObject(unsigned int cpuCount, unsigned int coreCount, SensorContainer *parent)
AllCpusObject::AllCpusObject(SensorContainer *parent)
: BaseCpuObject(QStringLiteral("all"), i18nc("@title", "All"), parent)
{
m_cpuCount->setValue(cpuCount);
m_coreCount->setValue(cpuCount);
}
void AllCpusObject::makeSensors()
{
BaseCpuObject::makeSensors();
m_cpuCount = new SensorProperty(QStringLiteral("cpuCount"), this);
m_coreCount = new SensorProperty(QStringLiteral("coreCount"), this);
}
void AllCpusObject::initialize()
{
BaseCpuObject::initialize();
m_cpuCount->setName(i18nc("@title", "Number of CPUs"));
m_cpuCount->setShortName(i18nc("@title, Short fort 'Number of CPUs'", "CPUs"));
m_cpuCount->setDescription(i18nc("@info", "Number of physical CPUs installed in the system"));
......@@ -127,3 +128,9 @@ void AllCpusObject::initialize()
m_coreCount->setShortName(i18nc("@title, Short fort 'Number of Cores'", "Cores"));
m_coreCount->setDescription(i18nc("@info", "Number of CPU cores across all physical CPUS"));
}
void AllCpusObject::setCounts(unsigned int cpuCount, unsigned int coreCount)
{
m_cpuCount->setValue(cpuCount);
m_coreCount->setValue(cpuCount);
}
......@@ -39,9 +39,8 @@ protected:
class CpuObject : public BaseCpuObject {
public:
CpuObject(const QString &id, const QString &name, SensorContainer *parent);
protected:
void initialize() override;
protected:
void makeSensors() override;
SensorProperty *m_frequency;
......@@ -50,10 +49,10 @@ protected:
class AllCpusObject : public BaseCpuObject {
public:
AllCpusObject(unsigned int cpuCount, unsigned int coreCount, SensorContainer *parent);
protected:
AllCpusObject(SensorContainer *parent);
void setCounts(unsigned int cpuCount, unsigned int coreCount);
void initialize() override;
protected:
void makeSensors() override;
SensorProperty *m_cpuCount;
......
......@@ -37,6 +37,8 @@ void FreeBsdCpuObject::makeSensors()
void FreeBsdCpuObject::initialize()
{
CpuObject::initialize();
const QByteArray prefix = QByteArrayLiteral("dev.cpu.") + id().right(1).toLocal8Bit();
// For min and max frequency we have to parse the values return by freq_levels because only
// minimum is exposed as a single value
......@@ -80,7 +82,7 @@ void FreeBsdCpuObject::update(long system, long user, long idle)
m_system->setValue(m_usageComputer.systemUsage);
m_user->setValue(m_usageComputer.userUsage);
m_usage->setValue(m_usageComputer.totalUsage);
for (auto sensor : m_sysctlSensors) {
sensor->update();
}
......@@ -104,15 +106,19 @@ FreeBsdCpuPluginPrivate::FreeBsdCpuPluginPrivate(CpuPlugin* q)
int numCpu;
readSysctl("hw.ncpu", &numCpu);
for (int i = 0; i < numCpu; ++i) {
m_cpus.push_back(new FreeBsdCpuObject(QStringLiteral("cpu%1").arg(i), i18nc("@title", "CPU %1", i + 1), m_container));
auto cpu = new FreeBsdCpuObject(QStringLiteral("cpu%1").arg(i), i18nc("@title", "CPU %1", i + 1), m_container);
cpu->initialize();
m_cpus.push_back(cpu);
}
m_allCpus = new FreeBsdAllCpusObject(numCpu, numCpu, m_container);
m_allCpus = new FreeBsdAllCpusObject(m_container);
m_allCpus->initialize();
}
void FreeBsdCpuPluginPrivate::update()
{
auto isSubscribed = [] (const SensorObject *o) {return o->isSubscribed();};
if (std::none_of(m_container->objects().cbegin(), m_container->objects().cend(), isSubscribed)) {
const auto objects = m_container->objects();
if (std::none_of(objects.cbegin(), objects.cend(), isSubscribed)) {
return;
}
auto updateCpu = [] (auto *cpu, long *cp_time){
......
......@@ -12,9 +12,9 @@ class FreeBsdCpuObject : public CpuObject {
public:
FreeBsdCpuObject(const QString &id, const QString &name, SensorContainer *parent);
void update(long system, long user, long idle);
void initialize() override;
private:
void makeSensors() override;
void initialize() override;
UsageComputer m_usageComputer;
QVector<SysctlSensor<int>*> m_sysctlSensors;
};
......
......@@ -57,17 +57,22 @@ void TemperatureSensor::update()
#endif
}
LinuxCpuObject::LinuxCpuObject(const QString &id, const QString &name, SensorContainer *parent, double frequency)
LinuxCpuObject::LinuxCpuObject(const QString &id, const QString &name, SensorContainer *parent)
: CpuObject(id, name, parent)
{
m_frequency->setValue(frequency);
}
void LinuxCpuObject::initialize(double initialFrequency)
{
CpuObject::initialize();
m_frequency->setValue(initialFrequency);
bool ok;
const double max = readCpuFreq(id, "cpuinfo_max_freq", ok);
const double max = readCpuFreq(id(), "cpuinfo_max_freq", ok);
if (ok) {
m_frequency->setMax(max);
}
const double min = readCpuFreq(id, "cpuinfo_min_freq", ok);
const double min = readCpuFreq(id(), "cpuinfo_min_freq", ok);
if (ok) {
m_frequency->setMin(min);
}
......
......@@ -21,11 +21,13 @@ private:
class LinuxCpuObject : public CpuObject
{
public:
LinuxCpuObject(const QString &id, const QString &name, SensorContainer *parent, double frequency = 0);
LinuxCpuObject(const QString &id, const QString &name, SensorContainer *parent);
void update(unsigned long long system, unsigned long long user, unsigned long long wait, unsigned long long idle);
TemperatureSensor* temperatureSensor();
void initialize(double initialFrequency);
private:
void initialize() override {};
void makeSensors() override;
UsageComputer m_usageComputer;
TemperatureSensor *m_temperatureSensor;
......
......@@ -62,11 +62,14 @@ LinuxCpuPluginPrivate::LinuxCpuPluginPrivate(CpuPlugin *q)
}
}
const QString name = i18nc("@title", "CPU %1 Core %2", physicalId + 1, ++numCores[physicalId]);
auto cpu = new LinuxCpuObject(QStringLiteral("cpu%1").arg(processor), name, m_container, frequency);
auto cpu = new LinuxCpuObject(QStringLiteral("cpu%1").arg(processor), name, m_container);
cpu->initialize(frequency);
m_cpus.push_back(cpu);
m_cpusBySystemIds.insert({physicalId, coreId}, cpu);
}
m_allCpus = new LinuxAllCpusObject(numCores.keys().size(), numCores.size(), m_container);
m_allCpus = new LinuxAllCpusObject(m_container);
m_allCpus->initialize();
m_allCpus->setCounts(numCores.keys().size(), numCores.size());
addSensors();
}
......@@ -74,7 +77,8 @@ LinuxCpuPluginPrivate::LinuxCpuPluginPrivate(CpuPlugin *q)
void LinuxCpuPluginPrivate::update()
{
auto isSubscribed = [] (const SensorObject *o) {return o->isSubscribed();};
if (std::none_of(m_container->objects().cbegin(), m_container->objects().cend(), isSubscribed)) {
const auto objects = m_container->objects();
if (std::none_of(objects.cbegin(), objects.cend(), isSubscribed)) {
return;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment