Skip to content
This repository was archived by the owner on Mar 20, 2025. It is now read-only.

Commit ba48fb2

Browse files
committed
Call: Use std ranges to modernize find functions
1 parent d848442 commit ba48fb2

File tree

2 files changed

+35
-40
lines changed

2 files changed

+35
-40
lines changed

src/client/QXmppCall.cpp

+31-37
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "StringLiterals.h"
1919

2020
#include <chrono>
21+
#include <ranges>
2122

2223
// gstreamer
2324
#include <gst/gst.h>
@@ -153,7 +154,7 @@ GstCaps *QXmppCallPrivate::ptMap(uint sessionId, uint pt)
153154
return nullptr;
154155
}
155156

156-
bool QXmppCallPrivate::isFormatSupported(const QString &codecName) const
157+
bool QXmppCallPrivate::isFormatSupported(const QString &codecName)
157158
{
158159
GstElementFactory *factory;
159160
factory = gst_element_factory_find(codecName.toLatin1().data());
@@ -164,67 +165,60 @@ bool QXmppCallPrivate::isFormatSupported(const QString &codecName) const
164165
return true;
165166
}
166167

168+
bool QXmppCallPrivate::isCodecSupported(const GstCodec &codec)
169+
{
170+
return isFormatSupported(codec.gstPay) &&
171+
isFormatSupported(codec.gstDepay) &&
172+
isFormatSupported(codec.gstEnc) &&
173+
isFormatSupported(codec.gstDec);
174+
}
175+
167176
void QXmppCallPrivate::filterGStreamerFormats(QList<GstCodec> &formats)
168177
{
169-
auto it = formats.begin();
170-
while (it != formats.end()) {
171-
bool supported = isFormatSupported(it->gstPay) &&
172-
isFormatSupported(it->gstDepay) &&
173-
isFormatSupported(it->gstEnc) &&
174-
isFormatSupported(it->gstDec);
175-
if (!supported) {
176-
it = formats.erase(it);
177-
} else {
178-
++it;
179-
}
180-
}
178+
auto removedRange = std::ranges::remove_if(formats, std::not_fn(isCodecSupported));
179+
formats.erase(removedRange.begin(), removedRange.end());
181180
}
182181

183182
QXmppCallStream *QXmppCallPrivate::findStreamByMedia(QStringView media)
184183
{
185-
for (auto stream : std::as_const(streams)) {
186-
if (stream->media() == media) {
187-
return stream;
188-
}
184+
if (auto stream = std::ranges::find(streams, media, &QXmppCallStream::media);
185+
stream != streams.end()) {
186+
return *stream;
189187
}
190188
return nullptr;
191189
}
192190

193191
QXmppCallStream *QXmppCallPrivate::findStreamByName(QStringView name)
194192
{
195-
for (auto stream : std::as_const(streams)) {
196-
if (stream->name() == name) {
197-
return stream;
198-
}
193+
if (auto stream = std::ranges::find(streams, name, &QXmppCallStream::name);
194+
stream != streams.end()) {
195+
return *stream;
199196
}
200197
return nullptr;
201198
}
202199

203-
QXmppCallStream *QXmppCallPrivate::findStreamById(const int id)
200+
QXmppCallStream *QXmppCallPrivate::findStreamById(int id)
204201
{
205-
for (auto stream : std::as_const(streams)) {
206-
if (stream->id() == id) {
207-
return stream;
208-
}
202+
if (auto stream = std::ranges::find(streams, id, &QXmppCallStream::id);
203+
stream != streams.end()) {
204+
return *stream;
209205
}
210206
return nullptr;
211207
}
212208

213209
void QXmppCallPrivate::handleAck(const QXmppIq &ack)
214210
{
215-
const QString id = ack.id();
216-
for (int i = 0; i < requests.size(); ++i) {
217-
if (id == requests[i].id()) {
218-
// process acknowledgement
219-
const QXmppJingleIq request = requests.takeAt(i);
220-
q->debug(u"Received ACK for packet %1"_s.arg(id));
211+
auto request = std::ranges::find(requests, ack.id(), &QXmppJingleIq::id);
212+
if (request != requests.end()) {
213+
// process acknowledgement
214+
q->debug(u"Received ACK for packet %1"_s.arg(ack.id()));
221215

222-
// handle termination
223-
if (request.action() == QXmppJingleIq::SessionTerminate) {
224-
q->terminated();
225-
}
226-
return;
216+
// handle termination
217+
if (request->action() == QXmppJingleIq::SessionTerminate) {
218+
q->terminated();
227219
}
220+
221+
requests.erase(request);
228222
}
229223
}
230224

src/client/QXmppCall_p.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ class QXmppCallPrivate : public QObject
5252
void ssrcActive(uint sessionId, uint ssrc);
5353
void padAdded(GstPad *pad);
5454
GstCaps *ptMap(uint sessionId, uint pt);
55-
bool isFormatSupported(const QString &codecName) const;
56-
void filterGStreamerFormats(QList<GstCodec> &formats);
55+
static bool isFormatSupported(const QString &codecName);
56+
static bool isCodecSupported(const GstCodec &codec);
57+
static void filterGStreamerFormats(QList<GstCodec> &formats);
5758

5859
QXmppCallStream *createStream(const QString &media, const QString &creator, const QString &name);
5960
QXmppCallStream *findStreamByMedia(QStringView media);
6061
QXmppCallStream *findStreamByName(QStringView name);
61-
QXmppCallStream *findStreamById(const int id);
62+
QXmppCallStream *findStreamById(int id);
6263
QXmppJingleIq::Content localContent(QXmppCallStream *stream) const;
6364

6465
void handleAck(const QXmppIq &iq);

0 commit comments

Comments
 (0)