From 41cc92f73e39cec5dfea6b1164176610cccc7df4 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 15 Aug 2015 13:58:32 +0300 Subject: Add strong typed int for item color. --- src/resources/beinginfo.cpp | 4 ++-- src/resources/beinginfo.h | 7 ++++--- src/resources/db/colordb.cpp | 36 ++++++++++++++++++++---------------- src/resources/db/colordb.h | 29 +++++++++++++++++------------ src/resources/iteminfo.cpp | 20 ++++++++++---------- src/resources/iteminfo.h | 17 +++++++++-------- 6 files changed, 62 insertions(+), 51 deletions(-) (limited to 'src/resources') diff --git a/src/resources/beinginfo.cpp b/src/resources/beinginfo.cpp index f63e65134..1f1033e3c 100644 --- a/src/resources/beinginfo.cpp +++ b/src/resources/beinginfo.cpp @@ -206,12 +206,12 @@ void BeingInfo::setColorsList(const std::string &name) mColors = ColorDB::getColorsList(name); } -std::string BeingInfo::getColor(const int idx) const +std::string BeingInfo::getColor(const ItemColor idx) const { if (!mColors) return std::string(); - const std::map ::const_iterator + const std::map ::const_iterator it = mColors->find(idx); if (it == mColors->end()) return std::string(); diff --git a/src/resources/beinginfo.h b/src/resources/beinginfo.h index 0a6330470..3429515e0 100644 --- a/src/resources/beinginfo.h +++ b/src/resources/beinginfo.h @@ -28,6 +28,7 @@ #include "enums/resources/map/blocktype.h" #include "enums/simpletypes/beingtypeid.h" +#include "enums/simpletypes/itemcolor.h" #include "resources/beingmenuitem.h" #include "resources/cursor.h" @@ -38,7 +39,7 @@ struct Attack; namespace ColorDB { - class ItemColor; + class ItemColorData; } typedef std::map Attacks; @@ -312,7 +313,7 @@ class BeingInfo final void setColorsList(const std::string &name); - std::string getColor(const int idx) const A_WARN_UNUSED; + std::string getColor(const ItemColor idx) const A_WARN_UNUSED; void addMenu(const std::string &name, const std::string &command); @@ -332,7 +333,7 @@ class BeingInfo final std::vector mMenu; unsigned char mBlockWalkMask; BlockType::BlockType mBlockType; - const std::map *mColors; + const std::map *mColors; int mTargetOffsetX; int mTargetOffsetY; int mNameOffsetX; diff --git a/src/resources/db/colordb.cpp b/src/resources/db/colordb.cpp index 1b636e407..62f7beef6 100644 --- a/src/resources/db/colordb.cpp +++ b/src/resources/db/colordb.cpp @@ -41,7 +41,7 @@ void ColorDB::load() if (mLoaded) unload(); - std::map colors; + std::map colors; ColorListsIterator it = mColorLists.find("hair"); if (it != mColorLists.end()) colors = it->second; @@ -68,7 +68,7 @@ void ColorDB::load() } void ColorDB::loadHair(const std::string &fileName, - std::map &colors) + std::map &colors) { XML::Document *doc = new XML::Document(fileName, UseResman_true, @@ -78,8 +78,8 @@ void ColorDB::loadHair(const std::string &fileName, if (!root || !xmlNameEqual(root, "colors")) { logger->log("ColorDB: Failed to find hair colors file."); - if (colors.find(0) == colors.end()) - colors[0] = ItemColor(0, "", ""); + if (colors.find(ItemColor_zero) == colors.end()) + colors[ItemColor_zero] = ItemColorData(ItemColor_zero, "", ""); delete doc; return; } @@ -95,12 +95,13 @@ void ColorDB::loadHair(const std::string &fileName, } else if (xmlNameEqual(node, "color")) { - const int id = XML::getProperty(node, "id", 0); + const ItemColor id = fromInt(XML::getProperty( + node, "id", 0), ItemColor); if (colors.find(id) != colors.end()) - logger->log("ColorDB: Redefinition of dye ID %d", id); + logger->log("ColorDB: Redefinition of dye ID %d", toInt(id, int)); - colors[id] = ItemColor(id, XML::langProperty(node, "name", ""), + colors[id] = ItemColorData(id, XML::langProperty(node, "name", ""), XML::getProperty(node, "value", "#FFFFFF")); } } @@ -135,7 +136,7 @@ void ColorDB::loadColorLists(const std::string &fileName) if (name.empty()) continue; - std::map colors; + std::map colors; const ColorListsIterator it = mColorLists.find(name); if (it != mColorLists.end()) @@ -145,11 +146,14 @@ void ColorDB::loadColorLists(const std::string &fileName) { if (xmlNameEqual(colorNode, "color")) { - ItemColor c(XML::getProperty(colorNode, "id", -1), - XML::langProperty(colorNode, "name", ""), - XML::getProperty(colorNode, "value", "")); - if (c.id > -1) - colors[c.id] = c; + const int id = XML::getProperty(colorNode, "id", -1); + if (id > -1) + { + ItemColorData c(fromInt(id, ItemColor), + XML::langProperty(colorNode, "name", ""), + XML::getProperty(colorNode, "value", "")); + colors[c.id] = c; + } } } mColorLists[name] = colors; @@ -166,7 +170,7 @@ void ColorDB::unload() mLoaded = false; } -std::string &ColorDB::getHairColorName(const int id) +std::string &ColorDB::getHairColorName(const ItemColor id) { if (!mLoaded) load(); @@ -182,7 +186,7 @@ std::string &ColorDB::getHairColorName(const int id) if (i == (*it).second.end()) { - logger->log("ColorDB: Error, unknown dye ID# %d", id); + logger->log("ColorDB: Error, unknown dye ID# %d", toInt(id, int)); return mFail; } else @@ -196,7 +200,7 @@ int ColorDB::getHairSize() return mHairColorsSize; } -const std::map +const std::map *ColorDB::getColorsList(const std::string &name) { const ColorListsIterator it = mColorLists.find(name); diff --git a/src/resources/db/colordb.h b/src/resources/db/colordb.h index c2923f382..6468b76e3 100644 --- a/src/resources/db/colordb.h +++ b/src/resources/db/colordb.h @@ -22,6 +22,8 @@ #ifndef RESOURCES_DB_COLORDB_H #define RESOURCES_DB_COLORDB_H +#include "enums/simpletypes/itemcolor.h" + #include #include @@ -32,24 +34,25 @@ */ namespace ColorDB { - class ItemColor final + class ItemColorData final { public: - ItemColor() : - id(0), + ItemColorData() : + id(ItemColor_zero), name(), color() { } - ItemColor(const int id0, const std::string &name0, - const std::string &color0) : + ItemColorData(const ItemColor id0, + const std::string &name0, + const std::string &color0) : id(id0), name(name0), color(color0) { } - int id; + ItemColor id; std::string name; std::string color; }; @@ -63,7 +66,7 @@ namespace ColorDB * Loads the color data from colors.xml. */ void loadHair(const std::string &fileName, - std::map &colors); + std::map &colors); void loadColorLists(const std::string &fileName); @@ -72,17 +75,19 @@ namespace ColorDB */ void unload(); - std::string &getHairColorName(const int id) A_WARN_UNUSED; + std::string &getHairColorName(const ItemColor id) A_WARN_UNUSED; int getHairSize() A_WARN_UNUSED; - const std::map *getColorsList(const std::string - &name) A_WARN_UNUSED; + const std::map *getColorsList(const std::string + &name) + A_WARN_UNUSED; // Color DB - typedef std::map Colors; + typedef std::map Colors; typedef Colors::iterator ColorIterator; - typedef std::map > ColorLists; + typedef std::map > + ColorLists; typedef ColorLists::iterator ColorListsIterator; } // namespace ColorDB diff --git a/src/resources/iteminfo.cpp b/src/resources/iteminfo.cpp index 768825f44..c439be5c5 100644 --- a/src/resources/iteminfo.cpp +++ b/src/resources/iteminfo.cpp @@ -201,12 +201,12 @@ void ItemInfo::setColorsList(const std::string &name) } } -std::string ItemInfo::getDyeColorsString(const int color) const +std::string ItemInfo::getDyeColorsString(const ItemColor color) const { if (!mColors || mColorList.empty()) return ""; - const std::map ::const_iterator + const std::map ::const_iterator it = mColors->find(color); if (it == mColors->end()) return ""; @@ -214,23 +214,23 @@ std::string ItemInfo::getDyeColorsString(const int color) const return it->second.color; } -const std::string ItemInfo::getDescription(const unsigned char color) const +const std::string ItemInfo::getDescription(const ItemColor color) const { return replaceColors(mDescription, color); } -const std::string ItemInfo::getName(const unsigned char color) const +const std::string ItemInfo::getName(const ItemColor color) const { return replaceColors(mName, color); } const std::string ItemInfo::replaceColors(std::string str, - const unsigned char color) const + const ItemColor color) const { std::string name; if (mColors && !mColorList.empty()) { - const std::map ::const_iterator + const std::map ::const_iterator it = mColors->find(color); if (it == mColors->end()) name = "unknown"; @@ -369,24 +369,24 @@ void ItemInfo::setSprite(const std::string &animationFile, mAnimationFiles[static_cast(gender) + race * 4] = animationFile; } -std::string ItemInfo::getColorName(const int idx) const +std::string ItemInfo::getColorName(const ItemColor idx) const { if (!mColors) return std::string(); - const std::map ::const_iterator + const std::map ::const_iterator it = mColors->find(idx); if (it == mColors->end()) return std::string(); return it->second.name; } -std::string ItemInfo::getColor(const int idx) const +std::string ItemInfo::getColor(const ItemColor idx) const { if (!mColors) return std::string(); - const std::map ::const_iterator + const std::map ::const_iterator it = mColors->find(idx); if (it == mColors->end()) return std::string(); diff --git a/src/resources/iteminfo.h b/src/resources/iteminfo.h index 4d40390d9..976d30c1d 100644 --- a/src/resources/iteminfo.h +++ b/src/resources/iteminfo.h @@ -26,6 +26,7 @@ #include "enums/being/gender.h" #include "enums/simpletypes/beingtypeid.h" +#include "enums/simpletypes/itemcolor.h" #include "resources/cursor.h" #include "resources/itemtype.h" @@ -34,7 +35,7 @@ namespace ColorDB { - class ItemColor; + class ItemColorData; } // sprite, @@ -69,7 +70,7 @@ class ItemInfo final const std::string &getName() const A_WARN_UNUSED { return mName; } - const std::string getName(const unsigned char color) + const std::string getName(const ItemColor color) const A_WARN_UNUSED; void setDisplay(const SpriteDisplay &display) @@ -84,7 +85,7 @@ class ItemInfo final const std::string &getDescription() const A_WARN_UNUSED { return mDescription; } - const std::string getDescription(const unsigned char color) + const std::string getDescription(const ItemColor color) const A_WARN_UNUSED; void setEffect(const std::string &effect) @@ -238,7 +239,7 @@ class ItemInfo final const SpriteToItemMap *getSpriteToItemReplaceMap(const int directions) const A_WARN_UNUSED; - std::string getDyeColorsString(const int color) const A_WARN_UNUSED; + std::string getDyeColorsString(const ItemColor color) const A_WARN_UNUSED; void setColorsList(const std::string &name); @@ -246,7 +247,7 @@ class ItemInfo final { return !mColorList.empty(); } const std::string replaceColors(std::string str, - const unsigned char color) + const ItemColor color) const A_WARN_UNUSED; void setPickupCursor(const std::string &cursor) @@ -267,9 +268,9 @@ class ItemInfo final int getColorsSize() const { return mColors ? static_cast(mColors->size()) : 0; } - std::string getColorName(const int idx) const; + std::string getColorName(const ItemColor idx) const; - std::string getColor(const int idx) const; + std::string getColor(const ItemColor idx) const; int mDrawBefore[10]; int mDrawAfter[10]; @@ -317,7 +318,7 @@ class ItemInfo final /** Stores the names of sounds to be played at certain event. */ std::map mSounds; std::map mTags; - const std::map *mColors; + const std::map *mColors; std::string mColorList; int mHitEffectId; int mCriticalHitEffectId; -- cgit v1.2.3-60-g2f50