Skip to content

Commit ee8d03f

Browse files
authored
use stl-style and ranged-for iteration for variant containers (#48322)
1 parent 13de5fb commit ee8d03f

24 files changed

Lines changed: 68 additions & 115 deletions

src/core/packet/retryrequestpacket.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ bool RetryRequestPacket::fromVariant(const Variant &in)
155155
return false;
156156

157157
requests.clear();
158-
foreach(const Variant &i, obj["requests"].toList())
158+
for(const Variant &i : obj["requests"].toList())
159159
{
160160
if(typeId(i) != VariantType::Hash)
161161
return false;
@@ -283,7 +283,7 @@ bool RetryRequestPacket::fromVariant(const Variant &in)
283283
if(typeId(vrequestData["headers"]) != VariantType::List)
284284
return false;
285285

286-
foreach(const Variant &i, vrequestData["headers"].toList())
286+
for(const Variant &i : vrequestData["headers"].toList())
287287
{
288288
VariantList list = i.toList();
289289
if(list.count() != 2)
@@ -333,11 +333,8 @@ bool RetryRequestPacket::fromVariant(const Variant &in)
333333
return false;
334334

335335
VariantHash vlastIds = vinspect["last-ids"].toHash();
336-
QHashIterator<QString, Variant> it(vlastIds);
337-
while(it.hasNext())
336+
for(auto it = vlastIds.constBegin(); it != vlastIds.constEnd(); ++it)
338337
{
339-
it.next();
340-
341338
if(typeId(it.value()) != VariantType::ByteArray)
342339
return false;
343340

src/core/packet/wscontrolpacket.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
7575
WsControlPacket out;
7676
77-
foreach(const Variant &vitem, vitems)
77+
for(const Variant &vitem : vitems)
7878
{
7979
Message msg;
8080
@@ -262,7 +262,7 @@ bool WsControlPacket::fromVariant(const Variant &in)
262262
VariantList vitems = obj["items"].toList();
263263

264264
items.clear();
265-
foreach(const Variant &v, vitems)
265+
for(const Variant &v : vitems)
266266
{
267267
if(typeId(v) != VariantType::Hash)
268268
return false;

src/core/tnetstring.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,8 @@ QByteArray fromVariant(const Variant &in)
8585
QByteArray fromHash(const VariantHash &in)
8686
{
8787
QByteArray val;
88-
QHashIterator<QString, Variant> it(in);
89-
while(it.hasNext())
88+
for(auto it = in.constBegin(); it != in.constEnd(); ++it)
9089
{
91-
it.next();
9290
val += fromByteArray(it.key().toUtf8());
9391
val += fromVariant(it.value());
9492
}
@@ -98,7 +96,7 @@ QByteArray fromHash(const VariantHash &in)
9896
QByteArray fromList(const VariantList &in)
9997
{
10098
QByteArray val;
101-
foreach(const Variant &v, in)
99+
for(const Variant &v : in)
102100
val += fromVariant(v);
103101
return QByteArray::number(val.size()) + ':' + val + ']';
104102
}
@@ -393,16 +391,15 @@ QString variantToString(const Variant &in, int indent)
393391
else
394392
out += ' ';
395393

396-
QHashIterator<QString, Variant> it(hash);
397-
while(it.hasNext())
394+
for(auto it = hash.constBegin(); it != hash.constEnd(); ++it)
398395
{
399-
it.next();
400-
401396
if(indent >= 0)
402397
out += QString(indent + 2, ' ');
403398

404399
out += '\"' + byteArrayToEscapedString(it.key().toUtf8()) + "\": " + variantToString(it.value(), indent >= 0 ? indent + 2 : -1);
405-
if(it.hasNext())
400+
auto next_it = it;
401+
++next_it;
402+
if(next_it != hash.constEnd())
406403
out += ',';
407404

408405
if(indent >= 0)

src/core/zhttprequestpacket.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ bool ZhttpRequestPacket::fromVariant(const Variant &in)
207207
else if(typeId(obj["id"]) == VariantType::List)
208208
{
209209
VariantList vl = obj["id"].toList();
210-
foreach(const Variant &v, vl)
210+
for(const Variant &v : vl)
211211
{
212212
if(typeId(v) != VariantType::Hash)
213213
return false;
@@ -370,7 +370,7 @@ bool ZhttpRequestPacket::fromVariant(const Variant &in)
370370
if(typeId(obj["headers"]) != VariantType::List)
371371
return false;
372372

373-
foreach(const Variant &i, obj["headers"].toList())
373+
for(const Variant &i : obj["headers"].toList())
374374
{
375375
VariantList list = i.toList();
376376
if(list.count() != 2)

src/core/zhttpresponsepacket.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ bool ZhttpResponsePacket::fromVariant(const Variant &in)
150150
else if(typeId(obj["id"]) == VariantType::List)
151151
{
152152
VariantList vl = obj["id"].toList();
153-
foreach(const Variant &v, vl)
153+
for(const Variant &v : vl)
154154
{
155155
if(typeId(v) != VariantType::Hash)
156156
return false;
@@ -277,7 +277,7 @@ bool ZhttpResponsePacket::fromVariant(const Variant &in)
277277
if(typeId(obj["headers"]) != VariantType::List)
278278
return false;
279279

280-
foreach(const Variant &i, obj["headers"].toList())
280+
for(const Variant &i : obj["headers"].toList())
281281
{
282282
VariantList list = i.toList();
283283
if(list.count() != 2)

src/handler/conncheckworker.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ ConnCheckWorker::ConnCheckWorker(ZrpcRequest *req, ZrpcManager *proxyControlClie
4242

4343
VariantList vids = args["ids"].toList();
4444

45-
foreach(const Variant &vid, vids)
45+
for(const Variant &vid : vids)
4646
{
4747
if(typeId(vid) != VariantType::ByteArray)
4848
{
@@ -53,7 +53,7 @@ ConnCheckWorker::ConnCheckWorker(ZrpcRequest *req, ZrpcManager *proxyControlClie
5353
cids_ += QString::fromUtf8(vid.toByteArray());
5454
}
5555

56-
foreach(const QString &cid, cids_)
56+
for(const QString &cid : cids_)
5757
{
5858
if(!stats->checkConnection(cid.toUtf8()))
5959
missing_ += cid;
@@ -82,7 +82,7 @@ void ConnCheckWorker::doFinish()
8282
cids_.remove(cid);
8383

8484
VariantList result;
85-
foreach(const QString &cid, cids_)
85+
for(const QString &cid : cids_)
8686
result += cid.toUtf8();
8787

8888
req_->respond(result);

src/handler/controlrequest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ConnCheck : public Deferred
4040
finishedConnection = req->finished.connect(boost::bind(&ConnCheck::req_finished, this));
4141

4242
VariantList vcids;
43-
foreach(const QString &cid, cids)
43+
for(const QString &cid : cids)
4444
vcids += cid.toUtf8();
4545

4646
VariantHash args;
@@ -66,7 +66,7 @@ class ConnCheck : public Deferred
6666
VariantList result = vresult.toList();
6767

6868
CidSet out;
69-
foreach(const Variant &vcid, result)
69+
for(const Variant &vcid : result)
7070
{
7171
if(typeId(vcid) != VariantType::ByteArray)
7272
{

src/handler/handlerengine.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static QList<PublishItem> parseItems(const VariantList &vitems, bool *ok = 0, QS
9494
{
9595
QList<PublishItem> out;
9696

97-
foreach(const Variant &vitem, vitems)
97+
for(const Variant &vitem : vitems)
9898
{
9999
bool ok_;
100100
PublishItem item = PublishItem::fromVariant(vitem, QString(), &ok_, errorMessage);
@@ -163,7 +163,7 @@ class InspectWorker : public Deferred
163163
return;
164164
}
165165

166-
foreach(const Variant &vheader, args["headers"].toList())
166+
for(const Variant &vheader : args["headers"].toList())
167167
{
168168
if(typeId(vheader) != VariantType::List)
169169
{
@@ -488,7 +488,7 @@ class AcceptWorker : public Deferred
488488

489489
VariantList packets = args["conn-max"].toList();
490490

491-
foreach(const Variant &data, packets)
491+
for(const Variant &data : packets)
492492
{
493493
StatsPacket p;
494494
if(!p.fromVariant("conn-max", data) || p.type != StatsPacket::ConnectionsMax)
@@ -557,7 +557,7 @@ class AcceptWorker : public Deferred
557557
}
558558

559559
VariantList vchannels = args["channels"].toList();
560-
foreach(const Variant &v, vchannels)
560+
for(const Variant &v : vchannels)
561561
{
562562
if(typeId(v) != VariantType::ByteArray)
563563
{
@@ -588,7 +588,7 @@ class AcceptWorker : public Deferred
588588
return;
589589
}
590590

591-
foreach(const Variant &vr, args["requests"].toList())
591+
for(const Variant &vr : args["requests"].toList())
592592
{
593593
RequestState rs = RequestState::fromVariant(vr);
594594
if(rs.rid.first.isEmpty())
@@ -650,7 +650,7 @@ class AcceptWorker : public Deferred
650650
return;
651651
}
652652

653-
foreach(const Variant &vheader, rd["headers"].toList())
653+
for(const Variant &vheader : rd["headers"].toList())
654654
{
655655
if(typeId(vheader) != VariantType::List)
656656
{
@@ -726,11 +726,8 @@ class AcceptWorker : public Deferred
726726
}
727727

728728
VariantHash vlastIds = vinspect["last-ids"].toHash();
729-
QHashIterator<QString, Variant> it(vlastIds);
730-
while(it.hasNext())
729+
for(auto it = vlastIds.constBegin(); it != vlastIds.constEnd(); ++it)
731730
{
732-
it.next();
733-
734731
if(typeId(it.value()) != VariantType::ByteArray)
735732
{
736733
respondError("bad-request");
@@ -866,7 +863,7 @@ class AcceptWorker : public Deferred
866863
if(!rd.contains("headers") || typeId(rd["headers"]) != VariantType::List)
867864
return HttpRequestData();
868865

869-
foreach(const Variant &vheader, rd["headers"].toList())
866+
for(const Variant &vheader : rd["headers"].toList())
870867
{
871868
if(typeId(vheader) != VariantType::List)
872869
return HttpRequestData();
@@ -1939,7 +1936,7 @@ class HandlerEngine::Private
19391936
{
19401937
VariantList packets = args["conn-max"].toList();
19411938

1942-
foreach(const Variant &data, packets)
1939+
for(const Variant &data : packets)
19431940
{
19441941
StatsPacket p;
19451942
if(!p.fromVariant("conn-max", data) || p.type != StatsPacket::ConnectionsMax)

src/handler/httpsession.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -919,10 +919,8 @@ class HttpSession::Private
919919
{
920920
// Don't add the same header name twice. We'll collect all values for a single header
921921
bool found = false;
922-
QMapIterator<QString, Variant> it(vheaders);
923-
while(it.hasNext())
922+
for(auto it = vheaders.constBegin(); it != vheaders.constEnd(); ++it)
924923
{
925-
it.next();
926924
const QString &name = it.key();
927925

928926
QByteArray uname = name.toUtf8();

src/handler/instruct.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ Instruct Instruct::fromResponse(const HttpResponseData &response, bool *ok, QStr
448448
return Instruct();
449449
}
450450

451-
foreach(const Variant &vchannel, vchannels)
451+
for(const Variant &vchannel : vchannels)
452452
{
453453
QString cpn = "channel";
454454
Channel c;
@@ -477,7 +477,7 @@ Instruct Instruct::fromResponse(const HttpResponseData &response, bool *ok, QStr
477477
return Instruct();
478478
}
479479

480-
foreach(const Variant &vfilter, vfilters)
480+
for(const Variant &vfilter : vfilters)
481481
{
482482
QString filter = getString(vfilter, &ok_);
483483
if(!ok_)
@@ -585,10 +585,8 @@ Instruct Instruct::fromResponse(const HttpResponseData &response, bool *ok, QStr
585585
{
586586
VariantHash hmeta = vmeta.toHash();
587587

588-
QHashIterator<QString, Variant> it(hmeta);
589-
while(it.hasNext())
588+
for(auto it = hmeta.constBegin(); it != hmeta.constEnd(); ++it)
590589
{
591-
it.next();
592590
const QString &key = it.key();
593591
const Variant &vval = it.value();
594592

@@ -606,10 +604,8 @@ Instruct Instruct::fromResponse(const HttpResponseData &response, bool *ok, QStr
606604
{
607605
VariantMap mmeta = vmeta.toMap();
608606

609-
QMapIterator<QString, Variant> it(mmeta);
610-
while(it.hasNext())
607+
for(auto it = mmeta.constBegin(); it != mmeta.constEnd(); ++it)
611608
{
612-
it.next();
613609
const QString &key = it.key();
614610
const Variant &vval = it.value();
615611

@@ -680,7 +676,7 @@ Instruct Instruct::fromResponse(const HttpResponseData &response, bool *ok, QStr
680676
Variant vheaders = keyedObjectGetValue(in, "headers");
681677
if(typeId(vheaders) == VariantType::List)
682678
{
683-
foreach(const Variant &vheader, vheaders.toList())
679+
for(const Variant &vheader : vheaders.toList())
684680
{
685681
if(typeId(vheader) != VariantType::List)
686682
{
@@ -718,10 +714,8 @@ Instruct Instruct::fromResponse(const HttpResponseData &response, bool *ok, QStr
718714
{
719715
VariantHash hheaders = vheaders.toHash();
720716

721-
QHashIterator<QString, Variant> it(hheaders);
722-
while(it.hasNext())
717+
for(auto it = hheaders.constBegin(); it != hheaders.constEnd(); ++it)
723718
{
724-
it.next();
725719
const QString &key = it.key();
726720
const Variant &vval = it.value();
727721

@@ -739,10 +733,8 @@ Instruct Instruct::fromResponse(const HttpResponseData &response, bool *ok, QStr
739733
{
740734
VariantMap mheaders = vheaders.toMap();
741735

742-
QMapIterator<QString, Variant> it(mheaders);
743-
while(it.hasNext())
736+
for(auto it = mheaders.constBegin(); it != mheaders.constEnd(); ++it)
744737
{
745-
it.next();
746738
const QString &key = it.key();
747739
const Variant &vval = it.value();
748740

0 commit comments

Comments
 (0)