From 405c23831a4e1363f26f848215c8140f6d6d9f78 Mon Sep 17 00:00:00 2001 From: Björn Steinbrink Date: Wed, 8 Mar 2006 19:13:26 +0000 Subject: Added a 'dtor' helper functor to delete objects in arrays and containers using STL algorithms. --- src/Makefile.am | 1 + src/beingmanager.cpp | 8 +++----- src/engine.cpp | 14 ++++---------- src/flooritemmanager.cpp | 8 +++----- src/graphic/spriteset.cpp | 12 +++++------- src/gui/button.cpp | 14 ++++---------- src/gui/playerbox.cpp | 6 +++--- src/gui/scrollarea.cpp | 8 ++++---- src/gui/skill.cpp | 9 ++++----- src/gui/textfield.cpp | 6 +++--- src/gui/windowcontainer.cpp | 6 +++--- src/main.cpp | 8 +++----- src/map.cpp | 7 +++---- src/utils/dtor.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 14 files changed, 87 insertions(+), 64 deletions(-) create mode 100644 src/utils/dtor.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 8adbdf0a..eaa559d5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -172,6 +172,7 @@ tmw_SOURCES = graphic/spriteset.cpp \ resources/soundeffect.cpp \ resources/buddylist.h \ resources/buddylist.cpp \ + utils/dtor.h \ base64.cpp \ base64.h \ being.cpp \ diff --git a/src/beingmanager.cpp b/src/beingmanager.cpp index c7edc82b..c5890048 100644 --- a/src/beingmanager.cpp +++ b/src/beingmanager.cpp @@ -31,6 +31,8 @@ #include "net/messageout.h" #include "net/protocol.h" +#include "utils/dtor.h" + class FindBeingFunctor { public: @@ -148,11 +150,7 @@ void BeingManager::clear() mBeings.remove(player_node); } - for (BeingIterator i = mBeings.begin(); i != mBeings.end(); i++) - { - delete (*i); - } - + for_each(mBeings.begin(), mBeings.end(), make_dtor(mBeings)); mBeings.clear(); if (player_node) diff --git a/src/engine.cpp b/src/engine.cpp index 6aff0dc9..2cf0fc87 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -50,6 +50,8 @@ #include "resources/mapreader.h" #include "resources/resourcemanager.h" +#include "utils/dtor.h" + extern Minimap *minimap; char itemCurrenyQ[10] = "0"; @@ -103,20 +105,12 @@ Engine::Engine(Network *network): Engine::~Engine() { // Delete sprite sets - std::map::iterator i; - for (i = monsterset.begin(); i != monsterset.end(); i++) - { - delete i->second; - } + for_each(monsterset.begin(), monsterset.end(), make_dtor(monsterset)); monsterset.clear(); delete npcset; delete emotionset; - //delete weaponset; - for (unsigned int i = 0; i < weaponset.size(); i++) - { - delete weaponset[i]; - } + for_each(weaponset.begin(), weaponset.end(), make_dtor(weaponset)); weaponset.clear(); delete itemset; diff --git a/src/flooritemmanager.cpp b/src/flooritemmanager.cpp index f87c925b..680616a8 100644 --- a/src/flooritemmanager.cpp +++ b/src/flooritemmanager.cpp @@ -25,6 +25,8 @@ #include "floor_item.h" +#include "utils/dtor.h" + FloorItemManager::~FloorItemManager() { clear(); @@ -46,11 +48,7 @@ void FloorItemManager::destroy(FloorItem *item) void FloorItemManager::clear() { - FloorItemIterator i; - for (i = mFloorItems.begin(); i != mFloorItems.end(); i++) - { - delete *i; - } + for_each(mFloorItems.begin(), mFloorItems.end(), make_dtor(mFloorItems)); mFloorItems.clear(); } diff --git a/src/graphic/spriteset.cpp b/src/graphic/spriteset.cpp index e0a1ef36..96ba4313 100644 --- a/src/graphic/spriteset.cpp +++ b/src/graphic/spriteset.cpp @@ -25,13 +25,13 @@ #include "../resources/image.h" +#include "../utils/dtor.h" + Spriteset::Spriteset(Image *img, int width, int height) { - int x, y; - - for (y = 0; y + height <= img->getHeight(); y += height) + for (int y = 0; y + height <= img->getHeight(); y += height) { - for (x = 0; x + width <= img->getWidth(); x += width) + for (int x = 0; x + width <= img->getWidth(); x += width) { spriteset.push_back(img->getSubImage(x, y, width, height)); } @@ -40,7 +40,5 @@ Spriteset::Spriteset(Image *img, int width, int height) Spriteset::~Spriteset() { - for (unsigned int i = 0; i < spriteset.size(); i++) { - delete spriteset[i]; - } + for_each(spriteset.begin(), spriteset.end(), make_dtor(spriteset)); } diff --git a/src/gui/button.cpp b/src/gui/button.cpp index 78fbf7c7..8e57181e 100644 --- a/src/gui/button.cpp +++ b/src/gui/button.cpp @@ -34,6 +34,8 @@ #include "../resources/image.h" #include "../resources/resourcemanager.h" +#include "../utils/dtor.h" + ImageRect Button::button[4]; int Button::mInstances = 0; @@ -80,17 +82,9 @@ Button::~Button() if (mInstances == 0) { - int a, x, y, mode; - - for (mode = 0; mode < 4; mode++) + for (int mode = 0; mode < 4; mode++) { - a = 0; - for (y = 0; y < 3; y++) { - for (x = 0; x < 3; x++) { - delete button[mode].grid[a]; - a++; - } - } + for_each(button[mode].grid, button[mode].grid + 9, dtor()); } } } diff --git a/src/gui/playerbox.cpp b/src/gui/playerbox.cpp index 6423b8c4..e61cabde 100644 --- a/src/gui/playerbox.cpp +++ b/src/gui/playerbox.cpp @@ -32,6 +32,8 @@ #include "../resources/image.h" #include "../resources/resourcemanager.h" +#include "../utils/dtor.h" + extern std::vector hairset; extern Spriteset *playerset; @@ -76,9 +78,7 @@ PlayerBox::~PlayerBox() if (instances == 0) { - for (int a = 0; a < 9; a++) { - delete background.grid[a]; - } + for_each(background.grid, background.grid + 9, dtor()); } } diff --git a/src/gui/scrollarea.cpp b/src/gui/scrollarea.cpp index 7f39b5a8..3703ead8 100644 --- a/src/gui/scrollarea.cpp +++ b/src/gui/scrollarea.cpp @@ -30,6 +30,8 @@ #include "../resources/image.h" #include "../resources/resourcemanager.h" +#include "../utils/dtor.h" + int ScrollArea::instances = 0; ImageRect ScrollArea::background; ImageRect ScrollArea::vMarker; @@ -60,10 +62,8 @@ ScrollArea::~ScrollArea() if (instances == 0) { - for (int a = 0; a < 9; a++) { - delete background.grid[a]; - delete vMarker.grid[a]; - } + for_each(background.grid, background.grid + 9, dtor()); + for_each(vMarker.grid, vMarker.grid + 9, dtor()); buttons[UP][0]->decRef(); buttons[UP][1]->decRef(); diff --git a/src/gui/skill.cpp b/src/gui/skill.cpp index ba117b67..f86d2356 100644 --- a/src/gui/skill.cpp +++ b/src/gui/skill.cpp @@ -30,8 +30,10 @@ #include "scrollarea.h" #include "../localplayer.h" - #include "../graphics.h" + +#include "../utils/dtor.h" + extern Graphics *graphics; const char *skill_db[] = { @@ -193,9 +195,6 @@ void SkillDialog::setSkill(int id, int lvl, int mp) void SkillDialog::cleanList() { - for (int i = skillList.size() - 1; i >= 0; i--) - { - delete skillList[i]; - } + for_each(skillList.begin(), skillList.end(), make_dtor(skillList)); skillList.clear(); } diff --git a/src/gui/textfield.cpp b/src/gui/textfield.cpp index ec3b55b4..aa9d04fd 100644 --- a/src/gui/textfield.cpp +++ b/src/gui/textfield.cpp @@ -32,6 +32,8 @@ #include "../resources/image.h" #include "../resources/resourcemanager.h" +#include "../utils/dtor.h" + int TextField::instances = 0; ImageRect TextField::skin; @@ -74,9 +76,7 @@ TextField::~TextField() if (instances == 0) { - for (int a = 0; a < 9; a++) { - delete skin.grid[a]; - } + for_each(skin.grid, skin.grid + 9, dtor()); } } diff --git a/src/gui/windowcontainer.cpp b/src/gui/windowcontainer.cpp index 3803b652..14aaaf68 100644 --- a/src/gui/windowcontainer.cpp +++ b/src/gui/windowcontainer.cpp @@ -23,11 +23,11 @@ #include "windowcontainer.h" +#include "../utils/dtor.h" + void WindowContainer::logic() { - for (WidgetIterator i = mDeathList.begin(); i != mDeathList.end(); i++) { - delete (*i); - } + for_each(mDeathList.begin(), mDeathList.end(), make_dtor(mDeathList)); mDeathList.clear(); gcn::Container::logic(); diff --git a/src/main.cpp b/src/main.cpp index 4013cbec..eba01d0f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -77,6 +77,8 @@ #include "resources/image.h" #include "resources/resourcemanager.h" +#include "utils/dtor.h" + // Account infos char n_server, n_character; @@ -319,11 +321,7 @@ void exit_engine() config.write(); delete gui; delete graphics; - //delete hairset; - for (unsigned int i = 0; i < hairset.size(); i++) - { - delete hairset[i]; - } + for_each(hairset.begin(), hairset.end(), make_dtor(hairset)); hairset.clear(); delete playerset; diff --git a/src/map.cpp b/src/map.cpp index 33e399d7..57cd5a97 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -33,6 +33,8 @@ #include "resources/image.h" +#include "utils/dtor.h" + /** * A location on a tile map. Used for pathfinding, open list. */ @@ -70,10 +72,7 @@ Map::~Map() delete[] tiles; // Clean up tilesets - for (TilesetIterator i = tilesets.begin(); i != tilesets.end(); i++) - { - delete (*i); - } + for_each(tilesets.begin(), tilesets.end(), make_dtor(tilesets)); tilesets.clear(); } diff --git a/src/utils/dtor.h b/src/utils/dtor.h new file mode 100644 index 00000000..b969f15d --- /dev/null +++ b/src/utils/dtor.h @@ -0,0 +1,44 @@ +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#include +#include + +template +struct dtor : public std::unary_function +{ + void operator()(T &ptr) { delete ptr; } +}; + +template +struct dtor > : + public std::unary_function , void> +{ + void operator()(std::pair &pair) { delete pair.second; } +}; + +template +inline dtor make_dtor(Cont const&) +{ + return dtor(); +} -- cgit v1.2.3-70-g09d2