summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2011-03-22 22:29:39 +0100
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2011-03-24 17:04:29 +0100
commit94d97450bc3a52cd90baa696320bd08d91ac301f (patch)
treefc2f5db6583040772aac1d39a14928bdfe7aa4eb /src/utils
parent7967b82c19bfa5bd2249abcb807a7a55af031abe (diff)
downloadmanaserv-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.h37
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