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
This commit is contained in:
Loic Blot 2017-08-18 08:07:59 +02:00
parent 55ab4264dc
commit 1d086aee7c
No known key found for this signature in database
GPG Key ID: EFAA458E8C153987
10 changed files with 107 additions and 152 deletions

@ -46,5 +46,5 @@ video::ITexture *guiScalingImageButton(video::IVideoDriver *driver, video::IText
*/
void draw2DImageFilterScaled(video::IVideoDriver *driver, video::ITexture *txr,
const core::rect<s32> &destrect, const core::rect<s32> &srcrect,
const core::rect<s32> *cliprect = 0, video::SColor *const colors = 0,
const core::rect<s32> *cliprect = 0, const video::SColor *const colors = 0,
bool usealpha = false);

@ -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)

@ -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<aabb3f>::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);

@ -18,7 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "imagefilters.h"
#include "util/numeric.h"
#include <math.h>
#include <cmath>
/* Fill in RGB values for transparent pixels, to correct for odd colors
* appearing at borders when blending. This is because many PNG optimizers

@ -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,12 +1054,11 @@ bool intlGUIEditBox::processMouse(const SEvent& event)
else
{
if (!AbsoluteClippingRect.isPointInside(
core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y)))
{
core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y))) {
return false;
}
else
{
// move cursor
CursorPos = getCursorPos(event.MouseInput.X, event.MouseInput.Y);
@ -1075,7 +1071,6 @@ bool intlGUIEditBox::processMouse(const SEvent& event)
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]);

@ -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; i<sizeof(trans_table_19)/sizeof(trans_table_19[0]); i++)
{
if(trans_table_19[i][1] == c_from)
{
return trans_table_19[i][0];
for (const auto &tt : trans_table_19) {
if(tt[1] == c_from) {
return tt[0];
}
}
return c_from;
@ -116,7 +114,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);
@ -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,12 +205,11 @@ 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);
// Read the wear
@ -220,7 +217,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
std::getline(is, wear_str, ' ');
if(wear_str.empty())
break;
else
wear = stoi(wear_str);
// Read metadata
@ -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_size; i++)
{
m_items.push_back(ItemStack());
for (u32 i=0; i < m_size; i++) {
m_items.emplace_back();
}
//setDirty(true);
@ -427,15 +419,10 @@ void InventoryList::serialize(std::ostream &os) const
os<<"Width "<<m_width<<"\n";
for(u32 i=0; i<m_items.size(); i++)
{
const ItemStack &item = m_items[i];
if(item.empty())
{
for (const auto &item : m_items) {
if (item.empty()) {
os<<"Empty";
}
else
{
} else {
os<<"Item ";
item.serialize(os);
}
@ -464,17 +451,16 @@ void InventoryList::deSerialize(std::istream &is)
std::string name;
std::getline(iss, name, ' ');
if(name == "EndInventoryList")
{
if (name == "EndInventoryList") {
break;
}
// This is a temporary backwards compatibility fix
else if(name == "end")
{
if (name == "end") {
break;
}
else if(name == "Width")
{
if (name == "Width") {
iss >> 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<m_items.size(); i++)
{
if(!m_items[i].empty())
for (const auto &m_item : m_items) {
if (!m_item.empty())
num++;
}
return num;
@ -672,19 +657,16 @@ bool InventoryList::roomForItem(const ItemStack &item_) const
bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
{
u32 count = item.count;
if(count == 0)
if (count == 0)
return true;
for(std::vector<ItemStack>::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;
}
}
@ -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<ItemStack>::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; i<m_lists.size(); i++)
{
delete m_lists[i];
for (auto &m_list : m_lists) {
delete m_list;
}
m_lists.clear();
}
@ -825,11 +802,8 @@ void Inventory::clear()
void Inventory::clearContents()
{
m_dirty = true;
for(u32 i=0; i<m_lists.size(); i++)
{
InventoryList *list = m_lists[i];
for(u32 j=0; j<list->getSize(); j++)
{
for (InventoryList *list : m_lists) {
for (u32 j=0; j<list->getSize(); 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; i<other.m_lists.size(); i++)
{
m_lists.push_back(new InventoryList(*other.m_lists[i]));
for (InventoryList *list : other.m_lists) {
m_lists.push_back(new InventoryList(*list));
}
}
return *this;
@ -878,9 +851,7 @@ bool Inventory::operator == (const Inventory &other) const
void Inventory::serialize(std::ostream &os) const
{
for(u32 i=0; i<m_lists.size(); i++)
{
InventoryList *list = m_lists[i];
for (InventoryList *list : m_lists) {
os<<"List "<<list->getName()<<" "<<list->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;
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<const InventoryList*> Inventory::getLists()
{
std::vector<const InventoryList*> lists;
for(u32 i=0; i<m_lists.size(); i++)
{
InventoryList *list = m_lists[i];
for (auto list : m_lists) {
lists.push_back(list);
}
return lists;

@ -172,7 +172,7 @@ class InventoryList
{
public:
InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef);
~InventoryList();
~InventoryList() = default;
void clearItems();
void setSize(u32 newsize);
void setWidth(u32 newWidth);

@ -774,17 +774,15 @@ void ICraftAction::apply(InventoryManager *mgr,
// Add the new replacements to the list
IItemDefManager *itemdef = gamedef->getItemDefManager();
for (std::vector<ItemStack>::iterator it = temp.begin();
it != temp.end(); ++it) {
for (std::vector<ItemStack>::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<ItemStack>::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 "

@ -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);

@ -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());
}