summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2022-04-12 10:05:49 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-04-14 07:34:33 +0000
commitb6a6f51ab7aa39224ade83c2e3f960979a3930cd (patch)
tree70a2998af7684e15f0dcb97bc5b5030d36ff32c3
parent955d67cab9c78af4df3f7aca78ab4935e96bf0a0 (diff)
downloadqtconnectivity-b6a6f51ab7aa39224ade83c2e3f960979a3930cd.tar.gz
DeviceDiscoveryBroadcastReceiver: reduce relocations
Store global strings as strings, not as pointers to strings. Since we can now no longer use nullptr as an end-of-list marker, use a ranged for-loop over the array, which figures out the array's size automatically. Reference: http://library.bagrintsev.me/CPP/dsohowto.pdf Section 2.4 Task-number: QTBUG-100536 Change-Id: I16866fbbc8833390e0a34bc73e3c2256ae0f6ddb Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> (cherry picked from commit 692cedd20ab2862ed8b4c42d69c07b7802786b6d) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/bluetooth/android/devicediscoverybroadcastreceiver.cpp33
1 files changed, 15 insertions, 18 deletions
diff --git a/src/bluetooth/android/devicediscoverybroadcastreceiver.cpp b/src/bluetooth/android/devicediscoverybroadcastreceiver.cpp
index 869d948b..2875190f 100644
--- a/src/bluetooth/android/devicediscoverybroadcastreceiver.cpp
+++ b/src/bluetooth/android/devicediscoverybroadcastreceiver.cpp
@@ -92,23 +92,24 @@ Q_GLOBAL_STATIC_WITH_ARGS(QBitArray, initializedCacheTracker, (initializeMinorCa
// class names
-static const char * const javaBluetoothDeviceClassName = "android/bluetooth/BluetoothDevice";
-static const char * const javaBluetoothClassDeviceMajorClassName = "android/bluetooth/BluetoothClass$Device$Major";
-static const char * const javaBluetoothClassDeviceClassName = "android/bluetooth/BluetoothClass$Device";
+static const char javaBluetoothDeviceClassName[] = "android/bluetooth/BluetoothDevice";
+static const char javaBluetoothClassDeviceMajorClassName[] = "android/bluetooth/BluetoothClass$Device$Major";
+static const char javaBluetoothClassDeviceClassName[] = "android/bluetooth/BluetoothClass$Device";
// field names device type (LE vs classic)
-static const char * const javaDeviceTypeClassic = "DEVICE_TYPE_CLASSIC";
-static const char * const javaDeviceTypeDual = "DEVICE_TYPE_DUAL";
-static const char * const javaDeviceTypeLE = "DEVICE_TYPE_LE";
-static const char * const javaDeviceTypeUnknown = "DEVICE_TYPE_UNKNOWN";
+static const char javaDeviceTypeClassic[] = "DEVICE_TYPE_CLASSIC";
+static const char javaDeviceTypeDual[] = "DEVICE_TYPE_DUAL";
+static const char javaDeviceTypeLE[] = "DEVICE_TYPE_LE";
+static const char javaDeviceTypeUnknown[] = "DEVICE_TYPE_UNKNOWN";
struct MajorClassJavaToQtMapping
{
- char const * javaFieldName;
- QBluetoothDeviceInfo::MajorDeviceClass qtMajor;
+ const char javaFieldName[14];
+ QBluetoothDeviceInfo::MajorDeviceClass qtMajor : 16;
};
+static_assert(sizeof(MajorClassJavaToQtMapping) == 16);
-static const MajorClassJavaToQtMapping majorMappings[] = {
+static constexpr MajorClassJavaToQtMapping majorMappings[] = {
{ "AUDIO_VIDEO", QBluetoothDeviceInfo::AudioVideoDevice },
{ "COMPUTER", QBluetoothDeviceInfo::ComputerDevice },
{ "HEALTH", QBluetoothDeviceInfo::HealthDevice },
@@ -120,7 +121,6 @@ static const MajorClassJavaToQtMapping majorMappings[] = {
{ "TOY", QBluetoothDeviceInfo::ToyDevice },
{ "UNCATEGORIZED", QBluetoothDeviceInfo::UncategorizedDevice },
{ "WEARABLE", QBluetoothDeviceInfo::WearableDevice },
- { nullptr, QBluetoothDeviceInfo::UncategorizedDevice } //end of list
};
// QBluetoothDeviceInfo::MajorDeviceClass value plus 1 matches index
@@ -303,12 +303,11 @@ QBluetoothDeviceInfo::MajorDeviceClass resolveAndroidMajorClass(jint javaType)
if (it == cachedMajorTypes()->end()) {
QJniEnvironment env;
// precache all major device class fields
- int i = 0;
jint fieldValue;
QBluetoothDeviceInfo::MajorDeviceClass result = QBluetoothDeviceInfo::UncategorizedDevice;
auto clazz = env->FindClass(javaBluetoothClassDeviceMajorClassName);
- while (majorMappings[i].javaFieldName != nullptr) {
- auto fieldId = env->GetStaticFieldID(clazz, majorMappings[i].javaFieldName, "I");
+ for (const auto &majorMapping : majorMappings) {
+ auto fieldId = env->GetStaticFieldID(clazz, majorMapping.javaFieldName, "I");
if (!env->ExceptionCheck())
fieldValue = env->GetStaticIntField(clazz, fieldId);
if (env.checkAndClearExceptions()) {
@@ -317,13 +316,11 @@ QBluetoothDeviceInfo::MajorDeviceClass resolveAndroidMajorClass(jint javaType)
// add fallback value because field not readable
cachedMajorTypes()->insert(javaType, QBluetoothDeviceInfo::UncategorizedDevice);
} else {
- cachedMajorTypes()->insert(fieldValue, majorMappings[i].qtMajor);
+ cachedMajorTypes()->insert(fieldValue, majorMapping.qtMajor);
}
if (fieldValue == javaType)
- result = majorMappings[i].qtMajor;
-
- i++;
+ result = majorMapping.qtMajor;
}
return result;