diff options
author | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2011-03-22 22:29:39 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2011-03-24 17:04:29 +0100 |
commit | 94d97450bc3a52cd90baa696320bd08d91ac301f (patch) | |
tree | fc2f5db6583040772aac1d39a14928bdfe7aa4eb /src/utils | |
parent | 7967b82c19bfa5bd2249abcb807a7a55af031abe (diff) | |
download | manaserv-94d97450bc3a52cd90baa696320bd08d91ac301f.tar.gz manaserv-94d97450bc3a52cd90baa696320bd08d91ac301f.tar.xz manaserv-94d97450bc3a52cd90baa696320bd08d91ac301f.zip |
Use a map to quickly find items and monsters by their name
Introduced a template class NameMap, which provides a nice API for
mapping any custom types by their name.
This change also makes any duplicate item or monster definitions be
complete ignored, rather than being merged into the first definition.
Reviewed-by: Philipp Sehmisch
Reviewed-by: Yohann Ferreira
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/string.h | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/src/utils/string.h b/src/utils/string.h index 6127bfe..3b05c37 100644 --- a/src/utils/string.h +++ b/src/utils/string.h @@ -23,6 +23,7 @@ #include <string> #include <sstream> +#include <map> namespace utils { @@ -81,6 +82,40 @@ namespace utils ss << arg; return ss.str(); } -} + + /** + * A case-insensitive name map, mapping instances from a user-specified + * type by their name. + */ + template<typename T> class NameMap + { + public: + void insert(const std::string &name, T value) + { + mMap.insert(std::make_pair(toLower(name), value)); + } + + T find(const std::string &name) const + { + typename Map::const_iterator result = mMap.find(toLower(name)); + return result != mMap.end() ? result->second : T(); + } + + bool contains(const std::string &name) const + { + return mMap.find(toLower(name)) != mMap.end(); + } + + void clear() + { + mMap.clear(); + } + + private: + typedef std::map<std::string, T> Map; + Map mMap; + }; + +} // namespace utils #endif // UTILS_STRING_H |