QHostAddress: fix assignment operators
QHostAddress allowed assignment from a QString, but the respective constructor is explicit, and rightfully so. So it does not make sense that the assignment operator is provided, because of the asymmetry caused between QHostAddress addr = funcReturningQString(); // ERROR addr = funcReturningQString(); // OK (until now) By the same token, since SpecialAddress is implicitly convertible to QHostAddress, provide the missing assignment operator from that enum. Add tests, rewriting the _data() function to use the enum instead of an int to pass SpecialAddress values, and to test !=, too. Added setAddress(SpecialAddress), since a) it was missing and b) to share code between the ctor and the assignment operator. Change-Id: Ief64c493be13ada8c6968801d9ed083b267fa902 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>bb10
parent
630f8e7ad6
commit
4077fcc615
|
|
@ -517,6 +517,19 @@ QHostAddress::QHostAddress(const QHostAddress &address)
|
|||
QHostAddress::QHostAddress(SpecialAddress address)
|
||||
: d(new QHostAddressPrivate)
|
||||
{
|
||||
setAddress(address);
|
||||
}
|
||||
|
||||
/*!
|
||||
\overload
|
||||
\since 5.8
|
||||
|
||||
Sets the special address specified by \a address.
|
||||
*/
|
||||
void QHostAddress::setAddress(SpecialAddress address)
|
||||
{
|
||||
d->clear();
|
||||
|
||||
Q_IPV6ADDR ip6;
|
||||
memset(&ip6, 0, sizeof ip6);
|
||||
quint32 ip4 = INADDR_ANY;
|
||||
|
|
@ -567,6 +580,7 @@ QHostAddress &QHostAddress::operator=(const QHostAddress &address)
|
|||
return *this;
|
||||
}
|
||||
|
||||
#if QT_DEPRECATED_SINCE(5, 8)
|
||||
/*!
|
||||
Assigns the host address \a address to this object, and returns a
|
||||
reference to this object.
|
||||
|
|
@ -578,6 +592,20 @@ QHostAddress &QHostAddress::operator=(const QString &address)
|
|||
setAddress(address);
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*!
|
||||
\since 5.8
|
||||
Assigns the special address \a address to this object, and returns a
|
||||
reference to this object.
|
||||
|
||||
\sa setAddress()
|
||||
*/
|
||||
QHostAddress &QHostAddress::operator=(SpecialAddress address)
|
||||
{
|
||||
setAddress(address);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*!
|
||||
\fn bool QHostAddress::operator!=(const QHostAddress &other) const
|
||||
|
|
|
|||
|
|
@ -108,7 +108,11 @@ public:
|
|||
#endif
|
||||
|
||||
QHostAddress &operator=(const QHostAddress &other);
|
||||
#if QT_DEPRECATED_SINCE(5, 8)
|
||||
QT_DEPRECATED_X("use = QHostAddress(string) instead")
|
||||
QHostAddress &operator=(const QString &address);
|
||||
#endif
|
||||
QHostAddress &operator=(SpecialAddress address);
|
||||
|
||||
void swap(QHostAddress &other) Q_DECL_NOTHROW { d.swap(other.d); }
|
||||
|
||||
|
|
@ -118,6 +122,7 @@ public:
|
|||
void setAddress(const Q_IPV6ADDR &ip6Addr);
|
||||
void setAddress(const sockaddr *address);
|
||||
bool setAddress(const QString &address);
|
||||
void setAddress(SpecialAddress address);
|
||||
|
||||
QAbstractSocket::NetworkLayerProtocol protocol() const;
|
||||
quint32 toIPv4Address() const; // ### Qt6: merge with next overload
|
||||
|
|
|
|||
|
|
@ -46,6 +46,8 @@
|
|||
# include <netinet/in.h>
|
||||
#endif
|
||||
|
||||
Q_DECLARE_METATYPE(QHostAddress::SpecialAddress)
|
||||
|
||||
class tst_QHostAddress : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
|
@ -232,51 +234,55 @@ void tst_QHostAddress::setAddress_QString()
|
|||
void tst_QHostAddress::specialAddresses_data()
|
||||
{
|
||||
QTest::addColumn<QString>("text");
|
||||
QTest::addColumn<int>("address");
|
||||
QTest::addColumn<QHostAddress::SpecialAddress>("address");
|
||||
QTest::addColumn<bool>("result");
|
||||
|
||||
QTest::newRow("localhost_1") << QString("127.0.0.1") << (int)QHostAddress::LocalHost << true;
|
||||
QTest::newRow("localhost_2") << QString("127.0.0.2") << (int)QHostAddress::LocalHost << false;
|
||||
QTest::newRow("localhost_3") << QString("127.0.0.2") << (int)QHostAddress::LocalHostIPv6 << false;
|
||||
QTest::newRow("localhost_1") << QString("127.0.0.1") << QHostAddress::LocalHost << true;
|
||||
QTest::newRow("localhost_2") << QString("127.0.0.2") << QHostAddress::LocalHost << false;
|
||||
QTest::newRow("localhost_3") << QString("127.0.0.2") << QHostAddress::LocalHostIPv6 << false;
|
||||
|
||||
QTest::newRow("localhost_ipv6_4") << QString("::1") << (int)QHostAddress::LocalHostIPv6 << true;
|
||||
QTest::newRow("localhost_ipv6_5") << QString("::2") << (int)QHostAddress::LocalHostIPv6 << false;
|
||||
QTest::newRow("localhost_ipv6_6") << QString("::1") << (int)QHostAddress::LocalHost << false;
|
||||
QTest::newRow("localhost_ipv6_4") << QString("::1") << QHostAddress::LocalHostIPv6 << true;
|
||||
QTest::newRow("localhost_ipv6_5") << QString("::2") << QHostAddress::LocalHostIPv6 << false;
|
||||
QTest::newRow("localhost_ipv6_6") << QString("::1") << QHostAddress::LocalHost << false;
|
||||
|
||||
QTest::newRow("null_1") << QString("") << (int)QHostAddress::Null << true;
|
||||
QTest::newRow("null_2") << QString("bjarne") << (int)QHostAddress::Null << true;
|
||||
QTest::newRow("null_1") << QString("") << QHostAddress::Null << true;
|
||||
QTest::newRow("null_2") << QString("bjarne") << QHostAddress::Null << true;
|
||||
|
||||
QTest::newRow("compare_from_null") << QString("") << (int)QHostAddress::Broadcast << false;
|
||||
QTest::newRow("compare_from_null") << QString("") << QHostAddress::Broadcast << false;
|
||||
|
||||
QTest::newRow("broadcast_1") << QString("255.255.255.255") << (int)QHostAddress::Any << false;
|
||||
QTest::newRow("broadcast_2") << QString("255.255.255.255") << (int)QHostAddress::Broadcast << true;
|
||||
QTest::newRow("broadcast_1") << QString("255.255.255.255") << QHostAddress::Any << false;
|
||||
QTest::newRow("broadcast_2") << QString("255.255.255.255") << QHostAddress::Broadcast << true;
|
||||
|
||||
QTest::newRow("any_ipv6") << QString("::") << (int)QHostAddress::AnyIPv6 << true;
|
||||
QTest::newRow("any_ipv4") << QString("0.0.0.0") << (int)QHostAddress::AnyIPv4 << true;
|
||||
QTest::newRow("any_ipv6") << QString("::") << QHostAddress::AnyIPv6 << true;
|
||||
QTest::newRow("any_ipv4") << QString("0.0.0.0") << QHostAddress::AnyIPv4 << true;
|
||||
|
||||
QTest::newRow("dual_not_ipv6") << QString("::") << (int)QHostAddress::Any << false;
|
||||
QTest::newRow("dual_not_ipv4") << QString("0.0.0.0") << (int)QHostAddress::Any << false;
|
||||
QTest::newRow("dual_not_ipv6") << QString("::") << QHostAddress::Any << false;
|
||||
QTest::newRow("dual_not_ipv4") << QString("0.0.0.0") << QHostAddress::Any << false;
|
||||
}
|
||||
|
||||
|
||||
void tst_QHostAddress::specialAddresses()
|
||||
{
|
||||
QFETCH(QString, text);
|
||||
QFETCH(int, address);
|
||||
QFETCH(QHostAddress::SpecialAddress, address);
|
||||
QFETCH(bool, result);
|
||||
QVERIFY((QHostAddress(text) == (QHostAddress::SpecialAddress)address) == result);
|
||||
QCOMPARE(QHostAddress(text) == address, result);
|
||||
|
||||
//check special address equal to itself (QTBUG-22898), note two overloads of operator==
|
||||
QVERIFY(QHostAddress((QHostAddress::SpecialAddress)address) == QHostAddress((QHostAddress::SpecialAddress)address));
|
||||
QVERIFY(QHostAddress((QHostAddress::SpecialAddress)address) == (QHostAddress::SpecialAddress)address);
|
||||
QVERIFY(QHostAddress(address) == QHostAddress(address));
|
||||
QVERIFY(QHostAddress(address) == address);
|
||||
QVERIFY(!(QHostAddress(address) != QHostAddress(address)));
|
||||
QVERIFY(!(QHostAddress(address) != address));
|
||||
|
||||
{
|
||||
QHostAddress ha;
|
||||
ha.setAddress(address);
|
||||
QVERIFY(ha == address);
|
||||
}
|
||||
|
||||
QHostAddress setter;
|
||||
setter.setAddress(text);
|
||||
if (result) {
|
||||
QVERIFY(setter == (QHostAddress::SpecialAddress) address);
|
||||
} else {
|
||||
QVERIFY(!((QHostAddress::SpecialAddress) address == setter));
|
||||
}
|
||||
QCOMPARE(setter == address, result);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -359,6 +365,11 @@ void tst_QHostAddress::isEqual()
|
|||
QCOMPARE(second.isEqual(first, QHostAddress::ConversionModeFlag(flags)), result);
|
||||
}
|
||||
|
||||
QT_WARNING_PUSH
|
||||
#ifdef QT_WARNING_DISABLE_DEPRECATED
|
||||
QT_WARNING_DISABLE_DEPRECATED
|
||||
#endif
|
||||
|
||||
void tst_QHostAddress::assignment()
|
||||
{
|
||||
QHostAddress address;
|
||||
|
|
@ -379,6 +390,8 @@ void tst_QHostAddress::assignment()
|
|||
#endif // !Q_OS_WINRT
|
||||
}
|
||||
|
||||
QT_WARNING_POP
|
||||
|
||||
void tst_QHostAddress::scopeId()
|
||||
{
|
||||
QHostAddress address("fe80::2e0:4cff:fefb:662a%eth0");
|
||||
|
|
|
|||
Loading…
Reference in New Issue