1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmDefinitions.h"
7 #include <unordered_set>
10 #include <cm/string_view>
12 cmDefinitions::Def cmDefinitions::NoDef;
14 cmDefinitions::Def const& cmDefinitions::GetInternal(const std::string& key,
16 StackIter end, bool raise)
20 auto it = begin->Map.find(cm::String::borrow(key));
21 if (it != begin->Map.end()) {
28 return cmDefinitions::NoDef;
30 Def const& def = cmDefinitions::GetInternal(key, it, end, raise);
34 return begin->Map.emplace(key, def).first->second;
37 cmValue cmDefinitions::Get(const std::string& key, StackIter begin,
40 Def const& def = cmDefinitions::GetInternal(key, begin, end, false);
41 return def.Value ? cmValue(def.Value.str_if_stable()) : nullptr;
44 void cmDefinitions::Raise(const std::string& key, StackIter begin,
47 cmDefinitions::GetInternal(key, begin, end, true);
50 bool cmDefinitions::HasKey(const std::string& key, StackIter begin,
53 for (StackIter it = begin; it != end; ++it) {
54 if (it->Map.find(cm::String::borrow(key)) != it->Map.end()) {
61 cmDefinitions cmDefinitions::MakeClosure(StackIter begin, StackIter end)
63 cmDefinitions closure;
64 std::unordered_set<cm::string_view> undefined;
65 for (StackIter it = begin; it != end; ++it) {
66 // Consider local definitions.
67 for (auto const& mi : it->Map) {
68 // Use this key if it is not already set or unset.
69 if (closure.Map.find(mi.first) == closure.Map.end() &&
70 undefined.find(mi.first.view()) == undefined.end()) {
71 if (mi.second.Value) {
72 closure.Map.insert(mi);
74 undefined.emplace(mi.first.view());
82 std::vector<std::string> cmDefinitions::ClosureKeys(StackIter begin,
85 std::vector<std::string> defined;
86 std::unordered_set<cm::string_view> bound;
88 for (StackIter it = begin; it != end; ++it) {
89 defined.reserve(defined.size() + it->Map.size());
90 for (auto const& mi : it->Map) {
91 // Use this key if it is not already set or unset.
92 if (bound.emplace(mi.first.view()).second && mi.second.Value) {
93 defined.push_back(*mi.first.str_if_stable());
101 void cmDefinitions::Set(const std::string& key, cm::string_view value)
103 this->Map[key] = Def(value);
106 void cmDefinitions::Unset(const std::string& key)
108 this->Map[key] = Def();