diff options
author | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2011-10-19 22:56:32 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2011-10-20 23:10:40 +0200 |
commit | 4e49081ca1f0d658308582ef3084837bb58c33ec (patch) | |
tree | 85ab83e760a3bd017a1ef1ab2054eef049aa0f8e /src/utils | |
parent | 03ff7c110e536de1f8b239817e50cb07c492da6f (diff) | |
download | manaserv-4e49081ca1f0d658308582ef3084837bb58c33ec.tar.gz manaserv-4e49081ca1f0d658308582ef3084837bb58c33ec.tar.xz manaserv-4e49081ca1f0d658308582ef3084837bb58c33ec.zip |
Return a const & from NameMap to avoid potential deep copies
When I introduced NameMap it was only used with pointers, but now it is
also used with std::string so it probably makes sense to keep a default-
constructed value around so that a reference can be returned rather than
a copy.
NameMap::find was renamed to NameMap::value to make it more clear that
it doesn't return an iterator, like std::map::find.
Reviewed-by: Yohann Ferreira
Reviewed-by: Ben Longbons
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/string.h | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/utils/string.h b/src/utils/string.h index 3b05c37..a21081d 100644 --- a/src/utils/string.h +++ b/src/utils/string.h @@ -90,15 +90,19 @@ namespace utils template<typename T> class NameMap { public: + NameMap() + : mDefault() + {} + void insert(const std::string &name, T value) { mMap.insert(std::make_pair(toLower(name), value)); } - T find(const std::string &name) const + const T &value(const std::string &name) const { typename Map::const_iterator result = mMap.find(toLower(name)); - return result != mMap.end() ? result->second : T(); + return result != mMap.end() ? result->second : mDefault; } bool contains(const std::string &name) const @@ -113,7 +117,9 @@ namespace utils private: typedef std::map<std::string, T> Map; + Map mMap; + const T mDefault; }; } // namespace utils |