diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2023-07-18 17:21:01 +0200 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2023-08-03 22:23:08 +0200 |
commit | 5d90296f312f72ffa6a5322df7b565c3e5521684 (patch) | |
tree | dbe22479bb1704518cf3ab4e8be6499bff41233a | |
parent | d4d43974fce2c56c718157a1a46e23e2fb39aba9 (diff) |
QBluetoothUuid: keep custom quint128 and related methods when 128-bit ints are not supported
... to retain SC with prior Qt versions.
For QBluetoothUuid::toUInt128() this is done by introducing
an extra condition to the method declaration and definition.
If the 128-bit ints are not supported, we shouldn't hide the
method under QT_BLUETOOTH_REMOVED_SINCE.
The QBluetoothUuid(quint128) c-tor is a bit more tricky,
because we need to provide a forwarding c-tor to be able to
use QUuid(quint128) c-tor. And at the same time, we need
to keep BC by keeping the constructor that takes custom
struct quint128.
This is achieved by having an unconstrained c-tor declaration
and two implementations:
* an implementation in qbluetoothuuid.cpp is only available
when Qt supports 128-bit ints. It just forwards to the
QUuid c-tor.
* an implementation in removed_api.cpp serves as a backup
implementation to keep BC for the case when Qt supports
128-bit ints. And at the same time it serves as a main
implementation when 128-bit ints are not supported.
Also extend the unit-tests to explicitly check that both
APIs are available on all platforms.
The test additionally makes sure that QUuid::toUInt128() and
QBluetoothUuid::toUInt128() provide the result which is similar to
QUuid::toBytes(). This guarantees compatibility between platforms.
This commit amends 1e903be81f43da4e31385bb7866bb4d3f07e5eba.
Pick-to: 6.6
Change-Id: I6ba3ff6278e6db11a372b46f001429f4466520a0
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
-rw-r--r-- | src/bluetooth/qbluetoothuuid.cpp | 31 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothuuid.h | 6 | ||||
-rw-r--r-- | src/bluetooth/removed_api.cpp | 23 | ||||
-rw-r--r-- | tests/auto/qbluetoothuuid/tst_qbluetoothuuid.cpp | 17 |
4 files changed, 65 insertions, 12 deletions
diff --git a/src/bluetooth/qbluetoothuuid.cpp b/src/bluetooth/qbluetoothuuid.cpp index 47761e52..5338c978 100644 --- a/src/bluetooth/qbluetoothuuid.cpp +++ b/src/bluetooth/qbluetoothuuid.cpp @@ -532,6 +532,37 @@ Q_CONSTRUCTOR_FUNCTION(registerQBluetoothUuid) */ /*! + \fn quint128 QBluetoothUuid::toUInt128() const + + Returns the 128 bit representation of this UUID. + + \note The returned value is in big endian order. + + This method is only available when the platform \e {does not} support + native 128 bit integral values. Otherwise, the call will resolve to + \l QUuid::toUInt128(), which also allows to specify the desired endian + order of the returned value. +*/ + +/*! + \fn QBluetoothUuid::QBluetoothUuid(quint128 uuid) + + Constructs a new Bluetooth UUID from a 128 bit \a uuid. + + \note The \a uuid must be in big endian order. +*/ +#ifdef QT_SUPPORTS_INT128 +// This implementation is used only when 128-bit ints are supported. +// The old implementation which uses struct quint128 can be found in +// removed_api.cpp. We need to keep it there, because we need to provide both +// implementations for BC +QBluetoothUuid::QBluetoothUuid(quint128 uuid) + : QUuid(uuid, QSysInfo::BigEndian) +{ +} +#endif + +/*! Creates a QBluetoothUuid object from the string \a uuid, which must be formatted as five hex fields separated by '-', e.g., "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where 'x' is a hex digit. diff --git a/src/bluetooth/qbluetoothuuid.h b/src/bluetooth/qbluetoothuuid.h index 7717410e..d599c573 100644 --- a/src/bluetooth/qbluetoothuuid.h +++ b/src/bluetooth/qbluetoothuuid.h @@ -18,7 +18,7 @@ Q_FORWARD_DECLARE_OBJC_CLASS(CBUUID); QT_BEGIN_NAMESPACE -#if QT_BLUETOOTH_REMOVED_SINCE(6, 6) +#if QT_BLUETOOTH_REMOVED_SINCE(6, 6) || !defined(QT_SUPPORTS_INT128) struct quint128 { quint8 data[16]; @@ -351,6 +351,7 @@ public: explicit constexpr QBluetoothUuid(quint32 uuid) noexcept : QUuid(uuid, 0x0, 0x1000, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb) {}; + explicit QBluetoothUuid(quint128 uuid); explicit QBluetoothUuid(QUuid::Id128Bytes uuid) noexcept : QUuid(uuid) {}; explicit QBluetoothUuid(const QString &uuid); @@ -377,8 +378,7 @@ public: quint16 toUInt16(bool *ok = nullptr) const; quint32 toUInt32(bool *ok = nullptr) const; -#if QT_BLUETOOTH_REMOVED_SINCE(6, 6) - explicit QBluetoothUuid(quint128 uuid); +#if QT_BLUETOOTH_REMOVED_SINCE(6, 6) || !defined(QT_SUPPORTS_INT128) quint128 toUInt128() const; #endif diff --git a/src/bluetooth/removed_api.cpp b/src/bluetooth/removed_api.cpp index 2a346424..e2536be9 100644 --- a/src/bluetooth/removed_api.cpp +++ b/src/bluetooth/removed_api.cpp @@ -2,21 +2,18 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #define QT_BLUETOOTH_BUILD_REMOVED_API -// before we undef __SIZEOF_INT128__ -#include <cstddef> -#include <cstdint> -#ifdef __SIZEOF_INT128__ -// ensure QtCore/qtypes.h doesn't define quint128 -# undef __SIZEOF_INT128__ -#endif +// Undefine Qt 128-bit int types +#define QT_NO_INT128 #include "qtbluetoothglobal.h" QT_USE_NAMESPACE -#if QT_BLUETOOTH_REMOVED_SINCE(6, 6) -#include "qbluetoothaddress.h" // inlined API +// This part is not typical to the removed_api.cpp, because we need to have +// an extra condition. When adding new removed APIs for Qt 6.6, do not put them +// into this section. Instead, add them to the section below. +#if QT_BLUETOOTH_REMOVED_SINCE(6, 6) || !defined(QT_SUPPORTS_INT128) #include "qbluetoothuuid.h" @@ -34,4 +31,12 @@ quint128 QBluetoothUuid::toUInt128() const memcpy(uuid.data, bytes.data, sizeof(uuid.data)); return uuid; } + +#endif // QT_BLUETOOTH_REMOVED_SINCE(6, 6) || !defined(QT_SUPPORTS_INT128) + +// This is a common section for Qt 6.6 removed APIs +#if QT_BLUETOOTH_REMOVED_SINCE(6, 6) + +#include "qbluetoothaddress.h" // inlined API + #endif // QT_BLUETOOTH_REMOVED_SINCE(6, 6) diff --git a/tests/auto/qbluetoothuuid/tst_qbluetoothuuid.cpp b/tests/auto/qbluetoothuuid/tst_qbluetoothuuid.cpp index e31b0667..7a31300c 100644 --- a/tests/auto/qbluetoothuuid/tst_qbluetoothuuid.cpp +++ b/tests/auto/qbluetoothuuid/tst_qbluetoothuuid.cpp @@ -359,6 +359,23 @@ void tst_QBluetoothUuid::tst_comparison() for (int var = 0; var < 16; ++var) { QVERIFY(quuid128.toBytes().data[var] == uuid128.data[var]); } + + // check that toUInt128() call returns the value in the same format as + // QUuid::Id128Bytes, no matter what version we use (it can be + // QUuid::toUint128() on platforms that define __SIZEOF_INT128__ or + // QBluetoothUuid::toUint128() on other platforms). + const quint128 i128 = quuid128.toUInt128(); + static_assert(sizeof(i128) == 16); // uint128 or QUuid::Id128Bytes + uchar dst[16]; + memcpy(dst, &i128, sizeof(i128)); + for (int var = 0; var < 16; ++var) + QCOMPARE_EQ(dst[var], uuid128.data[var]); + + // check that we always have a c-tor taking quint128 + QBluetoothUuid other{i128}; + const auto bytes = other.toBytes(); + for (int var = 0; var < 16; ++var) + QCOMPARE_EQ(bytes.data[var], uuid128.data[var]); } } |