From 1d086aee7cc193bed2f8ca09cc2e020f509378f1 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Fri, 18 Aug 2017 08:07:59 +0200 Subject: [PATCH] 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 --- src/guiscalingfilter.h | 2 +- src/httpfetch.h | 2 +- src/hud.cpp | 16 ++--- src/imagefilters.cpp | 2 +- src/intlGUIEditBox.cpp | 42 +++++------- src/inventory.cpp | 137 +++++++++++++++----------------------- src/inventory.h | 2 +- src/inventorymanager.cpp | 34 +++++----- src/inventorymanager.h | 14 ++-- src/itemstackmetadata.cpp | 8 +-- 10 files changed, 107 insertions(+), 152 deletions(-) diff --git a/src/guiscalingfilter.h b/src/guiscalingfilter.h index 7408ff5eb..4661bf8da 100644 --- a/src/guiscalingfilter.h +++ b/src/guiscalingfilter.h @@ -46,5 +46,5 @@ video::ITexture *guiScalingImageButton(video::IVideoDriver *driver, video::IText */ void draw2DImageFilterScaled(video::IVideoDriver *driver, video::ITexture *txr, const core::rect &destrect, const core::rect &srcrect, - const core::rect *cliprect = 0, video::SColor *const colors = 0, + const core::rect *cliprect = 0, const video::SColor *const colors = 0, bool usealpha = false); diff --git a/src/httpfetch.h b/src/httpfetch.h index 206576849..db43e3a4b 100644 --- a/src/httpfetch.h +++ b/src/httpfetch.h @@ -76,7 +76,7 @@ struct HTTPFetchResult unsigned long caller = HTTPFETCH_DISCARD; unsigned long request_id = 0; - HTTPFetchResult() {} + HTTPFetchResult() = default; HTTPFetchResult(const HTTPFetchRequest &fetch_request) : caller(fetch_request.caller), request_id(fetch_request.request_id) diff --git a/src/hud.cpp b/src/hud.cpp index 70903ce15..14e784a62 100644 --- a/src/hud.cpp +++ b/src/hud.cpp @@ -55,8 +55,8 @@ Hud::Hud(gui::IGUIEnvironment *guienv, Client *client, LocalPlayer *player, m_hotbar_imagesize *= m_hud_scaling; m_padding = m_hotbar_imagesize / 12; - for (unsigned int i = 0; i < 4; i++) - hbar_colors[i] = video::SColor(255, 255, 255, 255); + for (auto &hbar_color : hbar_colors) + hbar_color = video::SColor(255, 255, 255, 255); tsrc = client->getTextureSource(); @@ -220,7 +220,7 @@ void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount, // Store hotbar_image in member variable, used by drawItem() if (hotbar_image != player->hotbar_image) { hotbar_image = player->hotbar_image; - if (hotbar_image != "") + if (!hotbar_image.empty()) use_hotbar_image = tsrc->isKnownSourceImage(hotbar_image); else use_hotbar_image = false; @@ -229,7 +229,7 @@ void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount, // Store hotbar_selected_image in member variable, used by drawItem() if (hotbar_selected_image != player->hotbar_selected_image) { hotbar_selected_image = player->hotbar_selected_image; - if (hotbar_selected_image != "") + if (!hotbar_selected_image.empty()) use_hotbar_selected_image = tsrc->isKnownSourceImage(hotbar_selected_image); else use_hotbar_selected_image = false; @@ -572,7 +572,7 @@ void Hud::updateSelectionMesh(const v3s16 &camera_offset) m_selection_mesh = NULL; } - if (!m_selection_boxes.size()) { + if (m_selection_boxes.empty()) { // No pointed object return; } @@ -597,10 +597,8 @@ void Hud::updateSelectionMesh(const v3s16 &camera_offset) aabb3f halo_box(100.0, 100.0, 100.0, -100.0, -100.0, -100.0); m_halo_boxes.clear(); - for (std::vector::iterator - i = m_selection_boxes.begin(); - i != m_selection_boxes.end(); ++i) { - halo_box.addInternalBox(*i); + for (const auto &selection_box : m_selection_boxes) { + halo_box.addInternalBox(selection_box); } m_halo_boxes.push_back(halo_box); diff --git a/src/imagefilters.cpp b/src/imagefilters.cpp index b34027725..dd029628f 100644 --- a/src/imagefilters.cpp +++ b/src/imagefilters.cpp @@ -18,7 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "imagefilters.h" #include "util/numeric.h" -#include +#include /* Fill in RGB values for transparent pixels, to correct for odd colors * appearing at borders when blending. This is because many PNG optimizers diff --git a/src/intlGUIEditBox.cpp b/src/intlGUIEditBox.cpp index 2aacc7259..671b37c5d 100644 --- a/src/intlGUIEditBox.cpp +++ b/src/intlGUIEditBox.cpp @@ -630,8 +630,7 @@ bool intlGUIEditBox::processKey(const SEvent& event) if ( !this->IsEnabled ) break; - if (Text.size()) - { + if (!Text.empty()) { core::stringw s; if (MarkBegin != MarkEnd) @@ -670,8 +669,7 @@ bool intlGUIEditBox::processKey(const SEvent& event) if ( !this->IsEnabled ) break; - if (Text.size() != 0) - { + if (!Text.empty()) { core::stringw s; if (MarkBegin != MarkEnd) @@ -820,8 +818,7 @@ void intlGUIEditBox::draw() const bool prevOver = OverrideColorEnabled; const video::SColor prevColor = OverrideColor; - if (Text.size()) - { + if (!Text.empty()) { if (!IsEnabled && !OverrideColorEnabled) { OverrideColorEnabled = true; @@ -908,7 +905,7 @@ void intlGUIEditBox::draw() // draw marked text s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos); - if (s.size()) + if (!s.empty()) font->draw(s.c_str(), CurrentTextRect, OverrideColorEnabled ? OverrideColor : skin->getColor(EGDC_HIGH_LIGHT_TEXT), false, true, &localClipRect); @@ -1057,24 +1054,22 @@ bool intlGUIEditBox::processMouse(const SEvent& event) else { if (!AbsoluteClippingRect.isPointInside( - core::position2d(event.MouseInput.X, event.MouseInput.Y))) - { + core::position2d(event.MouseInput.X, event.MouseInput.Y))) { return false; } - else - { - // move cursor - CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y); - s32 newMarkBegin = MarkBegin; - if (!MouseMarking) - newMarkBegin = CursorPos; - MouseMarking = true; - setTextMarkers( newMarkBegin, CursorPos); - calculateScrollPos(); - return true; - } + // move cursor + CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y); + + s32 newMarkBegin = MarkBegin; + if (!MouseMarking) + newMarkBegin = CursorPos; + + MouseMarking = true; + setTextMarkers( newMarkBegin, CursorPos); + calculateScrollPos(); + return true; } default: break; @@ -1185,8 +1180,7 @@ void intlGUIEditBox::breakText() if (c == L' ' || c == 0 || i == (size-1)) { - if (word.size()) - { + if (!word.empty()) { // here comes the next whitespace, look if // we can break the last word to the next line. s32 whitelgth = font->getDimension(whitespace.c_str()).Width; @@ -1488,7 +1482,7 @@ void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeRe setAutoScroll(in->getAttributeAsBool("AutoScroll")); core::stringw ch = in->getAttributeAsStringW("PasswordChar"); - if (!ch.size()) + if (ch.empty()) setPasswordBox(in->getAttributeAsBool("PasswordBox")); else setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]); diff --git a/src/inventory.cpp b/src/inventory.cpp index 4da380a24..82325997e 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -35,11 +35,9 @@ with this program; if not, write to the Free Software Foundation, Inc., static content_t content_translate_from_19_to_internal(content_t c_from) { - for(u32 i=0; igetAlias(name); @@ -136,7 +134,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) NameIdMapping legacy_nimap; content_mapnode_get_name_id_mapping(&legacy_nimap); legacy_nimap.getName(material, name); - if(name == "") + if(name.empty()) name = "unknown_block"; if (itemdef) name = itemdef->getAlias(name); @@ -207,21 +205,20 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) // Read the count std::string count_str; std::getline(is, count_str, ' '); - if(count_str.empty()) - { + if (count_str.empty()) { count = 1; break; } - else - count = stoi(count_str); + + count = stoi(count_str); // Read the wear std::string wear_str; std::getline(is, wear_str, ' '); if(wear_str.empty()) break; - else - wear = stoi(wear_str); + + wear = stoi(wear_str); // Read metadata metadata.deSerialize(is); @@ -388,17 +385,12 @@ InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager clearItems(); } -InventoryList::~InventoryList() -{ -} - void InventoryList::clearItems() { m_items.clear(); - for(u32 i=0; i> m_width; if (iss.fail()) throw SerializationError("incorrect width property"); @@ -551,9 +537,8 @@ u32 InventoryList::getWidth() const u32 InventoryList::getUsedSlots() const { u32 num = 0; - for(u32 i=0; i::const_reverse_iterator - i = m_items.rbegin(); - i != m_items.rend(); ++i) - { - if(count == 0) + + for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) { + if (count == 0) break; - if (i->name == item.name - && (!match_meta || (i->metadata == item.metadata))) { + if (i->name == item.name && (!match_meta || (i->metadata == item.metadata))) { if (i->count >= count) return true; - else - count -= i->count; + + count -= i->count; } } return false; @@ -694,15 +676,11 @@ bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const ItemStack InventoryList::removeItem(const ItemStack &item) { ItemStack removed; - for(std::vector::reverse_iterator - i = m_items.rbegin(); - i != m_items.rend(); ++i) - { - if(i->name == item.name) - { + for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) { + if (i->name == item.name) { u32 still_to_remove = item.count - removed.count; removed.addItem(i->takeItem(still_to_remove), m_itemdef); - if(removed.count == item.count) + if (removed.count == item.count) break; } } @@ -815,9 +793,8 @@ Inventory::~Inventory() void Inventory::clear() { m_dirty = true; - for(u32 i=0; igetSize(); j++) - { + for (InventoryList *list : m_lists) { + for (u32 j=0; jgetSize(); j++) { list->deleteItem(j); } } @@ -855,9 +829,8 @@ Inventory & Inventory::operator = (const Inventory &other) m_dirty = true; clear(); m_itemdef = other.m_itemdef; - for(u32 i=0; igetName()<<" "<getSize()<<"\n"; list->serialize(os); } @@ -902,17 +873,16 @@ void Inventory::deSerialize(std::istream &is) std::string name; std::getline(iss, name, ' '); - if(name == "EndInventory") - { + if (name == "EndInventory") { break; } + // This is a temporary backwards compatibility fix - else if(name == "end") - { + if (name == "end") { break; } - else if(name == "List") - { + + if (name == "List") { std::string listname; u32 listsize; @@ -944,15 +914,14 @@ InventoryList * Inventory::addList(const std::string &name, u32 size) } return m_lists[i]; } - else - { - //don't create list with invalid name - if (name.find(" ") != std::string::npos) return NULL; - InventoryList *list = new InventoryList(name, size, m_itemdef); - m_lists.push_back(list); - return list; - } + + //don't create list with invalid name + if (name.find(' ') != std::string::npos) return NULL; + + InventoryList *list = new InventoryList(name, size, m_itemdef); + m_lists.push_back(list); + return list; } InventoryList * Inventory::getList(const std::string &name) @@ -966,9 +935,7 @@ InventoryList * Inventory::getList(const std::string &name) std::vector Inventory::getLists() { std::vector lists; - for(u32 i=0; igetItemDefManager(); - for (std::vector::iterator it = temp.begin(); - it != temp.end(); ++it) { - for (std::vector::iterator jt = output_replacements.begin(); - jt != output_replacements.end(); ++jt) { - if (it->name == jt->name) { - *it = jt->addItem(*it, itemdef); - if (it->empty()) + for (auto &itemstack : temp) { + for (auto &output_replacement : output_replacements) { + if (itemstack.name == output_replacement.name) { + itemstack = output_replacement.addItem(itemstack, itemdef); + if (itemstack.empty()) continue; } } - output_replacements.push_back(*it); + output_replacements.push_back(itemstack); } actionstream << player->getDescription() @@ -795,7 +793,8 @@ void ICraftAction::apply(InventoryManager *mgr, // Decrement counter if (count_remaining == 1) break; - else if (count_remaining > 1) + + if (count_remaining > 1) count_remaining--; // Get next crafting result @@ -806,24 +805,23 @@ void ICraftAction::apply(InventoryManager *mgr, // Put the replacements in the inventory or drop them on the floor, if // the invenotry is full - for (std::vector::iterator it = output_replacements.begin(); - it != output_replacements.end(); ++it) { + for (auto &output_replacement : output_replacements) { if (list_main) - *it = list_main->addItem(*it); - if (it->empty()) + output_replacement = list_main->addItem(output_replacement); + if (output_replacement.empty()) continue; - u16 count = it->count; + u16 count = output_replacement.count; do { - PLAYER_TO_SA(player)->item_OnDrop(*it, player, + PLAYER_TO_SA(player)->item_OnDrop(output_replacement, player, player->getBasePosition()); - if (count >= it->count) { + if (count >= output_replacement.count) { errorstream << "Couldn't drop replacement stack " << - it->getItemString() << " because drop loop didn't " + output_replacement.getItemString() << " because drop loop didn't " "decrease count." << std::endl; break; } - } while (!it->empty()); + } while (!output_replacement.empty()); } infostream<<"ICraftAction::apply(): crafted " diff --git a/src/inventorymanager.h b/src/inventorymanager.h index 53830d5c1..9ae92e5f4 100644 --- a/src/inventorymanager.h +++ b/src/inventorymanager.h @@ -54,7 +54,7 @@ struct InventoryLocation type = PLAYER; name = name_; } - void setNodeMeta(v3s16 p_) + void setNodeMeta(const v3s16 &p_) { type = NODEMETA; p = p_; @@ -105,8 +105,8 @@ struct InventoryAction; class InventoryManager { public: - InventoryManager(){} - virtual ~InventoryManager(){} + InventoryManager() = default; + virtual ~InventoryManager() = default; // Get an inventory (server and client) virtual Inventory* getInventory(const InventoryLocation &loc){return NULL;} @@ -131,7 +131,7 @@ struct InventoryAction virtual void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef) = 0; virtual void clientApply(InventoryManager *mgr, IGameDef *gamedef) = 0; - virtual ~InventoryAction() {}; + virtual ~InventoryAction() = default;; }; struct IMoveAction : public InventoryAction @@ -151,7 +151,7 @@ struct IMoveAction : public InventoryAction bool caused_by_move_somewhere = false; u32 move_count = 0; - IMoveAction() {} + IMoveAction() = default; IMoveAction(std::istream &is, bool somewhere); @@ -189,7 +189,7 @@ struct IDropAction : public InventoryAction std::string from_list; s16 from_i = -1; - IDropAction() {} + IDropAction() = default; IDropAction(std::istream &is); @@ -218,7 +218,7 @@ struct ICraftAction : public InventoryAction u16 count = 0; InventoryLocation craft_inv; - ICraftAction() {} + ICraftAction() = default; ICraftAction(std::istream &is); diff --git a/src/itemstackmetadata.cpp b/src/itemstackmetadata.cpp index 65829fd68..02c548abb 100644 --- a/src/itemstackmetadata.cpp +++ b/src/itemstackmetadata.cpp @@ -13,11 +13,9 @@ void ItemStackMetadata::serialize(std::ostream &os) const { std::ostringstream os2; os2 << DESERIALIZE_START; - for (StringMap::const_iterator - it = m_stringvars.begin(); - it != m_stringvars.end(); ++it) { - os2 << it->first << DESERIALIZE_KV_DELIM - << it->second << DESERIALIZE_PAIR_DELIM; + for (const auto &stringvar : m_stringvars) { + os2 << stringvar.first << DESERIALIZE_KV_DELIM + << stringvar.second << DESERIALIZE_PAIR_DELIM; } os << serializeJsonStringIfNeeded(os2.str()); }