From 80ed972e53a2c9dcac37bb53fcf9d1cfe7c20446 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Mon, 31 Dec 2018 06:35:51 +0100 Subject: [PATCH] Ref T494, style * removed some old "foreach" loops * fixed some CLANG warnings --- .../components/voiceroomscomponent.cpp | 7 +- src/blackmisc/audio/voiceroom.cpp | 80 +++++++------------ src/blacksound/soundgenerator.cpp | 40 +++++----- 3 files changed, 51 insertions(+), 76 deletions(-) diff --git a/src/blackgui/components/voiceroomscomponent.cpp b/src/blackgui/components/voiceroomscomponent.cpp index 3f9c34796..e8408a0de 100644 --- a/src/blackgui/components/voiceroomscomponent.cpp +++ b/src/blackgui/components/voiceroomscomponent.cpp @@ -28,6 +28,7 @@ using namespace BlackCore; using namespace BlackCore::Context; using namespace BlackMisc::Audio; +using namespace BlackMisc::Aviation; namespace BlackGui { @@ -60,7 +61,7 @@ namespace BlackGui void CVoiceRoomsComponent::onVoiceRoomUrlsReturnPressed() { - Q_ASSERT(sGui->getIContextOwnAircraft()); // voice room resolution is part of own aircraft + if (!sGui || sGui->isShuttingDown() || !sGui->getIContextOwnAircraft()) { return; } QString url1; QString url2; if (ui->cb_CockpitVoiceRoom1Override->isChecked()) { url1 = ui->le_CockpitVoiceRoomCom1->text().trimmed(); } @@ -70,8 +71,8 @@ namespace BlackGui void CVoiceRoomsComponent::setVoiceRoomUrlFieldsReadOnlyState() { - bool c1 = ui->cb_CockpitVoiceRoom1Override->isChecked(); - bool c2 = ui->cb_CockpitVoiceRoom2Override->isChecked(); + const bool c1 = ui->cb_CockpitVoiceRoom1Override->isChecked(); + const bool c2 = ui->cb_CockpitVoiceRoom2Override->isChecked(); ui->le_CockpitVoiceRoomCom1->setReadOnly(!c1); ui->le_CockpitVoiceRoomCom2->setReadOnly(!c2); CGuiUtility::forceStyleSheetUpdate(this); diff --git a/src/blackmisc/audio/voiceroom.cpp b/src/blackmisc/audio/voiceroom.cpp index c00c4c6e2..315cfeec3 100644 --- a/src/blackmisc/audio/voiceroom.cpp +++ b/src/blackmisc/audio/voiceroom.cpp @@ -12,13 +12,13 @@ #include "blackmisc/variant.h" #include +#include #include namespace BlackMisc { namespace Audio { - CVoiceRoom::CVoiceRoom(const QString &voiceRoomUrl, bool connected) : m_connected(connected), m_audioPlaying(false) { @@ -28,83 +28,61 @@ namespace BlackMisc CVariant CVoiceRoom::propertyByIndex(const CPropertyIndex &index) const { if (index.isMyself()) { return CVariant::from(*this); } - ColumnIndex i = index.frontCasted(); + const ColumnIndex i = index.frontCasted(); switch (i) { - case IndexAudioPlaying: - return CVariant::from(this->isAudioPlaying()); - case IndexConnected: - return CVariant::from(this->isConnected()); - case IndexChannel: - return CVariant::from(this->getChannel()); - case IndexHostname: - return CVariant::from(this->getHostname()); - case IndexUrl: - return CVariant::from(this->getVoiceRoomUrl()); - default: - return CValueObject::propertyByIndex(index); + case IndexAudioPlaying: return CVariant::from(this->isAudioPlaying()); + case IndexConnected: return CVariant::from(this->isConnected()); + case IndexChannel: return CVariant::from(this->getChannel()); + case IndexHostname: return CVariant::from(this->getHostname()); + case IndexUrl: return CVariant::from(this->getVoiceRoomUrl()); + default: return CValueObject::propertyByIndex(index); } } void CVoiceRoom::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant) { if (index.isMyself()) { (*this) = variant.to(); return; } - ColumnIndex i = index.frontCasted(); + const ColumnIndex i = index.frontCasted(); switch (i) { - case IndexAudioPlaying: - this->setAudioPlaying(variant.toBool()); - break; - case IndexConnected: - this->setConnected(variant.toBool()); - break; - case IndexChannel: - this->setChannel(variant.value()); - break; - case IndexHostname: - this->setHostName(variant.value()); - break; - case IndexUrl: - this->setVoiceRoomUrl(variant.value()); - break; - default: - CValueObject::setPropertyByIndex(index, variant); - break; + case IndexAudioPlaying: this->setAudioPlaying(variant.toBool()); break; + case IndexConnected: this->setConnected(variant.toBool()); break; + case IndexChannel: this->setChannel(variant.value()); break; + case IndexHostname: this->setHostName(variant.value()); break; + case IndexUrl: this->setVoiceRoomUrl(variant.value()); break; + default: CValueObject::setPropertyByIndex(index, variant); break; } } - QString CVoiceRoom::convertToQString(bool /* i18n */) const + QString CVoiceRoom::convertToQString(bool i18n) const { - if (!this->isValid()) return "Invalid"; - QString s = this->getVoiceRoomUrl(false); - s.append(this->isConnected() ? " connected" : " unconnected"); - if (this->m_audioPlaying) s.append(" playing"); - return s; + Q_UNUSED(i18n); + if (!this->isValid()) { return QStringLiteral("Invalid"); } + return this->getVoiceRoomUrl(false) % + (this->isConnected() ? u" connected" : u" unconnected") % + (m_audioPlaying ? u" playing" : u""); } QString CVoiceRoom::getVoiceRoomUrl(bool noProtocol) const { - if (!this->isValid()) return {}; - QString url(noProtocol ? "" : CVoiceRoom::protocolComplete()); - url.append(this->m_hostname); - url.append("/"); - url.append(this->m_channel); - return url; + if (!this->isValid()) { return {}; } + return (noProtocol ? QStringLiteral("") : CVoiceRoom::protocolComplete()) % m_hostname % u"/" % m_channel; } void CVoiceRoom::setVoiceRoomUrl(const QString &serverUrl) { if (serverUrl.isEmpty()) { - m_hostname = ""; - m_channel = ""; + m_hostname.clear(); + m_channel.clear(); } else if (serverUrl.contains("/")) { QString url = serverUrl.trimmed().toLower(); url.replace(CVoiceRoom::protocolComplete(), ""); url.replace(CVoiceRoom::protocol(), ""); - QStringList splitParts = url.split("/"); + const QStringList splitParts = url.split("/"); m_hostname = splitParts.at(0); m_channel = splitParts.at(1); } @@ -112,7 +90,7 @@ namespace BlackMisc bool CVoiceRoom::isAtis() const { - return (this->m_channel.contains("ATIS", Qt::CaseInsensitive)); + return (m_channel.contains("ATIS", Qt::CaseInsensitive)); } - } // Voice -} // BlackMisc + } // ns +} // ns diff --git a/src/blacksound/soundgenerator.cpp b/src/blacksound/soundgenerator.cpp index e7ebc0d4d..c24ab726d 100644 --- a/src/blacksound/soundgenerator.cpp +++ b/src/blacksound/soundgenerator.cpp @@ -167,7 +167,7 @@ namespace BlackSound const int bytesForAllChannels = m_audioFormat.channelCount() * bytesPerSample; qint64 totalLength = 0; - foreach(Tone t, m_tones) + for (const Tone &t : m_tones) { totalLength += m_audioFormat.sampleRate() * bytesForAllChannels * t.m_durationMs / 1000; } @@ -175,10 +175,10 @@ namespace BlackSound Q_ASSERT(totalLength % bytesForAllChannels == 0); Q_UNUSED(bytesForAllChannels) // suppress warning in release builds - m_buffer.resize(totalLength); + m_buffer.resize(static_cast(totalLength)); // potentially dangerous cast, but I see no use case where the int range on any of our platforms is exceeded unsigned char *bufferPointer = reinterpret_cast(m_buffer.data()); // clazy:exclude=detaching-member - foreach(Tone t, m_tones) + for (const Tone &t : m_tones) { qint64 bytesPerTone = m_audioFormat.sampleRate() * bytesForAllChannels * t.m_durationMs / 1000; qint64 last0AmplitudeSample = bytesPerTone; // last sample when amplitude was 0 @@ -296,35 +296,31 @@ namespace BlackSound // RIFF header if (m_audioFormat.byteOrder() == QAudioFormat::LittleEndian) + { memcpy(&header.riff.descriptor.id[0], "RIFF", 4); + } else + { memcpy(&header.riff.descriptor.id[0], "RIFX", 4); + } - qToLittleEndian(quint32(m_buffer.size() + headerLength - 8), + qToLittleEndian(quint32(static_cast(m_buffer.size()) + headerLength - 8), reinterpret_cast(&header.riff.descriptor.size)); memcpy(&header.riff.type[0], "WAVE", 4); // WAVE header memcpy(&header.wave.descriptor.id[0], "fmt ", 4); - qToLittleEndian(quint32(16), - reinterpret_cast(&header.wave.descriptor.size)); - qToLittleEndian(quint16(1), - reinterpret_cast(&header.wave.audioFormat)); - qToLittleEndian(quint16(m_audioFormat.channelCount()), - reinterpret_cast(&header.wave.numChannels)); - qToLittleEndian(quint32(m_audioFormat.sampleRate()), - reinterpret_cast(&header.wave.sampleRate)); - qToLittleEndian(quint32(m_audioFormat.sampleRate() * m_audioFormat.channelCount() * m_audioFormat.sampleSize() / 8), - reinterpret_cast(&header.wave.byteRate)); - qToLittleEndian(quint16(m_audioFormat.channelCount() * m_audioFormat.sampleSize() / 8), - reinterpret_cast(&header.wave.blockAlign)); - qToLittleEndian(quint16(m_audioFormat.sampleSize()), - reinterpret_cast(&header.wave.bitsPerSample)); + qToLittleEndian(quint32(16), reinterpret_cast(&header.wave.descriptor.size)); + qToLittleEndian(quint16(1), reinterpret_cast(&header.wave.audioFormat)); + qToLittleEndian(quint16(m_audioFormat.channelCount()), reinterpret_cast(&header.wave.numChannels)); + qToLittleEndian(quint32(m_audioFormat.sampleRate()), reinterpret_cast(&header.wave.sampleRate)); + qToLittleEndian(quint32(m_audioFormat.sampleRate() * m_audioFormat.channelCount() * m_audioFormat.sampleSize() / 8), reinterpret_cast(&header.wave.byteRate)); + qToLittleEndian(quint16(m_audioFormat.channelCount() * m_audioFormat.sampleSize() / 8), reinterpret_cast(&header.wave.blockAlign)); + qToLittleEndian(quint16(m_audioFormat.sampleSize()), reinterpret_cast(&header.wave.bitsPerSample)); // DATA header memcpy(&header.data.descriptor.id[0], "data", 4); - qToLittleEndian(quint32(m_buffer.size()), - reinterpret_cast(&header.data.descriptor.size)); + qToLittleEndian(quint32(m_buffer.size()), reinterpret_cast(&header.data.descriptor.size)); success = file.write(reinterpret_cast(&header), headerLength) == headerLength; success = success && file.write(m_buffer) == m_buffer.size(); @@ -336,7 +332,7 @@ namespace BlackSound { if (tones.isEmpty()) { return 0; } qint64 d = 0; - foreach(Tone t, tones) + for (const Tone &t : tones) { d += t.m_durationMs; } @@ -356,7 +352,7 @@ namespace BlackSound while (len - total > 0) { const qint64 chunkSize = qMin((m_buffer.size() - m_position), (len - total)); - memcpy(data + total, m_buffer.constData() + m_position, chunkSize); + memcpy(data + total, m_buffer.constData() + m_position, static_cast(chunkSize)); m_position = (m_position + chunkSize) % m_buffer.size(); total += chunkSize; if (m_position == 0 &&