Skip to content

Commit 1d086ae

Browse files
committedAug 18, 2017
Modernize various files (part 2)
* range-based for loops * emplace_back instead of push_back * code style * C++ headers instead of C headers * Default operators * empty stl function
1 parent 55ab426 commit 1d086ae

10 files changed

+107
-152
lines changed
 

‎src/guiscalingfilter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ video::ITexture *guiScalingImageButton(video::IVideoDriver *driver, video::IText
4646
*/
4747
void draw2DImageFilterScaled(video::IVideoDriver *driver, video::ITexture *txr,
4848
const core::rect<s32> &destrect, const core::rect<s32> &srcrect,
49-
const core::rect<s32> *cliprect = 0, video::SColor *const colors = 0,
49+
const core::rect<s32> *cliprect = 0, const video::SColor *const colors = 0,
5050
bool usealpha = false);

‎src/httpfetch.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ struct HTTPFetchResult
7676
unsigned long caller = HTTPFETCH_DISCARD;
7777
unsigned long request_id = 0;
7878

79-
HTTPFetchResult() {}
79+
HTTPFetchResult() = default;
8080

8181
HTTPFetchResult(const HTTPFetchRequest &fetch_request)
8282
: caller(fetch_request.caller), request_id(fetch_request.request_id)

‎src/hud.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ Hud::Hud(gui::IGUIEnvironment *guienv, Client *client, LocalPlayer *player,
5555
m_hotbar_imagesize *= m_hud_scaling;
5656
m_padding = m_hotbar_imagesize / 12;
5757

58-
for (unsigned int i = 0; i < 4; i++)
59-
hbar_colors[i] = video::SColor(255, 255, 255, 255);
58+
for (auto &hbar_color : hbar_colors)
59+
hbar_color = video::SColor(255, 255, 255, 255);
6060

6161
tsrc = client->getTextureSource();
6262

@@ -220,7 +220,7 @@ void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount,
220220
// Store hotbar_image in member variable, used by drawItem()
221221
if (hotbar_image != player->hotbar_image) {
222222
hotbar_image = player->hotbar_image;
223-
if (hotbar_image != "")
223+
if (!hotbar_image.empty())
224224
use_hotbar_image = tsrc->isKnownSourceImage(hotbar_image);
225225
else
226226
use_hotbar_image = false;
@@ -229,7 +229,7 @@ void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount,
229229
// Store hotbar_selected_image in member variable, used by drawItem()
230230
if (hotbar_selected_image != player->hotbar_selected_image) {
231231
hotbar_selected_image = player->hotbar_selected_image;
232-
if (hotbar_selected_image != "")
232+
if (!hotbar_selected_image.empty())
233233
use_hotbar_selected_image = tsrc->isKnownSourceImage(hotbar_selected_image);
234234
else
235235
use_hotbar_selected_image = false;
@@ -572,7 +572,7 @@ void Hud::updateSelectionMesh(const v3s16 &camera_offset)
572572
m_selection_mesh = NULL;
573573
}
574574

575-
if (!m_selection_boxes.size()) {
575+
if (m_selection_boxes.empty()) {
576576
// No pointed object
577577
return;
578578
}
@@ -597,10 +597,8 @@ void Hud::updateSelectionMesh(const v3s16 &camera_offset)
597597
aabb3f halo_box(100.0, 100.0, 100.0, -100.0, -100.0, -100.0);
598598
m_halo_boxes.clear();
599599

600-
for (std::vector<aabb3f>::iterator
601-
i = m_selection_boxes.begin();
602-
i != m_selection_boxes.end(); ++i) {
603-
halo_box.addInternalBox(*i);
600+
for (const auto &selection_box : m_selection_boxes) {
601+
halo_box.addInternalBox(selection_box);
604602
}
605603

606604
m_halo_boxes.push_back(halo_box);

‎src/imagefilters.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1818

1919
#include "imagefilters.h"
2020
#include "util/numeric.h"
21-
#include <math.h>
21+
#include <cmath>
2222

2323
/* Fill in RGB values for transparent pixels, to correct for odd colors
2424
* appearing at borders when blending. This is because many PNG optimizers

‎src/intlGUIEditBox.cpp

+18-24
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
630630
if ( !this->IsEnabled )
631631
break;
632632

633-
if (Text.size())
634-
{
633+
if (!Text.empty()) {
635634
core::stringw s;
636635

637636
if (MarkBegin != MarkEnd)
@@ -670,8 +669,7 @@ bool intlGUIEditBox::processKey(const SEvent& event)
670669
if ( !this->IsEnabled )
671670
break;
672671

673-
if (Text.size() != 0)
674-
{
672+
if (!Text.empty()) {
675673
core::stringw s;
676674

677675
if (MarkBegin != MarkEnd)
@@ -820,8 +818,7 @@ void intlGUIEditBox::draw()
820818
const bool prevOver = OverrideColorEnabled;
821819
const video::SColor prevColor = OverrideColor;
822820

823-
if (Text.size())
824-
{
821+
if (!Text.empty()) {
825822
if (!IsEnabled && !OverrideColorEnabled)
826823
{
827824
OverrideColorEnabled = true;
@@ -908,7 +905,7 @@ void intlGUIEditBox::draw()
908905
// draw marked text
909906
s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
910907

911-
if (s.size())
908+
if (!s.empty())
912909
font->draw(s.c_str(), CurrentTextRect,
913910
OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
914911
false, true, &localClipRect);
@@ -1057,24 +1054,22 @@ bool intlGUIEditBox::processMouse(const SEvent& event)
10571054
else
10581055
{
10591056
if (!AbsoluteClippingRect.isPointInside(
1060-
core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y)))
1061-
{
1057+
core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y))) {
10621058
return false;
10631059
}
1064-
else
1065-
{
1066-
// move cursor
1067-
CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
10681060

1069-
s32 newMarkBegin = MarkBegin;
1070-
if (!MouseMarking)
1071-
newMarkBegin = CursorPos;
10721061

1073-
MouseMarking = true;
1074-
setTextMarkers( newMarkBegin, CursorPos);
1075-
calculateScrollPos();
1076-
return true;
1077-
}
1062+
// move cursor
1063+
CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
1064+
1065+
s32 newMarkBegin = MarkBegin;
1066+
if (!MouseMarking)
1067+
newMarkBegin = CursorPos;
1068+
1069+
MouseMarking = true;
1070+
setTextMarkers( newMarkBegin, CursorPos);
1071+
calculateScrollPos();
1072+
return true;
10781073
}
10791074
default:
10801075
break;
@@ -1185,8 +1180,7 @@ void intlGUIEditBox::breakText()
11851180

11861181
if (c == L' ' || c == 0 || i == (size-1))
11871182
{
1188-
if (word.size())
1189-
{
1183+
if (!word.empty()) {
11901184
// here comes the next whitespace, look if
11911185
// we can break the last word to the next line.
11921186
s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
@@ -1488,7 +1482,7 @@ void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeRe
14881482
setAutoScroll(in->getAttributeAsBool("AutoScroll"));
14891483
core::stringw ch = in->getAttributeAsStringW("PasswordChar");
14901484

1491-
if (!ch.size())
1485+
if (ch.empty())
14921486
setPasswordBox(in->getAttributeAsBool("PasswordBox"));
14931487
else
14941488
setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);

‎src/inventory.cpp

+52-85
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3535

3636
static content_t content_translate_from_19_to_internal(content_t c_from)
3737
{
38-
for(u32 i=0; i<sizeof(trans_table_19)/sizeof(trans_table_19[0]); i++)
39-
{
40-
if(trans_table_19[i][1] == c_from)
41-
{
42-
return trans_table_19[i][0];
38+
for (const auto &tt : trans_table_19) {
39+
if(tt[1] == c_from) {
40+
return tt[0];
4341
}
4442
}
4543
return c_from;
@@ -116,7 +114,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
116114
NameIdMapping legacy_nimap;
117115
content_mapnode_get_name_id_mapping(&legacy_nimap);
118116
legacy_nimap.getName(material, name);
119-
if(name == "")
117+
if(name.empty())
120118
name = "unknown_block";
121119
if (itemdef)
122120
name = itemdef->getAlias(name);
@@ -136,7 +134,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
136134
NameIdMapping legacy_nimap;
137135
content_mapnode_get_name_id_mapping(&legacy_nimap);
138136
legacy_nimap.getName(material, name);
139-
if(name == "")
137+
if(name.empty())
140138
name = "unknown_block";
141139
if (itemdef)
142140
name = itemdef->getAlias(name);
@@ -207,21 +205,20 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
207205
// Read the count
208206
std::string count_str;
209207
std::getline(is, count_str, ' ');
210-
if(count_str.empty())
211-
{
208+
if (count_str.empty()) {
212209
count = 1;
213210
break;
214211
}
215-
else
216-
count = stoi(count_str);
212+
213+
count = stoi(count_str);
217214

218215
// Read the wear
219216
std::string wear_str;
220217
std::getline(is, wear_str, ' ');
221218
if(wear_str.empty())
222219
break;
223-
else
224-
wear = stoi(wear_str);
220+
221+
wear = stoi(wear_str);
225222

226223
// Read metadata
227224
metadata.deSerialize(is);
@@ -388,17 +385,12 @@ InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager
388385
clearItems();
389386
}
390387

391-
InventoryList::~InventoryList()
392-
{
393-
}
394-
395388
void InventoryList::clearItems()
396389
{
397390
m_items.clear();
398391

399-
for(u32 i=0; i<m_size; i++)
400-
{
401-
m_items.push_back(ItemStack());
392+
for (u32 i=0; i < m_size; i++) {
393+
m_items.emplace_back();
402394
}
403395

404396
//setDirty(true);
@@ -427,15 +419,10 @@ void InventoryList::serialize(std::ostream &os) const
427419

428420
os<<"Width "<<m_width<<"\n";
429421

430-
for(u32 i=0; i<m_items.size(); i++)
431-
{
432-
const ItemStack &item = m_items[i];
433-
if(item.empty())
434-
{
422+
for (const auto &item : m_items) {
423+
if (item.empty()) {
435424
os<<"Empty";
436-
}
437-
else
438-
{
425+
} else {
439426
os<<"Item ";
440427
item.serialize(os);
441428
}
@@ -464,17 +451,16 @@ void InventoryList::deSerialize(std::istream &is)
464451
std::string name;
465452
std::getline(iss, name, ' ');
466453

467-
if(name == "EndInventoryList")
468-
{
454+
if (name == "EndInventoryList") {
469455
break;
470456
}
457+
471458
// This is a temporary backwards compatibility fix
472-
else if(name == "end")
473-
{
459+
if (name == "end") {
474460
break;
475461
}
476-
else if(name == "Width")
477-
{
462+
463+
if (name == "Width") {
478464
iss >> m_width;
479465
if (iss.fail())
480466
throw SerializationError("incorrect width property");
@@ -551,9 +537,8 @@ u32 InventoryList::getWidth() const
551537
u32 InventoryList::getUsedSlots() const
552538
{
553539
u32 num = 0;
554-
for(u32 i=0; i<m_items.size(); i++)
555-
{
556-
if(!m_items[i].empty())
540+
for (const auto &m_item : m_items) {
541+
if (!m_item.empty())
557542
num++;
558543
}
559544
return num;
@@ -672,20 +657,17 @@ bool InventoryList::roomForItem(const ItemStack &item_) const
672657
bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
673658
{
674659
u32 count = item.count;
675-
if(count == 0)
660+
if (count == 0)
676661
return true;
677-
for(std::vector<ItemStack>::const_reverse_iterator
678-
i = m_items.rbegin();
679-
i != m_items.rend(); ++i)
680-
{
681-
if(count == 0)
662+
663+
for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) {
664+
if (count == 0)
682665
break;
683-
if (i->name == item.name
684-
&& (!match_meta || (i->metadata == item.metadata))) {
666+
if (i->name == item.name && (!match_meta || (i->metadata == item.metadata))) {
685667
if (i->count >= count)
686668
return true;
687-
else
688-
count -= i->count;
669+
670+
count -= i->count;
689671
}
690672
}
691673
return false;
@@ -694,15 +676,11 @@ bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
694676
ItemStack InventoryList::removeItem(const ItemStack &item)
695677
{
696678
ItemStack removed;
697-
for(std::vector<ItemStack>::reverse_iterator
698-
i = m_items.rbegin();
699-
i != m_items.rend(); ++i)
700-
{
701-
if(i->name == item.name)
702-
{
679+
for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) {
680+
if (i->name == item.name) {
703681
u32 still_to_remove = item.count - removed.count;
704682
removed.addItem(i->takeItem(still_to_remove), m_itemdef);
705-
if(removed.count == item.count)
683+
if (removed.count == item.count)
706684
break;
707685
}
708686
}
@@ -815,21 +793,17 @@ Inventory::~Inventory()
815793
void Inventory::clear()
816794
{
817795
m_dirty = true;
818-
for(u32 i=0; i<m_lists.size(); i++)
819-
{
820-
delete m_lists[i];
796+
for (auto &m_list : m_lists) {
797+
delete m_list;
821798
}
822799
m_lists.clear();
823800
}
824801

825802
void Inventory::clearContents()
826803
{
827804
m_dirty = true;
828-
for(u32 i=0; i<m_lists.size(); i++)
829-
{
830-
InventoryList *list = m_lists[i];
831-
for(u32 j=0; j<list->getSize(); j++)
832-
{
805+
for (InventoryList *list : m_lists) {
806+
for (u32 j=0; j<list->getSize(); j++) {
833807
list->deleteItem(j);
834808
}
835809
}
@@ -855,9 +829,8 @@ Inventory & Inventory::operator = (const Inventory &other)
855829
m_dirty = true;
856830
clear();
857831
m_itemdef = other.m_itemdef;
858-
for(u32 i=0; i<other.m_lists.size(); i++)
859-
{
860-
m_lists.push_back(new InventoryList(*other.m_lists[i]));
832+
for (InventoryList *list : other.m_lists) {
833+
m_lists.push_back(new InventoryList(*list));
861834
}
862835
}
863836
return *this;
@@ -878,9 +851,7 @@ bool Inventory::operator == (const Inventory &other) const
878851

879852
void Inventory::serialize(std::ostream &os) const
880853
{
881-
for(u32 i=0; i<m_lists.size(); i++)
882-
{
883-
InventoryList *list = m_lists[i];
854+
for (InventoryList *list : m_lists) {
884855
os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
885856
list->serialize(os);
886857
}
@@ -902,17 +873,16 @@ void Inventory::deSerialize(std::istream &is)
902873
std::string name;
903874
std::getline(iss, name, ' ');
904875

905-
if(name == "EndInventory")
906-
{
876+
if (name == "EndInventory") {
907877
break;
908878
}
879+
909880
// This is a temporary backwards compatibility fix
910-
else if(name == "end")
911-
{
881+
if (name == "end") {
912882
break;
913883
}
914-
else if(name == "List")
915-
{
884+
885+
if (name == "List") {
916886
std::string listname;
917887
u32 listsize;
918888

@@ -944,15 +914,14 @@ InventoryList * Inventory::addList(const std::string &name, u32 size)
944914
}
945915
return m_lists[i];
946916
}
947-
else
948-
{
949-
//don't create list with invalid name
950-
if (name.find(" ") != std::string::npos) return NULL;
951917

952-
InventoryList *list = new InventoryList(name, size, m_itemdef);
953-
m_lists.push_back(list);
954-
return list;
955-
}
918+
919+
//don't create list with invalid name
920+
if (name.find(' ') != std::string::npos) return NULL;
921+
922+
InventoryList *list = new InventoryList(name, size, m_itemdef);
923+
m_lists.push_back(list);
924+
return list;
956925
}
957926

958927
InventoryList * Inventory::getList(const std::string &name)
@@ -966,9 +935,7 @@ InventoryList * Inventory::getList(const std::string &name)
966935
std::vector<const InventoryList*> Inventory::getLists()
967936
{
968937
std::vector<const InventoryList*> lists;
969-
for(u32 i=0; i<m_lists.size(); i++)
970-
{
971-
InventoryList *list = m_lists[i];
938+
for (auto list : m_lists) {
972939
lists.push_back(list);
973940
}
974941
return lists;

‎src/inventory.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class InventoryList
172172
{
173173
public:
174174
InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef);
175-
~InventoryList();
175+
~InventoryList() = default;
176176
void clearItems();
177177
void setSize(u32 newsize);
178178
void setWidth(u32 newWidth);

‎src/inventorymanager.cpp

+16-18
Original file line numberDiff line numberDiff line change
@@ -774,17 +774,15 @@ void ICraftAction::apply(InventoryManager *mgr,
774774

775775
// Add the new replacements to the list
776776
IItemDefManager *itemdef = gamedef->getItemDefManager();
777-
for (std::vector<ItemStack>::iterator it = temp.begin();
778-
it != temp.end(); ++it) {
779-
for (std::vector<ItemStack>::iterator jt = output_replacements.begin();
780-
jt != output_replacements.end(); ++jt) {
781-
if (it->name == jt->name) {
782-
*it = jt->addItem(*it, itemdef);
783-
if (it->empty())
777+
for (auto &itemstack : temp) {
778+
for (auto &output_replacement : output_replacements) {
779+
if (itemstack.name == output_replacement.name) {
780+
itemstack = output_replacement.addItem(itemstack, itemdef);
781+
if (itemstack.empty())
784782
continue;
785783
}
786784
}
787-
output_replacements.push_back(*it);
785+
output_replacements.push_back(itemstack);
788786
}
789787

790788
actionstream << player->getDescription()
@@ -795,7 +793,8 @@ void ICraftAction::apply(InventoryManager *mgr,
795793
// Decrement counter
796794
if (count_remaining == 1)
797795
break;
798-
else if (count_remaining > 1)
796+
797+
if (count_remaining > 1)
799798
count_remaining--;
800799

801800
// Get next crafting result
@@ -806,24 +805,23 @@ void ICraftAction::apply(InventoryManager *mgr,
806805

807806
// Put the replacements in the inventory or drop them on the floor, if
808807
// the invenotry is full
809-
for (std::vector<ItemStack>::iterator it = output_replacements.begin();
810-
it != output_replacements.end(); ++it) {
808+
for (auto &output_replacement : output_replacements) {
811809
if (list_main)
812-
*it = list_main->addItem(*it);
813-
if (it->empty())
810+
output_replacement = list_main->addItem(output_replacement);
811+
if (output_replacement.empty())
814812
continue;
815-
u16 count = it->count;
813+
u16 count = output_replacement.count;
816814
do {
817-
PLAYER_TO_SA(player)->item_OnDrop(*it, player,
815+
PLAYER_TO_SA(player)->item_OnDrop(output_replacement, player,
818816
player->getBasePosition());
819-
if (count >= it->count) {
817+
if (count >= output_replacement.count) {
820818
errorstream << "Couldn't drop replacement stack " <<
821-
it->getItemString() << " because drop loop didn't "
819+
output_replacement.getItemString() << " because drop loop didn't "
822820
"decrease count." << std::endl;
823821

824822
break;
825823
}
826-
} while (!it->empty());
824+
} while (!output_replacement.empty());
827825
}
828826

829827
infostream<<"ICraftAction::apply(): crafted "

‎src/inventorymanager.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct InventoryLocation
5454
type = PLAYER;
5555
name = name_;
5656
}
57-
void setNodeMeta(v3s16 p_)
57+
void setNodeMeta(const v3s16 &p_)
5858
{
5959
type = NODEMETA;
6060
p = p_;
@@ -105,8 +105,8 @@ struct InventoryAction;
105105
class InventoryManager
106106
{
107107
public:
108-
InventoryManager(){}
109-
virtual ~InventoryManager(){}
108+
InventoryManager() = default;
109+
virtual ~InventoryManager() = default;
110110

111111
// Get an inventory (server and client)
112112
virtual Inventory* getInventory(const InventoryLocation &loc){return NULL;}
@@ -131,7 +131,7 @@ struct InventoryAction
131131
virtual void apply(InventoryManager *mgr, ServerActiveObject *player,
132132
IGameDef *gamedef) = 0;
133133
virtual void clientApply(InventoryManager *mgr, IGameDef *gamedef) = 0;
134-
virtual ~InventoryAction() {};
134+
virtual ~InventoryAction() = default;;
135135
};
136136

137137
struct IMoveAction : public InventoryAction
@@ -151,7 +151,7 @@ struct IMoveAction : public InventoryAction
151151
bool caused_by_move_somewhere = false;
152152
u32 move_count = 0;
153153

154-
IMoveAction() {}
154+
IMoveAction() = default;
155155

156156
IMoveAction(std::istream &is, bool somewhere);
157157

@@ -189,7 +189,7 @@ struct IDropAction : public InventoryAction
189189
std::string from_list;
190190
s16 from_i = -1;
191191

192-
IDropAction() {}
192+
IDropAction() = default;
193193

194194
IDropAction(std::istream &is);
195195

@@ -218,7 +218,7 @@ struct ICraftAction : public InventoryAction
218218
u16 count = 0;
219219
InventoryLocation craft_inv;
220220

221-
ICraftAction() {}
221+
ICraftAction() = default;
222222

223223
ICraftAction(std::istream &is);
224224

‎src/itemstackmetadata.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ void ItemStackMetadata::serialize(std::ostream &os) const
1313
{
1414
std::ostringstream os2;
1515
os2 << DESERIALIZE_START;
16-
for (StringMap::const_iterator
17-
it = m_stringvars.begin();
18-
it != m_stringvars.end(); ++it) {
19-
os2 << it->first << DESERIALIZE_KV_DELIM
20-
<< it->second << DESERIALIZE_PAIR_DELIM;
16+
for (const auto &stringvar : m_stringvars) {
17+
os2 << stringvar.first << DESERIALIZE_KV_DELIM
18+
<< stringvar.second << DESERIALIZE_PAIR_DELIM;
2119
}
2220
os << serializeJsonStringIfNeeded(os2.str());
2321
}

0 commit comments

Comments
 (0)
Please sign in to comment.