GN: Use std::unique_ptr for owning pointers in Scope::NamedScopeMap. BUG=602726 Review URL: https://codereview.chromium.org/1885513003 Cr-Original-Commit-Position: refs/heads/master@{#388002} Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src Cr-Mirrored-Commit: ac5bf2c0d550e62f6cc58862990e7c4a1017cdf7
diff --git a/tools/gn/scope.cc b/tools/gn/scope.cc index d3d29d4..93ce966 100644 --- a/tools/gn/scope.cc +++ b/tools/gn/scope.cc
@@ -5,7 +5,7 @@ #include "tools/gn/scope.h" #include "base/logging.h" -#include "base/stl_util.h" +#include "base/memory/ptr_util.h" #include "tools/gn/parse_tree.h" #include "tools/gn/template.h" @@ -64,8 +64,6 @@ } Scope::~Scope() { - STLDeleteContainerPairSecondPointers(target_defaults_.begin(), - target_defaults_.end()); } const Value* Scope::GetValue(const base::StringPiece& ident, @@ -315,12 +313,9 @@ } } - // Be careful to delete any pointer we're about to clobber. - Scope** dest_scope = &dest->target_defaults_[current_name]; - if (*dest_scope) - delete *dest_scope; - *dest_scope = new Scope(settings_); - pair.second->NonRecursiveMergeTo(*dest_scope, options, node_for_err, + std::unique_ptr<Scope>& dest_scope = dest->target_defaults_[current_name]; + dest_scope = base::WrapUnique(new Scope(settings_)); + pair.second->NonRecursiveMergeTo(dest_scope.get(), options, node_for_err, "<SHOULDN'T HAPPEN>", err); } @@ -412,19 +407,19 @@ if (GetTargetDefaults(target_type)) return nullptr; - Scope** dest = &target_defaults_[target_type]; - if (*dest) { + std::unique_ptr<Scope>& dest = target_defaults_[target_type]; + if (dest) { NOTREACHED(); // Already set. - return *dest; + return dest.get(); } - *dest = new Scope(settings_); - return *dest; + dest = base::WrapUnique(new Scope(settings_)); + return dest.get(); } const Scope* Scope::GetTargetDefaults(const std::string& target_type) const { NamedScopeMap::const_iterator found = target_defaults_.find(target_type); if (found != target_defaults_.end()) - return found->second; + return found->second.get(); if (containing()) return containing()->GetTargetDefaults(target_type); return nullptr;
diff --git a/tools/gn/scope.h b/tools/gn/scope.h index 72aa0c3..e9bd139 100644 --- a/tools/gn/scope.h +++ b/tools/gn/scope.h
@@ -333,10 +333,9 @@ RecordMap; RecordMap values_; - // Owning pointers. Note that this can't use string pieces since the names - // are constructed from Values which might be deallocated before this goes - // out of scope. - typedef base::hash_map<std::string, Scope*> NamedScopeMap; + // Note that this can't use string pieces since the names are constructed from + // Values which might be deallocated before this goes out of scope. + typedef base::hash_map<std::string, std::unique_ptr<Scope>> NamedScopeMap; NamedScopeMap target_defaults_; // Null indicates not set and that we should fallback to the containing