From 89e92af7948cde05bbb07a082835344f3f3adb4e Mon Sep 17 00:00:00 2001 From: Alex Blasche Date: Wed, 22 Aug 2018 12:10:21 +0200 Subject: Cleanup lowenergyscanner example with clang tidy and clazy help Change-Id: Iaaf169b1ac9b3d4c348d9cabf737b395198a921f Reviewed-by: Oliver Wolff --- .../lowenergyscanner/characteristicinfo.cpp | 6 +--- .../lowenergyscanner/characteristicinfo.h | 2 +- examples/bluetooth/lowenergyscanner/device.cpp | 40 ++++++++++++---------- examples/bluetooth/lowenergyscanner/device.h | 16 ++++----- examples/bluetooth/lowenergyscanner/deviceinfo.cpp | 4 --- examples/bluetooth/lowenergyscanner/deviceinfo.h | 2 +- examples/bluetooth/lowenergyscanner/main.cpp | 4 +-- .../bluetooth/lowenergyscanner/serviceinfo.cpp | 4 --- examples/bluetooth/lowenergyscanner/serviceinfo.h | 4 +-- 9 files changed, 37 insertions(+), 45 deletions(-) (limited to 'examples') diff --git a/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp b/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp index f64ddf79..2402f4d1 100644 --- a/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp +++ b/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp @@ -53,10 +53,6 @@ #include "qbluetoothuuid.h" #include -CharacteristicInfo::CharacteristicInfo() -{ -} - CharacteristicInfo::CharacteristicInfo(const QLowEnergyCharacteristic &characteristic): m_characteristic(characteristic) { @@ -131,7 +127,7 @@ QString CharacteristicInfo::getHandle() const QString CharacteristicInfo::getPermission() const { QString properties = "( "; - int permission = m_characteristic.properties(); + uint permission = m_characteristic.properties(); if (permission & QLowEnergyCharacteristic::Read) properties += QStringLiteral(" Read"); if (permission & QLowEnergyCharacteristic::Write) diff --git a/examples/bluetooth/lowenergyscanner/characteristicinfo.h b/examples/bluetooth/lowenergyscanner/characteristicinfo.h index ccc280e8..3f3600ab 100644 --- a/examples/bluetooth/lowenergyscanner/characteristicinfo.h +++ b/examples/bluetooth/lowenergyscanner/characteristicinfo.h @@ -65,7 +65,7 @@ class CharacteristicInfo: public QObject Q_PROPERTY(QString characteristicPermission READ getPermission NOTIFY characteristicChanged) public: - CharacteristicInfo(); + CharacteristicInfo() = default; CharacteristicInfo(const QLowEnergyCharacteristic &characteristic); void setCharacteristic(const QLowEnergyCharacteristic &characteristic); QString getName() const; diff --git a/examples/bluetooth/lowenergyscanner/device.cpp b/examples/bluetooth/lowenergyscanner/device.cpp index 4e54d1e3..afdf13af 100644 --- a/examples/bluetooth/lowenergyscanner/device.cpp +++ b/examples/bluetooth/lowenergyscanner/device.cpp @@ -60,8 +60,7 @@ #include #include -Device::Device(): - connected(false), controller(0), m_deviceScanState(false), randomAddress(false) +Device::Device() { //! [les-devicediscovery-1] discoveryAgent = new QBluetoothDeviceDiscoveryAgent(); @@ -109,7 +108,7 @@ void Device::startDeviceDiscovery() void Device::addDevice(const QBluetoothDeviceInfo &info) { if (info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) { - DeviceInfo *d = new DeviceInfo(info); + auto d = new DeviceInfo(info); devices.append(d); setUpdate("Last device added: " + d->getName()); } @@ -151,9 +150,13 @@ void Device::scanServices(const QString &address) { // We need the current device for service discovery. - for (int i = 0; i < devices.size(); i++) { - if (((DeviceInfo*)devices.at(i))->getAddress() == address ) - currentDevice.setDevice(((DeviceInfo*)devices.at(i))->getDevice()); + for (auto d: qAsConst(devices)) { + auto device = qobject_cast(d); + if (!device) + continue; + + if (device->getAddress() == address ) + currentDevice.setDevice(device->getDevice()); } if (!currentDevice.getDevice().isValid()) { @@ -173,7 +176,7 @@ void Device::scanServices(const QString &address) if (controller && m_previousAddress != currentDevice.getAddress()) { controller->disconnectFromDevice(); delete controller; - controller = 0; + controller = nullptr; } //! [les-controller-1] @@ -211,7 +214,7 @@ void Device::addLowEnergyService(const QBluetoothUuid &serviceUuid) return; } //! [les-service-1] - ServiceInfo *serv = new ServiceInfo(service); + auto serv = new ServiceInfo(service); m_services.append(serv); emit servicesUpdated(); @@ -228,9 +231,12 @@ void Device::serviceScanDone() void Device::connectToService(const QString &uuid) { - QLowEnergyService *service = 0; - for (int i = 0; i < m_services.size(); i++) { - ServiceInfo *serviceInfo = (ServiceInfo*)m_services.at(i); + QLowEnergyService *service = nullptr; + for (auto s: qAsConst(m_services)) { + auto serviceInfo = qobject_cast(s); + if (!serviceInfo) + continue; + if (serviceInfo->getUuid() == uuid) { service = serviceInfo->service(); break; @@ -257,7 +263,7 @@ void Device::connectToService(const QString &uuid) //discovery already done const QList chars = service->characteristics(); for (const QLowEnergyCharacteristic &ch : chars) { - CharacteristicInfo *cInfo = new CharacteristicInfo(ch); + auto cInfo = new CharacteristicInfo(ch); m_characteristics.append(cInfo); } @@ -279,7 +285,7 @@ void Device::errorReceived(QLowEnergyController::Error /*error*/) setUpdate(QString("Back\n(%1)").arg(controller->errorString())); } -void Device::setUpdate(QString message) +void Device::setUpdate(const QString &message) { m_message = message; emit updateChanged(); @@ -318,7 +324,7 @@ void Device::serviceDetailsDiscovered(QLowEnergyService::ServiceState newState) return; } - QLowEnergyService *service = qobject_cast(sender()); + auto service = qobject_cast(sender()); if (!service) return; @@ -327,7 +333,7 @@ void Device::serviceDetailsDiscovered(QLowEnergyService::ServiceState newState) //! [les-chars] const QList chars = service->characteristics(); for (const QLowEnergyCharacteristic &ch : chars) { - CharacteristicInfo *cInfo = new CharacteristicInfo(ch); + auto cInfo = new CharacteristicInfo(ch); m_characteristics.append(cInfo); } //! [les-chars] @@ -356,9 +362,7 @@ bool Device::state() bool Device::hasControllerError() const { - if (controller && controller->error() != QLowEnergyController::NoError) - return true; - return false; + return (controller && controller->error() != QLowEnergyController::NoError); } bool Device::isRandomAddress() const diff --git a/examples/bluetooth/lowenergyscanner/device.h b/examples/bluetooth/lowenergyscanner/device.h index a30cf9b6..ef94c7f9 100644 --- a/examples/bluetooth/lowenergyscanner/device.h +++ b/examples/bluetooth/lowenergyscanner/device.h @@ -122,18 +122,18 @@ Q_SIGNALS: void randomAddressChanged(); private: - void setUpdate(QString message); + void setUpdate(const QString &message); QBluetoothDeviceDiscoveryAgent *discoveryAgent; DeviceInfo currentDevice; - QList devices; - QList m_services; - QList m_characteristics; + QList devices; + QList m_services; + QList m_characteristics; QString m_previousAddress; QString m_message; - bool connected; - QLowEnergyController *controller; - bool m_deviceScanState; - bool randomAddress; + bool connected = false; + QLowEnergyController *controller = nullptr; + bool m_deviceScanState = false; + bool randomAddress = false; }; #endif // DEVICE_H diff --git a/examples/bluetooth/lowenergyscanner/deviceinfo.cpp b/examples/bluetooth/lowenergyscanner/deviceinfo.cpp index 69fedcea..23acc440 100644 --- a/examples/bluetooth/lowenergyscanner/deviceinfo.cpp +++ b/examples/bluetooth/lowenergyscanner/deviceinfo.cpp @@ -53,10 +53,6 @@ #include "deviceinfo.h" -DeviceInfo::DeviceInfo() -{ -} - DeviceInfo::DeviceInfo(const QBluetoothDeviceInfo &d) { device = d; diff --git a/examples/bluetooth/lowenergyscanner/deviceinfo.h b/examples/bluetooth/lowenergyscanner/deviceinfo.h index 8b20fa76..249afad2 100644 --- a/examples/bluetooth/lowenergyscanner/deviceinfo.h +++ b/examples/bluetooth/lowenergyscanner/deviceinfo.h @@ -64,7 +64,7 @@ class DeviceInfo: public QObject Q_PROPERTY(QString deviceName READ getName NOTIFY deviceChanged) Q_PROPERTY(QString deviceAddress READ getAddress NOTIFY deviceChanged) public: - DeviceInfo(); + DeviceInfo() = default; DeviceInfo(const QBluetoothDeviceInfo &d); QString getAddress() const; QString getName() const; diff --git a/examples/bluetooth/lowenergyscanner/main.cpp b/examples/bluetooth/lowenergyscanner/main.cpp index b3fac945..351ab13a 100644 --- a/examples/bluetooth/lowenergyscanner/main.cpp +++ b/examples/bluetooth/lowenergyscanner/main.cpp @@ -62,11 +62,11 @@ int main(int argc, char *argv[]) QGuiApplication app(argc, argv); Device d; - QQuickView *view = new QQuickView; + auto view = new QQuickView; view->rootContext()->setContextProperty("device", &d); view->setSource(QUrl("qrc:/assets/main.qml")); view->setResizeMode(QQuickView::SizeRootObjectToView); view->show(); - return app.exec(); + return QGuiApplication::exec(); } diff --git a/examples/bluetooth/lowenergyscanner/serviceinfo.cpp b/examples/bluetooth/lowenergyscanner/serviceinfo.cpp index 844c9cec..720619d0 100644 --- a/examples/bluetooth/lowenergyscanner/serviceinfo.cpp +++ b/examples/bluetooth/lowenergyscanner/serviceinfo.cpp @@ -51,10 +51,6 @@ #include "serviceinfo.h" -ServiceInfo::ServiceInfo() -{ -} - ServiceInfo::ServiceInfo(QLowEnergyService *service): m_service(service) { diff --git a/examples/bluetooth/lowenergyscanner/serviceinfo.h b/examples/bluetooth/lowenergyscanner/serviceinfo.h index e360fde7..6709be69 100644 --- a/examples/bluetooth/lowenergyscanner/serviceinfo.h +++ b/examples/bluetooth/lowenergyscanner/serviceinfo.h @@ -60,7 +60,7 @@ class ServiceInfo: public QObject Q_PROPERTY(QString serviceUuid READ getUuid NOTIFY serviceChanged) Q_PROPERTY(QString serviceType READ getType NOTIFY serviceChanged) public: - ServiceInfo(); + ServiceInfo() = default; ServiceInfo(QLowEnergyService *service); QLowEnergyService *service() const; QString getUuid() const; @@ -71,7 +71,7 @@ Q_SIGNALS: void serviceChanged(); private: - QLowEnergyService *m_service; + QLowEnergyService *m_service = nullptr; }; #endif // SERVICEINFO_H -- cgit v1.2.3