Use C++ for setting the default argument
This commit is contained in:
parent
ca4a81b8e8
commit
95f01c4bea
6 changed files with 7 additions and 70 deletions
|
@ -1,61 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef DEFAULTARG_H
|
||||
#define DEFAULTARG_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
template<class T>
|
||||
struct default_arg {
|
||||
static T get(); //Not defined for any other value
|
||||
};
|
||||
|
||||
//bool -> false
|
||||
template<>
|
||||
struct default_arg<bool> {
|
||||
static bool get() { return false; }
|
||||
};
|
||||
|
||||
//int -> -1
|
||||
template<>
|
||||
struct default_arg<int> {
|
||||
static int get() { return -1; }
|
||||
};
|
||||
|
||||
//QByteArray-> empty qbytearray
|
||||
template<>
|
||||
struct default_arg<QByteArray> {
|
||||
static QByteArray get() { return QByteArray(); }
|
||||
};
|
||||
|
||||
//QStrings -> empty string
|
||||
template<>
|
||||
struct default_arg<QString> {
|
||||
static QString get() { return QString(); }
|
||||
};
|
||||
|
||||
template<class T>
|
||||
struct default_arg<T*> {
|
||||
static T* get() { return NULL;}
|
||||
};
|
||||
|
||||
|
||||
#endif // DEFAULTARG_H
|
|
@ -73,7 +73,7 @@ Device::Device(QObject* parent, const NetworkPackage& identityPackage, DeviceLin
|
|||
, m_deviceName(identityPackage.get<QString>("deviceName"))
|
||||
, m_deviceType(str2type(identityPackage.get<QString>("deviceType")))
|
||||
, m_pairStatus(Device::NotPaired)
|
||||
, m_protocolVersion(identityPackage.get<int>("protocolVersion"))
|
||||
, m_protocolVersion(identityPackage.get<int>("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<int>("protocolVersion");
|
||||
m_protocolVersion = identityPackage.get<int>("protocolVersion", -1);
|
||||
if (m_protocolVersion != NetworkPackage::ProtocolVersion) {
|
||||
qCWarning(KDECONNECT_CORE) << m_deviceName << "- warning, device uses a different protocol version" << m_protocolVersion << "expected" << NetworkPackage::ProtocolVersion;
|
||||
}
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
#include <QStringList>
|
||||
#include <QVariant>
|
||||
|
||||
#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<typename T> T get(const QString& key, const T& defaultValue = default_arg<T>::get()) {
|
||||
template<typename T> T get(const QString& key, const T& defaultValue = {}) {
|
||||
return get(key, QVariant(defaultValue)).template value<T>(); //Important note: Awesome template syntax is awesome
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,6 @@
|
|||
#include <QUrl>
|
||||
|
||||
#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<typename T> T get(const QString& key, const T& defaultValue = default_arg<T>::get()) const {
|
||||
template<typename T> T get(const QString& key, const T& defaultValue = {}) const {
|
||||
return mBody.value(key,defaultValue).template value<T>(); //Important note: Awesome template syntax is awesome
|
||||
}
|
||||
template<typename T> void set(const QString& key, const T& value) { mBody[key] = QVariant(value); }
|
||||
|
|
|
@ -61,8 +61,8 @@ BatteryPlugin::~BatteryPlugin()
|
|||
|
||||
bool BatteryPlugin::receivePackage(const NetworkPackage& np)
|
||||
{
|
||||
bool isCharging = np.get<bool>("isCharging");
|
||||
int currentCharge = np.get<int>("currentCharge");
|
||||
bool isCharging = np.get<bool>("isCharging", false);
|
||||
int currentCharge = np.get<int>("currentCharge", -1);
|
||||
int thresholdEvent = np.get<int>("thresholdEvent", (int)ThresholdNone);
|
||||
|
||||
if (batteryDbusInterface->charge() != currentCharge
|
||||
|
|
|
@ -84,7 +84,7 @@ void NetworkPackageTests::networkPackageIdentityTest()
|
|||
NetworkPackage np("");
|
||||
NetworkPackage::createIdentityPackage(&np);
|
||||
|
||||
QCOMPARE( np.get<int>("protocolVersion") , NetworkPackage::ProtocolVersion );
|
||||
QCOMPARE( np.get<int>("protocolVersion", -1) , NetworkPackage::ProtocolVersion );
|
||||
QCOMPARE( np.type() , PACKAGE_TYPE_IDENTITY );
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue