diff options
Diffstat (limited to 'Source/cmDefinitions.cxx')
-rw-r--r-- | Source/cmDefinitions.cxx | 80 |
1 files changed, 44 insertions, 36 deletions
diff --git a/Source/cmDefinitions.cxx b/Source/cmDefinitions.cxx index 5fafaf943..69a64276b 100644 --- a/Source/cmDefinitions.cxx +++ b/Source/cmDefinitions.cxx @@ -2,10 +2,13 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmDefinitions.h" -#include <assert.h> -#include <set> +#include <cassert> +#include <functional> +#include <unordered_set> #include <utility> +#include <cm/string_view> + cmDefinitions::Def cmDefinitions::NoDef; cmDefinitions::Def const& cmDefinitions::GetInternal(const std::string& key, @@ -13,10 +16,12 @@ cmDefinitions::Def const& cmDefinitions::GetInternal(const std::string& key, StackIter end, bool raise) { assert(begin != end); - MapType::iterator i = begin->Map.find(key); - if (i != begin->Map.end()) { - i->second.Used = true; - return i->second; + { + auto it = begin->Map.find(cm::String::borrow(key)); + if (it != begin->Map.end()) { + it->second.Used = true; + return it->second; + } } StackIter it = begin; ++it; @@ -27,14 +32,14 @@ cmDefinitions::Def const& cmDefinitions::GetInternal(const std::string& key, if (!raise) { return def; } - return begin->Map.insert(MapType::value_type(key, def)).first->second; + return begin->Map.emplace(key, def).first->second; } const std::string* cmDefinitions::Get(const std::string& key, StackIter begin, StackIter end) { Def const& def = cmDefinitions::GetInternal(key, begin, end, false); - return def.Exists ? &def : nullptr; + return def.Value ? def.Value.str_if_stable() : nullptr; } void cmDefinitions::Raise(const std::string& key, StackIter begin, @@ -47,47 +52,27 @@ bool cmDefinitions::HasKey(const std::string& key, StackIter begin, StackIter end) { for (StackIter it = begin; it != end; ++it) { - MapType::const_iterator i = it->Map.find(key); - if (i != it->Map.end()) { + if (it->Map.find(cm::String::borrow(key)) != it->Map.end()) { return true; } } return false; } -void cmDefinitions::Set(const std::string& key, const char* value) -{ - Def def(value); - this->Map[key] = def; -} - -std::vector<std::string> cmDefinitions::UnusedKeys() const -{ - std::vector<std::string> keys; - keys.reserve(this->Map.size()); - // Consider local definitions. - for (auto const& mi : this->Map) { - if (!mi.second.Used) { - keys.push_back(mi.first); - } - } - return keys; -} - cmDefinitions cmDefinitions::MakeClosure(StackIter begin, StackIter end) { cmDefinitions closure; - std::set<std::string> undefined; + std::unordered_set<cm::string_view> undefined; for (StackIter it = begin; it != end; ++it) { // Consider local definitions. for (auto const& mi : it->Map) { // Use this key if it is not already set or unset. if (closure.Map.find(mi.first) == closure.Map.end() && - undefined.find(mi.first) == undefined.end()) { - if (mi.second.Exists) { + undefined.find(mi.first.view()) == undefined.end()) { + if (mi.second.Value) { closure.Map.insert(mi); } else { - undefined.insert(mi.first); + undefined.emplace(mi.first.view()); } } } @@ -98,18 +83,41 @@ cmDefinitions cmDefinitions::MakeClosure(StackIter begin, StackIter end) std::vector<std::string> cmDefinitions::ClosureKeys(StackIter begin, StackIter end) { - std::set<std::string> bound; std::vector<std::string> defined; + std::unordered_set<cm::string_view> bound; for (StackIter it = begin; it != end; ++it) { defined.reserve(defined.size() + it->Map.size()); for (auto const& mi : it->Map) { // Use this key if it is not already set or unset. - if (bound.insert(mi.first).second && mi.second.Exists) { - defined.push_back(mi.first); + if (bound.emplace(mi.first.view()).second && mi.second.Value) { + defined.push_back(*mi.first.str_if_stable()); } } } return defined; } + +void cmDefinitions::Set(const std::string& key, cm::string_view value) +{ + this->Map[key] = Def(value); +} + +void cmDefinitions::Unset(const std::string& key) +{ + this->Map[key] = Def(); +} + +std::vector<std::string> cmDefinitions::UnusedKeys() const +{ + std::vector<std::string> keys; + keys.reserve(this->Map.size()); + // Consider local definitions. + for (auto const& mi : this->Map) { + if (!mi.second.Used) { + keys.push_back(*mi.first.str_if_stable()); + } + } + return keys; +} |