Warning, file /plasma/libksysguard/systemstats/SysctlSensor.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #pragma once 0008 0009 #include "SensorProperty.h" 0010 0011 #ifdef Q_OS_FREEBSD 0012 #include <sys/sysctl.h> 0013 #include <sys/types.h> 0014 #endif 0015 0016 namespace KSysGuard 0017 { 0018 /** 0019 * Convenience subclass of SensorProperty that reads a value of type T from FreeBSD's sysctl interface 0020 */ 0021 template<typename T> 0022 class SysctlSensor : public SensorProperty 0023 { 0024 public: 0025 /** 0026 * Contstrucor. 0027 * @param sysctlName The name of the sysctl entry 0028 */ 0029 SysctlSensor(const QString &id, const QByteArray &sysctlName, SensorObject *parent); 0030 SysctlSensor(const QString &id, const QByteArray &sysctlName, const QVariant &initialValue, SensorObject *parent); 0031 SysctlSensor(const QString &id, const QString &name, const QByteArray &sysctlName, SensorObject *parent); 0032 SysctlSensor(const QString &id, const QString &name, const QByteArray &sysctlName, const QVariant &initialValue, SensorObject *parent); 0033 0034 /** 0035 * Update this sensor. 0036 * 0037 * This will cause the sensor to call syscctl and update the value from that. 0038 * It should be called periodically so values are updated properly. 0039 */ 0040 void update() override; 0041 /** 0042 * Set the function used to convert the data from sysctl to the value of this sensor. 0043 * 0044 * This accepts a function that takes a reference to an object of type T and converts that to a 0045 * QVariant. Use this if you need to convert the value into another unit or do a calculation to 0046 * arrive at the sensor value. By default the value is stored as is in a QVariant. 0047 */ 0048 void setConversionFunction(const std::function<QVariant(const T &)> &function) 0049 { 0050 m_conversionFunction = function; 0051 } 0052 0053 private: 0054 const QByteArray m_sysctlName; 0055 std::function<QVariant(const T &)> m_conversionFunction = [](const T &t) { 0056 return QVariant::fromValue(t); 0057 }; 0058 }; 0059 0060 #ifdef Q_OS_FREEBSD 0061 0062 template<typename T> 0063 SysctlSensor<T>::SysctlSensor(const QString &id, const QByteArray &sysctlName, SensorObject *parent) 0064 : SysctlSensor(id, id, sysctlName, QVariant{}, parent) 0065 { 0066 } 0067 0068 template<typename T> 0069 SysctlSensor<T>::SysctlSensor(const QString &id, const QByteArray &sysctlName, const QVariant &initialValue, SensorObject *parent) 0070 : SysctlSensor(id, id, sysctlName, initialValue, parent) 0071 { 0072 } 0073 0074 template<typename T> 0075 SysctlSensor<T>::SysctlSensor(const QString &id, const QString &name, const QByteArray &sysctlName, SensorObject *parent) 0076 : SysctlSensor(id, name, sysctlName, QVariant{}, parent) 0077 { 0078 } 0079 0080 template<typename T> 0081 SysctlSensor<T>::SysctlSensor(const QString &id, const QString &name, const QByteArray &sysctlName, const QVariant &initialValue, SensorObject *parent) 0082 : SensorProperty(id, name, initialValue, parent) 0083 , m_sysctlName(sysctlName) 0084 { 0085 } 0086 0087 template<typename T> 0088 void SysctlSensor<T>::update() 0089 { 0090 if (!isSubscribed()) { 0091 return; 0092 } 0093 T value{}; 0094 size_t size = sizeof(T); 0095 if (sysctlbyname(m_sysctlName.constData(), &value, &size, nullptr, 0) != -1) { 0096 setValue(m_conversionFunction(value)); 0097 } 0098 } 0099 #endif 0100 0101 } // namespace KSysGuard