From 95f01c4beac836697d602902a6c446ade48d79ff Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Fri, 11 Sep 2015 12:34:52 +0200 Subject: [PATCH] Use C++ for setting the default argument --- core/default_args.h | 61 ------------------------------- core/device.cpp | 4 +- core/kdeconnectpluginconfig.h | 3 +- core/networkpackage.h | 3 +- plugins/battery/batteryplugin.cpp | 4 +- tests/networkpackagetests.cpp | 2 +- 6 files changed, 7 insertions(+), 70 deletions(-) delete mode 100644 core/default_args.h diff --git a/core/default_args.h b/core/default_args.h deleted file mode 100644 index d198c6f44..000000000 --- a/core/default_args.h +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Copyright 2013 Albert Vaca - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License or (at your option) version 3 or any later version - * accepted by the membership of KDE e.V. (or its successor approved - * by the membership of KDE e.V.), which shall act as a proxy - * defined in Section 14 of version 3 of the license. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef DEFAULTARG_H -#define DEFAULTARG_H - -#include - -template -struct default_arg { - static T get(); //Not defined for any other value -}; - -//bool -> false -template<> -struct default_arg { - static bool get() { return false; } -}; - -//int -> -1 -template<> -struct default_arg { - static int get() { return -1; } -}; - -//QByteArray-> empty qbytearray -template<> -struct default_arg { - static QByteArray get() { return QByteArray(); } -}; - -//QStrings -> empty string -template<> -struct default_arg { - static QString get() { return QString(); } -}; - -template -struct default_arg { - static T* get() { return NULL;} -}; - - -#endif // DEFAULTARG_H diff --git a/core/device.cpp b/core/device.cpp index 4a222e186..a82069618 100644 --- a/core/device.cpp +++ b/core/device.cpp @@ -73,7 +73,7 @@ Device::Device(QObject* parent, const NetworkPackage& identityPackage, DeviceLin , m_deviceName(identityPackage.get("deviceName")) , m_deviceType(str2type(identityPackage.get("deviceType"))) , m_pairStatus(Device::NotPaired) - , m_protocolVersion(identityPackage.get("protocolVersion")) + , m_protocolVersion(identityPackage.get("protocolVersion", -1)) { addLink(identityPackage, dl); @@ -272,7 +272,7 @@ void Device::addLink(const NetworkPackage& identityPackage, DeviceLink* link) { //qCDebug(KDECONNECT_CORE) << "Adding link to" << id() << "via" << link->provider(); - m_protocolVersion = identityPackage.get("protocolVersion"); + m_protocolVersion = identityPackage.get("protocolVersion", -1); if (m_protocolVersion != NetworkPackage::ProtocolVersion) { qCWarning(KDECONNECT_CORE) << m_deviceName << "- warning, device uses a different protocol version" << m_protocolVersion << "expected" << NetworkPackage::ProtocolVersion; } diff --git a/core/kdeconnectpluginconfig.h b/core/kdeconnectpluginconfig.h index 96195e4be..8e047d00e 100644 --- a/core/kdeconnectpluginconfig.h +++ b/core/kdeconnectpluginconfig.h @@ -26,7 +26,6 @@ #include #include -#include "default_args.h" #include "kdeconnectcore_export.h" struct KdeConnectPluginConfigPrivate; @@ -56,7 +55,7 @@ public: /** * Convenience method that will convert the QVariant to whatever type for you */ - template T get(const QString& key, const T& defaultValue = default_arg::get()) { + template T get(const QString& key, const T& defaultValue = {}) { return get(key, QVariant(defaultValue)).template value(); //Important note: Awesome template syntax is awesome } diff --git a/core/networkpackage.h b/core/networkpackage.h index 29549e5d2..49dfeb610 100644 --- a/core/networkpackage.h +++ b/core/networkpackage.h @@ -33,7 +33,6 @@ #include #include "kdeconnectcore_export.h" -#include "default_args.h" class FileTransferJob; @@ -69,7 +68,7 @@ public: const QVariantMap& body() const { return mBody; } //Get and set info from body. Note that id and type can not be accessed through these. - template T get(const QString& key, const T& defaultValue = default_arg::get()) const { + template T get(const QString& key, const T& defaultValue = {}) const { return mBody.value(key,defaultValue).template value(); //Important note: Awesome template syntax is awesome } template void set(const QString& key, const T& value) { mBody[key] = QVariant(value); } diff --git a/plugins/battery/batteryplugin.cpp b/plugins/battery/batteryplugin.cpp index 700f25d77..32cf089c7 100644 --- a/plugins/battery/batteryplugin.cpp +++ b/plugins/battery/batteryplugin.cpp @@ -61,8 +61,8 @@ BatteryPlugin::~BatteryPlugin() bool BatteryPlugin::receivePackage(const NetworkPackage& np) { - bool isCharging = np.get("isCharging"); - int currentCharge = np.get("currentCharge"); + bool isCharging = np.get("isCharging", false); + int currentCharge = np.get("currentCharge", -1); int thresholdEvent = np.get("thresholdEvent", (int)ThresholdNone); if (batteryDbusInterface->charge() != currentCharge diff --git a/tests/networkpackagetests.cpp b/tests/networkpackagetests.cpp index d7079b4b5..cb4050667 100644 --- a/tests/networkpackagetests.cpp +++ b/tests/networkpackagetests.cpp @@ -84,7 +84,7 @@ void NetworkPackageTests::networkPackageIdentityTest() NetworkPackage np(""); NetworkPackage::createIdentityPackage(&np); - QCOMPARE( np.get("protocolVersion") , NetworkPackage::ProtocolVersion ); + QCOMPARE( np.get("protocolVersion", -1) , NetworkPackage::ProtocolVersion ); QCOMPARE( np.type() , PACKAGE_TYPE_IDENTITY ); }