Skip to content

Commit 816bca3

Browse files
committedAug 16, 2017
client.cpp: modernize code
* Range based for loops * Empty operator on stl containers
1 parent 90dfafc commit 816bca3

File tree

2 files changed

+47
-74
lines changed

2 files changed

+47
-74
lines changed
 

‎src/client.cpp

+36-53
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ void Client::scanModSubfolder(const std::string &mod_name, const std::string &mo
144144
{
145145
std::string full_path = mod_path + DIR_DELIM + mod_subpath;
146146
std::vector<fs::DirListNode> mod = fs::GetDirListing(full_path);
147-
for (unsigned int j=0; j < mod.size(); j++){
148-
std::string filename = mod[j].name;
149-
if (mod[j].dir) {
147+
for (const fs::DirListNode &j : mod) {
148+
std::string filename = j.name;
149+
if (j.dir) {
150150
scanModSubfolder(mod_name, mod_path, mod_subpath
151151
+ filename + DIR_DELIM);
152152
continue;
@@ -230,10 +230,8 @@ Client::~Client()
230230
delete m_inventory_from_server;
231231

232232
// Delete detached inventories
233-
for (std::unordered_map<std::string, Inventory*>::iterator
234-
i = m_detached_inventories.begin();
235-
i != m_detached_inventories.end(); ++i) {
236-
delete i->second;
233+
for (auto &m_detached_inventorie : m_detached_inventories) {
234+
delete m_detached_inventorie.second;
237235
}
238236

239237
// cleanup 3d model meshes on client shutdown
@@ -556,15 +554,13 @@ void Client::step(float dtime)
556554
Update positions of sounds attached to objects
557555
*/
558556
{
559-
for(std::unordered_map<int, u16>::iterator i = m_sounds_to_objects.begin();
560-
i != m_sounds_to_objects.end(); ++i) {
561-
int client_id = i->first;
562-
u16 object_id = i->second;
557+
for (auto &m_sounds_to_object : m_sounds_to_objects) {
558+
int client_id = m_sounds_to_object.first;
559+
u16 object_id = m_sounds_to_object.second;
563560
ClientActiveObject *cao = m_env.getActiveObject(object_id);
564-
if(!cao)
561+
if (!cao)
565562
continue;
566-
v3f pos = cao->getPosition();
567-
m_sound->updateSoundPosition(client_id, pos);
563+
m_sound->updateSoundPosition(client_id, cao->getPosition());
568564
}
569565
}
570566

@@ -628,8 +624,7 @@ bool Client::loadMedia(const std::string &data, const std::string &filename)
628624
NULL
629625
};
630626
name = removeStringEnd(filename, image_ext);
631-
if(name != "")
632-
{
627+
if (!name.empty()) {
633628
verbosestream<<"Client: Attempting to load image "
634629
<<"file \""<<filename<<"\""<<std::endl;
635630

@@ -644,18 +639,17 @@ bool Client::loadMedia(const std::string &data, const std::string &filename)
644639

645640
// Read image
646641
video::IImage *img = vdrv->createImageFromFile(rfile);
647-
if(!img){
642+
if (!img) {
648643
errorstream<<"Client: Cannot create image from data of "
649644
<<"file \""<<filename<<"\""<<std::endl;
650645
rfile->drop();
651646
return false;
652647
}
653-
else {
654-
m_tsrc->insertSourceImage(filename, img);
655-
img->drop();
656-
rfile->drop();
657-
return true;
658-
}
648+
649+
m_tsrc->insertSourceImage(filename, img);
650+
img->drop();
651+
rfile->drop();
652+
return true;
659653
}
660654

661655
const char *sound_ext[] = {
@@ -664,8 +658,7 @@ bool Client::loadMedia(const std::string &data, const std::string &filename)
664658
".ogg", NULL
665659
};
666660
name = removeStringEnd(filename, sound_ext);
667-
if(name != "")
668-
{
661+
if (!name.empty()) {
669662
verbosestream<<"Client: Attempting to load sound "
670663
<<"file \""<<filename<<"\""<<std::endl;
671664
m_sound->loadSoundData(name, data);
@@ -676,9 +669,9 @@ bool Client::loadMedia(const std::string &data, const std::string &filename)
676669
".x", ".b3d", ".md2", ".obj",
677670
NULL
678671
};
672+
679673
name = removeStringEnd(filename, model_ext);
680-
if(name != "")
681-
{
674+
if (!name.empty()) {
682675
verbosestream<<"Client: Storing model into memory: "
683676
<<"\""<<filename<<"\""<<std::endl;
684677
if(m_mesh_data.count(filename))
@@ -732,9 +725,8 @@ void Client::request_media(const std::vector<std::string> &file_requests)
732725

733726
pkt << (u16) (file_requests_size & 0xFFFF);
734727

735-
for(std::vector<std::string>::const_iterator i = file_requests.begin();
736-
i != file_requests.end(); ++i) {
737-
pkt << (*i);
728+
for (const std::string &file_request : file_requests) {
729+
pkt << file_request;
738730
}
739731

740732
Send(&pkt);
@@ -1020,7 +1012,7 @@ void Client::startAuth(AuthMechanism chosen_auth_mechanism)
10201012
&verifier, &salt);
10211013

10221014
NetworkPacket resp_pkt(TOSERVER_FIRST_SRP, 0);
1023-
resp_pkt << salt << verifier << (u8)((m_password == "") ? 1 : 0);
1015+
resp_pkt << salt << verifier << (u8)((m_password.empty()) ? 1 : 0);
10241016

10251017
Send(&resp_pkt);
10261018
break;
@@ -1062,12 +1054,8 @@ void Client::sendDeletedBlocks(std::vector<v3s16> &blocks)
10621054

10631055
pkt << (u8) blocks.size();
10641056

1065-
u32 k = 0;
1066-
for(std::vector<v3s16>::iterator
1067-
j = blocks.begin();
1068-
j != blocks.end(); ++j) {
1069-
pkt << *j;
1070-
k++;
1057+
for (const v3s16 &block : blocks) {
1058+
pkt << block;
10711059
}
10721060

10731061
Send(&pkt);
@@ -1089,9 +1077,8 @@ void Client::sendRemovedSounds(std::vector<s32> &soundList)
10891077

10901078
pkt << (u16) (server_ids & 0xFFFF);
10911079

1092-
for(std::vector<s32>::iterator i = soundList.begin();
1093-
i != soundList.end(); ++i)
1094-
pkt << *i;
1080+
for (int sound_id : soundList)
1081+
pkt << sound_id;
10951082

10961083
Send(&pkt);
10971084
}
@@ -1354,10 +1341,8 @@ void Client::removeNode(v3s16 p)
13541341
catch(InvalidPositionException &e) {
13551342
}
13561343

1357-
for(std::map<v3s16, MapBlock *>::iterator
1358-
i = modified_blocks.begin();
1359-
i != modified_blocks.end(); ++i) {
1360-
addUpdateMeshTaskWithEdge(i->first, false, true);
1344+
for (const auto &modified_block : modified_blocks) {
1345+
addUpdateMeshTaskWithEdge(modified_block.first, false, true);
13611346
}
13621347
}
13631348

@@ -1374,7 +1359,7 @@ MapNode Client::getNode(v3s16 p, bool *is_valid_position)
13741359
v3s16 ppos = floatToInt(m_env.getLocalPlayer()->getPosition(), BS);
13751360
if ((u32) ppos.getDistanceFrom(p) > m_csm_noderange_limit) {
13761361
*is_valid_position = false;
1377-
return MapNode();
1362+
return {};
13781363
}
13791364
}
13801365
return m_env.getMap().getNodeNoEx(p, is_valid_position);
@@ -1393,10 +1378,8 @@ void Client::addNode(v3s16 p, MapNode n, bool remove_metadata)
13931378
catch(InvalidPositionException &e) {
13941379
}
13951380

1396-
for(std::map<v3s16, MapBlock *>::iterator
1397-
i = modified_blocks.begin();
1398-
i != modified_blocks.end(); ++i) {
1399-
addUpdateMeshTaskWithEdge(i->first, false, true);
1381+
for (const auto &modified_block : modified_blocks) {
1382+
addUpdateMeshTaskWithEdge(modified_block.first, false, true);
14001383
}
14011384
}
14021385

@@ -1567,7 +1550,7 @@ bool Client::getChatMessage(std::wstring &res)
15671550
void Client::typeChatMessage(const std::wstring &message)
15681551
{
15691552
// Discard empty line
1570-
if(message == L"")
1553+
if (message.empty())
15711554
return;
15721555

15731556
// If message was ate by script API, don't send it to server
@@ -1677,8 +1660,8 @@ float Client::mediaReceiveProgress()
16771660
{
16781661
if (m_media_downloader)
16791662
return m_media_downloader->getProgress();
1680-
else
1681-
return 1.0; // downloader only exists when not yet done
1663+
1664+
return 1.0; // downloader only exists when not yet done
16821665
}
16831666

16841667
typedef struct TextureUpdateArgs {
@@ -1746,7 +1729,7 @@ void Client::afterContentReceived()
17461729
RenderingEngine::draw_load_screen(text, guienv, m_tsrc, 0, 72);
17471730
m_nodedef->updateAliases(m_itemdef);
17481731
std::string texture_path = g_settings->get("texture_path");
1749-
if (texture_path != "" && fs::IsDir(texture_path))
1732+
if (!texture_path.empty() && fs::IsDir(texture_path))
17501733
m_nodedef->applyTextureOverrides(texture_path + DIR_DELIM + "override.txt");
17511734
m_nodedef->setNodeRegistrationStatus(true);
17521735
m_nodedef->runNodeResolveCallbacks();

‎src/client.h

+11-21
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,7 @@ struct ClientEvent
204204
class PacketCounter
205205
{
206206
public:
207-
PacketCounter()
208-
{
209-
}
207+
PacketCounter() = default;
210208

211209
void add(u16 command)
212210
{
@@ -223,23 +221,15 @@ class PacketCounter
223221

224222
void clear()
225223
{
226-
for(std::map<u16, u16>::iterator
227-
i = m_packets.begin();
228-
i != m_packets.end(); ++i)
229-
{
230-
i->second = 0;
224+
for (auto &m_packet : m_packets) {
225+
m_packet.second = 0;
231226
}
232227
}
233228

234229
void print(std::ostream &o)
235230
{
236-
for(std::map<u16, u16>::iterator
237-
i = m_packets.begin();
238-
i != m_packets.end(); ++i)
239-
{
240-
o<<"cmd "<<i->first
241-
<<" count "<<i->second
242-
<<std::endl;
231+
for (const auto &m_packet : m_packets) {
232+
o << "cmd "<< m_packet.first <<" count "<< m_packet.second << std::endl;
243233
}
244234
}
245235

@@ -544,12 +534,12 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
544534
m_client_event_queue.push(event);
545535
}
546536

547-
void showGameChat(const bool show = true);
548-
void showGameHud(const bool show = true);
549-
void showMinimap(const bool show = true);
550-
void showProfiler(const bool show = true);
551-
void showGameFog(const bool show = true);
552-
void showGameDebug(const bool show = true);
537+
void showGameChat(bool show = true);
538+
void showGameHud(bool show = true);
539+
void showMinimap(bool show = true);
540+
void showProfiler(bool show = true);
541+
void showGameFog(bool show = true);
542+
void showGameDebug(bool show = true);
553543

554544
const Address getServerAddress()
555545
{

2 commit comments

Comments
 (2)

JRottm commented on Aug 19, 2017

@JRottm
Contributor

Hi @nerzhul, you removed the "const" from the bool param of showGameChat()...showGameDebug() in client.h, but you forgot to make the same change in the implementation in client.cpp.

nerzhul commented on Aug 19, 2017

@nerzhul
ContributorAuthor

yeah it's accepted by compilers, clang-tidy reported it's not necessary to add them in the declaration they have no effect, just setting it in definition

Please sign in to comment.