From 4e49081ca1f0d658308582ef3084837bb58c33ec Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Wed, 19 Oct 2011 22:56:32 +0200 Subject: 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 --- src/utils/string.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/utils') diff --git a/src/utils/string.h b/src/utils/string.h index 3b05c371..a21081d5 100644 --- a/src/utils/string.h +++ b/src/utils/string.h @@ -90,15 +90,19 @@ namespace utils template 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 Map; + Map mMap; + const T mDefault; }; } // namespace utils -- cgit v1.2.3-60-g2f50